PR fortran/14923
[official-gcc.git] / gcc / dwarf2out.c
blobfa1bdad359e1bcb61f71df132c80abf9907de5a6
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 #ifdef HAVE_LD_RO_RW_SECTION_MIXING
125 int fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
126 int per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
127 int lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
128 int flags;
130 flags = (! flag_pic
131 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
132 && (fde_encoding & 0x70) != DW_EH_PE_aligned
133 && (per_encoding & 0x70) != DW_EH_PE_absptr
134 && (per_encoding & 0x70) != DW_EH_PE_aligned
135 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
136 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
137 ? 0 : SECTION_WRITE;
138 named_section_flags (EH_FRAME_SECTION_NAME, flags);
139 #else
140 named_section_flags (EH_FRAME_SECTION_NAME, SECTION_WRITE);
141 #endif
142 #endif
145 /* Version of targetm.eh_frame_section for systems using collect2. */
146 void
147 collect2_eh_frame_section (void)
149 tree label = get_file_function_name ('F');
151 data_section ();
152 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
153 targetm.asm_out.globalize_label (asm_out_file, IDENTIFIER_POINTER (label));
154 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
157 /* Default version of targetm.eh_frame_section. */
158 void
159 default_eh_frame_section (void)
161 #ifdef EH_FRAME_SECTION_NAME
162 named_section_eh_frame_section ();
163 #else
164 collect2_eh_frame_section ();
165 #endif
168 /* Array of RTXes referenced by the debugging information, which therefore
169 must be kept around forever. */
170 static GTY(()) varray_type used_rtx_varray;
172 /* A pointer to the base of a list of incomplete types which might be
173 completed at some later time. incomplete_types_list needs to be a VARRAY
174 because we want to tell the garbage collector about it. */
175 static GTY(()) varray_type incomplete_types;
177 /* A pointer to the base of a table of references to declaration
178 scopes. This table is a display which tracks the nesting
179 of declaration scopes at the current scope and containing
180 scopes. This table is used to find the proper place to
181 define type declaration DIE's. */
182 static GTY(()) varray_type decl_scope_table;
184 /* How to start an assembler comment. */
185 #ifndef ASM_COMMENT_START
186 #define ASM_COMMENT_START ";#"
187 #endif
189 typedef struct dw_cfi_struct *dw_cfi_ref;
190 typedef struct dw_fde_struct *dw_fde_ref;
191 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
193 /* Call frames are described using a sequence of Call Frame
194 Information instructions. The register number, offset
195 and address fields are provided as possible operands;
196 their use is selected by the opcode field. */
198 enum dw_cfi_oprnd_type {
199 dw_cfi_oprnd_unused,
200 dw_cfi_oprnd_reg_num,
201 dw_cfi_oprnd_offset,
202 dw_cfi_oprnd_addr,
203 dw_cfi_oprnd_loc
206 typedef union dw_cfi_oprnd_struct GTY(())
208 unsigned long GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
209 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
210 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
211 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
213 dw_cfi_oprnd;
215 typedef struct dw_cfi_struct GTY(())
217 dw_cfi_ref dw_cfi_next;
218 enum dwarf_call_frame_info dw_cfi_opc;
219 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
220 dw_cfi_oprnd1;
221 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
222 dw_cfi_oprnd2;
224 dw_cfi_node;
226 /* This is how we define the location of the CFA. We use to handle it
227 as REG + OFFSET all the time, but now it can be more complex.
228 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
229 Instead of passing around REG and OFFSET, we pass a copy
230 of this structure. */
231 typedef struct cfa_loc GTY(())
233 unsigned long reg;
234 HOST_WIDE_INT offset;
235 HOST_WIDE_INT base_offset;
236 int indirect; /* 1 if CFA is accessed via a dereference. */
237 } dw_cfa_location;
239 /* All call frame descriptions (FDE's) in the GCC generated DWARF
240 refer to a single Common Information Entry (CIE), defined at
241 the beginning of the .debug_frame section. This use of a single
242 CIE obviates the need to keep track of multiple CIE's
243 in the DWARF generation routines below. */
245 typedef struct dw_fde_struct GTY(())
247 tree decl;
248 const char *dw_fde_begin;
249 const char *dw_fde_current_label;
250 const char *dw_fde_end;
251 dw_cfi_ref dw_fde_cfi;
252 unsigned funcdef_number;
253 unsigned all_throwers_are_sibcalls : 1;
254 unsigned nothrow : 1;
255 unsigned uses_eh_lsda : 1;
257 dw_fde_node;
259 /* Maximum size (in bytes) of an artificially generated label. */
260 #define MAX_ARTIFICIAL_LABEL_BYTES 30
262 /* The size of addresses as they appear in the Dwarf 2 data.
263 Some architectures use word addresses to refer to code locations,
264 but Dwarf 2 info always uses byte addresses. On such machines,
265 Dwarf 2 addresses need to be larger than the architecture's
266 pointers. */
267 #ifndef DWARF2_ADDR_SIZE
268 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
269 #endif
271 /* The size in bytes of a DWARF field indicating an offset or length
272 relative to a debug info section, specified to be 4 bytes in the
273 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
274 as PTR_SIZE. */
276 #ifndef DWARF_OFFSET_SIZE
277 #define DWARF_OFFSET_SIZE 4
278 #endif
280 /* According to the (draft) DWARF 3 specification, the initial length
281 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
282 bytes are 0xffffffff, followed by the length stored in the next 8
283 bytes.
285 However, the SGI/MIPS ABI uses an initial length which is equal to
286 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
288 #ifndef DWARF_INITIAL_LENGTH_SIZE
289 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
290 #endif
292 #define DWARF_VERSION 2
294 /* Round SIZE up to the nearest BOUNDARY. */
295 #define DWARF_ROUND(SIZE,BOUNDARY) \
296 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
298 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
299 #ifndef DWARF_CIE_DATA_ALIGNMENT
300 #ifdef STACK_GROWS_DOWNWARD
301 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
302 #else
303 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
304 #endif
305 #endif
307 /* A pointer to the base of a table that contains frame description
308 information for each routine. */
309 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
311 /* Number of elements currently allocated for fde_table. */
312 static GTY(()) unsigned fde_table_allocated;
314 /* Number of elements in fde_table currently in use. */
315 static GTY(()) unsigned fde_table_in_use;
317 /* Size (in elements) of increments by which we may expand the
318 fde_table. */
319 #define FDE_TABLE_INCREMENT 256
321 /* A list of call frame insns for the CIE. */
322 static GTY(()) dw_cfi_ref cie_cfi_head;
324 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
325 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
326 attribute that accelerates the lookup of the FDE associated
327 with the subprogram. This variable holds the table index of the FDE
328 associated with the current function (body) definition. */
329 static unsigned current_funcdef_fde;
330 #endif
332 struct indirect_string_node GTY(())
334 const char *str;
335 unsigned int refcount;
336 unsigned int form;
337 char *label;
340 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
342 static GTY(()) int dw2_string_counter;
343 static GTY(()) unsigned long dwarf2out_cfi_label_num;
345 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
347 /* Forward declarations for functions defined in this file. */
349 static char *stripattributes (const char *);
350 static const char *dwarf_cfi_name (unsigned);
351 static dw_cfi_ref new_cfi (void);
352 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
353 static void add_fde_cfi (const char *, dw_cfi_ref);
354 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
355 static void lookup_cfa (dw_cfa_location *);
356 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
357 static void initial_return_save (rtx);
358 static HOST_WIDE_INT stack_adjust_offset (rtx);
359 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
360 static void output_call_frame_info (int);
361 static void dwarf2out_stack_adjust (rtx);
362 static void queue_reg_save (const char *, rtx, HOST_WIDE_INT);
363 static void flush_queued_reg_saves (void);
364 static bool clobbers_queued_reg_save (rtx);
365 static void dwarf2out_frame_debug_expr (rtx, const char *);
367 /* Support for complex CFA locations. */
368 static void output_cfa_loc (dw_cfi_ref);
369 static void get_cfa_from_loc_descr (dw_cfa_location *,
370 struct dw_loc_descr_struct *);
371 static struct dw_loc_descr_struct *build_cfa_loc
372 (dw_cfa_location *);
373 static void def_cfa_1 (const char *, dw_cfa_location *);
375 /* How to start an assembler comment. */
376 #ifndef ASM_COMMENT_START
377 #define ASM_COMMENT_START ";#"
378 #endif
380 /* Data and reference forms for relocatable data. */
381 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
382 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
384 #ifndef DEBUG_FRAME_SECTION
385 #define DEBUG_FRAME_SECTION ".debug_frame"
386 #endif
388 #ifndef FUNC_BEGIN_LABEL
389 #define FUNC_BEGIN_LABEL "LFB"
390 #endif
392 #ifndef FUNC_END_LABEL
393 #define FUNC_END_LABEL "LFE"
394 #endif
396 #ifndef FRAME_BEGIN_LABEL
397 #define FRAME_BEGIN_LABEL "Lframe"
398 #endif
399 #define CIE_AFTER_SIZE_LABEL "LSCIE"
400 #define CIE_END_LABEL "LECIE"
401 #define FDE_LABEL "LSFDE"
402 #define FDE_AFTER_SIZE_LABEL "LASFDE"
403 #define FDE_END_LABEL "LEFDE"
404 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
405 #define LINE_NUMBER_END_LABEL "LELT"
406 #define LN_PROLOG_AS_LABEL "LASLTP"
407 #define LN_PROLOG_END_LABEL "LELTP"
408 #define DIE_LABEL_PREFIX "DW"
410 /* The DWARF 2 CFA column which tracks the return address. Normally this
411 is the column for PC, or the first column after all of the hard
412 registers. */
413 #ifndef DWARF_FRAME_RETURN_COLUMN
414 #ifdef PC_REGNUM
415 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
416 #else
417 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
418 #endif
419 #endif
421 /* The mapping from gcc register number to DWARF 2 CFA column number. By
422 default, we just provide columns for all registers. */
423 #ifndef DWARF_FRAME_REGNUM
424 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
425 #endif
427 /* The offset from the incoming value of %sp to the top of the stack frame
428 for the current function. */
429 #ifndef INCOMING_FRAME_SP_OFFSET
430 #define INCOMING_FRAME_SP_OFFSET 0
431 #endif
433 /* Hook used by __throw. */
436 expand_builtin_dwarf_sp_column (void)
438 return GEN_INT (DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
441 /* Return a pointer to a copy of the section string name S with all
442 attributes stripped off, and an asterisk prepended (for assemble_name). */
444 static inline char *
445 stripattributes (const char *s)
447 char *stripped = xmalloc (strlen (s) + 2);
448 char *p = stripped;
450 *p++ = '*';
452 while (*s && *s != ',')
453 *p++ = *s++;
455 *p = '\0';
456 return stripped;
459 /* Generate code to initialize the register size table. */
461 void
462 expand_builtin_init_dwarf_reg_sizes (tree address)
464 int i;
465 enum machine_mode mode = TYPE_MODE (char_type_node);
466 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, 0);
467 rtx mem = gen_rtx_MEM (BLKmode, addr);
468 bool wrote_return_column = false;
470 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
471 if (DWARF_FRAME_REGNUM (i) < DWARF_FRAME_REGISTERS)
473 HOST_WIDE_INT offset = DWARF_FRAME_REGNUM (i) * GET_MODE_SIZE (mode);
474 enum machine_mode save_mode = reg_raw_mode[i];
475 HOST_WIDE_INT size;
477 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
478 save_mode = choose_hard_reg_mode (i, 1, true);
479 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
481 if (save_mode == VOIDmode)
482 continue;
483 wrote_return_column = true;
485 size = GET_MODE_SIZE (save_mode);
486 if (offset < 0)
487 continue;
489 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
492 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
493 if (! wrote_return_column)
494 abort ();
495 i = DWARF_ALT_FRAME_RETURN_COLUMN;
496 wrote_return_column = false;
497 #else
498 i = DWARF_FRAME_RETURN_COLUMN;
499 #endif
501 if (! wrote_return_column)
503 enum machine_mode save_mode = Pmode;
504 HOST_WIDE_INT offset = i * GET_MODE_SIZE (mode);
505 HOST_WIDE_INT size = GET_MODE_SIZE (save_mode);
506 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
510 /* Convert a DWARF call frame info. operation to its string name */
512 static const char *
513 dwarf_cfi_name (unsigned int cfi_opc)
515 switch (cfi_opc)
517 case DW_CFA_advance_loc:
518 return "DW_CFA_advance_loc";
519 case DW_CFA_offset:
520 return "DW_CFA_offset";
521 case DW_CFA_restore:
522 return "DW_CFA_restore";
523 case DW_CFA_nop:
524 return "DW_CFA_nop";
525 case DW_CFA_set_loc:
526 return "DW_CFA_set_loc";
527 case DW_CFA_advance_loc1:
528 return "DW_CFA_advance_loc1";
529 case DW_CFA_advance_loc2:
530 return "DW_CFA_advance_loc2";
531 case DW_CFA_advance_loc4:
532 return "DW_CFA_advance_loc4";
533 case DW_CFA_offset_extended:
534 return "DW_CFA_offset_extended";
535 case DW_CFA_restore_extended:
536 return "DW_CFA_restore_extended";
537 case DW_CFA_undefined:
538 return "DW_CFA_undefined";
539 case DW_CFA_same_value:
540 return "DW_CFA_same_value";
541 case DW_CFA_register:
542 return "DW_CFA_register";
543 case DW_CFA_remember_state:
544 return "DW_CFA_remember_state";
545 case DW_CFA_restore_state:
546 return "DW_CFA_restore_state";
547 case DW_CFA_def_cfa:
548 return "DW_CFA_def_cfa";
549 case DW_CFA_def_cfa_register:
550 return "DW_CFA_def_cfa_register";
551 case DW_CFA_def_cfa_offset:
552 return "DW_CFA_def_cfa_offset";
554 /* DWARF 3 */
555 case DW_CFA_def_cfa_expression:
556 return "DW_CFA_def_cfa_expression";
557 case DW_CFA_expression:
558 return "DW_CFA_expression";
559 case DW_CFA_offset_extended_sf:
560 return "DW_CFA_offset_extended_sf";
561 case DW_CFA_def_cfa_sf:
562 return "DW_CFA_def_cfa_sf";
563 case DW_CFA_def_cfa_offset_sf:
564 return "DW_CFA_def_cfa_offset_sf";
566 /* SGI/MIPS specific */
567 case DW_CFA_MIPS_advance_loc8:
568 return "DW_CFA_MIPS_advance_loc8";
570 /* GNU extensions */
571 case DW_CFA_GNU_window_save:
572 return "DW_CFA_GNU_window_save";
573 case DW_CFA_GNU_args_size:
574 return "DW_CFA_GNU_args_size";
575 case DW_CFA_GNU_negative_offset_extended:
576 return "DW_CFA_GNU_negative_offset_extended";
578 default:
579 return "DW_CFA_<unknown>";
583 /* Return a pointer to a newly allocated Call Frame Instruction. */
585 static inline dw_cfi_ref
586 new_cfi (void)
588 dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
590 cfi->dw_cfi_next = NULL;
591 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
592 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
594 return cfi;
597 /* Add a Call Frame Instruction to list of instructions. */
599 static inline void
600 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
602 dw_cfi_ref *p;
604 /* Find the end of the chain. */
605 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
608 *p = cfi;
611 /* Generate a new label for the CFI info to refer to. */
613 char *
614 dwarf2out_cfi_label (void)
616 static char label[20];
618 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
619 ASM_OUTPUT_LABEL (asm_out_file, label);
620 return label;
623 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
624 or to the CIE if LABEL is NULL. */
626 static void
627 add_fde_cfi (const char *label, dw_cfi_ref cfi)
629 if (label)
631 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
633 if (*label == 0)
634 label = dwarf2out_cfi_label ();
636 if (fde->dw_fde_current_label == NULL
637 || strcmp (label, fde->dw_fde_current_label) != 0)
639 dw_cfi_ref xcfi;
641 fde->dw_fde_current_label = label = xstrdup (label);
643 /* Set the location counter to the new label. */
644 xcfi = new_cfi ();
645 xcfi->dw_cfi_opc = DW_CFA_advance_loc4;
646 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
647 add_cfi (&fde->dw_fde_cfi, xcfi);
650 add_cfi (&fde->dw_fde_cfi, cfi);
653 else
654 add_cfi (&cie_cfi_head, cfi);
657 /* Subroutine of lookup_cfa. */
659 static inline void
660 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
662 switch (cfi->dw_cfi_opc)
664 case DW_CFA_def_cfa_offset:
665 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
666 break;
667 case DW_CFA_def_cfa_register:
668 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
669 break;
670 case DW_CFA_def_cfa:
671 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
672 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
673 break;
674 case DW_CFA_def_cfa_expression:
675 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
676 break;
677 default:
678 break;
682 /* Find the previous value for the CFA. */
684 static void
685 lookup_cfa (dw_cfa_location *loc)
687 dw_cfi_ref cfi;
689 loc->reg = (unsigned long) -1;
690 loc->offset = 0;
691 loc->indirect = 0;
692 loc->base_offset = 0;
694 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
695 lookup_cfa_1 (cfi, loc);
697 if (fde_table_in_use)
699 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
700 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
701 lookup_cfa_1 (cfi, loc);
705 /* The current rule for calculating the DWARF2 canonical frame address. */
706 static dw_cfa_location cfa;
708 /* The register used for saving registers to the stack, and its offset
709 from the CFA. */
710 static dw_cfa_location cfa_store;
712 /* The running total of the size of arguments pushed onto the stack. */
713 static HOST_WIDE_INT args_size;
715 /* The last args_size we actually output. */
716 static HOST_WIDE_INT old_args_size;
718 /* Entry point to update the canonical frame address (CFA).
719 LABEL is passed to add_fde_cfi. The value of CFA is now to be
720 calculated from REG+OFFSET. */
722 void
723 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
725 dw_cfa_location loc;
726 loc.indirect = 0;
727 loc.base_offset = 0;
728 loc.reg = reg;
729 loc.offset = offset;
730 def_cfa_1 (label, &loc);
733 /* This routine does the actual work. The CFA is now calculated from
734 the dw_cfa_location structure. */
736 static void
737 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
739 dw_cfi_ref cfi;
740 dw_cfa_location old_cfa, loc;
742 cfa = *loc_p;
743 loc = *loc_p;
745 if (cfa_store.reg == loc.reg && loc.indirect == 0)
746 cfa_store.offset = loc.offset;
748 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
749 lookup_cfa (&old_cfa);
751 /* If nothing changed, no need to issue any call frame instructions. */
752 if (loc.reg == old_cfa.reg && loc.offset == old_cfa.offset
753 && loc.indirect == old_cfa.indirect
754 && (loc.indirect == 0 || loc.base_offset == old_cfa.base_offset))
755 return;
757 cfi = new_cfi ();
759 if (loc.reg == old_cfa.reg && !loc.indirect)
761 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction,
762 indicating the CFA register did not change but the offset
763 did. */
764 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
765 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
768 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
769 else if (loc.offset == old_cfa.offset && old_cfa.reg != (unsigned long) -1
770 && !loc.indirect)
772 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
773 indicating the CFA register has changed to <register> but the
774 offset has not changed. */
775 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
776 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
778 #endif
780 else if (loc.indirect == 0)
782 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
783 indicating the CFA register has changed to <register> with
784 the specified offset. */
785 cfi->dw_cfi_opc = DW_CFA_def_cfa;
786 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
787 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
789 else
791 /* Construct a DW_CFA_def_cfa_expression instruction to
792 calculate the CFA using a full location expression since no
793 register-offset pair is available. */
794 struct dw_loc_descr_struct *loc_list;
796 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
797 loc_list = build_cfa_loc (&loc);
798 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
801 add_fde_cfi (label, cfi);
804 /* Add the CFI for saving a register. REG is the CFA column number.
805 LABEL is passed to add_fde_cfi.
806 If SREG is -1, the register is saved at OFFSET from the CFA;
807 otherwise it is saved in SREG. */
809 static void
810 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
812 dw_cfi_ref cfi = new_cfi ();
814 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
816 /* The following comparison is correct. -1 is used to indicate that
817 the value isn't a register number. */
818 if (sreg == (unsigned int) -1)
820 if (reg & ~0x3f)
821 /* The register number won't fit in 6 bits, so we have to use
822 the long form. */
823 cfi->dw_cfi_opc = DW_CFA_offset_extended;
824 else
825 cfi->dw_cfi_opc = DW_CFA_offset;
827 #ifdef ENABLE_CHECKING
829 /* If we get an offset that is not a multiple of
830 DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
831 definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
832 description. */
833 HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
835 if (check_offset * DWARF_CIE_DATA_ALIGNMENT != offset)
836 abort ();
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 /* We could emit a DW_CFA_same_value in this case, but don't bother. */
847 return;
848 else
850 cfi->dw_cfi_opc = DW_CFA_register;
851 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
854 add_fde_cfi (label, cfi);
857 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
858 This CFI tells the unwinder that it needs to restore the window registers
859 from the previous frame's window save area.
861 ??? Perhaps we should note in the CIE where windows are saved (instead of
862 assuming 0(cfa)) and what registers are in the window. */
864 void
865 dwarf2out_window_save (const char *label)
867 dw_cfi_ref cfi = new_cfi ();
869 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
870 add_fde_cfi (label, cfi);
873 /* Add a CFI to update the running total of the size of arguments
874 pushed onto the stack. */
876 void
877 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
879 dw_cfi_ref cfi;
881 if (size == old_args_size)
882 return;
884 old_args_size = size;
886 cfi = new_cfi ();
887 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
888 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
889 add_fde_cfi (label, cfi);
892 /* Entry point for saving a register to the stack. REG is the GCC register
893 number. LABEL and OFFSET are passed to reg_save. */
895 void
896 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
898 reg_save (label, DWARF_FRAME_REGNUM (reg), -1, offset);
901 /* Entry point for saving the return address in the stack.
902 LABEL and OFFSET are passed to reg_save. */
904 void
905 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
907 reg_save (label, DWARF_FRAME_RETURN_COLUMN, -1, offset);
910 /* Entry point for saving the return address in a register.
911 LABEL and SREG are passed to reg_save. */
913 void
914 dwarf2out_return_reg (const char *label, unsigned int sreg)
916 reg_save (label, DWARF_FRAME_RETURN_COLUMN, sreg, 0);
919 /* Record the initial position of the return address. RTL is
920 INCOMING_RETURN_ADDR_RTX. */
922 static void
923 initial_return_save (rtx rtl)
925 unsigned int reg = (unsigned int) -1;
926 HOST_WIDE_INT offset = 0;
928 switch (GET_CODE (rtl))
930 case REG:
931 /* RA is in a register. */
932 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
933 break;
935 case MEM:
936 /* RA is on the stack. */
937 rtl = XEXP (rtl, 0);
938 switch (GET_CODE (rtl))
940 case REG:
941 if (REGNO (rtl) != STACK_POINTER_REGNUM)
942 abort ();
943 offset = 0;
944 break;
946 case PLUS:
947 if (REGNO (XEXP (rtl, 0)) != STACK_POINTER_REGNUM)
948 abort ();
949 offset = INTVAL (XEXP (rtl, 1));
950 break;
952 case MINUS:
953 if (REGNO (XEXP (rtl, 0)) != STACK_POINTER_REGNUM)
954 abort ();
955 offset = -INTVAL (XEXP (rtl, 1));
956 break;
958 default:
959 abort ();
962 break;
964 case PLUS:
965 /* The return address is at some offset from any value we can
966 actually load. For instance, on the SPARC it is in %i7+8. Just
967 ignore the offset for now; it doesn't matter for unwinding frames. */
968 if (GET_CODE (XEXP (rtl, 1)) != CONST_INT)
969 abort ();
970 initial_return_save (XEXP (rtl, 0));
971 return;
973 default:
974 abort ();
977 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
980 /* Given a SET, calculate the amount of stack adjustment it
981 contains. */
983 static HOST_WIDE_INT
984 stack_adjust_offset (rtx pattern)
986 rtx src = SET_SRC (pattern);
987 rtx dest = SET_DEST (pattern);
988 HOST_WIDE_INT offset = 0;
989 enum rtx_code code;
991 if (dest == stack_pointer_rtx)
993 /* (set (reg sp) (plus (reg sp) (const_int))) */
994 code = GET_CODE (src);
995 if (! (code == PLUS || code == MINUS)
996 || XEXP (src, 0) != stack_pointer_rtx
997 || GET_CODE (XEXP (src, 1)) != CONST_INT)
998 return 0;
1000 offset = INTVAL (XEXP (src, 1));
1001 if (code == PLUS)
1002 offset = -offset;
1004 else if (GET_CODE (dest) == MEM)
1006 /* (set (mem (pre_dec (reg sp))) (foo)) */
1007 src = XEXP (dest, 0);
1008 code = GET_CODE (src);
1010 switch (code)
1012 case PRE_MODIFY:
1013 case POST_MODIFY:
1014 if (XEXP (src, 0) == stack_pointer_rtx)
1016 rtx val = XEXP (XEXP (src, 1), 1);
1017 /* We handle only adjustments by constant amount. */
1018 if (GET_CODE (XEXP (src, 1)) != PLUS ||
1019 GET_CODE (val) != CONST_INT)
1020 abort ();
1021 offset = -INTVAL (val);
1022 break;
1024 return 0;
1026 case PRE_DEC:
1027 case POST_DEC:
1028 if (XEXP (src, 0) == stack_pointer_rtx)
1030 offset = GET_MODE_SIZE (GET_MODE (dest));
1031 break;
1033 return 0;
1035 case PRE_INC:
1036 case POST_INC:
1037 if (XEXP (src, 0) == stack_pointer_rtx)
1039 offset = -GET_MODE_SIZE (GET_MODE (dest));
1040 break;
1042 return 0;
1044 default:
1045 return 0;
1048 else
1049 return 0;
1051 return offset;
1054 /* Check INSN to see if it looks like a push or a stack adjustment, and
1055 make a note of it if it does. EH uses this information to find out how
1056 much extra space it needs to pop off the stack. */
1058 static void
1059 dwarf2out_stack_adjust (rtx insn)
1061 HOST_WIDE_INT offset;
1062 const char *label;
1063 int i;
1065 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1066 with this function. Proper support would require all frame-related
1067 insns to be marked, and to be able to handle saving state around
1068 epilogues textually in the middle of the function. */
1069 if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1070 return;
1072 if (!flag_asynchronous_unwind_tables && GET_CODE (insn) == CALL_INSN)
1074 /* Extract the size of the args from the CALL rtx itself. */
1075 insn = PATTERN (insn);
1076 if (GET_CODE (insn) == PARALLEL)
1077 insn = XVECEXP (insn, 0, 0);
1078 if (GET_CODE (insn) == SET)
1079 insn = SET_SRC (insn);
1080 if (GET_CODE (insn) != CALL)
1081 abort ();
1083 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1084 return;
1087 /* If only calls can throw, and we have a frame pointer,
1088 save up adjustments until we see the CALL_INSN. */
1089 else if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1090 return;
1092 if (GET_CODE (insn) == BARRIER)
1094 /* When we see a BARRIER, we know to reset args_size to 0. Usually
1095 the compiler will have already emitted a stack adjustment, but
1096 doesn't bother for calls to noreturn functions. */
1097 #ifdef STACK_GROWS_DOWNWARD
1098 offset = -args_size;
1099 #else
1100 offset = args_size;
1101 #endif
1103 else if (GET_CODE (PATTERN (insn)) == SET)
1104 offset = stack_adjust_offset (PATTERN (insn));
1105 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1106 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1108 /* There may be stack adjustments inside compound insns. Search
1109 for them. */
1110 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1111 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1112 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1114 else
1115 return;
1117 if (offset == 0)
1118 return;
1120 if (cfa.reg == STACK_POINTER_REGNUM)
1121 cfa.offset += offset;
1123 #ifndef STACK_GROWS_DOWNWARD
1124 offset = -offset;
1125 #endif
1127 args_size += offset;
1128 if (args_size < 0)
1129 args_size = 0;
1131 label = dwarf2out_cfi_label ();
1132 def_cfa_1 (label, &cfa);
1133 dwarf2out_args_size (label, args_size);
1136 #endif
1138 /* We delay emitting a register save until either (a) we reach the end
1139 of the prologue or (b) the register is clobbered. This clusters
1140 register saves so that there are fewer pc advances. */
1142 struct queued_reg_save GTY(())
1144 struct queued_reg_save *next;
1145 rtx reg;
1146 HOST_WIDE_INT cfa_offset;
1149 static GTY(()) struct queued_reg_save *queued_reg_saves;
1151 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1152 static const char *last_reg_save_label;
1154 static void
1155 queue_reg_save (const char *label, rtx reg, HOST_WIDE_INT offset)
1157 struct queued_reg_save *q = ggc_alloc (sizeof (*q));
1159 q->next = queued_reg_saves;
1160 q->reg = reg;
1161 q->cfa_offset = offset;
1162 queued_reg_saves = q;
1164 last_reg_save_label = label;
1167 static void
1168 flush_queued_reg_saves (void)
1170 struct queued_reg_save *q, *next;
1172 for (q = queued_reg_saves; q; q = next)
1174 dwarf2out_reg_save (last_reg_save_label, REGNO (q->reg), q->cfa_offset);
1175 next = q->next;
1178 queued_reg_saves = NULL;
1179 last_reg_save_label = NULL;
1182 static bool
1183 clobbers_queued_reg_save (rtx insn)
1185 struct queued_reg_save *q;
1187 for (q = queued_reg_saves; q; q = q->next)
1188 if (modified_in_p (q->reg, insn))
1189 return true;
1191 return false;
1195 /* A temporary register holding an integral value used in adjusting SP
1196 or setting up the store_reg. The "offset" field holds the integer
1197 value, not an offset. */
1198 static dw_cfa_location cfa_temp;
1200 /* Record call frame debugging information for an expression EXPR,
1201 which either sets SP or FP (adjusting how we calculate the frame
1202 address) or saves a register to the stack. LABEL indicates the
1203 address of EXPR.
1205 This function encodes a state machine mapping rtxes to actions on
1206 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1207 users need not read the source code.
1209 The High-Level Picture
1211 Changes in the register we use to calculate the CFA: Currently we
1212 assume that if you copy the CFA register into another register, we
1213 should take the other one as the new CFA register; this seems to
1214 work pretty well. If it's wrong for some target, it's simple
1215 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1217 Changes in the register we use for saving registers to the stack:
1218 This is usually SP, but not always. Again, we deduce that if you
1219 copy SP into another register (and SP is not the CFA register),
1220 then the new register is the one we will be using for register
1221 saves. This also seems to work.
1223 Register saves: There's not much guesswork about this one; if
1224 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1225 register save, and the register used to calculate the destination
1226 had better be the one we think we're using for this purpose.
1228 Except: If the register being saved is the CFA register, and the
1229 offset is nonzero, we are saving the CFA, so we assume we have to
1230 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1231 the intent is to save the value of SP from the previous frame.
1233 Invariants / Summaries of Rules
1235 cfa current rule for calculating the CFA. It usually
1236 consists of a register and an offset.
1237 cfa_store register used by prologue code to save things to the stack
1238 cfa_store.offset is the offset from the value of
1239 cfa_store.reg to the actual CFA
1240 cfa_temp register holding an integral value. cfa_temp.offset
1241 stores the value, which will be used to adjust the
1242 stack pointer. cfa_temp is also used like cfa_store,
1243 to track stores to the stack via fp or a temp reg.
1245 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1246 with cfa.reg as the first operand changes the cfa.reg and its
1247 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1248 cfa_temp.offset.
1250 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1251 expression yielding a constant. This sets cfa_temp.reg
1252 and cfa_temp.offset.
1254 Rule 5: Create a new register cfa_store used to save items to the
1255 stack.
1257 Rules 10-14: Save a register to the stack. Define offset as the
1258 difference of the original location and cfa_store's
1259 location (or cfa_temp's location if cfa_temp is used).
1261 The Rules
1263 "{a,b}" indicates a choice of a xor b.
1264 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1266 Rule 1:
1267 (set <reg1> <reg2>:cfa.reg)
1268 effects: cfa.reg = <reg1>
1269 cfa.offset unchanged
1270 cfa_temp.reg = <reg1>
1271 cfa_temp.offset = cfa.offset
1273 Rule 2:
1274 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1275 {<const_int>,<reg>:cfa_temp.reg}))
1276 effects: cfa.reg = sp if fp used
1277 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1278 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1279 if cfa_store.reg==sp
1281 Rule 3:
1282 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1283 effects: cfa.reg = fp
1284 cfa_offset += +/- <const_int>
1286 Rule 4:
1287 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1288 constraints: <reg1> != fp
1289 <reg1> != sp
1290 effects: cfa.reg = <reg1>
1291 cfa_temp.reg = <reg1>
1292 cfa_temp.offset = cfa.offset
1294 Rule 5:
1295 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1296 constraints: <reg1> != fp
1297 <reg1> != sp
1298 effects: cfa_store.reg = <reg1>
1299 cfa_store.offset = cfa.offset - cfa_temp.offset
1301 Rule 6:
1302 (set <reg> <const_int>)
1303 effects: cfa_temp.reg = <reg>
1304 cfa_temp.offset = <const_int>
1306 Rule 7:
1307 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1308 effects: cfa_temp.reg = <reg1>
1309 cfa_temp.offset |= <const_int>
1311 Rule 8:
1312 (set <reg> (high <exp>))
1313 effects: none
1315 Rule 9:
1316 (set <reg> (lo_sum <exp> <const_int>))
1317 effects: cfa_temp.reg = <reg>
1318 cfa_temp.offset = <const_int>
1320 Rule 10:
1321 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1322 effects: cfa_store.offset -= <const_int>
1323 cfa.offset = cfa_store.offset if cfa.reg == sp
1324 cfa.reg = sp
1325 cfa.base_offset = -cfa_store.offset
1327 Rule 11:
1328 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1329 effects: cfa_store.offset += -/+ mode_size(mem)
1330 cfa.offset = cfa_store.offset if cfa.reg == sp
1331 cfa.reg = sp
1332 cfa.base_offset = -cfa_store.offset
1334 Rule 12:
1335 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1337 <reg2>)
1338 effects: cfa.reg = <reg1>
1339 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1341 Rule 13:
1342 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1343 effects: cfa.reg = <reg1>
1344 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1346 Rule 14:
1347 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1348 effects: cfa.reg = <reg1>
1349 cfa.base_offset = -cfa_temp.offset
1350 cfa_temp.offset -= mode_size(mem) */
1352 static void
1353 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1355 rtx src, dest;
1356 HOST_WIDE_INT offset;
1358 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1359 the PARALLEL independently. The first element is always processed if
1360 it is a SET. This is for backward compatibility. Other elements
1361 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1362 flag is set in them. */
1363 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1365 int par_index;
1366 int limit = XVECLEN (expr, 0);
1368 for (par_index = 0; par_index < limit; par_index++)
1369 if (GET_CODE (XVECEXP (expr, 0, par_index)) == SET
1370 && (RTX_FRAME_RELATED_P (XVECEXP (expr, 0, par_index))
1371 || par_index == 0))
1372 dwarf2out_frame_debug_expr (XVECEXP (expr, 0, par_index), label);
1374 return;
1377 if (GET_CODE (expr) != SET)
1378 abort ();
1380 src = SET_SRC (expr);
1381 dest = SET_DEST (expr);
1383 switch (GET_CODE (dest))
1385 case REG:
1386 /* Rule 1 */
1387 /* Update the CFA rule wrt SP or FP. Make sure src is
1388 relative to the current CFA register. */
1389 switch (GET_CODE (src))
1391 /* Setting FP from SP. */
1392 case REG:
1393 if (cfa.reg == (unsigned) REGNO (src))
1394 /* OK. */
1396 else
1397 abort ();
1399 /* We used to require that dest be either SP or FP, but the
1400 ARM copies SP to a temporary register, and from there to
1401 FP. So we just rely on the backends to only set
1402 RTX_FRAME_RELATED_P on appropriate insns. */
1403 cfa.reg = REGNO (dest);
1404 cfa_temp.reg = cfa.reg;
1405 cfa_temp.offset = cfa.offset;
1406 break;
1408 case PLUS:
1409 case MINUS:
1410 case LO_SUM:
1411 if (dest == stack_pointer_rtx)
1413 /* Rule 2 */
1414 /* Adjusting SP. */
1415 switch (GET_CODE (XEXP (src, 1)))
1417 case CONST_INT:
1418 offset = INTVAL (XEXP (src, 1));
1419 break;
1420 case REG:
1421 if ((unsigned) REGNO (XEXP (src, 1)) != cfa_temp.reg)
1422 abort ();
1423 offset = cfa_temp.offset;
1424 break;
1425 default:
1426 abort ();
1429 if (XEXP (src, 0) == hard_frame_pointer_rtx)
1431 /* Restoring SP from FP in the epilogue. */
1432 if (cfa.reg != (unsigned) HARD_FRAME_POINTER_REGNUM)
1433 abort ();
1434 cfa.reg = STACK_POINTER_REGNUM;
1436 else if (GET_CODE (src) == LO_SUM)
1437 /* Assume we've set the source reg of the LO_SUM from sp. */
1439 else if (XEXP (src, 0) != stack_pointer_rtx)
1440 abort ();
1442 if (GET_CODE (src) != MINUS)
1443 offset = -offset;
1444 if (cfa.reg == STACK_POINTER_REGNUM)
1445 cfa.offset += offset;
1446 if (cfa_store.reg == STACK_POINTER_REGNUM)
1447 cfa_store.offset += offset;
1449 else if (dest == hard_frame_pointer_rtx)
1451 /* Rule 3 */
1452 /* Either setting the FP from an offset of the SP,
1453 or adjusting the FP */
1454 if (! frame_pointer_needed)
1455 abort ();
1457 if (GET_CODE (XEXP (src, 0)) == REG
1458 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1459 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1461 offset = INTVAL (XEXP (src, 1));
1462 if (GET_CODE (src) != MINUS)
1463 offset = -offset;
1464 cfa.offset += offset;
1465 cfa.reg = HARD_FRAME_POINTER_REGNUM;
1467 else
1468 abort ();
1470 else
1472 if (GET_CODE (src) == MINUS)
1473 abort ();
1475 /* Rule 4 */
1476 if (GET_CODE (XEXP (src, 0)) == REG
1477 && REGNO (XEXP (src, 0)) == cfa.reg
1478 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1480 /* Setting a temporary CFA register that will be copied
1481 into the FP later on. */
1482 offset = - INTVAL (XEXP (src, 1));
1483 cfa.offset += offset;
1484 cfa.reg = REGNO (dest);
1485 /* Or used to save regs to the stack. */
1486 cfa_temp.reg = cfa.reg;
1487 cfa_temp.offset = cfa.offset;
1490 /* Rule 5 */
1491 else if (GET_CODE (XEXP (src, 0)) == REG
1492 && REGNO (XEXP (src, 0)) == cfa_temp.reg
1493 && XEXP (src, 1) == stack_pointer_rtx)
1495 /* Setting a scratch register that we will use instead
1496 of SP for saving registers to the stack. */
1497 if (cfa.reg != STACK_POINTER_REGNUM)
1498 abort ();
1499 cfa_store.reg = REGNO (dest);
1500 cfa_store.offset = cfa.offset - cfa_temp.offset;
1503 /* Rule 9 */
1504 else if (GET_CODE (src) == LO_SUM
1505 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1507 cfa_temp.reg = REGNO (dest);
1508 cfa_temp.offset = INTVAL (XEXP (src, 1));
1510 else
1511 abort ();
1513 break;
1515 /* Rule 6 */
1516 case CONST_INT:
1517 cfa_temp.reg = REGNO (dest);
1518 cfa_temp.offset = INTVAL (src);
1519 break;
1521 /* Rule 7 */
1522 case IOR:
1523 if (GET_CODE (XEXP (src, 0)) != REG
1524 || (unsigned) REGNO (XEXP (src, 0)) != cfa_temp.reg
1525 || GET_CODE (XEXP (src, 1)) != CONST_INT)
1526 abort ();
1528 if ((unsigned) REGNO (dest) != cfa_temp.reg)
1529 cfa_temp.reg = REGNO (dest);
1530 cfa_temp.offset |= INTVAL (XEXP (src, 1));
1531 break;
1533 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1534 which will fill in all of the bits. */
1535 /* Rule 8 */
1536 case HIGH:
1537 break;
1539 default:
1540 abort ();
1543 def_cfa_1 (label, &cfa);
1544 break;
1546 case MEM:
1547 if (GET_CODE (src) != REG)
1548 abort ();
1550 /* Saving a register to the stack. Make sure dest is relative to the
1551 CFA register. */
1552 switch (GET_CODE (XEXP (dest, 0)))
1554 /* Rule 10 */
1555 /* With a push. */
1556 case PRE_MODIFY:
1557 /* We can't handle variable size modifications. */
1558 if (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1)) != CONST_INT)
1559 abort ();
1560 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1562 if (REGNO (XEXP (XEXP (dest, 0), 0)) != STACK_POINTER_REGNUM
1563 || cfa_store.reg != STACK_POINTER_REGNUM)
1564 abort ();
1566 cfa_store.offset += offset;
1567 if (cfa.reg == STACK_POINTER_REGNUM)
1568 cfa.offset = cfa_store.offset;
1570 offset = -cfa_store.offset;
1571 break;
1573 /* Rule 11 */
1574 case PRE_INC:
1575 case PRE_DEC:
1576 offset = GET_MODE_SIZE (GET_MODE (dest));
1577 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1578 offset = -offset;
1580 if (REGNO (XEXP (XEXP (dest, 0), 0)) != STACK_POINTER_REGNUM
1581 || cfa_store.reg != STACK_POINTER_REGNUM)
1582 abort ();
1584 cfa_store.offset += offset;
1585 if (cfa.reg == STACK_POINTER_REGNUM)
1586 cfa.offset = cfa_store.offset;
1588 offset = -cfa_store.offset;
1589 break;
1591 /* Rule 12 */
1592 /* With an offset. */
1593 case PLUS:
1594 case MINUS:
1595 case LO_SUM:
1596 if (GET_CODE (XEXP (XEXP (dest, 0), 1)) != CONST_INT)
1597 abort ();
1598 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1599 if (GET_CODE (XEXP (dest, 0)) == MINUS)
1600 offset = -offset;
1602 if (cfa_store.reg == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)))
1603 offset -= cfa_store.offset;
1604 else if (cfa_temp.reg == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)))
1605 offset -= cfa_temp.offset;
1606 else
1607 abort ();
1608 break;
1610 /* Rule 13 */
1611 /* Without an offset. */
1612 case REG:
1613 if (cfa_store.reg == (unsigned) REGNO (XEXP (dest, 0)))
1614 offset = -cfa_store.offset;
1615 else if (cfa_temp.reg == (unsigned) REGNO (XEXP (dest, 0)))
1616 offset = -cfa_temp.offset;
1617 else
1618 abort ();
1619 break;
1621 /* Rule 14 */
1622 case POST_INC:
1623 if (cfa_temp.reg != (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)))
1624 abort ();
1625 offset = -cfa_temp.offset;
1626 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1627 break;
1629 default:
1630 abort ();
1633 if (REGNO (src) != STACK_POINTER_REGNUM
1634 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1635 && (unsigned) REGNO (src) == cfa.reg)
1637 /* We're storing the current CFA reg into the stack. */
1639 if (cfa.offset == 0)
1641 /* If the source register is exactly the CFA, assume
1642 we're saving SP like any other register; this happens
1643 on the ARM. */
1644 def_cfa_1 (label, &cfa);
1645 queue_reg_save (label, stack_pointer_rtx, offset);
1646 break;
1648 else
1650 /* Otherwise, we'll need to look in the stack to
1651 calculate the CFA. */
1652 rtx x = XEXP (dest, 0);
1654 if (GET_CODE (x) != REG)
1655 x = XEXP (x, 0);
1656 if (GET_CODE (x) != REG)
1657 abort ();
1659 cfa.reg = REGNO (x);
1660 cfa.base_offset = offset;
1661 cfa.indirect = 1;
1662 def_cfa_1 (label, &cfa);
1663 break;
1667 def_cfa_1 (label, &cfa);
1668 queue_reg_save (label, src, offset);
1669 break;
1671 default:
1672 abort ();
1676 /* Record call frame debugging information for INSN, which either
1677 sets SP or FP (adjusting how we calculate the frame address) or saves a
1678 register to the stack. If INSN is NULL_RTX, initialize our state. */
1680 void
1681 dwarf2out_frame_debug (rtx insn)
1683 const char *label;
1684 rtx src;
1686 if (insn == NULL_RTX)
1688 /* Flush any queued register saves. */
1689 flush_queued_reg_saves ();
1691 /* Set up state for generating call frame debug info. */
1692 lookup_cfa (&cfa);
1693 if (cfa.reg != (unsigned long) DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM))
1694 abort ();
1696 cfa.reg = STACK_POINTER_REGNUM;
1697 cfa_store = cfa;
1698 cfa_temp.reg = -1;
1699 cfa_temp.offset = 0;
1700 return;
1703 if (GET_CODE (insn) != INSN || clobbers_queued_reg_save (insn))
1704 flush_queued_reg_saves ();
1706 if (! RTX_FRAME_RELATED_P (insn))
1708 if (!ACCUMULATE_OUTGOING_ARGS)
1709 dwarf2out_stack_adjust (insn);
1711 return;
1714 label = dwarf2out_cfi_label ();
1715 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1716 if (src)
1717 insn = XEXP (src, 0);
1718 else
1719 insn = PATTERN (insn);
1721 dwarf2out_frame_debug_expr (insn, label);
1724 #endif
1726 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
1727 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1728 (enum dwarf_call_frame_info cfi);
1730 static enum dw_cfi_oprnd_type
1731 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1733 switch (cfi)
1735 case DW_CFA_nop:
1736 case DW_CFA_GNU_window_save:
1737 return dw_cfi_oprnd_unused;
1739 case DW_CFA_set_loc:
1740 case DW_CFA_advance_loc1:
1741 case DW_CFA_advance_loc2:
1742 case DW_CFA_advance_loc4:
1743 case DW_CFA_MIPS_advance_loc8:
1744 return dw_cfi_oprnd_addr;
1746 case DW_CFA_offset:
1747 case DW_CFA_offset_extended:
1748 case DW_CFA_def_cfa:
1749 case DW_CFA_offset_extended_sf:
1750 case DW_CFA_def_cfa_sf:
1751 case DW_CFA_restore_extended:
1752 case DW_CFA_undefined:
1753 case DW_CFA_same_value:
1754 case DW_CFA_def_cfa_register:
1755 case DW_CFA_register:
1756 return dw_cfi_oprnd_reg_num;
1758 case DW_CFA_def_cfa_offset:
1759 case DW_CFA_GNU_args_size:
1760 case DW_CFA_def_cfa_offset_sf:
1761 return dw_cfi_oprnd_offset;
1763 case DW_CFA_def_cfa_expression:
1764 case DW_CFA_expression:
1765 return dw_cfi_oprnd_loc;
1767 default:
1768 abort ();
1772 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
1773 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1774 (enum dwarf_call_frame_info cfi);
1776 static enum dw_cfi_oprnd_type
1777 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1779 switch (cfi)
1781 case DW_CFA_def_cfa:
1782 case DW_CFA_def_cfa_sf:
1783 case DW_CFA_offset:
1784 case DW_CFA_offset_extended_sf:
1785 case DW_CFA_offset_extended:
1786 return dw_cfi_oprnd_offset;
1788 case DW_CFA_register:
1789 return dw_cfi_oprnd_reg_num;
1791 default:
1792 return dw_cfi_oprnd_unused;
1796 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1798 /* Map register numbers held in the call frame info that gcc has
1799 collected using DWARF_FRAME_REGNUM to those that should be output in
1800 .debug_frame and .eh_frame. */
1801 #ifndef DWARF2_FRAME_REG_OUT
1802 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
1803 #endif
1805 /* Output a Call Frame Information opcode and its operand(s). */
1807 static void
1808 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
1810 unsigned long r;
1811 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
1812 dw2_asm_output_data (1, (cfi->dw_cfi_opc
1813 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
1814 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
1815 cfi->dw_cfi_oprnd1.dw_cfi_offset);
1816 else if (cfi->dw_cfi_opc == DW_CFA_offset)
1818 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1819 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
1820 "DW_CFA_offset, column 0x%lx", r);
1821 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1823 else if (cfi->dw_cfi_opc == DW_CFA_restore)
1825 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1826 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
1827 "DW_CFA_restore, column 0x%lx", r);
1829 else
1831 dw2_asm_output_data (1, cfi->dw_cfi_opc,
1832 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
1834 switch (cfi->dw_cfi_opc)
1836 case DW_CFA_set_loc:
1837 if (for_eh)
1838 dw2_asm_output_encoded_addr_rtx (
1839 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
1840 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
1841 NULL);
1842 else
1843 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
1844 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
1845 break;
1847 case DW_CFA_advance_loc1:
1848 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1849 fde->dw_fde_current_label, NULL);
1850 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1851 break;
1853 case DW_CFA_advance_loc2:
1854 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1855 fde->dw_fde_current_label, NULL);
1856 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1857 break;
1859 case DW_CFA_advance_loc4:
1860 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1861 fde->dw_fde_current_label, NULL);
1862 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1863 break;
1865 case DW_CFA_MIPS_advance_loc8:
1866 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1867 fde->dw_fde_current_label, NULL);
1868 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1869 break;
1871 case DW_CFA_offset_extended:
1872 case DW_CFA_def_cfa:
1873 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1874 dw2_asm_output_data_uleb128 (r, NULL);
1875 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1876 break;
1878 case DW_CFA_offset_extended_sf:
1879 case DW_CFA_def_cfa_sf:
1880 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1881 dw2_asm_output_data_uleb128 (r, NULL);
1882 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1883 break;
1885 case DW_CFA_restore_extended:
1886 case DW_CFA_undefined:
1887 case DW_CFA_same_value:
1888 case DW_CFA_def_cfa_register:
1889 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1890 dw2_asm_output_data_uleb128 (r, NULL);
1891 break;
1893 case DW_CFA_register:
1894 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1895 dw2_asm_output_data_uleb128 (r, NULL);
1896 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
1897 dw2_asm_output_data_uleb128 (r, NULL);
1898 break;
1900 case DW_CFA_def_cfa_offset:
1901 case DW_CFA_GNU_args_size:
1902 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
1903 break;
1905 case DW_CFA_def_cfa_offset_sf:
1906 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
1907 break;
1909 case DW_CFA_GNU_window_save:
1910 break;
1912 case DW_CFA_def_cfa_expression:
1913 case DW_CFA_expression:
1914 output_cfa_loc (cfi);
1915 break;
1917 case DW_CFA_GNU_negative_offset_extended:
1918 /* Obsoleted by DW_CFA_offset_extended_sf. */
1919 abort ();
1921 default:
1922 break;
1927 /* Output the call frame information used to record information
1928 that relates to calculating the frame pointer, and records the
1929 location of saved registers. */
1931 static void
1932 output_call_frame_info (int for_eh)
1934 unsigned int i;
1935 dw_fde_ref fde;
1936 dw_cfi_ref cfi;
1937 char l1[20], l2[20], section_start_label[20];
1938 bool any_lsda_needed = false;
1939 char augmentation[6];
1940 int augmentation_size;
1941 int fde_encoding = DW_EH_PE_absptr;
1942 int per_encoding = DW_EH_PE_absptr;
1943 int lsda_encoding = DW_EH_PE_absptr;
1945 /* Don't emit a CIE if there won't be any FDEs. */
1946 if (fde_table_in_use == 0)
1947 return;
1949 /* If we make FDEs linkonce, we may have to emit an empty label for
1950 an FDE that wouldn't otherwise be emitted. We want to avoid
1951 having an FDE kept around when the function it refers to is
1952 discarded. (Example where this matters: a primary function
1953 template in C++ requires EH information, but an explicit
1954 specialization doesn't. */
1955 if (TARGET_USES_WEAK_UNWIND_INFO
1956 && ! flag_asynchronous_unwind_tables
1957 && for_eh)
1958 for (i = 0; i < fde_table_in_use; i++)
1959 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
1960 && !fde_table[i].uses_eh_lsda
1961 && ! DECL_ONE_ONLY (fde_table[i].decl))
1962 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
1963 for_eh, /* empty */ 1);
1965 /* If we don't have any functions we'll want to unwind out of, don't
1966 emit any EH unwind information. Note that if exceptions aren't
1967 enabled, we won't have collected nothrow information, and if we
1968 asked for asynchronous tables, we always want this info. */
1969 if (for_eh)
1971 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
1973 for (i = 0; i < fde_table_in_use; i++)
1974 if (fde_table[i].uses_eh_lsda)
1975 any_eh_needed = any_lsda_needed = true;
1976 else if (TARGET_USES_WEAK_UNWIND_INFO
1977 && DECL_ONE_ONLY (fde_table[i].decl))
1978 any_eh_needed = 1;
1979 else if (! fde_table[i].nothrow
1980 && ! fde_table[i].all_throwers_are_sibcalls)
1981 any_eh_needed = true;
1983 if (! any_eh_needed)
1984 return;
1987 /* We're going to be generating comments, so turn on app. */
1988 if (flag_debug_asm)
1989 app_enable ();
1991 if (for_eh)
1992 targetm.asm_out.eh_frame_section ();
1993 else
1994 named_section_flags (DEBUG_FRAME_SECTION, SECTION_DEBUG);
1996 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
1997 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
1999 /* Output the CIE. */
2000 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2001 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2002 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2003 "Length of Common Information Entry");
2004 ASM_OUTPUT_LABEL (asm_out_file, l1);
2006 /* Now that the CIE pointer is PC-relative for EH,
2007 use 0 to identify the CIE. */
2008 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2009 (for_eh ? 0 : DW_CIE_ID),
2010 "CIE Identifier Tag");
2012 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2014 augmentation[0] = 0;
2015 augmentation_size = 0;
2016 if (for_eh)
2018 char *p;
2020 /* Augmentation:
2021 z Indicates that a uleb128 is present to size the
2022 augmentation section.
2023 L Indicates the encoding (and thus presence) of
2024 an LSDA pointer in the FDE augmentation.
2025 R Indicates a non-default pointer encoding for
2026 FDE code pointers.
2027 P Indicates the presence of an encoding + language
2028 personality routine in the CIE augmentation. */
2030 fde_encoding = TARGET_USES_WEAK_UNWIND_INFO
2031 ? ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1)
2032 : ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2033 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2034 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2036 p = augmentation + 1;
2037 if (eh_personality_libfunc)
2039 *p++ = 'P';
2040 augmentation_size += 1 + size_of_encoded_value (per_encoding);
2042 if (any_lsda_needed)
2044 *p++ = 'L';
2045 augmentation_size += 1;
2047 if (fde_encoding != DW_EH_PE_absptr)
2049 *p++ = 'R';
2050 augmentation_size += 1;
2052 if (p > augmentation + 1)
2054 augmentation[0] = 'z';
2055 *p = '\0';
2058 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
2059 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2061 int offset = ( 4 /* Length */
2062 + 4 /* CIE Id */
2063 + 1 /* CIE version */
2064 + strlen (augmentation) + 1 /* Augmentation */
2065 + size_of_uleb128 (1) /* Code alignment */
2066 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2067 + 1 /* RA column */
2068 + 1 /* Augmentation size */
2069 + 1 /* Personality encoding */ );
2070 int pad = -offset & (PTR_SIZE - 1);
2072 augmentation_size += pad;
2074 /* Augmentations should be small, so there's scarce need to
2075 iterate for a solution. Die if we exceed one uleb128 byte. */
2076 if (size_of_uleb128 (augmentation_size) != 1)
2077 abort ();
2081 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2082 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2083 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2084 "CIE Data Alignment Factor");
2085 dw2_asm_output_data (1, DWARF_FRAME_RETURN_COLUMN, "CIE RA Column");
2087 if (augmentation[0])
2089 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2090 if (eh_personality_libfunc)
2092 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2093 eh_data_format_name (per_encoding));
2094 dw2_asm_output_encoded_addr_rtx (per_encoding,
2095 eh_personality_libfunc, NULL);
2098 if (any_lsda_needed)
2099 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2100 eh_data_format_name (lsda_encoding));
2102 if (fde_encoding != DW_EH_PE_absptr)
2103 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2104 eh_data_format_name (fde_encoding));
2107 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2108 output_cfi (cfi, NULL, for_eh);
2110 /* Pad the CIE out to an address sized boundary. */
2111 ASM_OUTPUT_ALIGN (asm_out_file,
2112 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2113 ASM_OUTPUT_LABEL (asm_out_file, l2);
2115 /* Loop through all of the FDE's. */
2116 for (i = 0; i < fde_table_in_use; i++)
2118 fde = &fde_table[i];
2120 /* Don't emit EH unwind info for leaf functions that don't need it. */
2121 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2122 && (fde->nothrow || fde->all_throwers_are_sibcalls)
2123 && (! TARGET_USES_WEAK_UNWIND_INFO || ! DECL_ONE_ONLY (fde->decl))
2124 && !fde->uses_eh_lsda)
2125 continue;
2127 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2128 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2129 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2130 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2131 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2132 "FDE Length");
2133 ASM_OUTPUT_LABEL (asm_out_file, l1);
2135 if (for_eh)
2136 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2137 else
2138 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2139 "FDE CIE offset");
2141 if (for_eh)
2143 if (TARGET_USES_WEAK_UNWIND_INFO
2144 && DECL_ONE_ONLY (fde->decl))
2145 dw2_asm_output_encoded_addr_rtx (fde_encoding,
2146 gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER
2147 (DECL_ASSEMBLER_NAME (fde->decl))),
2148 "FDE initial location");
2149 else
2150 dw2_asm_output_encoded_addr_rtx (fde_encoding,
2151 gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin),
2152 "FDE initial location");
2153 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2154 fde->dw_fde_end, fde->dw_fde_begin,
2155 "FDE address range");
2157 else
2159 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2160 "FDE initial location");
2161 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2162 fde->dw_fde_end, fde->dw_fde_begin,
2163 "FDE address range");
2166 if (augmentation[0])
2168 if (any_lsda_needed)
2170 int size = size_of_encoded_value (lsda_encoding);
2172 if (lsda_encoding == DW_EH_PE_aligned)
2174 int offset = ( 4 /* Length */
2175 + 4 /* CIE offset */
2176 + 2 * size_of_encoded_value (fde_encoding)
2177 + 1 /* Augmentation size */ );
2178 int pad = -offset & (PTR_SIZE - 1);
2180 size += pad;
2181 if (size_of_uleb128 (size) != 1)
2182 abort ();
2185 dw2_asm_output_data_uleb128 (size, "Augmentation size");
2187 if (fde->uses_eh_lsda)
2189 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2190 fde->funcdef_number);
2191 dw2_asm_output_encoded_addr_rtx (
2192 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2193 "Language Specific Data Area");
2195 else
2197 if (lsda_encoding == DW_EH_PE_aligned)
2198 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2199 dw2_asm_output_data
2200 (size_of_encoded_value (lsda_encoding), 0,
2201 "Language Specific Data Area (none)");
2204 else
2205 dw2_asm_output_data_uleb128 (0, "Augmentation size");
2208 /* Loop through the Call Frame Instructions associated with
2209 this FDE. */
2210 fde->dw_fde_current_label = fde->dw_fde_begin;
2211 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2212 output_cfi (cfi, fde, for_eh);
2214 /* Pad the FDE out to an address sized boundary. */
2215 ASM_OUTPUT_ALIGN (asm_out_file,
2216 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2217 ASM_OUTPUT_LABEL (asm_out_file, l2);
2220 if (for_eh && targetm.terminate_dw2_eh_frame_info)
2221 dw2_asm_output_data (4, 0, "End of Table");
2222 #ifdef MIPS_DEBUGGING_INFO
2223 /* Work around Irix 6 assembler bug whereby labels at the end of a section
2224 get a value of 0. Putting .align 0 after the label fixes it. */
2225 ASM_OUTPUT_ALIGN (asm_out_file, 0);
2226 #endif
2228 /* Turn off app to make assembly quicker. */
2229 if (flag_debug_asm)
2230 app_disable ();
2233 /* Output a marker (i.e. a label) for the beginning of a function, before
2234 the prologue. */
2236 void
2237 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2238 const char *file ATTRIBUTE_UNUSED)
2240 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2241 dw_fde_ref fde;
2243 current_function_func_begin_label = 0;
2245 #ifdef IA64_UNWIND_INFO
2246 /* ??? current_function_func_begin_label is also used by except.c
2247 for call-site information. We must emit this label if it might
2248 be used. */
2249 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2250 && ! dwarf2out_do_frame ())
2251 return;
2252 #else
2253 if (! dwarf2out_do_frame ())
2254 return;
2255 #endif
2257 function_section (current_function_decl);
2258 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2259 current_function_funcdef_no);
2260 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2261 current_function_funcdef_no);
2262 current_function_func_begin_label = get_identifier (label);
2264 #ifdef IA64_UNWIND_INFO
2265 /* We can elide the fde allocation if we're not emitting debug info. */
2266 if (! dwarf2out_do_frame ())
2267 return;
2268 #endif
2270 /* Expand the fde table if necessary. */
2271 if (fde_table_in_use == fde_table_allocated)
2273 fde_table_allocated += FDE_TABLE_INCREMENT;
2274 fde_table = ggc_realloc (fde_table,
2275 fde_table_allocated * sizeof (dw_fde_node));
2276 memset (fde_table + fde_table_in_use, 0,
2277 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2280 /* Record the FDE associated with this function. */
2281 current_funcdef_fde = fde_table_in_use;
2283 /* Add the new FDE at the end of the fde_table. */
2284 fde = &fde_table[fde_table_in_use++];
2285 fde->decl = current_function_decl;
2286 fde->dw_fde_begin = xstrdup (label);
2287 fde->dw_fde_current_label = NULL;
2288 fde->dw_fde_end = NULL;
2289 fde->dw_fde_cfi = NULL;
2290 fde->funcdef_number = current_function_funcdef_no;
2291 fde->nothrow = current_function_nothrow;
2292 fde->uses_eh_lsda = cfun->uses_eh_lsda;
2293 fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2295 args_size = old_args_size = 0;
2297 /* We only want to output line number information for the genuine dwarf2
2298 prologue case, not the eh frame case. */
2299 #ifdef DWARF2_DEBUGGING_INFO
2300 if (file)
2301 dwarf2out_source_line (line, file);
2302 #endif
2305 /* Output a marker (i.e. a label) for the absolute end of the generated code
2306 for a function definition. This gets called *after* the epilogue code has
2307 been generated. */
2309 void
2310 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2311 const char *file ATTRIBUTE_UNUSED)
2313 dw_fde_ref fde;
2314 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2316 /* Output a label to mark the endpoint of the code generated for this
2317 function. */
2318 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2319 current_function_funcdef_no);
2320 ASM_OUTPUT_LABEL (asm_out_file, label);
2321 fde = &fde_table[fde_table_in_use - 1];
2322 fde->dw_fde_end = xstrdup (label);
2325 void
2326 dwarf2out_frame_init (void)
2328 /* Allocate the initial hunk of the fde_table. */
2329 fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2330 fde_table_allocated = FDE_TABLE_INCREMENT;
2331 fde_table_in_use = 0;
2333 /* Generate the CFA instructions common to all FDE's. Do it now for the
2334 sake of lookup_cfa. */
2336 #ifdef DWARF2_UNWIND_INFO
2337 /* On entry, the Canonical Frame Address is at SP. */
2338 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2339 initial_return_save (INCOMING_RETURN_ADDR_RTX);
2340 #endif
2343 void
2344 dwarf2out_frame_finish (void)
2346 /* Output call frame information. */
2347 if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
2348 output_call_frame_info (0);
2350 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2351 output_call_frame_info (1);
2353 #endif
2355 /* And now, the subset of the debugging information support code necessary
2356 for emitting location expressions. */
2358 /* We need some way to distinguish DW_OP_addr with a direct symbol
2359 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
2360 #define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
2363 typedef struct dw_val_struct *dw_val_ref;
2364 typedef struct die_struct *dw_die_ref;
2365 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2366 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2368 /* Each DIE may have a series of attribute/value pairs. Values
2369 can take on several forms. The forms that are used in this
2370 implementation are listed below. */
2372 enum dw_val_class
2374 dw_val_class_addr,
2375 dw_val_class_offset,
2376 dw_val_class_loc,
2377 dw_val_class_loc_list,
2378 dw_val_class_range_list,
2379 dw_val_class_const,
2380 dw_val_class_unsigned_const,
2381 dw_val_class_long_long,
2382 dw_val_class_vec,
2383 dw_val_class_flag,
2384 dw_val_class_die_ref,
2385 dw_val_class_fde_ref,
2386 dw_val_class_lbl_id,
2387 dw_val_class_lbl_offset,
2388 dw_val_class_str
2391 /* Describe a double word constant value. */
2392 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
2394 typedef struct dw_long_long_struct GTY(())
2396 unsigned long hi;
2397 unsigned long low;
2399 dw_long_long_const;
2401 /* Describe a floating point constant value, or a vector constant value. */
2403 typedef struct dw_vec_struct GTY(())
2405 unsigned char * GTY((length ("%h.length"))) array;
2406 unsigned length;
2407 unsigned elt_size;
2409 dw_vec_const;
2411 /* The dw_val_node describes an attribute's value, as it is
2412 represented internally. */
2414 typedef struct dw_val_struct GTY(())
2416 enum dw_val_class val_class;
2417 union dw_val_struct_union
2419 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2420 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2421 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2422 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2423 HOST_WIDE_INT GTY ((default)) val_int;
2424 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2425 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2426 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2427 struct dw_val_die_union
2429 dw_die_ref die;
2430 int external;
2431 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2432 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2433 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2434 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2435 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2437 GTY ((desc ("%1.val_class"))) v;
2439 dw_val_node;
2441 /* Locations in memory are described using a sequence of stack machine
2442 operations. */
2444 typedef struct dw_loc_descr_struct GTY(())
2446 dw_loc_descr_ref dw_loc_next;
2447 enum dwarf_location_atom dw_loc_opc;
2448 dw_val_node dw_loc_oprnd1;
2449 dw_val_node dw_loc_oprnd2;
2450 int dw_loc_addr;
2452 dw_loc_descr_node;
2454 /* Location lists are ranges + location descriptions for that range,
2455 so you can track variables that are in different places over
2456 their entire life. */
2457 typedef struct dw_loc_list_struct GTY(())
2459 dw_loc_list_ref dw_loc_next;
2460 const char *begin; /* Label for begin address of range */
2461 const char *end; /* Label for end address of range */
2462 char *ll_symbol; /* Label for beginning of location list.
2463 Only on head of list */
2464 const char *section; /* Section this loclist is relative to */
2465 dw_loc_descr_ref expr;
2466 } dw_loc_list_node;
2468 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2470 static const char *dwarf_stack_op_name (unsigned);
2471 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2472 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2473 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2474 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2475 static unsigned long size_of_locs (dw_loc_descr_ref);
2476 static void output_loc_operands (dw_loc_descr_ref);
2477 static void output_loc_sequence (dw_loc_descr_ref);
2479 /* Convert a DWARF stack opcode into its string name. */
2481 static const char *
2482 dwarf_stack_op_name (unsigned int op)
2484 switch (op)
2486 case DW_OP_addr:
2487 case INTERNAL_DW_OP_tls_addr:
2488 return "DW_OP_addr";
2489 case DW_OP_deref:
2490 return "DW_OP_deref";
2491 case DW_OP_const1u:
2492 return "DW_OP_const1u";
2493 case DW_OP_const1s:
2494 return "DW_OP_const1s";
2495 case DW_OP_const2u:
2496 return "DW_OP_const2u";
2497 case DW_OP_const2s:
2498 return "DW_OP_const2s";
2499 case DW_OP_const4u:
2500 return "DW_OP_const4u";
2501 case DW_OP_const4s:
2502 return "DW_OP_const4s";
2503 case DW_OP_const8u:
2504 return "DW_OP_const8u";
2505 case DW_OP_const8s:
2506 return "DW_OP_const8s";
2507 case DW_OP_constu:
2508 return "DW_OP_constu";
2509 case DW_OP_consts:
2510 return "DW_OP_consts";
2511 case DW_OP_dup:
2512 return "DW_OP_dup";
2513 case DW_OP_drop:
2514 return "DW_OP_drop";
2515 case DW_OP_over:
2516 return "DW_OP_over";
2517 case DW_OP_pick:
2518 return "DW_OP_pick";
2519 case DW_OP_swap:
2520 return "DW_OP_swap";
2521 case DW_OP_rot:
2522 return "DW_OP_rot";
2523 case DW_OP_xderef:
2524 return "DW_OP_xderef";
2525 case DW_OP_abs:
2526 return "DW_OP_abs";
2527 case DW_OP_and:
2528 return "DW_OP_and";
2529 case DW_OP_div:
2530 return "DW_OP_div";
2531 case DW_OP_minus:
2532 return "DW_OP_minus";
2533 case DW_OP_mod:
2534 return "DW_OP_mod";
2535 case DW_OP_mul:
2536 return "DW_OP_mul";
2537 case DW_OP_neg:
2538 return "DW_OP_neg";
2539 case DW_OP_not:
2540 return "DW_OP_not";
2541 case DW_OP_or:
2542 return "DW_OP_or";
2543 case DW_OP_plus:
2544 return "DW_OP_plus";
2545 case DW_OP_plus_uconst:
2546 return "DW_OP_plus_uconst";
2547 case DW_OP_shl:
2548 return "DW_OP_shl";
2549 case DW_OP_shr:
2550 return "DW_OP_shr";
2551 case DW_OP_shra:
2552 return "DW_OP_shra";
2553 case DW_OP_xor:
2554 return "DW_OP_xor";
2555 case DW_OP_bra:
2556 return "DW_OP_bra";
2557 case DW_OP_eq:
2558 return "DW_OP_eq";
2559 case DW_OP_ge:
2560 return "DW_OP_ge";
2561 case DW_OP_gt:
2562 return "DW_OP_gt";
2563 case DW_OP_le:
2564 return "DW_OP_le";
2565 case DW_OP_lt:
2566 return "DW_OP_lt";
2567 case DW_OP_ne:
2568 return "DW_OP_ne";
2569 case DW_OP_skip:
2570 return "DW_OP_skip";
2571 case DW_OP_lit0:
2572 return "DW_OP_lit0";
2573 case DW_OP_lit1:
2574 return "DW_OP_lit1";
2575 case DW_OP_lit2:
2576 return "DW_OP_lit2";
2577 case DW_OP_lit3:
2578 return "DW_OP_lit3";
2579 case DW_OP_lit4:
2580 return "DW_OP_lit4";
2581 case DW_OP_lit5:
2582 return "DW_OP_lit5";
2583 case DW_OP_lit6:
2584 return "DW_OP_lit6";
2585 case DW_OP_lit7:
2586 return "DW_OP_lit7";
2587 case DW_OP_lit8:
2588 return "DW_OP_lit8";
2589 case DW_OP_lit9:
2590 return "DW_OP_lit9";
2591 case DW_OP_lit10:
2592 return "DW_OP_lit10";
2593 case DW_OP_lit11:
2594 return "DW_OP_lit11";
2595 case DW_OP_lit12:
2596 return "DW_OP_lit12";
2597 case DW_OP_lit13:
2598 return "DW_OP_lit13";
2599 case DW_OP_lit14:
2600 return "DW_OP_lit14";
2601 case DW_OP_lit15:
2602 return "DW_OP_lit15";
2603 case DW_OP_lit16:
2604 return "DW_OP_lit16";
2605 case DW_OP_lit17:
2606 return "DW_OP_lit17";
2607 case DW_OP_lit18:
2608 return "DW_OP_lit18";
2609 case DW_OP_lit19:
2610 return "DW_OP_lit19";
2611 case DW_OP_lit20:
2612 return "DW_OP_lit20";
2613 case DW_OP_lit21:
2614 return "DW_OP_lit21";
2615 case DW_OP_lit22:
2616 return "DW_OP_lit22";
2617 case DW_OP_lit23:
2618 return "DW_OP_lit23";
2619 case DW_OP_lit24:
2620 return "DW_OP_lit24";
2621 case DW_OP_lit25:
2622 return "DW_OP_lit25";
2623 case DW_OP_lit26:
2624 return "DW_OP_lit26";
2625 case DW_OP_lit27:
2626 return "DW_OP_lit27";
2627 case DW_OP_lit28:
2628 return "DW_OP_lit28";
2629 case DW_OP_lit29:
2630 return "DW_OP_lit29";
2631 case DW_OP_lit30:
2632 return "DW_OP_lit30";
2633 case DW_OP_lit31:
2634 return "DW_OP_lit31";
2635 case DW_OP_reg0:
2636 return "DW_OP_reg0";
2637 case DW_OP_reg1:
2638 return "DW_OP_reg1";
2639 case DW_OP_reg2:
2640 return "DW_OP_reg2";
2641 case DW_OP_reg3:
2642 return "DW_OP_reg3";
2643 case DW_OP_reg4:
2644 return "DW_OP_reg4";
2645 case DW_OP_reg5:
2646 return "DW_OP_reg5";
2647 case DW_OP_reg6:
2648 return "DW_OP_reg6";
2649 case DW_OP_reg7:
2650 return "DW_OP_reg7";
2651 case DW_OP_reg8:
2652 return "DW_OP_reg8";
2653 case DW_OP_reg9:
2654 return "DW_OP_reg9";
2655 case DW_OP_reg10:
2656 return "DW_OP_reg10";
2657 case DW_OP_reg11:
2658 return "DW_OP_reg11";
2659 case DW_OP_reg12:
2660 return "DW_OP_reg12";
2661 case DW_OP_reg13:
2662 return "DW_OP_reg13";
2663 case DW_OP_reg14:
2664 return "DW_OP_reg14";
2665 case DW_OP_reg15:
2666 return "DW_OP_reg15";
2667 case DW_OP_reg16:
2668 return "DW_OP_reg16";
2669 case DW_OP_reg17:
2670 return "DW_OP_reg17";
2671 case DW_OP_reg18:
2672 return "DW_OP_reg18";
2673 case DW_OP_reg19:
2674 return "DW_OP_reg19";
2675 case DW_OP_reg20:
2676 return "DW_OP_reg20";
2677 case DW_OP_reg21:
2678 return "DW_OP_reg21";
2679 case DW_OP_reg22:
2680 return "DW_OP_reg22";
2681 case DW_OP_reg23:
2682 return "DW_OP_reg23";
2683 case DW_OP_reg24:
2684 return "DW_OP_reg24";
2685 case DW_OP_reg25:
2686 return "DW_OP_reg25";
2687 case DW_OP_reg26:
2688 return "DW_OP_reg26";
2689 case DW_OP_reg27:
2690 return "DW_OP_reg27";
2691 case DW_OP_reg28:
2692 return "DW_OP_reg28";
2693 case DW_OP_reg29:
2694 return "DW_OP_reg29";
2695 case DW_OP_reg30:
2696 return "DW_OP_reg30";
2697 case DW_OP_reg31:
2698 return "DW_OP_reg31";
2699 case DW_OP_breg0:
2700 return "DW_OP_breg0";
2701 case DW_OP_breg1:
2702 return "DW_OP_breg1";
2703 case DW_OP_breg2:
2704 return "DW_OP_breg2";
2705 case DW_OP_breg3:
2706 return "DW_OP_breg3";
2707 case DW_OP_breg4:
2708 return "DW_OP_breg4";
2709 case DW_OP_breg5:
2710 return "DW_OP_breg5";
2711 case DW_OP_breg6:
2712 return "DW_OP_breg6";
2713 case DW_OP_breg7:
2714 return "DW_OP_breg7";
2715 case DW_OP_breg8:
2716 return "DW_OP_breg8";
2717 case DW_OP_breg9:
2718 return "DW_OP_breg9";
2719 case DW_OP_breg10:
2720 return "DW_OP_breg10";
2721 case DW_OP_breg11:
2722 return "DW_OP_breg11";
2723 case DW_OP_breg12:
2724 return "DW_OP_breg12";
2725 case DW_OP_breg13:
2726 return "DW_OP_breg13";
2727 case DW_OP_breg14:
2728 return "DW_OP_breg14";
2729 case DW_OP_breg15:
2730 return "DW_OP_breg15";
2731 case DW_OP_breg16:
2732 return "DW_OP_breg16";
2733 case DW_OP_breg17:
2734 return "DW_OP_breg17";
2735 case DW_OP_breg18:
2736 return "DW_OP_breg18";
2737 case DW_OP_breg19:
2738 return "DW_OP_breg19";
2739 case DW_OP_breg20:
2740 return "DW_OP_breg20";
2741 case DW_OP_breg21:
2742 return "DW_OP_breg21";
2743 case DW_OP_breg22:
2744 return "DW_OP_breg22";
2745 case DW_OP_breg23:
2746 return "DW_OP_breg23";
2747 case DW_OP_breg24:
2748 return "DW_OP_breg24";
2749 case DW_OP_breg25:
2750 return "DW_OP_breg25";
2751 case DW_OP_breg26:
2752 return "DW_OP_breg26";
2753 case DW_OP_breg27:
2754 return "DW_OP_breg27";
2755 case DW_OP_breg28:
2756 return "DW_OP_breg28";
2757 case DW_OP_breg29:
2758 return "DW_OP_breg29";
2759 case DW_OP_breg30:
2760 return "DW_OP_breg30";
2761 case DW_OP_breg31:
2762 return "DW_OP_breg31";
2763 case DW_OP_regx:
2764 return "DW_OP_regx";
2765 case DW_OP_fbreg:
2766 return "DW_OP_fbreg";
2767 case DW_OP_bregx:
2768 return "DW_OP_bregx";
2769 case DW_OP_piece:
2770 return "DW_OP_piece";
2771 case DW_OP_deref_size:
2772 return "DW_OP_deref_size";
2773 case DW_OP_xderef_size:
2774 return "DW_OP_xderef_size";
2775 case DW_OP_nop:
2776 return "DW_OP_nop";
2777 case DW_OP_push_object_address:
2778 return "DW_OP_push_object_address";
2779 case DW_OP_call2:
2780 return "DW_OP_call2";
2781 case DW_OP_call4:
2782 return "DW_OP_call4";
2783 case DW_OP_call_ref:
2784 return "DW_OP_call_ref";
2785 case DW_OP_GNU_push_tls_address:
2786 return "DW_OP_GNU_push_tls_address";
2787 default:
2788 return "OP_<unknown>";
2792 /* Return a pointer to a newly allocated location description. Location
2793 descriptions are simple expression terms that can be strung
2794 together to form more complicated location (address) descriptions. */
2796 static inline dw_loc_descr_ref
2797 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
2798 unsigned HOST_WIDE_INT oprnd2)
2800 dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
2802 descr->dw_loc_opc = op;
2803 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
2804 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
2805 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
2806 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
2808 return descr;
2812 /* Add a location description term to a location description expression. */
2814 static inline void
2815 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
2817 dw_loc_descr_ref *d;
2819 /* Find the end of the chain. */
2820 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
2823 *d = descr;
2826 /* Return the size of a location descriptor. */
2828 static unsigned long
2829 size_of_loc_descr (dw_loc_descr_ref loc)
2831 unsigned long size = 1;
2833 switch (loc->dw_loc_opc)
2835 case DW_OP_addr:
2836 case INTERNAL_DW_OP_tls_addr:
2837 size += DWARF2_ADDR_SIZE;
2838 break;
2839 case DW_OP_const1u:
2840 case DW_OP_const1s:
2841 size += 1;
2842 break;
2843 case DW_OP_const2u:
2844 case DW_OP_const2s:
2845 size += 2;
2846 break;
2847 case DW_OP_const4u:
2848 case DW_OP_const4s:
2849 size += 4;
2850 break;
2851 case DW_OP_const8u:
2852 case DW_OP_const8s:
2853 size += 8;
2854 break;
2855 case DW_OP_constu:
2856 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2857 break;
2858 case DW_OP_consts:
2859 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2860 break;
2861 case DW_OP_pick:
2862 size += 1;
2863 break;
2864 case DW_OP_plus_uconst:
2865 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2866 break;
2867 case DW_OP_skip:
2868 case DW_OP_bra:
2869 size += 2;
2870 break;
2871 case DW_OP_breg0:
2872 case DW_OP_breg1:
2873 case DW_OP_breg2:
2874 case DW_OP_breg3:
2875 case DW_OP_breg4:
2876 case DW_OP_breg5:
2877 case DW_OP_breg6:
2878 case DW_OP_breg7:
2879 case DW_OP_breg8:
2880 case DW_OP_breg9:
2881 case DW_OP_breg10:
2882 case DW_OP_breg11:
2883 case DW_OP_breg12:
2884 case DW_OP_breg13:
2885 case DW_OP_breg14:
2886 case DW_OP_breg15:
2887 case DW_OP_breg16:
2888 case DW_OP_breg17:
2889 case DW_OP_breg18:
2890 case DW_OP_breg19:
2891 case DW_OP_breg20:
2892 case DW_OP_breg21:
2893 case DW_OP_breg22:
2894 case DW_OP_breg23:
2895 case DW_OP_breg24:
2896 case DW_OP_breg25:
2897 case DW_OP_breg26:
2898 case DW_OP_breg27:
2899 case DW_OP_breg28:
2900 case DW_OP_breg29:
2901 case DW_OP_breg30:
2902 case DW_OP_breg31:
2903 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2904 break;
2905 case DW_OP_regx:
2906 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2907 break;
2908 case DW_OP_fbreg:
2909 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2910 break;
2911 case DW_OP_bregx:
2912 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2913 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
2914 break;
2915 case DW_OP_piece:
2916 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2917 break;
2918 case DW_OP_deref_size:
2919 case DW_OP_xderef_size:
2920 size += 1;
2921 break;
2922 case DW_OP_call2:
2923 size += 2;
2924 break;
2925 case DW_OP_call4:
2926 size += 4;
2927 break;
2928 case DW_OP_call_ref:
2929 size += DWARF2_ADDR_SIZE;
2930 break;
2931 default:
2932 break;
2935 return size;
2938 /* Return the size of a series of location descriptors. */
2940 static unsigned long
2941 size_of_locs (dw_loc_descr_ref loc)
2943 unsigned long size;
2945 for (size = 0; loc != NULL; loc = loc->dw_loc_next)
2947 loc->dw_loc_addr = size;
2948 size += size_of_loc_descr (loc);
2951 return size;
2954 /* Output location description stack opcode's operands (if any). */
2956 static void
2957 output_loc_operands (dw_loc_descr_ref loc)
2959 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2960 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2962 switch (loc->dw_loc_opc)
2964 #ifdef DWARF2_DEBUGGING_INFO
2965 case DW_OP_addr:
2966 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2967 break;
2968 case DW_OP_const2u:
2969 case DW_OP_const2s:
2970 dw2_asm_output_data (2, val1->v.val_int, NULL);
2971 break;
2972 case DW_OP_const4u:
2973 case DW_OP_const4s:
2974 dw2_asm_output_data (4, val1->v.val_int, NULL);
2975 break;
2976 case DW_OP_const8u:
2977 case DW_OP_const8s:
2978 if (HOST_BITS_PER_LONG < 64)
2979 abort ();
2980 dw2_asm_output_data (8, val1->v.val_int, NULL);
2981 break;
2982 case DW_OP_skip:
2983 case DW_OP_bra:
2985 int offset;
2987 if (val1->val_class == dw_val_class_loc)
2988 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2989 else
2990 abort ();
2992 dw2_asm_output_data (2, offset, NULL);
2994 break;
2995 #else
2996 case DW_OP_addr:
2997 case DW_OP_const2u:
2998 case DW_OP_const2s:
2999 case DW_OP_const4u:
3000 case DW_OP_const4s:
3001 case DW_OP_const8u:
3002 case DW_OP_const8s:
3003 case DW_OP_skip:
3004 case DW_OP_bra:
3005 /* We currently don't make any attempt to make sure these are
3006 aligned properly like we do for the main unwind info, so
3007 don't support emitting things larger than a byte if we're
3008 only doing unwinding. */
3009 abort ();
3010 #endif
3011 case DW_OP_const1u:
3012 case DW_OP_const1s:
3013 dw2_asm_output_data (1, val1->v.val_int, NULL);
3014 break;
3015 case DW_OP_constu:
3016 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3017 break;
3018 case DW_OP_consts:
3019 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3020 break;
3021 case DW_OP_pick:
3022 dw2_asm_output_data (1, val1->v.val_int, NULL);
3023 break;
3024 case DW_OP_plus_uconst:
3025 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3026 break;
3027 case DW_OP_breg0:
3028 case DW_OP_breg1:
3029 case DW_OP_breg2:
3030 case DW_OP_breg3:
3031 case DW_OP_breg4:
3032 case DW_OP_breg5:
3033 case DW_OP_breg6:
3034 case DW_OP_breg7:
3035 case DW_OP_breg8:
3036 case DW_OP_breg9:
3037 case DW_OP_breg10:
3038 case DW_OP_breg11:
3039 case DW_OP_breg12:
3040 case DW_OP_breg13:
3041 case DW_OP_breg14:
3042 case DW_OP_breg15:
3043 case DW_OP_breg16:
3044 case DW_OP_breg17:
3045 case DW_OP_breg18:
3046 case DW_OP_breg19:
3047 case DW_OP_breg20:
3048 case DW_OP_breg21:
3049 case DW_OP_breg22:
3050 case DW_OP_breg23:
3051 case DW_OP_breg24:
3052 case DW_OP_breg25:
3053 case DW_OP_breg26:
3054 case DW_OP_breg27:
3055 case DW_OP_breg28:
3056 case DW_OP_breg29:
3057 case DW_OP_breg30:
3058 case DW_OP_breg31:
3059 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3060 break;
3061 case DW_OP_regx:
3062 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3063 break;
3064 case DW_OP_fbreg:
3065 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3066 break;
3067 case DW_OP_bregx:
3068 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3069 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3070 break;
3071 case DW_OP_piece:
3072 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3073 break;
3074 case DW_OP_deref_size:
3075 case DW_OP_xderef_size:
3076 dw2_asm_output_data (1, val1->v.val_int, NULL);
3077 break;
3079 case INTERNAL_DW_OP_tls_addr:
3080 #ifdef ASM_OUTPUT_DWARF_DTPREL
3081 ASM_OUTPUT_DWARF_DTPREL (asm_out_file, DWARF2_ADDR_SIZE,
3082 val1->v.val_addr);
3083 fputc ('\n', asm_out_file);
3084 #else
3085 abort ();
3086 #endif
3087 break;
3089 default:
3090 /* Other codes have no operands. */
3091 break;
3095 /* Output a sequence of location operations. */
3097 static void
3098 output_loc_sequence (dw_loc_descr_ref loc)
3100 for (; loc != NULL; loc = loc->dw_loc_next)
3102 /* Output the opcode. */
3103 dw2_asm_output_data (1, loc->dw_loc_opc,
3104 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3106 /* Output the operand(s) (if any). */
3107 output_loc_operands (loc);
3111 /* This routine will generate the correct assembly data for a location
3112 description based on a cfi entry with a complex address. */
3114 static void
3115 output_cfa_loc (dw_cfi_ref cfi)
3117 dw_loc_descr_ref loc;
3118 unsigned long size;
3120 /* Output the size of the block. */
3121 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3122 size = size_of_locs (loc);
3123 dw2_asm_output_data_uleb128 (size, NULL);
3125 /* Now output the operations themselves. */
3126 output_loc_sequence (loc);
3129 /* This function builds a dwarf location descriptor sequence from
3130 a dw_cfa_location. */
3132 static struct dw_loc_descr_struct *
3133 build_cfa_loc (dw_cfa_location *cfa)
3135 struct dw_loc_descr_struct *head, *tmp;
3137 if (cfa->indirect == 0)
3138 abort ();
3140 if (cfa->base_offset)
3142 if (cfa->reg <= 31)
3143 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3144 else
3145 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3147 else if (cfa->reg <= 31)
3148 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3149 else
3150 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3152 head->dw_loc_oprnd1.val_class = dw_val_class_const;
3153 tmp = new_loc_descr (DW_OP_deref, 0, 0);
3154 add_loc_descr (&head, tmp);
3155 if (cfa->offset != 0)
3157 tmp = new_loc_descr (DW_OP_plus_uconst, cfa->offset, 0);
3158 add_loc_descr (&head, tmp);
3161 return head;
3164 /* This function fills in aa dw_cfa_location structure from a dwarf location
3165 descriptor sequence. */
3167 static void
3168 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3170 struct dw_loc_descr_struct *ptr;
3171 cfa->offset = 0;
3172 cfa->base_offset = 0;
3173 cfa->indirect = 0;
3174 cfa->reg = -1;
3176 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3178 enum dwarf_location_atom op = ptr->dw_loc_opc;
3180 switch (op)
3182 case DW_OP_reg0:
3183 case DW_OP_reg1:
3184 case DW_OP_reg2:
3185 case DW_OP_reg3:
3186 case DW_OP_reg4:
3187 case DW_OP_reg5:
3188 case DW_OP_reg6:
3189 case DW_OP_reg7:
3190 case DW_OP_reg8:
3191 case DW_OP_reg9:
3192 case DW_OP_reg10:
3193 case DW_OP_reg11:
3194 case DW_OP_reg12:
3195 case DW_OP_reg13:
3196 case DW_OP_reg14:
3197 case DW_OP_reg15:
3198 case DW_OP_reg16:
3199 case DW_OP_reg17:
3200 case DW_OP_reg18:
3201 case DW_OP_reg19:
3202 case DW_OP_reg20:
3203 case DW_OP_reg21:
3204 case DW_OP_reg22:
3205 case DW_OP_reg23:
3206 case DW_OP_reg24:
3207 case DW_OP_reg25:
3208 case DW_OP_reg26:
3209 case DW_OP_reg27:
3210 case DW_OP_reg28:
3211 case DW_OP_reg29:
3212 case DW_OP_reg30:
3213 case DW_OP_reg31:
3214 cfa->reg = op - DW_OP_reg0;
3215 break;
3216 case DW_OP_regx:
3217 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3218 break;
3219 case DW_OP_breg0:
3220 case DW_OP_breg1:
3221 case DW_OP_breg2:
3222 case DW_OP_breg3:
3223 case DW_OP_breg4:
3224 case DW_OP_breg5:
3225 case DW_OP_breg6:
3226 case DW_OP_breg7:
3227 case DW_OP_breg8:
3228 case DW_OP_breg9:
3229 case DW_OP_breg10:
3230 case DW_OP_breg11:
3231 case DW_OP_breg12:
3232 case DW_OP_breg13:
3233 case DW_OP_breg14:
3234 case DW_OP_breg15:
3235 case DW_OP_breg16:
3236 case DW_OP_breg17:
3237 case DW_OP_breg18:
3238 case DW_OP_breg19:
3239 case DW_OP_breg20:
3240 case DW_OP_breg21:
3241 case DW_OP_breg22:
3242 case DW_OP_breg23:
3243 case DW_OP_breg24:
3244 case DW_OP_breg25:
3245 case DW_OP_breg26:
3246 case DW_OP_breg27:
3247 case DW_OP_breg28:
3248 case DW_OP_breg29:
3249 case DW_OP_breg30:
3250 case DW_OP_breg31:
3251 cfa->reg = op - DW_OP_breg0;
3252 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3253 break;
3254 case DW_OP_bregx:
3255 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3256 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3257 break;
3258 case DW_OP_deref:
3259 cfa->indirect = 1;
3260 break;
3261 case DW_OP_plus_uconst:
3262 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3263 break;
3264 default:
3265 internal_error ("DW_LOC_OP %s not implemented\n",
3266 dwarf_stack_op_name (ptr->dw_loc_opc));
3270 #endif /* .debug_frame support */
3272 /* And now, the support for symbolic debugging information. */
3273 #ifdef DWARF2_DEBUGGING_INFO
3275 /* .debug_str support. */
3276 static int output_indirect_string (void **, void *);
3278 static void dwarf2out_init (const char *);
3279 static void dwarf2out_finish (const char *);
3280 static void dwarf2out_define (unsigned int, const char *);
3281 static void dwarf2out_undef (unsigned int, const char *);
3282 static void dwarf2out_start_source_file (unsigned, const char *);
3283 static void dwarf2out_end_source_file (unsigned);
3284 static void dwarf2out_begin_block (unsigned, unsigned);
3285 static void dwarf2out_end_block (unsigned, unsigned);
3286 static bool dwarf2out_ignore_block (tree);
3287 static void dwarf2out_global_decl (tree);
3288 static void dwarf2out_type_decl (tree, int);
3289 static void dwarf2out_imported_module_or_decl (tree, tree);
3290 static void dwarf2out_abstract_function (tree);
3291 static void dwarf2out_var_location (rtx);
3292 static void dwarf2out_begin_function (tree);
3294 /* The debug hooks structure. */
3296 const struct gcc_debug_hooks dwarf2_debug_hooks =
3298 dwarf2out_init,
3299 dwarf2out_finish,
3300 dwarf2out_define,
3301 dwarf2out_undef,
3302 dwarf2out_start_source_file,
3303 dwarf2out_end_source_file,
3304 dwarf2out_begin_block,
3305 dwarf2out_end_block,
3306 dwarf2out_ignore_block,
3307 dwarf2out_source_line,
3308 dwarf2out_begin_prologue,
3309 debug_nothing_int_charstar, /* end_prologue */
3310 dwarf2out_end_epilogue,
3311 dwarf2out_begin_function,
3312 debug_nothing_int, /* end_function */
3313 dwarf2out_decl, /* function_decl */
3314 dwarf2out_global_decl,
3315 dwarf2out_type_decl, /* type_decl */
3316 dwarf2out_imported_module_or_decl,
3317 debug_nothing_tree, /* deferred_inline_function */
3318 /* The DWARF 2 backend tries to reduce debugging bloat by not
3319 emitting the abstract description of inline functions until
3320 something tries to reference them. */
3321 dwarf2out_abstract_function, /* outlining_inline_function */
3322 debug_nothing_rtx, /* label */
3323 debug_nothing_int, /* handle_pch */
3324 dwarf2out_var_location
3326 #endif
3328 /* NOTE: In the comments in this file, many references are made to
3329 "Debugging Information Entries". This term is abbreviated as `DIE'
3330 throughout the remainder of this file. */
3332 /* An internal representation of the DWARF output is built, and then
3333 walked to generate the DWARF debugging info. The walk of the internal
3334 representation is done after the entire program has been compiled.
3335 The types below are used to describe the internal representation. */
3337 /* Various DIE's use offsets relative to the beginning of the
3338 .debug_info section to refer to each other. */
3340 typedef long int dw_offset;
3342 /* Define typedefs here to avoid circular dependencies. */
3344 typedef struct dw_attr_struct *dw_attr_ref;
3345 typedef struct dw_line_info_struct *dw_line_info_ref;
3346 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3347 typedef struct pubname_struct *pubname_ref;
3348 typedef struct dw_ranges_struct *dw_ranges_ref;
3350 /* Each entry in the line_info_table maintains the file and
3351 line number associated with the label generated for that
3352 entry. The label gives the PC value associated with
3353 the line number entry. */
3355 typedef struct dw_line_info_struct GTY(())
3357 unsigned long dw_file_num;
3358 unsigned long dw_line_num;
3360 dw_line_info_entry;
3362 /* Line information for functions in separate sections; each one gets its
3363 own sequence. */
3364 typedef struct dw_separate_line_info_struct GTY(())
3366 unsigned long dw_file_num;
3367 unsigned long dw_line_num;
3368 unsigned long function;
3370 dw_separate_line_info_entry;
3372 /* Each DIE attribute has a field specifying the attribute kind,
3373 a link to the next attribute in the chain, and an attribute value.
3374 Attributes are typically linked below the DIE they modify. */
3376 typedef struct dw_attr_struct GTY(())
3378 enum dwarf_attribute dw_attr;
3379 dw_attr_ref dw_attr_next;
3380 dw_val_node dw_attr_val;
3382 dw_attr_node;
3384 /* The Debugging Information Entry (DIE) structure */
3386 typedef struct die_struct GTY(())
3388 enum dwarf_tag die_tag;
3389 char *die_symbol;
3390 dw_attr_ref die_attr;
3391 dw_die_ref die_parent;
3392 dw_die_ref die_child;
3393 dw_die_ref die_sib;
3394 dw_die_ref die_definition; /* ref from a specification to its definition */
3395 dw_offset die_offset;
3396 unsigned long die_abbrev;
3397 int die_mark;
3398 unsigned int decl_id;
3400 die_node;
3402 /* The pubname structure */
3404 typedef struct pubname_struct GTY(())
3406 dw_die_ref die;
3407 char *name;
3409 pubname_entry;
3411 struct dw_ranges_struct GTY(())
3413 int block_num;
3416 /* The limbo die list structure. */
3417 typedef struct limbo_die_struct GTY(())
3419 dw_die_ref die;
3420 tree created_for;
3421 struct limbo_die_struct *next;
3423 limbo_die_node;
3425 /* How to start an assembler comment. */
3426 #ifndef ASM_COMMENT_START
3427 #define ASM_COMMENT_START ";#"
3428 #endif
3430 /* Define a macro which returns nonzero for a TYPE_DECL which was
3431 implicitly generated for a tagged type.
3433 Note that unlike the gcc front end (which generates a NULL named
3434 TYPE_DECL node for each complete tagged type, each array type, and
3435 each function type node created) the g++ front end generates a
3436 _named_ TYPE_DECL node for each tagged type node created.
3437 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3438 generate a DW_TAG_typedef DIE for them. */
3440 #define TYPE_DECL_IS_STUB(decl) \
3441 (DECL_NAME (decl) == NULL_TREE \
3442 || (DECL_ARTIFICIAL (decl) \
3443 && is_tagged_type (TREE_TYPE (decl)) \
3444 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3445 /* This is necessary for stub decls that \
3446 appear in nested inline functions. */ \
3447 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3448 && (decl_ultimate_origin (decl) \
3449 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3451 /* Information concerning the compilation unit's programming
3452 language, and compiler version. */
3454 /* Fixed size portion of the DWARF compilation unit header. */
3455 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3456 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3458 /* Fixed size portion of public names info. */
3459 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3461 /* Fixed size portion of the address range info. */
3462 #define DWARF_ARANGES_HEADER_SIZE \
3463 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3464 DWARF2_ADDR_SIZE * 2) \
3465 - DWARF_INITIAL_LENGTH_SIZE)
3467 /* Size of padding portion in the address range info. It must be
3468 aligned to twice the pointer size. */
3469 #define DWARF_ARANGES_PAD_SIZE \
3470 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3471 DWARF2_ADDR_SIZE * 2) \
3472 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3474 /* Use assembler line directives if available. */
3475 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3476 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3477 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3478 #else
3479 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3480 #endif
3481 #endif
3483 /* Minimum line offset in a special line info. opcode.
3484 This value was chosen to give a reasonable range of values. */
3485 #define DWARF_LINE_BASE -10
3487 /* First special line opcode - leave room for the standard opcodes. */
3488 #define DWARF_LINE_OPCODE_BASE 10
3490 /* Range of line offsets in a special line info. opcode. */
3491 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3493 /* Flag that indicates the initial value of the is_stmt_start flag.
3494 In the present implementation, we do not mark any lines as
3495 the beginning of a source statement, because that information
3496 is not made available by the GCC front-end. */
3497 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3499 #ifdef DWARF2_DEBUGGING_INFO
3500 /* This location is used by calc_die_sizes() to keep track
3501 the offset of each DIE within the .debug_info section. */
3502 static unsigned long next_die_offset;
3503 #endif
3505 /* Record the root of the DIE's built for the current compilation unit. */
3506 static GTY(()) dw_die_ref comp_unit_die;
3508 /* A list of DIEs with a NULL parent waiting to be relocated. */
3509 static GTY(()) limbo_die_node *limbo_die_list;
3511 /* Filenames referenced by this compilation unit. */
3512 static GTY(()) varray_type file_table;
3513 static GTY(()) varray_type file_table_emitted;
3514 static GTY(()) size_t file_table_last_lookup_index;
3516 /* A hash table of references to DIE's that describe declarations.
3517 The key is a DECL_UID() which is a unique number identifying each decl. */
3518 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3520 /* Node of the variable location list. */
3521 struct var_loc_node GTY ((chain_next ("%h.next")))
3523 rtx GTY (()) var_loc_note;
3524 const char * GTY (()) label;
3525 struct var_loc_node * GTY (()) next;
3528 /* Variable location list. */
3529 struct var_loc_list_def GTY (())
3531 struct var_loc_node * GTY (()) first;
3533 /* Do not mark the last element of the chained list because
3534 it is marked through the chain. */
3535 struct var_loc_node * GTY ((skip ("%h"))) last;
3537 /* DECL_UID of the variable decl. */
3538 unsigned int decl_id;
3540 typedef struct var_loc_list_def var_loc_list;
3543 /* Table of decl location linked lists. */
3544 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3546 /* A pointer to the base of a list of references to DIE's that
3547 are uniquely identified by their tag, presence/absence of
3548 children DIE's, and list of attribute/value pairs. */
3549 static GTY((length ("abbrev_die_table_allocated")))
3550 dw_die_ref *abbrev_die_table;
3552 /* Number of elements currently allocated for abbrev_die_table. */
3553 static GTY(()) unsigned abbrev_die_table_allocated;
3555 /* Number of elements in type_die_table currently in use. */
3556 static GTY(()) unsigned abbrev_die_table_in_use;
3558 /* Size (in elements) of increments by which we may expand the
3559 abbrev_die_table. */
3560 #define ABBREV_DIE_TABLE_INCREMENT 256
3562 /* A pointer to the base of a table that contains line information
3563 for each source code line in .text in the compilation unit. */
3564 static GTY((length ("line_info_table_allocated")))
3565 dw_line_info_ref line_info_table;
3567 /* Number of elements currently allocated for line_info_table. */
3568 static GTY(()) unsigned line_info_table_allocated;
3570 /* Number of elements in line_info_table currently in use. */
3571 static GTY(()) unsigned line_info_table_in_use;
3573 /* A pointer to the base of a table that contains line information
3574 for each source code line outside of .text in the compilation unit. */
3575 static GTY ((length ("separate_line_info_table_allocated")))
3576 dw_separate_line_info_ref separate_line_info_table;
3578 /* Number of elements currently allocated for separate_line_info_table. */
3579 static GTY(()) unsigned separate_line_info_table_allocated;
3581 /* Number of elements in separate_line_info_table currently in use. */
3582 static GTY(()) unsigned separate_line_info_table_in_use;
3584 /* Size (in elements) of increments by which we may expand the
3585 line_info_table. */
3586 #define LINE_INFO_TABLE_INCREMENT 1024
3588 /* A pointer to the base of a table that contains a list of publicly
3589 accessible names. */
3590 static GTY ((length ("pubname_table_allocated"))) pubname_ref pubname_table;
3592 /* Number of elements currently allocated for pubname_table. */
3593 static GTY(()) unsigned pubname_table_allocated;
3595 /* Number of elements in pubname_table currently in use. */
3596 static GTY(()) unsigned pubname_table_in_use;
3598 /* Size (in elements) of increments by which we may expand the
3599 pubname_table. */
3600 #define PUBNAME_TABLE_INCREMENT 64
3602 /* Array of dies for which we should generate .debug_arange info. */
3603 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3605 /* Number of elements currently allocated for arange_table. */
3606 static GTY(()) unsigned arange_table_allocated;
3608 /* Number of elements in arange_table currently in use. */
3609 static GTY(()) unsigned arange_table_in_use;
3611 /* Size (in elements) of increments by which we may expand the
3612 arange_table. */
3613 #define ARANGE_TABLE_INCREMENT 64
3615 /* Array of dies for which we should generate .debug_ranges info. */
3616 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3618 /* Number of elements currently allocated for ranges_table. */
3619 static GTY(()) unsigned ranges_table_allocated;
3621 /* Number of elements in ranges_table currently in use. */
3622 static GTY(()) unsigned ranges_table_in_use;
3624 /* Size (in elements) of increments by which we may expand the
3625 ranges_table. */
3626 #define RANGES_TABLE_INCREMENT 64
3628 /* Whether we have location lists that need outputting */
3629 static GTY(()) unsigned have_location_lists;
3631 /* Unique label counter. */
3632 static GTY(()) unsigned int loclabel_num;
3634 #ifdef DWARF2_DEBUGGING_INFO
3635 /* Record whether the function being analyzed contains inlined functions. */
3636 static int current_function_has_inlines;
3637 #endif
3638 #if 0 && defined (MIPS_DEBUGGING_INFO)
3639 static int comp_unit_has_inlines;
3640 #endif
3642 /* Number of file tables emitted in maybe_emit_file(). */
3643 static GTY(()) int emitcount = 0;
3645 /* Number of internal labels generated by gen_internal_sym(). */
3646 static GTY(()) int label_num;
3648 #ifdef DWARF2_DEBUGGING_INFO
3650 /* Forward declarations for functions defined in this file. */
3652 static int is_pseudo_reg (rtx);
3653 static tree type_main_variant (tree);
3654 static int is_tagged_type (tree);
3655 static const char *dwarf_tag_name (unsigned);
3656 static const char *dwarf_attr_name (unsigned);
3657 static const char *dwarf_form_name (unsigned);
3658 #if 0
3659 static const char *dwarf_type_encoding_name (unsigned);
3660 #endif
3661 static tree decl_ultimate_origin (tree);
3662 static tree block_ultimate_origin (tree);
3663 static tree decl_class_context (tree);
3664 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3665 static inline enum dw_val_class AT_class (dw_attr_ref);
3666 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3667 static inline unsigned AT_flag (dw_attr_ref);
3668 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3669 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3670 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3671 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3672 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
3673 unsigned long);
3674 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3675 unsigned int, unsigned char *);
3676 static hashval_t debug_str_do_hash (const void *);
3677 static int debug_str_eq (const void *, const void *);
3678 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3679 static inline const char *AT_string (dw_attr_ref);
3680 static int AT_string_form (dw_attr_ref);
3681 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3682 static void add_AT_specification (dw_die_ref, dw_die_ref);
3683 static inline dw_die_ref AT_ref (dw_attr_ref);
3684 static inline int AT_ref_external (dw_attr_ref);
3685 static inline void set_AT_ref_external (dw_attr_ref, int);
3686 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3687 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3688 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3689 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3690 dw_loc_list_ref);
3691 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3692 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3693 static inline rtx AT_addr (dw_attr_ref);
3694 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3695 static void add_AT_lbl_offset (dw_die_ref, enum dwarf_attribute, const char *);
3696 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3697 unsigned HOST_WIDE_INT);
3698 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3699 unsigned long);
3700 static inline const char *AT_lbl (dw_attr_ref);
3701 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3702 static const char *get_AT_low_pc (dw_die_ref);
3703 static const char *get_AT_hi_pc (dw_die_ref);
3704 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3705 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3706 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3707 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3708 static bool is_c_family (void);
3709 static bool is_cxx (void);
3710 static bool is_java (void);
3711 static bool is_fortran (void);
3712 static bool is_ada (void);
3713 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3714 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3715 static inline void free_die (dw_die_ref);
3716 static void remove_children (dw_die_ref);
3717 static void add_child_die (dw_die_ref, dw_die_ref);
3718 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3719 static dw_die_ref lookup_type_die (tree);
3720 static void equate_type_number_to_die (tree, dw_die_ref);
3721 static hashval_t decl_die_table_hash (const void *);
3722 static int decl_die_table_eq (const void *, const void *);
3723 static dw_die_ref lookup_decl_die (tree);
3724 static hashval_t decl_loc_table_hash (const void *);
3725 static int decl_loc_table_eq (const void *, const void *);
3726 static var_loc_list *lookup_decl_loc (tree);
3727 static void equate_decl_number_to_die (tree, dw_die_ref);
3728 static void add_var_loc_to_decl (tree, struct var_loc_node *);
3729 static void print_spaces (FILE *);
3730 static void print_die (dw_die_ref, FILE *);
3731 static void print_dwarf_line_table (FILE *);
3732 static void reverse_die_lists (dw_die_ref);
3733 static void reverse_all_dies (dw_die_ref);
3734 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3735 static dw_die_ref pop_compile_unit (dw_die_ref);
3736 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3737 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3738 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3739 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3740 static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
3741 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3742 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3743 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3744 static void compute_section_prefix (dw_die_ref);
3745 static int is_type_die (dw_die_ref);
3746 static int is_comdat_die (dw_die_ref);
3747 static int is_symbol_die (dw_die_ref);
3748 static void assign_symbol_names (dw_die_ref);
3749 static void break_out_includes (dw_die_ref);
3750 static hashval_t htab_cu_hash (const void *);
3751 static int htab_cu_eq (const void *, const void *);
3752 static void htab_cu_del (void *);
3753 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3754 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3755 static void add_sibling_attributes (dw_die_ref);
3756 static void build_abbrev_table (dw_die_ref);
3757 static void output_location_lists (dw_die_ref);
3758 static int constant_size (long unsigned);
3759 static unsigned long size_of_die (dw_die_ref);
3760 static void calc_die_sizes (dw_die_ref);
3761 static void mark_dies (dw_die_ref);
3762 static void unmark_dies (dw_die_ref);
3763 static void unmark_all_dies (dw_die_ref);
3764 static unsigned long size_of_pubnames (void);
3765 static unsigned long size_of_aranges (void);
3766 static enum dwarf_form value_format (dw_attr_ref);
3767 static void output_value_format (dw_attr_ref);
3768 static void output_abbrev_section (void);
3769 static void output_die_symbol (dw_die_ref);
3770 static void output_die (dw_die_ref);
3771 static void output_compilation_unit_header (void);
3772 static void output_comp_unit (dw_die_ref, int);
3773 static const char *dwarf2_name (tree, int);
3774 static void add_pubname (tree, dw_die_ref);
3775 static void output_pubnames (void);
3776 static void add_arange (tree, dw_die_ref);
3777 static void output_aranges (void);
3778 static unsigned int add_ranges (tree);
3779 static void output_ranges (void);
3780 static void output_line_info (void);
3781 static void output_file_names (void);
3782 static dw_die_ref base_type_die (tree);
3783 static tree root_type (tree);
3784 static int is_base_type (tree);
3785 static bool is_subrange_type (tree);
3786 static dw_die_ref subrange_type_die (tree, dw_die_ref);
3787 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3788 static int type_is_enum (tree);
3789 static unsigned int dbx_reg_number (rtx);
3790 static dw_loc_descr_ref reg_loc_descriptor (rtx);
3791 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
3792 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
3793 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3794 static dw_loc_descr_ref based_loc_descr (unsigned, HOST_WIDE_INT, bool);
3795 static int is_based_loc (rtx);
3796 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode, bool);
3797 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
3798 static dw_loc_descr_ref loc_descriptor (rtx, bool);
3799 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3800 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3801 static tree field_type (tree);
3802 static unsigned int simple_type_align_in_bits (tree);
3803 static unsigned int simple_decl_align_in_bits (tree);
3804 static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
3805 static HOST_WIDE_INT field_byte_offset (tree);
3806 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3807 dw_loc_descr_ref);
3808 static void add_data_member_location_attribute (dw_die_ref, tree);
3809 static void add_const_value_attribute (dw_die_ref, rtx);
3810 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3811 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
3812 static void insert_float (rtx, unsigned char *);
3813 static rtx rtl_for_decl_location (tree);
3814 static void add_location_or_const_value_attribute (dw_die_ref, tree,
3815 enum dwarf_attribute);
3816 static void tree_add_const_value_attribute (dw_die_ref, tree);
3817 static void add_name_attribute (dw_die_ref, const char *);
3818 static void add_comp_dir_attribute (dw_die_ref);
3819 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3820 static void add_subscript_info (dw_die_ref, tree);
3821 static void add_byte_size_attribute (dw_die_ref, tree);
3822 static void add_bit_offset_attribute (dw_die_ref, tree);
3823 static void add_bit_size_attribute (dw_die_ref, tree);
3824 static void add_prototyped_attribute (dw_die_ref, tree);
3825 static void add_abstract_origin_attribute (dw_die_ref, tree);
3826 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3827 static void add_src_coords_attributes (dw_die_ref, tree);
3828 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3829 static void push_decl_scope (tree);
3830 static void pop_decl_scope (void);
3831 static dw_die_ref scope_die_for (tree, dw_die_ref);
3832 static inline int local_scope_p (dw_die_ref);
3833 static inline int class_or_namespace_scope_p (dw_die_ref);
3834 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3835 static const char *type_tag (tree);
3836 static tree member_declared_type (tree);
3837 #if 0
3838 static const char *decl_start_label (tree);
3839 #endif
3840 static void gen_array_type_die (tree, dw_die_ref);
3841 static void gen_set_type_die (tree, dw_die_ref);
3842 #if 0
3843 static void gen_entry_point_die (tree, dw_die_ref);
3844 #endif
3845 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
3846 static void gen_inlined_structure_type_die (tree, dw_die_ref);
3847 static void gen_inlined_union_type_die (tree, dw_die_ref);
3848 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3849 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
3850 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3851 static void gen_formal_types_die (tree, dw_die_ref);
3852 static void gen_subprogram_die (tree, dw_die_ref);
3853 static void gen_variable_die (tree, dw_die_ref);
3854 static void gen_label_die (tree, dw_die_ref);
3855 static void gen_lexical_block_die (tree, dw_die_ref, int);
3856 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3857 static void gen_field_die (tree, dw_die_ref);
3858 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3859 static dw_die_ref gen_compile_unit_die (const char *);
3860 static void gen_string_type_die (tree, dw_die_ref);
3861 static void gen_inheritance_die (tree, tree, dw_die_ref);
3862 static void gen_member_die (tree, dw_die_ref);
3863 static void gen_struct_or_union_type_die (tree, dw_die_ref);
3864 static void gen_subroutine_type_die (tree, dw_die_ref);
3865 static void gen_typedef_die (tree, dw_die_ref);
3866 static void gen_type_die (tree, dw_die_ref);
3867 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
3868 static void gen_block_die (tree, dw_die_ref, int);
3869 static void decls_for_scope (tree, dw_die_ref, int);
3870 static int is_redundant_typedef (tree);
3871 static void gen_namespace_die (tree);
3872 static void gen_decl_die (tree, dw_die_ref);
3873 static dw_die_ref force_decl_die (tree);
3874 static dw_die_ref force_type_die (tree);
3875 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3876 static void declare_in_namespace (tree, dw_die_ref);
3877 static unsigned lookup_filename (const char *);
3878 static void init_file_table (void);
3879 static void retry_incomplete_types (void);
3880 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3881 static void splice_child_die (dw_die_ref, dw_die_ref);
3882 static int file_info_cmp (const void *, const void *);
3883 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3884 const char *, const char *, unsigned);
3885 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
3886 const char *, const char *,
3887 const char *);
3888 static void output_loc_list (dw_loc_list_ref);
3889 static char *gen_internal_sym (const char *);
3891 static void prune_unmark_dies (dw_die_ref);
3892 static void prune_unused_types_mark (dw_die_ref, int);
3893 static void prune_unused_types_walk (dw_die_ref);
3894 static void prune_unused_types_walk_attribs (dw_die_ref);
3895 static void prune_unused_types_prune (dw_die_ref);
3896 static void prune_unused_types (void);
3897 static int maybe_emit_file (int);
3899 /* Section names used to hold DWARF debugging information. */
3900 #ifndef DEBUG_INFO_SECTION
3901 #define DEBUG_INFO_SECTION ".debug_info"
3902 #endif
3903 #ifndef DEBUG_ABBREV_SECTION
3904 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3905 #endif
3906 #ifndef DEBUG_ARANGES_SECTION
3907 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3908 #endif
3909 #ifndef DEBUG_MACINFO_SECTION
3910 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3911 #endif
3912 #ifndef DEBUG_LINE_SECTION
3913 #define DEBUG_LINE_SECTION ".debug_line"
3914 #endif
3915 #ifndef DEBUG_LOC_SECTION
3916 #define DEBUG_LOC_SECTION ".debug_loc"
3917 #endif
3918 #ifndef DEBUG_PUBNAMES_SECTION
3919 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
3920 #endif
3921 #ifndef DEBUG_STR_SECTION
3922 #define DEBUG_STR_SECTION ".debug_str"
3923 #endif
3924 #ifndef DEBUG_RANGES_SECTION
3925 #define DEBUG_RANGES_SECTION ".debug_ranges"
3926 #endif
3928 /* Standard ELF section names for compiled code and data. */
3929 #ifndef TEXT_SECTION_NAME
3930 #define TEXT_SECTION_NAME ".text"
3931 #endif
3933 /* Section flags for .debug_str section. */
3934 #define DEBUG_STR_SECTION_FLAGS \
3935 (HAVE_GAS_SHF_MERGE && flag_merge_constants \
3936 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3937 : SECTION_DEBUG)
3939 /* Labels we insert at beginning sections we can reference instead of
3940 the section names themselves. */
3942 #ifndef TEXT_SECTION_LABEL
3943 #define TEXT_SECTION_LABEL "Ltext"
3944 #endif
3945 #ifndef DEBUG_LINE_SECTION_LABEL
3946 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3947 #endif
3948 #ifndef DEBUG_INFO_SECTION_LABEL
3949 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3950 #endif
3951 #ifndef DEBUG_ABBREV_SECTION_LABEL
3952 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3953 #endif
3954 #ifndef DEBUG_LOC_SECTION_LABEL
3955 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3956 #endif
3957 #ifndef DEBUG_RANGES_SECTION_LABEL
3958 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3959 #endif
3960 #ifndef DEBUG_MACINFO_SECTION_LABEL
3961 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3962 #endif
3964 /* Definitions of defaults for formats and names of various special
3965 (artificial) labels which may be generated within this file (when the -g
3966 options is used and DWARF2_DEBUGGING_INFO is in effect.
3967 If necessary, these may be overridden from within the tm.h file, but
3968 typically, overriding these defaults is unnecessary. */
3970 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3971 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3972 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3973 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3974 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3975 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3976 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3977 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3979 #ifndef TEXT_END_LABEL
3980 #define TEXT_END_LABEL "Letext"
3981 #endif
3982 #ifndef BLOCK_BEGIN_LABEL
3983 #define BLOCK_BEGIN_LABEL "LBB"
3984 #endif
3985 #ifndef BLOCK_END_LABEL
3986 #define BLOCK_END_LABEL "LBE"
3987 #endif
3988 #ifndef LINE_CODE_LABEL
3989 #define LINE_CODE_LABEL "LM"
3990 #endif
3991 #ifndef SEPARATE_LINE_CODE_LABEL
3992 #define SEPARATE_LINE_CODE_LABEL "LSM"
3993 #endif
3995 /* We allow a language front-end to designate a function that is to be
3996 called to "demangle" any name before it it put into a DIE. */
3998 static const char *(*demangle_name_func) (const char *);
4000 void
4001 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4003 demangle_name_func = func;
4006 /* Test if rtl node points to a pseudo register. */
4008 static inline int
4009 is_pseudo_reg (rtx rtl)
4011 return ((GET_CODE (rtl) == REG && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4012 || (GET_CODE (rtl) == SUBREG
4013 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4016 /* Return a reference to a type, with its const and volatile qualifiers
4017 removed. */
4019 static inline tree
4020 type_main_variant (tree type)
4022 type = TYPE_MAIN_VARIANT (type);
4024 /* ??? There really should be only one main variant among any group of
4025 variants of a given type (and all of the MAIN_VARIANT values for all
4026 members of the group should point to that one type) but sometimes the C
4027 front-end messes this up for array types, so we work around that bug
4028 here. */
4029 if (TREE_CODE (type) == ARRAY_TYPE)
4030 while (type != TYPE_MAIN_VARIANT (type))
4031 type = TYPE_MAIN_VARIANT (type);
4033 return type;
4036 /* Return nonzero if the given type node represents a tagged type. */
4038 static inline int
4039 is_tagged_type (tree type)
4041 enum tree_code code = TREE_CODE (type);
4043 return (code == RECORD_TYPE || code == UNION_TYPE
4044 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4047 /* Convert a DIE tag into its string name. */
4049 static const char *
4050 dwarf_tag_name (unsigned int tag)
4052 switch (tag)
4054 case DW_TAG_padding:
4055 return "DW_TAG_padding";
4056 case DW_TAG_array_type:
4057 return "DW_TAG_array_type";
4058 case DW_TAG_class_type:
4059 return "DW_TAG_class_type";
4060 case DW_TAG_entry_point:
4061 return "DW_TAG_entry_point";
4062 case DW_TAG_enumeration_type:
4063 return "DW_TAG_enumeration_type";
4064 case DW_TAG_formal_parameter:
4065 return "DW_TAG_formal_parameter";
4066 case DW_TAG_imported_declaration:
4067 return "DW_TAG_imported_declaration";
4068 case DW_TAG_label:
4069 return "DW_TAG_label";
4070 case DW_TAG_lexical_block:
4071 return "DW_TAG_lexical_block";
4072 case DW_TAG_member:
4073 return "DW_TAG_member";
4074 case DW_TAG_pointer_type:
4075 return "DW_TAG_pointer_type";
4076 case DW_TAG_reference_type:
4077 return "DW_TAG_reference_type";
4078 case DW_TAG_compile_unit:
4079 return "DW_TAG_compile_unit";
4080 case DW_TAG_string_type:
4081 return "DW_TAG_string_type";
4082 case DW_TAG_structure_type:
4083 return "DW_TAG_structure_type";
4084 case DW_TAG_subroutine_type:
4085 return "DW_TAG_subroutine_type";
4086 case DW_TAG_typedef:
4087 return "DW_TAG_typedef";
4088 case DW_TAG_union_type:
4089 return "DW_TAG_union_type";
4090 case DW_TAG_unspecified_parameters:
4091 return "DW_TAG_unspecified_parameters";
4092 case DW_TAG_variant:
4093 return "DW_TAG_variant";
4094 case DW_TAG_common_block:
4095 return "DW_TAG_common_block";
4096 case DW_TAG_common_inclusion:
4097 return "DW_TAG_common_inclusion";
4098 case DW_TAG_inheritance:
4099 return "DW_TAG_inheritance";
4100 case DW_TAG_inlined_subroutine:
4101 return "DW_TAG_inlined_subroutine";
4102 case DW_TAG_module:
4103 return "DW_TAG_module";
4104 case DW_TAG_ptr_to_member_type:
4105 return "DW_TAG_ptr_to_member_type";
4106 case DW_TAG_set_type:
4107 return "DW_TAG_set_type";
4108 case DW_TAG_subrange_type:
4109 return "DW_TAG_subrange_type";
4110 case DW_TAG_with_stmt:
4111 return "DW_TAG_with_stmt";
4112 case DW_TAG_access_declaration:
4113 return "DW_TAG_access_declaration";
4114 case DW_TAG_base_type:
4115 return "DW_TAG_base_type";
4116 case DW_TAG_catch_block:
4117 return "DW_TAG_catch_block";
4118 case DW_TAG_const_type:
4119 return "DW_TAG_const_type";
4120 case DW_TAG_constant:
4121 return "DW_TAG_constant";
4122 case DW_TAG_enumerator:
4123 return "DW_TAG_enumerator";
4124 case DW_TAG_file_type:
4125 return "DW_TAG_file_type";
4126 case DW_TAG_friend:
4127 return "DW_TAG_friend";
4128 case DW_TAG_namelist:
4129 return "DW_TAG_namelist";
4130 case DW_TAG_namelist_item:
4131 return "DW_TAG_namelist_item";
4132 case DW_TAG_namespace:
4133 return "DW_TAG_namespace";
4134 case DW_TAG_packed_type:
4135 return "DW_TAG_packed_type";
4136 case DW_TAG_subprogram:
4137 return "DW_TAG_subprogram";
4138 case DW_TAG_template_type_param:
4139 return "DW_TAG_template_type_param";
4140 case DW_TAG_template_value_param:
4141 return "DW_TAG_template_value_param";
4142 case DW_TAG_thrown_type:
4143 return "DW_TAG_thrown_type";
4144 case DW_TAG_try_block:
4145 return "DW_TAG_try_block";
4146 case DW_TAG_variant_part:
4147 return "DW_TAG_variant_part";
4148 case DW_TAG_variable:
4149 return "DW_TAG_variable";
4150 case DW_TAG_volatile_type:
4151 return "DW_TAG_volatile_type";
4152 case DW_TAG_imported_module:
4153 return "DW_TAG_imported_module";
4154 case DW_TAG_MIPS_loop:
4155 return "DW_TAG_MIPS_loop";
4156 case DW_TAG_format_label:
4157 return "DW_TAG_format_label";
4158 case DW_TAG_function_template:
4159 return "DW_TAG_function_template";
4160 case DW_TAG_class_template:
4161 return "DW_TAG_class_template";
4162 case DW_TAG_GNU_BINCL:
4163 return "DW_TAG_GNU_BINCL";
4164 case DW_TAG_GNU_EINCL:
4165 return "DW_TAG_GNU_EINCL";
4166 default:
4167 return "DW_TAG_<unknown>";
4171 /* Convert a DWARF attribute code into its string name. */
4173 static const char *
4174 dwarf_attr_name (unsigned int attr)
4176 switch (attr)
4178 case DW_AT_sibling:
4179 return "DW_AT_sibling";
4180 case DW_AT_location:
4181 return "DW_AT_location";
4182 case DW_AT_name:
4183 return "DW_AT_name";
4184 case DW_AT_ordering:
4185 return "DW_AT_ordering";
4186 case DW_AT_subscr_data:
4187 return "DW_AT_subscr_data";
4188 case DW_AT_byte_size:
4189 return "DW_AT_byte_size";
4190 case DW_AT_bit_offset:
4191 return "DW_AT_bit_offset";
4192 case DW_AT_bit_size:
4193 return "DW_AT_bit_size";
4194 case DW_AT_element_list:
4195 return "DW_AT_element_list";
4196 case DW_AT_stmt_list:
4197 return "DW_AT_stmt_list";
4198 case DW_AT_low_pc:
4199 return "DW_AT_low_pc";
4200 case DW_AT_high_pc:
4201 return "DW_AT_high_pc";
4202 case DW_AT_language:
4203 return "DW_AT_language";
4204 case DW_AT_member:
4205 return "DW_AT_member";
4206 case DW_AT_discr:
4207 return "DW_AT_discr";
4208 case DW_AT_discr_value:
4209 return "DW_AT_discr_value";
4210 case DW_AT_visibility:
4211 return "DW_AT_visibility";
4212 case DW_AT_import:
4213 return "DW_AT_import";
4214 case DW_AT_string_length:
4215 return "DW_AT_string_length";
4216 case DW_AT_common_reference:
4217 return "DW_AT_common_reference";
4218 case DW_AT_comp_dir:
4219 return "DW_AT_comp_dir";
4220 case DW_AT_const_value:
4221 return "DW_AT_const_value";
4222 case DW_AT_containing_type:
4223 return "DW_AT_containing_type";
4224 case DW_AT_default_value:
4225 return "DW_AT_default_value";
4226 case DW_AT_inline:
4227 return "DW_AT_inline";
4228 case DW_AT_is_optional:
4229 return "DW_AT_is_optional";
4230 case DW_AT_lower_bound:
4231 return "DW_AT_lower_bound";
4232 case DW_AT_producer:
4233 return "DW_AT_producer";
4234 case DW_AT_prototyped:
4235 return "DW_AT_prototyped";
4236 case DW_AT_return_addr:
4237 return "DW_AT_return_addr";
4238 case DW_AT_start_scope:
4239 return "DW_AT_start_scope";
4240 case DW_AT_stride_size:
4241 return "DW_AT_stride_size";
4242 case DW_AT_upper_bound:
4243 return "DW_AT_upper_bound";
4244 case DW_AT_abstract_origin:
4245 return "DW_AT_abstract_origin";
4246 case DW_AT_accessibility:
4247 return "DW_AT_accessibility";
4248 case DW_AT_address_class:
4249 return "DW_AT_address_class";
4250 case DW_AT_artificial:
4251 return "DW_AT_artificial";
4252 case DW_AT_base_types:
4253 return "DW_AT_base_types";
4254 case DW_AT_calling_convention:
4255 return "DW_AT_calling_convention";
4256 case DW_AT_count:
4257 return "DW_AT_count";
4258 case DW_AT_data_member_location:
4259 return "DW_AT_data_member_location";
4260 case DW_AT_decl_column:
4261 return "DW_AT_decl_column";
4262 case DW_AT_decl_file:
4263 return "DW_AT_decl_file";
4264 case DW_AT_decl_line:
4265 return "DW_AT_decl_line";
4266 case DW_AT_declaration:
4267 return "DW_AT_declaration";
4268 case DW_AT_discr_list:
4269 return "DW_AT_discr_list";
4270 case DW_AT_encoding:
4271 return "DW_AT_encoding";
4272 case DW_AT_external:
4273 return "DW_AT_external";
4274 case DW_AT_frame_base:
4275 return "DW_AT_frame_base";
4276 case DW_AT_friend:
4277 return "DW_AT_friend";
4278 case DW_AT_identifier_case:
4279 return "DW_AT_identifier_case";
4280 case DW_AT_macro_info:
4281 return "DW_AT_macro_info";
4282 case DW_AT_namelist_items:
4283 return "DW_AT_namelist_items";
4284 case DW_AT_priority:
4285 return "DW_AT_priority";
4286 case DW_AT_segment:
4287 return "DW_AT_segment";
4288 case DW_AT_specification:
4289 return "DW_AT_specification";
4290 case DW_AT_static_link:
4291 return "DW_AT_static_link";
4292 case DW_AT_type:
4293 return "DW_AT_type";
4294 case DW_AT_use_location:
4295 return "DW_AT_use_location";
4296 case DW_AT_variable_parameter:
4297 return "DW_AT_variable_parameter";
4298 case DW_AT_virtuality:
4299 return "DW_AT_virtuality";
4300 case DW_AT_vtable_elem_location:
4301 return "DW_AT_vtable_elem_location";
4303 case DW_AT_allocated:
4304 return "DW_AT_allocated";
4305 case DW_AT_associated:
4306 return "DW_AT_associated";
4307 case DW_AT_data_location:
4308 return "DW_AT_data_location";
4309 case DW_AT_stride:
4310 return "DW_AT_stride";
4311 case DW_AT_entry_pc:
4312 return "DW_AT_entry_pc";
4313 case DW_AT_use_UTF8:
4314 return "DW_AT_use_UTF8";
4315 case DW_AT_extension:
4316 return "DW_AT_extension";
4317 case DW_AT_ranges:
4318 return "DW_AT_ranges";
4319 case DW_AT_trampoline:
4320 return "DW_AT_trampoline";
4321 case DW_AT_call_column:
4322 return "DW_AT_call_column";
4323 case DW_AT_call_file:
4324 return "DW_AT_call_file";
4325 case DW_AT_call_line:
4326 return "DW_AT_call_line";
4328 case DW_AT_MIPS_fde:
4329 return "DW_AT_MIPS_fde";
4330 case DW_AT_MIPS_loop_begin:
4331 return "DW_AT_MIPS_loop_begin";
4332 case DW_AT_MIPS_tail_loop_begin:
4333 return "DW_AT_MIPS_tail_loop_begin";
4334 case DW_AT_MIPS_epilog_begin:
4335 return "DW_AT_MIPS_epilog_begin";
4336 case DW_AT_MIPS_loop_unroll_factor:
4337 return "DW_AT_MIPS_loop_unroll_factor";
4338 case DW_AT_MIPS_software_pipeline_depth:
4339 return "DW_AT_MIPS_software_pipeline_depth";
4340 case DW_AT_MIPS_linkage_name:
4341 return "DW_AT_MIPS_linkage_name";
4342 case DW_AT_MIPS_stride:
4343 return "DW_AT_MIPS_stride";
4344 case DW_AT_MIPS_abstract_name:
4345 return "DW_AT_MIPS_abstract_name";
4346 case DW_AT_MIPS_clone_origin:
4347 return "DW_AT_MIPS_clone_origin";
4348 case DW_AT_MIPS_has_inlines:
4349 return "DW_AT_MIPS_has_inlines";
4351 case DW_AT_sf_names:
4352 return "DW_AT_sf_names";
4353 case DW_AT_src_info:
4354 return "DW_AT_src_info";
4355 case DW_AT_mac_info:
4356 return "DW_AT_mac_info";
4357 case DW_AT_src_coords:
4358 return "DW_AT_src_coords";
4359 case DW_AT_body_begin:
4360 return "DW_AT_body_begin";
4361 case DW_AT_body_end:
4362 return "DW_AT_body_end";
4363 case DW_AT_GNU_vector:
4364 return "DW_AT_GNU_vector";
4366 case DW_AT_VMS_rtnbeg_pd_address:
4367 return "DW_AT_VMS_rtnbeg_pd_address";
4369 default:
4370 return "DW_AT_<unknown>";
4374 /* Convert a DWARF value form code into its string name. */
4376 static const char *
4377 dwarf_form_name (unsigned int form)
4379 switch (form)
4381 case DW_FORM_addr:
4382 return "DW_FORM_addr";
4383 case DW_FORM_block2:
4384 return "DW_FORM_block2";
4385 case DW_FORM_block4:
4386 return "DW_FORM_block4";
4387 case DW_FORM_data2:
4388 return "DW_FORM_data2";
4389 case DW_FORM_data4:
4390 return "DW_FORM_data4";
4391 case DW_FORM_data8:
4392 return "DW_FORM_data8";
4393 case DW_FORM_string:
4394 return "DW_FORM_string";
4395 case DW_FORM_block:
4396 return "DW_FORM_block";
4397 case DW_FORM_block1:
4398 return "DW_FORM_block1";
4399 case DW_FORM_data1:
4400 return "DW_FORM_data1";
4401 case DW_FORM_flag:
4402 return "DW_FORM_flag";
4403 case DW_FORM_sdata:
4404 return "DW_FORM_sdata";
4405 case DW_FORM_strp:
4406 return "DW_FORM_strp";
4407 case DW_FORM_udata:
4408 return "DW_FORM_udata";
4409 case DW_FORM_ref_addr:
4410 return "DW_FORM_ref_addr";
4411 case DW_FORM_ref1:
4412 return "DW_FORM_ref1";
4413 case DW_FORM_ref2:
4414 return "DW_FORM_ref2";
4415 case DW_FORM_ref4:
4416 return "DW_FORM_ref4";
4417 case DW_FORM_ref8:
4418 return "DW_FORM_ref8";
4419 case DW_FORM_ref_udata:
4420 return "DW_FORM_ref_udata";
4421 case DW_FORM_indirect:
4422 return "DW_FORM_indirect";
4423 default:
4424 return "DW_FORM_<unknown>";
4428 /* Convert a DWARF type code into its string name. */
4430 #if 0
4431 static const char *
4432 dwarf_type_encoding_name (unsigned enc)
4434 switch (enc)
4436 case DW_ATE_address:
4437 return "DW_ATE_address";
4438 case DW_ATE_boolean:
4439 return "DW_ATE_boolean";
4440 case DW_ATE_complex_float:
4441 return "DW_ATE_complex_float";
4442 case DW_ATE_float:
4443 return "DW_ATE_float";
4444 case DW_ATE_signed:
4445 return "DW_ATE_signed";
4446 case DW_ATE_signed_char:
4447 return "DW_ATE_signed_char";
4448 case DW_ATE_unsigned:
4449 return "DW_ATE_unsigned";
4450 case DW_ATE_unsigned_char:
4451 return "DW_ATE_unsigned_char";
4452 default:
4453 return "DW_ATE_<unknown>";
4456 #endif
4458 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4459 instance of an inlined instance of a decl which is local to an inline
4460 function, so we have to trace all of the way back through the origin chain
4461 to find out what sort of node actually served as the original seed for the
4462 given block. */
4464 static tree
4465 decl_ultimate_origin (tree decl)
4467 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4468 nodes in the function to point to themselves; ignore that if
4469 we're trying to output the abstract instance of this function. */
4470 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4471 return NULL_TREE;
4473 #ifdef ENABLE_CHECKING
4474 if (DECL_FROM_INLINE (DECL_ORIGIN (decl)))
4475 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4476 most distant ancestor, this should never happen. */
4477 abort ();
4478 #endif
4480 return DECL_ABSTRACT_ORIGIN (decl);
4483 /* Determine the "ultimate origin" of a block. The block may be an inlined
4484 instance of an inlined instance of a block which is local to an inline
4485 function, so we have to trace all of the way back through the origin chain
4486 to find out what sort of node actually served as the original seed for the
4487 given block. */
4489 static tree
4490 block_ultimate_origin (tree block)
4492 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4494 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4495 nodes in the function to point to themselves; ignore that if
4496 we're trying to output the abstract instance of this function. */
4497 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4498 return NULL_TREE;
4500 if (immediate_origin == NULL_TREE)
4501 return NULL_TREE;
4502 else
4504 tree ret_val;
4505 tree lookahead = immediate_origin;
4509 ret_val = lookahead;
4510 lookahead = (TREE_CODE (ret_val) == BLOCK
4511 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4513 while (lookahead != NULL && lookahead != ret_val);
4515 return ret_val;
4519 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4520 of a virtual function may refer to a base class, so we check the 'this'
4521 parameter. */
4523 static tree
4524 decl_class_context (tree decl)
4526 tree context = NULL_TREE;
4528 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4529 context = DECL_CONTEXT (decl);
4530 else
4531 context = TYPE_MAIN_VARIANT
4532 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4534 if (context && !TYPE_P (context))
4535 context = NULL_TREE;
4537 return context;
4540 /* Add an attribute/value pair to a DIE. We build the lists up in reverse
4541 addition order, and correct that in reverse_all_dies. */
4543 static inline void
4544 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4546 if (die != NULL && attr != NULL)
4548 attr->dw_attr_next = die->die_attr;
4549 die->die_attr = attr;
4553 static inline enum dw_val_class
4554 AT_class (dw_attr_ref a)
4556 return a->dw_attr_val.val_class;
4559 /* Add a flag value attribute to a DIE. */
4561 static inline void
4562 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4564 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4566 attr->dw_attr_next = NULL;
4567 attr->dw_attr = attr_kind;
4568 attr->dw_attr_val.val_class = dw_val_class_flag;
4569 attr->dw_attr_val.v.val_flag = flag;
4570 add_dwarf_attr (die, attr);
4573 static inline unsigned
4574 AT_flag (dw_attr_ref a)
4576 if (a && AT_class (a) == dw_val_class_flag)
4577 return a->dw_attr_val.v.val_flag;
4579 abort ();
4582 /* Add a signed integer attribute value to a DIE. */
4584 static inline void
4585 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4587 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4589 attr->dw_attr_next = NULL;
4590 attr->dw_attr = attr_kind;
4591 attr->dw_attr_val.val_class = dw_val_class_const;
4592 attr->dw_attr_val.v.val_int = int_val;
4593 add_dwarf_attr (die, attr);
4596 static inline HOST_WIDE_INT
4597 AT_int (dw_attr_ref a)
4599 if (a && AT_class (a) == dw_val_class_const)
4600 return a->dw_attr_val.v.val_int;
4602 abort ();
4605 /* Add an unsigned integer attribute value to a DIE. */
4607 static inline void
4608 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4609 unsigned HOST_WIDE_INT unsigned_val)
4611 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4613 attr->dw_attr_next = NULL;
4614 attr->dw_attr = attr_kind;
4615 attr->dw_attr_val.val_class = dw_val_class_unsigned_const;
4616 attr->dw_attr_val.v.val_unsigned = unsigned_val;
4617 add_dwarf_attr (die, attr);
4620 static inline unsigned HOST_WIDE_INT
4621 AT_unsigned (dw_attr_ref a)
4623 if (a && AT_class (a) == dw_val_class_unsigned_const)
4624 return a->dw_attr_val.v.val_unsigned;
4626 abort ();
4629 /* Add an unsigned double integer attribute value to a DIE. */
4631 static inline void
4632 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4633 long unsigned int val_hi, long unsigned int val_low)
4635 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4637 attr->dw_attr_next = NULL;
4638 attr->dw_attr = attr_kind;
4639 attr->dw_attr_val.val_class = dw_val_class_long_long;
4640 attr->dw_attr_val.v.val_long_long.hi = val_hi;
4641 attr->dw_attr_val.v.val_long_long.low = val_low;
4642 add_dwarf_attr (die, attr);
4645 /* Add a floating point attribute value to a DIE and return it. */
4647 static inline void
4648 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4649 unsigned int length, unsigned int elt_size, unsigned char *array)
4651 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4653 attr->dw_attr_next = NULL;
4654 attr->dw_attr = attr_kind;
4655 attr->dw_attr_val.val_class = dw_val_class_vec;
4656 attr->dw_attr_val.v.val_vec.length = length;
4657 attr->dw_attr_val.v.val_vec.elt_size = elt_size;
4658 attr->dw_attr_val.v.val_vec.array = array;
4659 add_dwarf_attr (die, attr);
4662 /* Hash and equality functions for debug_str_hash. */
4664 static hashval_t
4665 debug_str_do_hash (const void *x)
4667 return htab_hash_string (((const struct indirect_string_node *)x)->str);
4670 static int
4671 debug_str_eq (const void *x1, const void *x2)
4673 return strcmp ((((const struct indirect_string_node *)x1)->str),
4674 (const char *)x2) == 0;
4677 /* Add a string attribute value to a DIE. */
4679 static inline void
4680 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4682 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4683 struct indirect_string_node *node;
4684 void **slot;
4686 if (! debug_str_hash)
4687 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4688 debug_str_eq, NULL);
4690 slot = htab_find_slot_with_hash (debug_str_hash, str,
4691 htab_hash_string (str), INSERT);
4692 if (*slot == NULL)
4693 *slot = ggc_alloc_cleared (sizeof (struct indirect_string_node));
4694 node = (struct indirect_string_node *) *slot;
4695 node->str = ggc_strdup (str);
4696 node->refcount++;
4698 attr->dw_attr_next = NULL;
4699 attr->dw_attr = attr_kind;
4700 attr->dw_attr_val.val_class = dw_val_class_str;
4701 attr->dw_attr_val.v.val_str = node;
4702 add_dwarf_attr (die, attr);
4705 static inline const char *
4706 AT_string (dw_attr_ref a)
4708 if (a && AT_class (a) == dw_val_class_str)
4709 return a->dw_attr_val.v.val_str->str;
4711 abort ();
4714 /* Find out whether a string should be output inline in DIE
4715 or out-of-line in .debug_str section. */
4717 static int
4718 AT_string_form (dw_attr_ref a)
4720 if (a && AT_class (a) == dw_val_class_str)
4722 struct indirect_string_node *node;
4723 unsigned int len;
4724 char label[32];
4726 node = a->dw_attr_val.v.val_str;
4727 if (node->form)
4728 return node->form;
4730 len = strlen (node->str) + 1;
4732 /* If the string is shorter or equal to the size of the reference, it is
4733 always better to put it inline. */
4734 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4735 return node->form = DW_FORM_string;
4737 /* If we cannot expect the linker to merge strings in .debug_str
4738 section, only put it into .debug_str if it is worth even in this
4739 single module. */
4740 if ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) == 0
4741 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
4742 return node->form = DW_FORM_string;
4744 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4745 ++dw2_string_counter;
4746 node->label = xstrdup (label);
4748 return node->form = DW_FORM_strp;
4751 abort ();
4754 /* Add a DIE reference attribute value to a DIE. */
4756 static inline void
4757 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4759 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4761 attr->dw_attr_next = NULL;
4762 attr->dw_attr = attr_kind;
4763 attr->dw_attr_val.val_class = dw_val_class_die_ref;
4764 attr->dw_attr_val.v.val_die_ref.die = targ_die;
4765 attr->dw_attr_val.v.val_die_ref.external = 0;
4766 add_dwarf_attr (die, attr);
4769 /* Add an AT_specification attribute to a DIE, and also make the back
4770 pointer from the specification to the definition. */
4772 static inline void
4773 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4775 add_AT_die_ref (die, DW_AT_specification, targ_die);
4776 if (targ_die->die_definition)
4777 abort ();
4778 targ_die->die_definition = die;
4781 static inline dw_die_ref
4782 AT_ref (dw_attr_ref a)
4784 if (a && AT_class (a) == dw_val_class_die_ref)
4785 return a->dw_attr_val.v.val_die_ref.die;
4787 abort ();
4790 static inline int
4791 AT_ref_external (dw_attr_ref a)
4793 if (a && AT_class (a) == dw_val_class_die_ref)
4794 return a->dw_attr_val.v.val_die_ref.external;
4796 return 0;
4799 static inline void
4800 set_AT_ref_external (dw_attr_ref a, int i)
4802 if (a && AT_class (a) == dw_val_class_die_ref)
4803 a->dw_attr_val.v.val_die_ref.external = i;
4804 else
4805 abort ();
4808 /* Add an FDE reference attribute value to a DIE. */
4810 static inline void
4811 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4813 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4815 attr->dw_attr_next = NULL;
4816 attr->dw_attr = attr_kind;
4817 attr->dw_attr_val.val_class = dw_val_class_fde_ref;
4818 attr->dw_attr_val.v.val_fde_index = targ_fde;
4819 add_dwarf_attr (die, attr);
4822 /* Add a location description attribute value to a DIE. */
4824 static inline void
4825 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4827 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4829 attr->dw_attr_next = NULL;
4830 attr->dw_attr = attr_kind;
4831 attr->dw_attr_val.val_class = dw_val_class_loc;
4832 attr->dw_attr_val.v.val_loc = loc;
4833 add_dwarf_attr (die, attr);
4836 static inline dw_loc_descr_ref
4837 AT_loc (dw_attr_ref a)
4839 if (a && AT_class (a) == dw_val_class_loc)
4840 return a->dw_attr_val.v.val_loc;
4842 abort ();
4845 static inline void
4846 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
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_loc_list;
4853 attr->dw_attr_val.v.val_loc_list = loc_list;
4854 add_dwarf_attr (die, attr);
4855 have_location_lists = 1;
4858 static inline dw_loc_list_ref
4859 AT_loc_list (dw_attr_ref a)
4861 if (a && AT_class (a) == dw_val_class_loc_list)
4862 return a->dw_attr_val.v.val_loc_list;
4864 abort ();
4867 /* Add an address constant attribute value to a DIE. */
4869 static inline void
4870 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
4872 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4874 attr->dw_attr_next = NULL;
4875 attr->dw_attr = attr_kind;
4876 attr->dw_attr_val.val_class = dw_val_class_addr;
4877 attr->dw_attr_val.v.val_addr = addr;
4878 add_dwarf_attr (die, attr);
4881 static inline rtx
4882 AT_addr (dw_attr_ref a)
4884 if (a && AT_class (a) == dw_val_class_addr)
4885 return a->dw_attr_val.v.val_addr;
4887 abort ();
4890 /* Add a label identifier attribute value to a DIE. */
4892 static inline void
4893 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
4895 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4897 attr->dw_attr_next = NULL;
4898 attr->dw_attr = attr_kind;
4899 attr->dw_attr_val.val_class = dw_val_class_lbl_id;
4900 attr->dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4901 add_dwarf_attr (die, attr);
4904 /* Add a section offset attribute value to a DIE. */
4906 static inline void
4907 add_AT_lbl_offset (dw_die_ref die, enum dwarf_attribute attr_kind, const char *label)
4909 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4911 attr->dw_attr_next = NULL;
4912 attr->dw_attr = attr_kind;
4913 attr->dw_attr_val.val_class = dw_val_class_lbl_offset;
4914 attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
4915 add_dwarf_attr (die, attr);
4918 /* Add an offset attribute value to a DIE. */
4920 static inline void
4921 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4922 unsigned HOST_WIDE_INT offset)
4924 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4926 attr->dw_attr_next = NULL;
4927 attr->dw_attr = attr_kind;
4928 attr->dw_attr_val.val_class = dw_val_class_offset;
4929 attr->dw_attr_val.v.val_offset = offset;
4930 add_dwarf_attr (die, attr);
4933 /* Add an range_list attribute value to a DIE. */
4935 static void
4936 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4937 long unsigned int offset)
4939 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4941 attr->dw_attr_next = NULL;
4942 attr->dw_attr = attr_kind;
4943 attr->dw_attr_val.val_class = dw_val_class_range_list;
4944 attr->dw_attr_val.v.val_offset = offset;
4945 add_dwarf_attr (die, attr);
4948 static inline const char *
4949 AT_lbl (dw_attr_ref a)
4951 if (a && (AT_class (a) == dw_val_class_lbl_id
4952 || AT_class (a) == dw_val_class_lbl_offset))
4953 return a->dw_attr_val.v.val_lbl_id;
4955 abort ();
4958 /* Get the attribute of type attr_kind. */
4960 static dw_attr_ref
4961 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4963 dw_attr_ref a;
4964 dw_die_ref spec = NULL;
4966 if (die != NULL)
4968 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
4969 if (a->dw_attr == attr_kind)
4970 return a;
4971 else if (a->dw_attr == DW_AT_specification
4972 || a->dw_attr == DW_AT_abstract_origin)
4973 spec = AT_ref (a);
4975 if (spec)
4976 return get_AT (spec, attr_kind);
4979 return NULL;
4982 /* Return the "low pc" attribute value, typically associated with a subprogram
4983 DIE. Return null if the "low pc" attribute is either not present, or if it
4984 cannot be represented as an assembler label identifier. */
4986 static inline const char *
4987 get_AT_low_pc (dw_die_ref die)
4989 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4991 return a ? AT_lbl (a) : NULL;
4994 /* Return the "high pc" attribute value, typically associated with a subprogram
4995 DIE. Return null if the "high pc" attribute is either not present, or if it
4996 cannot be represented as an assembler label identifier. */
4998 static inline const char *
4999 get_AT_hi_pc (dw_die_ref die)
5001 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5003 return a ? AT_lbl (a) : NULL;
5006 /* Return the value of the string attribute designated by ATTR_KIND, or
5007 NULL if it is not present. */
5009 static inline const char *
5010 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5012 dw_attr_ref a = get_AT (die, attr_kind);
5014 return a ? AT_string (a) : NULL;
5017 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5018 if it is not present. */
5020 static inline int
5021 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5023 dw_attr_ref a = get_AT (die, attr_kind);
5025 return a ? AT_flag (a) : 0;
5028 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5029 if it is not present. */
5031 static inline unsigned
5032 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5034 dw_attr_ref a = get_AT (die, attr_kind);
5036 return a ? AT_unsigned (a) : 0;
5039 static inline dw_die_ref
5040 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5042 dw_attr_ref a = get_AT (die, attr_kind);
5044 return a ? AT_ref (a) : NULL;
5047 /* Return TRUE if the language is C or C++. */
5049 static inline bool
5050 is_c_family (void)
5052 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5054 return (lang == DW_LANG_C || lang == DW_LANG_C89
5055 || lang == DW_LANG_C_plus_plus);
5058 /* Return TRUE if the language is C++. */
5060 static inline bool
5061 is_cxx (void)
5063 return (get_AT_unsigned (comp_unit_die, DW_AT_language)
5064 == DW_LANG_C_plus_plus);
5067 /* Return TRUE if the language is Fortran. */
5069 static inline bool
5070 is_fortran (void)
5072 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5074 return (lang == DW_LANG_Fortran77
5075 || lang == DW_LANG_Fortran90
5076 || lang == DW_LANG_Fortran95);
5079 /* Return TRUE if the language is Java. */
5081 static inline bool
5082 is_java (void)
5084 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5086 return lang == DW_LANG_Java;
5089 /* Return TRUE if the language is Ada. */
5091 static inline bool
5092 is_ada (void)
5094 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5096 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5099 /* Free up the memory used by A. */
5101 static inline void free_AT (dw_attr_ref);
5102 static inline void
5103 free_AT (dw_attr_ref a)
5105 if (AT_class (a) == dw_val_class_str)
5106 if (a->dw_attr_val.v.val_str->refcount)
5107 a->dw_attr_val.v.val_str->refcount--;
5110 /* Remove the specified attribute if present. */
5112 static void
5113 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5115 dw_attr_ref *p;
5116 dw_attr_ref removed = NULL;
5118 if (die != NULL)
5120 for (p = &(die->die_attr); *p; p = &((*p)->dw_attr_next))
5121 if ((*p)->dw_attr == attr_kind)
5123 removed = *p;
5124 *p = (*p)->dw_attr_next;
5125 break;
5128 if (removed != 0)
5129 free_AT (removed);
5133 /* Remove child die whose die_tag is specified tag. */
5135 static void
5136 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5138 dw_die_ref current, prev, next;
5139 current = die->die_child;
5140 prev = NULL;
5141 while (current != NULL)
5143 if (current->die_tag == tag)
5145 next = current->die_sib;
5146 if (prev == NULL)
5147 die->die_child = next;
5148 else
5149 prev->die_sib = next;
5150 free_die (current);
5151 current = next;
5153 else
5155 prev = current;
5156 current = current->die_sib;
5161 /* Free up the memory used by DIE. */
5163 static inline void
5164 free_die (dw_die_ref die)
5166 remove_children (die);
5169 /* Discard the children of this DIE. */
5171 static void
5172 remove_children (dw_die_ref die)
5174 dw_die_ref child_die = die->die_child;
5176 die->die_child = NULL;
5178 while (child_die != NULL)
5180 dw_die_ref tmp_die = child_die;
5181 dw_attr_ref a;
5183 child_die = child_die->die_sib;
5185 for (a = tmp_die->die_attr; a != NULL;)
5187 dw_attr_ref tmp_a = a;
5189 a = a->dw_attr_next;
5190 free_AT (tmp_a);
5193 free_die (tmp_die);
5197 /* Add a child DIE below its parent. We build the lists up in reverse
5198 addition order, and correct that in reverse_all_dies. */
5200 static inline void
5201 add_child_die (dw_die_ref die, dw_die_ref child_die)
5203 if (die != NULL && child_die != NULL)
5205 if (die == child_die)
5206 abort ();
5208 child_die->die_parent = die;
5209 child_die->die_sib = die->die_child;
5210 die->die_child = child_die;
5214 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5215 is the specification, to the front of PARENT's list of children. */
5217 static void
5218 splice_child_die (dw_die_ref parent, dw_die_ref child)
5220 dw_die_ref *p;
5222 /* We want the declaration DIE from inside the class, not the
5223 specification DIE at toplevel. */
5224 if (child->die_parent != parent)
5226 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5228 if (tmp)
5229 child = tmp;
5232 if (child->die_parent != parent
5233 && child->die_parent != get_AT_ref (parent, DW_AT_specification))
5234 abort ();
5236 for (p = &(child->die_parent->die_child); *p; p = &((*p)->die_sib))
5237 if (*p == child)
5239 *p = child->die_sib;
5240 break;
5243 child->die_parent = parent;
5244 child->die_sib = parent->die_child;
5245 parent->die_child = child;
5248 /* Return a pointer to a newly created DIE node. */
5250 static inline dw_die_ref
5251 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5253 dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5255 die->die_tag = tag_value;
5257 if (parent_die != NULL)
5258 add_child_die (parent_die, die);
5259 else
5261 limbo_die_node *limbo_node;
5263 limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5264 limbo_node->die = die;
5265 limbo_node->created_for = t;
5266 limbo_node->next = limbo_die_list;
5267 limbo_die_list = limbo_node;
5270 return die;
5273 /* Return the DIE associated with the given type specifier. */
5275 static inline dw_die_ref
5276 lookup_type_die (tree type)
5278 return TYPE_SYMTAB_DIE (type);
5281 /* Equate a DIE to a given type specifier. */
5283 static inline void
5284 equate_type_number_to_die (tree type, dw_die_ref type_die)
5286 TYPE_SYMTAB_DIE (type) = type_die;
5289 /* Returns a hash value for X (which really is a die_struct). */
5291 static hashval_t
5292 decl_die_table_hash (const void *x)
5294 return (hashval_t) ((const dw_die_ref) x)->decl_id;
5297 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5299 static int
5300 decl_die_table_eq (const void *x, const void *y)
5302 return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5305 /* Return the DIE associated with a given declaration. */
5307 static inline dw_die_ref
5308 lookup_decl_die (tree decl)
5310 return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5313 /* Returns a hash value for X (which really is a var_loc_list). */
5315 static hashval_t
5316 decl_loc_table_hash (const void *x)
5318 return (hashval_t) ((const var_loc_list *) x)->decl_id;
5321 /* Return nonzero if decl_id of var_loc_list X is the same as
5322 UID of decl *Y. */
5324 static int
5325 decl_loc_table_eq (const void *x, const void *y)
5327 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5330 /* Return the var_loc list associated with a given declaration. */
5332 static inline var_loc_list *
5333 lookup_decl_loc (tree decl)
5335 return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5338 /* Equate a DIE to a particular declaration. */
5340 static void
5341 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5343 unsigned int decl_id = DECL_UID (decl);
5344 void **slot;
5346 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5347 *slot = decl_die;
5348 decl_die->decl_id = decl_id;
5351 /* Add a variable location node to the linked list for DECL. */
5353 static void
5354 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5356 unsigned int decl_id = DECL_UID (decl);
5357 var_loc_list *temp;
5358 void **slot;
5360 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5361 if (*slot == NULL)
5363 temp = ggc_alloc_cleared (sizeof (var_loc_list));
5364 temp->decl_id = decl_id;
5365 *slot = temp;
5367 else
5368 temp = *slot;
5370 if (temp->last)
5372 /* If the current location is the same as the end of the list,
5373 we have nothing to do. */
5374 if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5375 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5377 /* Add LOC to the end of list and update LAST. */
5378 temp->last->next = loc;
5379 temp->last = loc;
5382 /* Do not add empty location to the beginning of the list. */
5383 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5385 temp->first = loc;
5386 temp->last = loc;
5390 /* Keep track of the number of spaces used to indent the
5391 output of the debugging routines that print the structure of
5392 the DIE internal representation. */
5393 static int print_indent;
5395 /* Indent the line the number of spaces given by print_indent. */
5397 static inline void
5398 print_spaces (FILE *outfile)
5400 fprintf (outfile, "%*s", print_indent, "");
5403 /* Print the information associated with a given DIE, and its children.
5404 This routine is a debugging aid only. */
5406 static void
5407 print_die (dw_die_ref die, FILE *outfile)
5409 dw_attr_ref a;
5410 dw_die_ref c;
5412 print_spaces (outfile);
5413 fprintf (outfile, "DIE %4lu: %s\n",
5414 die->die_offset, dwarf_tag_name (die->die_tag));
5415 print_spaces (outfile);
5416 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5417 fprintf (outfile, " offset: %lu\n", die->die_offset);
5419 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5421 print_spaces (outfile);
5422 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5424 switch (AT_class (a))
5426 case dw_val_class_addr:
5427 fprintf (outfile, "address");
5428 break;
5429 case dw_val_class_offset:
5430 fprintf (outfile, "offset");
5431 break;
5432 case dw_val_class_loc:
5433 fprintf (outfile, "location descriptor");
5434 break;
5435 case dw_val_class_loc_list:
5436 fprintf (outfile, "location list -> label:%s",
5437 AT_loc_list (a)->ll_symbol);
5438 break;
5439 case dw_val_class_range_list:
5440 fprintf (outfile, "range list");
5441 break;
5442 case dw_val_class_const:
5443 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5444 break;
5445 case dw_val_class_unsigned_const:
5446 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5447 break;
5448 case dw_val_class_long_long:
5449 fprintf (outfile, "constant (%lu,%lu)",
5450 a->dw_attr_val.v.val_long_long.hi,
5451 a->dw_attr_val.v.val_long_long.low);
5452 break;
5453 case dw_val_class_vec:
5454 fprintf (outfile, "floating-point or vector constant");
5455 break;
5456 case dw_val_class_flag:
5457 fprintf (outfile, "%u", AT_flag (a));
5458 break;
5459 case dw_val_class_die_ref:
5460 if (AT_ref (a) != NULL)
5462 if (AT_ref (a)->die_symbol)
5463 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5464 else
5465 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5467 else
5468 fprintf (outfile, "die -> <null>");
5469 break;
5470 case dw_val_class_lbl_id:
5471 case dw_val_class_lbl_offset:
5472 fprintf (outfile, "label: %s", AT_lbl (a));
5473 break;
5474 case dw_val_class_str:
5475 if (AT_string (a) != NULL)
5476 fprintf (outfile, "\"%s\"", AT_string (a));
5477 else
5478 fprintf (outfile, "<null>");
5479 break;
5480 default:
5481 break;
5484 fprintf (outfile, "\n");
5487 if (die->die_child != NULL)
5489 print_indent += 4;
5490 for (c = die->die_child; c != NULL; c = c->die_sib)
5491 print_die (c, outfile);
5493 print_indent -= 4;
5495 if (print_indent == 0)
5496 fprintf (outfile, "\n");
5499 /* Print the contents of the source code line number correspondence table.
5500 This routine is a debugging aid only. */
5502 static void
5503 print_dwarf_line_table (FILE *outfile)
5505 unsigned i;
5506 dw_line_info_ref line_info;
5508 fprintf (outfile, "\n\nDWARF source line information\n");
5509 for (i = 1; i < line_info_table_in_use; i++)
5511 line_info = &line_info_table[i];
5512 fprintf (outfile, "%5d: ", i);
5513 fprintf (outfile, "%-20s",
5514 VARRAY_CHAR_PTR (file_table, line_info->dw_file_num));
5515 fprintf (outfile, "%6ld", line_info->dw_line_num);
5516 fprintf (outfile, "\n");
5519 fprintf (outfile, "\n\n");
5522 /* Print the information collected for a given DIE. */
5524 void
5525 debug_dwarf_die (dw_die_ref die)
5527 print_die (die, stderr);
5530 /* Print all DWARF information collected for the compilation unit.
5531 This routine is a debugging aid only. */
5533 void
5534 debug_dwarf (void)
5536 print_indent = 0;
5537 print_die (comp_unit_die, stderr);
5538 if (! DWARF2_ASM_LINE_DEBUG_INFO)
5539 print_dwarf_line_table (stderr);
5542 /* We build up the lists of children and attributes by pushing new ones
5543 onto the beginning of the list. Reverse the lists for DIE so that
5544 they are in order of addition. */
5546 static void
5547 reverse_die_lists (dw_die_ref die)
5549 dw_die_ref c, cp, cn;
5550 dw_attr_ref a, ap, an;
5552 for (a = die->die_attr, ap = 0; a; a = an)
5554 an = a->dw_attr_next;
5555 a->dw_attr_next = ap;
5556 ap = a;
5559 die->die_attr = ap;
5561 for (c = die->die_child, cp = 0; c; c = cn)
5563 cn = c->die_sib;
5564 c->die_sib = cp;
5565 cp = c;
5568 die->die_child = cp;
5571 /* reverse_die_lists only reverses the single die you pass it. Since we used to
5572 reverse all dies in add_sibling_attributes, which runs through all the dies,
5573 it would reverse all the dies. Now, however, since we don't call
5574 reverse_die_lists in add_sibling_attributes, we need a routine to
5575 recursively reverse all the dies. This is that routine. */
5577 static void
5578 reverse_all_dies (dw_die_ref die)
5580 dw_die_ref c;
5582 reverse_die_lists (die);
5584 for (c = die->die_child; c; c = c->die_sib)
5585 reverse_all_dies (c);
5588 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5589 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5590 DIE that marks the start of the DIEs for this include file. */
5592 static dw_die_ref
5593 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5595 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5596 dw_die_ref new_unit = gen_compile_unit_die (filename);
5598 new_unit->die_sib = old_unit;
5599 return new_unit;
5602 /* Close an include-file CU and reopen the enclosing one. */
5604 static dw_die_ref
5605 pop_compile_unit (dw_die_ref old_unit)
5607 dw_die_ref new_unit = old_unit->die_sib;
5609 old_unit->die_sib = NULL;
5610 return new_unit;
5613 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5614 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5616 /* Calculate the checksum of a location expression. */
5618 static inline void
5619 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5621 CHECKSUM (loc->dw_loc_opc);
5622 CHECKSUM (loc->dw_loc_oprnd1);
5623 CHECKSUM (loc->dw_loc_oprnd2);
5626 /* Calculate the checksum of an attribute. */
5628 static void
5629 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5631 dw_loc_descr_ref loc;
5632 rtx r;
5634 CHECKSUM (at->dw_attr);
5636 /* We don't care about differences in file numbering. */
5637 if (at->dw_attr == DW_AT_decl_file
5638 /* Or that this was compiled with a different compiler snapshot; if
5639 the output is the same, that's what matters. */
5640 || at->dw_attr == DW_AT_producer)
5641 return;
5643 switch (AT_class (at))
5645 case dw_val_class_const:
5646 CHECKSUM (at->dw_attr_val.v.val_int);
5647 break;
5648 case dw_val_class_unsigned_const:
5649 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5650 break;
5651 case dw_val_class_long_long:
5652 CHECKSUM (at->dw_attr_val.v.val_long_long);
5653 break;
5654 case dw_val_class_vec:
5655 CHECKSUM (at->dw_attr_val.v.val_vec);
5656 break;
5657 case dw_val_class_flag:
5658 CHECKSUM (at->dw_attr_val.v.val_flag);
5659 break;
5660 case dw_val_class_str:
5661 CHECKSUM_STRING (AT_string (at));
5662 break;
5664 case dw_val_class_addr:
5665 r = AT_addr (at);
5666 switch (GET_CODE (r))
5668 case SYMBOL_REF:
5669 CHECKSUM_STRING (XSTR (r, 0));
5670 break;
5672 default:
5673 abort ();
5675 break;
5677 case dw_val_class_offset:
5678 CHECKSUM (at->dw_attr_val.v.val_offset);
5679 break;
5681 case dw_val_class_loc:
5682 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5683 loc_checksum (loc, ctx);
5684 break;
5686 case dw_val_class_die_ref:
5687 die_checksum (AT_ref (at), ctx, mark);
5688 break;
5690 case dw_val_class_fde_ref:
5691 case dw_val_class_lbl_id:
5692 case dw_val_class_lbl_offset:
5693 break;
5695 default:
5696 break;
5700 /* Calculate the checksum of a DIE. */
5702 static void
5703 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5705 dw_die_ref c;
5706 dw_attr_ref a;
5708 /* To avoid infinite recursion. */
5709 if (die->die_mark)
5711 CHECKSUM (die->die_mark);
5712 return;
5714 die->die_mark = ++(*mark);
5716 CHECKSUM (die->die_tag);
5718 for (a = die->die_attr; a; a = a->dw_attr_next)
5719 attr_checksum (a, ctx, mark);
5721 for (c = die->die_child; c; c = c->die_sib)
5722 die_checksum (c, ctx, mark);
5725 #undef CHECKSUM
5726 #undef CHECKSUM_STRING
5728 /* Do the location expressions look same? */
5729 static inline int
5730 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
5732 return loc1->dw_loc_opc == loc2->dw_loc_opc
5733 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
5734 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
5737 /* Do the values look the same? */
5738 static int
5739 same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
5741 dw_loc_descr_ref loc1, loc2;
5742 rtx r1, r2;
5744 if (v1->val_class != v2->val_class)
5745 return 0;
5747 switch (v1->val_class)
5749 case dw_val_class_const:
5750 return v1->v.val_int == v2->v.val_int;
5751 case dw_val_class_unsigned_const:
5752 return v1->v.val_unsigned == v2->v.val_unsigned;
5753 case dw_val_class_long_long:
5754 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
5755 && v1->v.val_long_long.low == v2->v.val_long_long.low;
5756 case dw_val_class_vec:
5757 if (v1->v.val_vec.length != v2->v.val_vec.length
5758 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
5759 return 0;
5760 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
5761 v1->v.val_vec.length * v1->v.val_vec.elt_size))
5762 return 0;
5763 return 1;
5764 case dw_val_class_flag:
5765 return v1->v.val_flag == v2->v.val_flag;
5766 case dw_val_class_str:
5767 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
5769 case dw_val_class_addr:
5770 r1 = v1->v.val_addr;
5771 r2 = v2->v.val_addr;
5772 if (GET_CODE (r1) != GET_CODE (r2))
5773 return 0;
5774 switch (GET_CODE (r1))
5776 case SYMBOL_REF:
5777 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
5779 default:
5780 abort ();
5783 case dw_val_class_offset:
5784 return v1->v.val_offset == v2->v.val_offset;
5786 case dw_val_class_loc:
5787 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
5788 loc1 && loc2;
5789 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
5790 if (!same_loc_p (loc1, loc2, mark))
5791 return 0;
5792 return !loc1 && !loc2;
5794 case dw_val_class_die_ref:
5795 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
5797 case dw_val_class_fde_ref:
5798 case dw_val_class_lbl_id:
5799 case dw_val_class_lbl_offset:
5800 return 1;
5802 default:
5803 return 1;
5807 /* Do the attributes look the same? */
5809 static int
5810 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
5812 if (at1->dw_attr != at2->dw_attr)
5813 return 0;
5815 /* We don't care about differences in file numbering. */
5816 if (at1->dw_attr == DW_AT_decl_file
5817 /* Or that this was compiled with a different compiler snapshot; if
5818 the output is the same, that's what matters. */
5819 || at1->dw_attr == DW_AT_producer)
5820 return 1;
5822 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
5825 /* Do the dies look the same? */
5827 static int
5828 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
5830 dw_die_ref c1, c2;
5831 dw_attr_ref a1, a2;
5833 /* To avoid infinite recursion. */
5834 if (die1->die_mark)
5835 return die1->die_mark == die2->die_mark;
5836 die1->die_mark = die2->die_mark = ++(*mark);
5838 if (die1->die_tag != die2->die_tag)
5839 return 0;
5841 for (a1 = die1->die_attr, a2 = die2->die_attr;
5842 a1 && a2;
5843 a1 = a1->dw_attr_next, a2 = a2->dw_attr_next)
5844 if (!same_attr_p (a1, a2, mark))
5845 return 0;
5846 if (a1 || a2)
5847 return 0;
5849 for (c1 = die1->die_child, c2 = die2->die_child;
5850 c1 && c2;
5851 c1 = c1->die_sib, c2 = c2->die_sib)
5852 if (!same_die_p (c1, c2, mark))
5853 return 0;
5854 if (c1 || c2)
5855 return 0;
5857 return 1;
5860 /* Do the dies look the same? Wrapper around same_die_p. */
5862 static int
5863 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
5865 int mark = 0;
5866 int ret = same_die_p (die1, die2, &mark);
5868 unmark_all_dies (die1);
5869 unmark_all_dies (die2);
5871 return ret;
5874 /* The prefix to attach to symbols on DIEs in the current comdat debug
5875 info section. */
5876 static char *comdat_symbol_id;
5878 /* The index of the current symbol within the current comdat CU. */
5879 static unsigned int comdat_symbol_number;
5881 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
5882 children, and set comdat_symbol_id accordingly. */
5884 static void
5885 compute_section_prefix (dw_die_ref unit_die)
5887 const char *die_name = get_AT_string (unit_die, DW_AT_name);
5888 const char *base = die_name ? lbasename (die_name) : "anonymous";
5889 char *name = alloca (strlen (base) + 64);
5890 char *p;
5891 int i, mark;
5892 unsigned char checksum[16];
5893 struct md5_ctx ctx;
5895 /* Compute the checksum of the DIE, then append part of it as hex digits to
5896 the name filename of the unit. */
5898 md5_init_ctx (&ctx);
5899 mark = 0;
5900 die_checksum (unit_die, &ctx, &mark);
5901 unmark_all_dies (unit_die);
5902 md5_finish_ctx (&ctx, checksum);
5904 sprintf (name, "%s.", base);
5905 clean_symbol_name (name);
5907 p = name + strlen (name);
5908 for (i = 0; i < 4; i++)
5910 sprintf (p, "%.2x", checksum[i]);
5911 p += 2;
5914 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
5915 comdat_symbol_number = 0;
5918 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
5920 static int
5921 is_type_die (dw_die_ref die)
5923 switch (die->die_tag)
5925 case DW_TAG_array_type:
5926 case DW_TAG_class_type:
5927 case DW_TAG_enumeration_type:
5928 case DW_TAG_pointer_type:
5929 case DW_TAG_reference_type:
5930 case DW_TAG_string_type:
5931 case DW_TAG_structure_type:
5932 case DW_TAG_subroutine_type:
5933 case DW_TAG_union_type:
5934 case DW_TAG_ptr_to_member_type:
5935 case DW_TAG_set_type:
5936 case DW_TAG_subrange_type:
5937 case DW_TAG_base_type:
5938 case DW_TAG_const_type:
5939 case DW_TAG_file_type:
5940 case DW_TAG_packed_type:
5941 case DW_TAG_volatile_type:
5942 case DW_TAG_typedef:
5943 return 1;
5944 default:
5945 return 0;
5949 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
5950 Basically, we want to choose the bits that are likely to be shared between
5951 compilations (types) and leave out the bits that are specific to individual
5952 compilations (functions). */
5954 static int
5955 is_comdat_die (dw_die_ref c)
5957 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
5958 we do for stabs. The advantage is a greater likelihood of sharing between
5959 objects that don't include headers in the same order (and therefore would
5960 put the base types in a different comdat). jason 8/28/00 */
5962 if (c->die_tag == DW_TAG_base_type)
5963 return 0;
5965 if (c->die_tag == DW_TAG_pointer_type
5966 || c->die_tag == DW_TAG_reference_type
5967 || c->die_tag == DW_TAG_const_type
5968 || c->die_tag == DW_TAG_volatile_type)
5970 dw_die_ref t = get_AT_ref (c, DW_AT_type);
5972 return t ? is_comdat_die (t) : 0;
5975 return is_type_die (c);
5978 /* Returns 1 iff C is the sort of DIE that might be referred to from another
5979 compilation unit. */
5981 static int
5982 is_symbol_die (dw_die_ref c)
5984 return (is_type_die (c)
5985 || (get_AT (c, DW_AT_declaration)
5986 && !get_AT (c, DW_AT_specification)));
5989 static char *
5990 gen_internal_sym (const char *prefix)
5992 char buf[256];
5994 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
5995 return xstrdup (buf);
5998 /* Assign symbols to all worthy DIEs under DIE. */
6000 static void
6001 assign_symbol_names (dw_die_ref die)
6003 dw_die_ref c;
6005 if (is_symbol_die (die))
6007 if (comdat_symbol_id)
6009 char *p = alloca (strlen (comdat_symbol_id) + 64);
6011 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6012 comdat_symbol_id, comdat_symbol_number++);
6013 die->die_symbol = xstrdup (p);
6015 else
6016 die->die_symbol = gen_internal_sym ("LDIE");
6019 for (c = die->die_child; c != NULL; c = c->die_sib)
6020 assign_symbol_names (c);
6023 struct cu_hash_table_entry
6025 dw_die_ref cu;
6026 unsigned min_comdat_num, max_comdat_num;
6027 struct cu_hash_table_entry *next;
6030 /* Routines to manipulate hash table of CUs. */
6031 static hashval_t
6032 htab_cu_hash (const void *of)
6034 const struct cu_hash_table_entry *entry = of;
6036 return htab_hash_string (entry->cu->die_symbol);
6039 static int
6040 htab_cu_eq (const void *of1, const void *of2)
6042 const struct cu_hash_table_entry *entry1 = of1;
6043 const struct die_struct *entry2 = of2;
6045 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6048 static void
6049 htab_cu_del (void *what)
6051 struct cu_hash_table_entry *next, *entry = what;
6053 while (entry)
6055 next = entry->next;
6056 free (entry);
6057 entry = next;
6061 /* Check whether we have already seen this CU and set up SYM_NUM
6062 accordingly. */
6063 static int
6064 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6066 struct cu_hash_table_entry dummy;
6067 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6069 dummy.max_comdat_num = 0;
6071 slot = (struct cu_hash_table_entry **)
6072 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6073 INSERT);
6074 entry = *slot;
6076 for (; entry; last = entry, entry = entry->next)
6078 if (same_die_p_wrap (cu, entry->cu))
6079 break;
6082 if (entry)
6084 *sym_num = entry->min_comdat_num;
6085 return 1;
6088 entry = xcalloc (1, sizeof (struct cu_hash_table_entry));
6089 entry->cu = cu;
6090 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6091 entry->next = *slot;
6092 *slot = entry;
6094 return 0;
6097 /* Record SYM_NUM to record of CU in HTABLE. */
6098 static void
6099 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6101 struct cu_hash_table_entry **slot, *entry;
6103 slot = (struct cu_hash_table_entry **)
6104 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6105 NO_INSERT);
6106 entry = *slot;
6108 entry->max_comdat_num = sym_num;
6111 /* Traverse the DIE (which is always comp_unit_die), and set up
6112 additional compilation units for each of the include files we see
6113 bracketed by BINCL/EINCL. */
6115 static void
6116 break_out_includes (dw_die_ref die)
6118 dw_die_ref *ptr;
6119 dw_die_ref unit = NULL;
6120 limbo_die_node *node, **pnode;
6121 htab_t cu_hash_table;
6123 for (ptr = &(die->die_child); *ptr;)
6125 dw_die_ref c = *ptr;
6127 if (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6128 || (unit && is_comdat_die (c)))
6130 /* This DIE is for a secondary CU; remove it from the main one. */
6131 *ptr = c->die_sib;
6133 if (c->die_tag == DW_TAG_GNU_BINCL)
6135 unit = push_new_compile_unit (unit, c);
6136 free_die (c);
6138 else if (c->die_tag == DW_TAG_GNU_EINCL)
6140 unit = pop_compile_unit (unit);
6141 free_die (c);
6143 else
6144 add_child_die (unit, c);
6146 else
6148 /* Leave this DIE in the main CU. */
6149 ptr = &(c->die_sib);
6150 continue;
6154 #if 0
6155 /* We can only use this in debugging, since the frontend doesn't check
6156 to make sure that we leave every include file we enter. */
6157 if (unit != NULL)
6158 abort ();
6159 #endif
6161 assign_symbol_names (die);
6162 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6163 for (node = limbo_die_list, pnode = &limbo_die_list;
6164 node;
6165 node = node->next)
6167 int is_dupl;
6169 compute_section_prefix (node->die);
6170 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6171 &comdat_symbol_number);
6172 assign_symbol_names (node->die);
6173 if (is_dupl)
6174 *pnode = node->next;
6175 else
6177 pnode = &node->next;
6178 record_comdat_symbol_number (node->die, cu_hash_table,
6179 comdat_symbol_number);
6182 htab_delete (cu_hash_table);
6185 /* Traverse the DIE and add a sibling attribute if it may have the
6186 effect of speeding up access to siblings. To save some space,
6187 avoid generating sibling attributes for DIE's without children. */
6189 static void
6190 add_sibling_attributes (dw_die_ref die)
6192 dw_die_ref c;
6194 if (die->die_tag != DW_TAG_compile_unit
6195 && die->die_sib && die->die_child != NULL)
6196 /* Add the sibling link to the front of the attribute list. */
6197 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6199 for (c = die->die_child; c != NULL; c = c->die_sib)
6200 add_sibling_attributes (c);
6203 /* Output all location lists for the DIE and its children. */
6205 static void
6206 output_location_lists (dw_die_ref die)
6208 dw_die_ref c;
6209 dw_attr_ref d_attr;
6211 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6212 if (AT_class (d_attr) == dw_val_class_loc_list)
6213 output_loc_list (AT_loc_list (d_attr));
6215 for (c = die->die_child; c != NULL; c = c->die_sib)
6216 output_location_lists (c);
6220 /* The format of each DIE (and its attribute value pairs) is encoded in an
6221 abbreviation table. This routine builds the abbreviation table and assigns
6222 a unique abbreviation id for each abbreviation entry. The children of each
6223 die are visited recursively. */
6225 static void
6226 build_abbrev_table (dw_die_ref die)
6228 unsigned long abbrev_id;
6229 unsigned int n_alloc;
6230 dw_die_ref c;
6231 dw_attr_ref d_attr, a_attr;
6233 /* Scan the DIE references, and mark as external any that refer to
6234 DIEs from other CUs (i.e. those which are not marked). */
6235 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6236 if (AT_class (d_attr) == dw_val_class_die_ref
6237 && AT_ref (d_attr)->die_mark == 0)
6239 if (AT_ref (d_attr)->die_symbol == 0)
6240 abort ();
6242 set_AT_ref_external (d_attr, 1);
6245 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6247 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6249 if (abbrev->die_tag == die->die_tag)
6251 if ((abbrev->die_child != NULL) == (die->die_child != NULL))
6253 a_attr = abbrev->die_attr;
6254 d_attr = die->die_attr;
6256 while (a_attr != NULL && d_attr != NULL)
6258 if ((a_attr->dw_attr != d_attr->dw_attr)
6259 || (value_format (a_attr) != value_format (d_attr)))
6260 break;
6262 a_attr = a_attr->dw_attr_next;
6263 d_attr = d_attr->dw_attr_next;
6266 if (a_attr == NULL && d_attr == NULL)
6267 break;
6272 if (abbrev_id >= abbrev_die_table_in_use)
6274 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6276 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6277 abbrev_die_table = ggc_realloc (abbrev_die_table,
6278 sizeof (dw_die_ref) * n_alloc);
6280 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6281 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6282 abbrev_die_table_allocated = n_alloc;
6285 ++abbrev_die_table_in_use;
6286 abbrev_die_table[abbrev_id] = die;
6289 die->die_abbrev = abbrev_id;
6290 for (c = die->die_child; c != NULL; c = c->die_sib)
6291 build_abbrev_table (c);
6294 /* Return the power-of-two number of bytes necessary to represent VALUE. */
6296 static int
6297 constant_size (long unsigned int value)
6299 int log;
6301 if (value == 0)
6302 log = 0;
6303 else
6304 log = floor_log2 (value);
6306 log = log / 8;
6307 log = 1 << (floor_log2 (log) + 1);
6309 return log;
6312 /* Return the size of a DIE as it is represented in the
6313 .debug_info section. */
6315 static unsigned long
6316 size_of_die (dw_die_ref die)
6318 unsigned long size = 0;
6319 dw_attr_ref a;
6321 size += size_of_uleb128 (die->die_abbrev);
6322 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6324 switch (AT_class (a))
6326 case dw_val_class_addr:
6327 size += DWARF2_ADDR_SIZE;
6328 break;
6329 case dw_val_class_offset:
6330 size += DWARF_OFFSET_SIZE;
6331 break;
6332 case dw_val_class_loc:
6334 unsigned long lsize = size_of_locs (AT_loc (a));
6336 /* Block length. */
6337 size += constant_size (lsize);
6338 size += lsize;
6340 break;
6341 case dw_val_class_loc_list:
6342 size += DWARF_OFFSET_SIZE;
6343 break;
6344 case dw_val_class_range_list:
6345 size += DWARF_OFFSET_SIZE;
6346 break;
6347 case dw_val_class_const:
6348 size += size_of_sleb128 (AT_int (a));
6349 break;
6350 case dw_val_class_unsigned_const:
6351 size += constant_size (AT_unsigned (a));
6352 break;
6353 case dw_val_class_long_long:
6354 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6355 break;
6356 case dw_val_class_vec:
6357 size += 1 + (a->dw_attr_val.v.val_vec.length
6358 * a->dw_attr_val.v.val_vec.elt_size); /* block */
6359 break;
6360 case dw_val_class_flag:
6361 size += 1;
6362 break;
6363 case dw_val_class_die_ref:
6364 if (AT_ref_external (a))
6365 size += DWARF2_ADDR_SIZE;
6366 else
6367 size += DWARF_OFFSET_SIZE;
6368 break;
6369 case dw_val_class_fde_ref:
6370 size += DWARF_OFFSET_SIZE;
6371 break;
6372 case dw_val_class_lbl_id:
6373 size += DWARF2_ADDR_SIZE;
6374 break;
6375 case dw_val_class_lbl_offset:
6376 size += DWARF_OFFSET_SIZE;
6377 break;
6378 case dw_val_class_str:
6379 if (AT_string_form (a) == DW_FORM_strp)
6380 size += DWARF_OFFSET_SIZE;
6381 else
6382 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6383 break;
6384 default:
6385 abort ();
6389 return size;
6392 /* Size the debugging information associated with a given DIE. Visits the
6393 DIE's children recursively. Updates the global variable next_die_offset, on
6394 each time through. Uses the current value of next_die_offset to update the
6395 die_offset field in each DIE. */
6397 static void
6398 calc_die_sizes (dw_die_ref die)
6400 dw_die_ref c;
6402 die->die_offset = next_die_offset;
6403 next_die_offset += size_of_die (die);
6405 for (c = die->die_child; c != NULL; c = c->die_sib)
6406 calc_die_sizes (c);
6408 if (die->die_child != NULL)
6409 /* Count the null byte used to terminate sibling lists. */
6410 next_die_offset += 1;
6413 /* Set the marks for a die and its children. We do this so
6414 that we know whether or not a reference needs to use FORM_ref_addr; only
6415 DIEs in the same CU will be marked. We used to clear out the offset
6416 and use that as the flag, but ran into ordering problems. */
6418 static void
6419 mark_dies (dw_die_ref die)
6421 dw_die_ref c;
6423 if (die->die_mark)
6424 abort ();
6426 die->die_mark = 1;
6427 for (c = die->die_child; c; c = c->die_sib)
6428 mark_dies (c);
6431 /* Clear the marks for a die and its children. */
6433 static void
6434 unmark_dies (dw_die_ref die)
6436 dw_die_ref c;
6438 if (!die->die_mark)
6439 abort ();
6441 die->die_mark = 0;
6442 for (c = die->die_child; c; c = c->die_sib)
6443 unmark_dies (c);
6446 /* Clear the marks for a die, its children and referred dies. */
6448 static void
6449 unmark_all_dies (dw_die_ref die)
6451 dw_die_ref c;
6452 dw_attr_ref a;
6454 if (!die->die_mark)
6455 return;
6456 die->die_mark = 0;
6458 for (c = die->die_child; c; c = c->die_sib)
6459 unmark_all_dies (c);
6461 for (a = die->die_attr; a; a = a->dw_attr_next)
6462 if (AT_class (a) == dw_val_class_die_ref)
6463 unmark_all_dies (AT_ref (a));
6466 /* Return the size of the .debug_pubnames table generated for the
6467 compilation unit. */
6469 static unsigned long
6470 size_of_pubnames (void)
6472 unsigned long size;
6473 unsigned i;
6475 size = DWARF_PUBNAMES_HEADER_SIZE;
6476 for (i = 0; i < pubname_table_in_use; i++)
6478 pubname_ref p = &pubname_table[i];
6479 size += DWARF_OFFSET_SIZE + strlen (p->name) + 1;
6482 size += DWARF_OFFSET_SIZE;
6483 return size;
6486 /* Return the size of the information in the .debug_aranges section. */
6488 static unsigned long
6489 size_of_aranges (void)
6491 unsigned long size;
6493 size = DWARF_ARANGES_HEADER_SIZE;
6495 /* Count the address/length pair for this compilation unit. */
6496 size += 2 * DWARF2_ADDR_SIZE;
6497 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6499 /* Count the two zero words used to terminated the address range table. */
6500 size += 2 * DWARF2_ADDR_SIZE;
6501 return size;
6504 /* Select the encoding of an attribute value. */
6506 static enum dwarf_form
6507 value_format (dw_attr_ref a)
6509 switch (a->dw_attr_val.val_class)
6511 case dw_val_class_addr:
6512 return DW_FORM_addr;
6513 case dw_val_class_range_list:
6514 case dw_val_class_offset:
6515 if (DWARF_OFFSET_SIZE == 4)
6516 return DW_FORM_data4;
6517 if (DWARF_OFFSET_SIZE == 8)
6518 return DW_FORM_data8;
6519 abort ();
6520 case dw_val_class_loc_list:
6521 /* FIXME: Could be DW_FORM_data8, with a > 32 bit size
6522 .debug_loc section */
6523 return DW_FORM_data4;
6524 case dw_val_class_loc:
6525 switch (constant_size (size_of_locs (AT_loc (a))))
6527 case 1:
6528 return DW_FORM_block1;
6529 case 2:
6530 return DW_FORM_block2;
6531 default:
6532 abort ();
6534 case dw_val_class_const:
6535 return DW_FORM_sdata;
6536 case dw_val_class_unsigned_const:
6537 switch (constant_size (AT_unsigned (a)))
6539 case 1:
6540 return DW_FORM_data1;
6541 case 2:
6542 return DW_FORM_data2;
6543 case 4:
6544 return DW_FORM_data4;
6545 case 8:
6546 return DW_FORM_data8;
6547 default:
6548 abort ();
6550 case dw_val_class_long_long:
6551 return DW_FORM_block1;
6552 case dw_val_class_vec:
6553 return DW_FORM_block1;
6554 case dw_val_class_flag:
6555 return DW_FORM_flag;
6556 case dw_val_class_die_ref:
6557 if (AT_ref_external (a))
6558 return DW_FORM_ref_addr;
6559 else
6560 return DW_FORM_ref;
6561 case dw_val_class_fde_ref:
6562 return DW_FORM_data;
6563 case dw_val_class_lbl_id:
6564 return DW_FORM_addr;
6565 case dw_val_class_lbl_offset:
6566 return DW_FORM_data;
6567 case dw_val_class_str:
6568 return AT_string_form (a);
6570 default:
6571 abort ();
6575 /* Output the encoding of an attribute value. */
6577 static void
6578 output_value_format (dw_attr_ref a)
6580 enum dwarf_form form = value_format (a);
6582 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6585 /* Output the .debug_abbrev section which defines the DIE abbreviation
6586 table. */
6588 static void
6589 output_abbrev_section (void)
6591 unsigned long abbrev_id;
6593 dw_attr_ref a_attr;
6595 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6597 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6599 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6600 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6601 dwarf_tag_name (abbrev->die_tag));
6603 if (abbrev->die_child != NULL)
6604 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6605 else
6606 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6608 for (a_attr = abbrev->die_attr; a_attr != NULL;
6609 a_attr = a_attr->dw_attr_next)
6611 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6612 dwarf_attr_name (a_attr->dw_attr));
6613 output_value_format (a_attr);
6616 dw2_asm_output_data (1, 0, NULL);
6617 dw2_asm_output_data (1, 0, NULL);
6620 /* Terminate the table. */
6621 dw2_asm_output_data (1, 0, NULL);
6624 /* Output a symbol we can use to refer to this DIE from another CU. */
6626 static inline void
6627 output_die_symbol (dw_die_ref die)
6629 char *sym = die->die_symbol;
6631 if (sym == 0)
6632 return;
6634 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6635 /* We make these global, not weak; if the target doesn't support
6636 .linkonce, it doesn't support combining the sections, so debugging
6637 will break. */
6638 targetm.asm_out.globalize_label (asm_out_file, sym);
6640 ASM_OUTPUT_LABEL (asm_out_file, sym);
6643 /* Return a new location list, given the begin and end range, and the
6644 expression. gensym tells us whether to generate a new internal symbol for
6645 this location list node, which is done for the head of the list only. */
6647 static inline dw_loc_list_ref
6648 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6649 const char *section, unsigned int gensym)
6651 dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
6653 retlist->begin = begin;
6654 retlist->end = end;
6655 retlist->expr = expr;
6656 retlist->section = section;
6657 if (gensym)
6658 retlist->ll_symbol = gen_internal_sym ("LLST");
6660 return retlist;
6663 /* Add a location description expression to a location list. */
6665 static inline void
6666 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6667 const char *begin, const char *end,
6668 const char *section)
6670 dw_loc_list_ref *d;
6672 /* Find the end of the chain. */
6673 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
6676 /* Add a new location list node to the list. */
6677 *d = new_loc_list (descr, begin, end, section, 0);
6680 /* Output the location list given to us. */
6682 static void
6683 output_loc_list (dw_loc_list_ref list_head)
6685 dw_loc_list_ref curr = list_head;
6687 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
6689 /* Walk the location list, and output each range + expression. */
6690 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
6692 unsigned long size;
6693 if (separate_line_info_table_in_use == 0)
6695 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
6696 "Location list begin address (%s)",
6697 list_head->ll_symbol);
6698 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
6699 "Location list end address (%s)",
6700 list_head->ll_symbol);
6702 else
6704 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
6705 "Location list begin address (%s)",
6706 list_head->ll_symbol);
6707 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
6708 "Location list end address (%s)",
6709 list_head->ll_symbol);
6711 size = size_of_locs (curr->expr);
6713 /* Output the block length for this list of location operations. */
6714 if (size > 0xffff)
6715 abort ();
6716 dw2_asm_output_data (2, size, "%s", "Location expression size");
6718 output_loc_sequence (curr->expr);
6721 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6722 "Location list terminator begin (%s)",
6723 list_head->ll_symbol);
6724 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6725 "Location list terminator end (%s)",
6726 list_head->ll_symbol);
6729 /* Output the DIE and its attributes. Called recursively to generate
6730 the definitions of each child DIE. */
6732 static void
6733 output_die (dw_die_ref die)
6735 dw_attr_ref a;
6736 dw_die_ref c;
6737 unsigned long size;
6739 /* If someone in another CU might refer to us, set up a symbol for
6740 them to point to. */
6741 if (die->die_symbol)
6742 output_die_symbol (die);
6744 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
6745 die->die_offset, dwarf_tag_name (die->die_tag));
6747 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6749 const char *name = dwarf_attr_name (a->dw_attr);
6751 switch (AT_class (a))
6753 case dw_val_class_addr:
6754 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
6755 break;
6757 case dw_val_class_offset:
6758 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
6759 "%s", name);
6760 break;
6762 case dw_val_class_range_list:
6764 char *p = strchr (ranges_section_label, '\0');
6766 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
6767 a->dw_attr_val.v.val_offset);
6768 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
6769 "%s", name);
6770 *p = '\0';
6772 break;
6774 case dw_val_class_loc:
6775 size = size_of_locs (AT_loc (a));
6777 /* Output the block length for this list of location operations. */
6778 dw2_asm_output_data (constant_size (size), size, "%s", name);
6780 output_loc_sequence (AT_loc (a));
6781 break;
6783 case dw_val_class_const:
6784 /* ??? It would be slightly more efficient to use a scheme like is
6785 used for unsigned constants below, but gdb 4.x does not sign
6786 extend. Gdb 5.x does sign extend. */
6787 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
6788 break;
6790 case dw_val_class_unsigned_const:
6791 dw2_asm_output_data (constant_size (AT_unsigned (a)),
6792 AT_unsigned (a), "%s", name);
6793 break;
6795 case dw_val_class_long_long:
6797 unsigned HOST_WIDE_INT first, second;
6799 dw2_asm_output_data (1,
6800 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6801 "%s", name);
6803 if (WORDS_BIG_ENDIAN)
6805 first = a->dw_attr_val.v.val_long_long.hi;
6806 second = a->dw_attr_val.v.val_long_long.low;
6808 else
6810 first = a->dw_attr_val.v.val_long_long.low;
6811 second = a->dw_attr_val.v.val_long_long.hi;
6814 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6815 first, "long long constant");
6816 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6817 second, NULL);
6819 break;
6821 case dw_val_class_vec:
6823 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
6824 unsigned int len = a->dw_attr_val.v.val_vec.length;
6825 unsigned int i;
6826 unsigned char *p;
6828 dw2_asm_output_data (1, len * elt_size, "%s", name);
6829 if (elt_size > sizeof (HOST_WIDE_INT))
6831 elt_size /= 2;
6832 len *= 2;
6834 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
6835 i < len;
6836 i++, p += elt_size)
6837 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
6838 "fp or vector constant word %u", i);
6839 break;
6842 case dw_val_class_flag:
6843 dw2_asm_output_data (1, AT_flag (a), "%s", name);
6844 break;
6846 case dw_val_class_loc_list:
6848 char *sym = AT_loc_list (a)->ll_symbol;
6850 if (sym == 0)
6851 abort ();
6852 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, "%s", name);
6854 break;
6856 case dw_val_class_die_ref:
6857 if (AT_ref_external (a))
6859 char *sym = AT_ref (a)->die_symbol;
6861 if (sym == 0)
6862 abort ();
6863 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, "%s", name);
6865 else if (AT_ref (a)->die_offset == 0)
6866 abort ();
6867 else
6868 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
6869 "%s", name);
6870 break;
6872 case dw_val_class_fde_ref:
6874 char l1[20];
6876 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
6877 a->dw_attr_val.v.val_fde_index * 2);
6878 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, "%s", name);
6880 break;
6882 case dw_val_class_lbl_id:
6883 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
6884 break;
6886 case dw_val_class_lbl_offset:
6887 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a), "%s", name);
6888 break;
6890 case dw_val_class_str:
6891 if (AT_string_form (a) == DW_FORM_strp)
6892 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
6893 a->dw_attr_val.v.val_str->label,
6894 "%s: \"%s\"", name, AT_string (a));
6895 else
6896 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
6897 break;
6899 default:
6900 abort ();
6904 for (c = die->die_child; c != NULL; c = c->die_sib)
6905 output_die (c);
6907 /* Add null byte to terminate sibling list. */
6908 if (die->die_child != NULL)
6909 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
6910 die->die_offset);
6913 /* Output the compilation unit that appears at the beginning of the
6914 .debug_info section, and precedes the DIE descriptions. */
6916 static void
6917 output_compilation_unit_header (void)
6919 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
6920 dw2_asm_output_data (4, 0xffffffff,
6921 "Initial length escape value indicating 64-bit DWARF extension");
6922 dw2_asm_output_data (DWARF_OFFSET_SIZE,
6923 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
6924 "Length of Compilation Unit Info");
6925 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
6926 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
6927 "Offset Into Abbrev. Section");
6928 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
6931 /* Output the compilation unit DIE and its children. */
6933 static void
6934 output_comp_unit (dw_die_ref die, int output_if_empty)
6936 const char *secname;
6937 char *oldsym, *tmp;
6939 /* Unless we are outputting main CU, we may throw away empty ones. */
6940 if (!output_if_empty && die->die_child == NULL)
6941 return;
6943 /* Even if there are no children of this DIE, we must output the information
6944 about the compilation unit. Otherwise, on an empty translation unit, we
6945 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
6946 will then complain when examining the file. First mark all the DIEs in
6947 this CU so we know which get local refs. */
6948 mark_dies (die);
6950 build_abbrev_table (die);
6952 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
6953 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
6954 calc_die_sizes (die);
6956 oldsym = die->die_symbol;
6957 if (oldsym)
6959 tmp = alloca (strlen (oldsym) + 24);
6961 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
6962 secname = tmp;
6963 die->die_symbol = NULL;
6965 else
6966 secname = (const char *) DEBUG_INFO_SECTION;
6968 /* Output debugging information. */
6969 named_section_flags (secname, SECTION_DEBUG);
6970 output_compilation_unit_header ();
6971 output_die (die);
6973 /* Leave the marks on the main CU, so we can check them in
6974 output_pubnames. */
6975 if (oldsym)
6977 unmark_dies (die);
6978 die->die_symbol = oldsym;
6982 /* The DWARF2 pubname for a nested thingy looks like "A::f". The
6983 output of lang_hooks.decl_printable_name for C++ looks like
6984 "A::f(int)". Let's drop the argument list, and maybe the scope. */
6986 static const char *
6987 dwarf2_name (tree decl, int scope)
6989 return lang_hooks.decl_printable_name (decl, scope ? 1 : 0);
6992 /* Add a new entry to .debug_pubnames if appropriate. */
6994 static void
6995 add_pubname (tree decl, dw_die_ref die)
6997 pubname_ref p;
6999 if (! TREE_PUBLIC (decl))
7000 return;
7002 if (pubname_table_in_use == pubname_table_allocated)
7004 pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
7005 pubname_table
7006 = ggc_realloc (pubname_table,
7007 (pubname_table_allocated * sizeof (pubname_entry)));
7008 memset (pubname_table + pubname_table_in_use, 0,
7009 PUBNAME_TABLE_INCREMENT * sizeof (pubname_entry));
7012 p = &pubname_table[pubname_table_in_use++];
7013 p->die = die;
7014 p->name = xstrdup (dwarf2_name (decl, 1));
7017 /* Output the public names table used to speed up access to externally
7018 visible names. For now, only generate entries for externally
7019 visible procedures. */
7021 static void
7022 output_pubnames (void)
7024 unsigned i;
7025 unsigned long pubnames_length = size_of_pubnames ();
7027 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7028 dw2_asm_output_data (4, 0xffffffff,
7029 "Initial length escape value indicating 64-bit DWARF extension");
7030 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7031 "Length of Public Names Info");
7032 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7033 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7034 "Offset of Compilation Unit Info");
7035 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7036 "Compilation Unit Length");
7038 for (i = 0; i < pubname_table_in_use; i++)
7040 pubname_ref pub = &pubname_table[i];
7042 /* We shouldn't see pubnames for DIEs outside of the main CU. */
7043 if (pub->die->die_mark == 0)
7044 abort ();
7046 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7047 "DIE offset");
7049 dw2_asm_output_nstring (pub->name, -1, "external name");
7052 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7055 /* Add a new entry to .debug_aranges if appropriate. */
7057 static void
7058 add_arange (tree decl, dw_die_ref die)
7060 if (! DECL_SECTION_NAME (decl))
7061 return;
7063 if (arange_table_in_use == arange_table_allocated)
7065 arange_table_allocated += ARANGE_TABLE_INCREMENT;
7066 arange_table = ggc_realloc (arange_table,
7067 (arange_table_allocated
7068 * sizeof (dw_die_ref)));
7069 memset (arange_table + arange_table_in_use, 0,
7070 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7073 arange_table[arange_table_in_use++] = die;
7076 /* Output the information that goes into the .debug_aranges table.
7077 Namely, define the beginning and ending address range of the
7078 text section generated for this compilation unit. */
7080 static void
7081 output_aranges (void)
7083 unsigned i;
7084 unsigned long aranges_length = size_of_aranges ();
7086 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7087 dw2_asm_output_data (4, 0xffffffff,
7088 "Initial length escape value indicating 64-bit DWARF extension");
7089 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7090 "Length of Address Ranges Info");
7091 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7092 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7093 "Offset of Compilation Unit Info");
7094 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7095 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7097 /* We need to align to twice the pointer size here. */
7098 if (DWARF_ARANGES_PAD_SIZE)
7100 /* Pad using a 2 byte words so that padding is correct for any
7101 pointer size. */
7102 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7103 2 * DWARF2_ADDR_SIZE);
7104 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7105 dw2_asm_output_data (2, 0, NULL);
7108 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7109 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7110 text_section_label, "Length");
7112 for (i = 0; i < arange_table_in_use; i++)
7114 dw_die_ref die = arange_table[i];
7116 /* We shouldn't see aranges for DIEs outside of the main CU. */
7117 if (die->die_mark == 0)
7118 abort ();
7120 if (die->die_tag == DW_TAG_subprogram)
7122 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7123 "Address");
7124 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7125 get_AT_low_pc (die), "Length");
7127 else
7129 /* A static variable; extract the symbol from DW_AT_location.
7130 Note that this code isn't currently hit, as we only emit
7131 aranges for functions (jason 9/23/99). */
7132 dw_attr_ref a = get_AT (die, DW_AT_location);
7133 dw_loc_descr_ref loc;
7135 if (! a || AT_class (a) != dw_val_class_loc)
7136 abort ();
7138 loc = AT_loc (a);
7139 if (loc->dw_loc_opc != DW_OP_addr)
7140 abort ();
7142 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7143 loc->dw_loc_oprnd1.v.val_addr, "Address");
7144 dw2_asm_output_data (DWARF2_ADDR_SIZE,
7145 get_AT_unsigned (die, DW_AT_byte_size),
7146 "Length");
7150 /* Output the terminator words. */
7151 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7152 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7155 /* Add a new entry to .debug_ranges. Return the offset at which it
7156 was placed. */
7158 static unsigned int
7159 add_ranges (tree block)
7161 unsigned int in_use = ranges_table_in_use;
7163 if (in_use == ranges_table_allocated)
7165 ranges_table_allocated += RANGES_TABLE_INCREMENT;
7166 ranges_table
7167 = ggc_realloc (ranges_table, (ranges_table_allocated
7168 * sizeof (struct dw_ranges_struct)));
7169 memset (ranges_table + ranges_table_in_use, 0,
7170 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7173 ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7174 ranges_table_in_use = in_use + 1;
7176 return in_use * 2 * DWARF2_ADDR_SIZE;
7179 static void
7180 output_ranges (void)
7182 unsigned i;
7183 static const char *const start_fmt = "Offset 0x%x";
7184 const char *fmt = start_fmt;
7186 for (i = 0; i < ranges_table_in_use; i++)
7188 int block_num = ranges_table[i].block_num;
7190 if (block_num)
7192 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7193 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7195 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7196 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7198 /* If all code is in the text section, then the compilation
7199 unit base address defaults to DW_AT_low_pc, which is the
7200 base of the text section. */
7201 if (separate_line_info_table_in_use == 0)
7203 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7204 text_section_label,
7205 fmt, i * 2 * DWARF2_ADDR_SIZE);
7206 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7207 text_section_label, NULL);
7210 /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7211 compilation unit base address to zero, which allows us to
7212 use absolute addresses, and not worry about whether the
7213 target supports cross-section arithmetic. */
7214 else
7216 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7217 fmt, i * 2 * DWARF2_ADDR_SIZE);
7218 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7221 fmt = NULL;
7223 else
7225 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7226 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7227 fmt = start_fmt;
7232 /* Data structure containing information about input files. */
7233 struct file_info
7235 char *path; /* Complete file name. */
7236 char *fname; /* File name part. */
7237 int length; /* Length of entire string. */
7238 int file_idx; /* Index in input file table. */
7239 int dir_idx; /* Index in directory table. */
7242 /* Data structure containing information about directories with source
7243 files. */
7244 struct dir_info
7246 char *path; /* Path including directory name. */
7247 int length; /* Path length. */
7248 int prefix; /* Index of directory entry which is a prefix. */
7249 int count; /* Number of files in this directory. */
7250 int dir_idx; /* Index of directory used as base. */
7251 int used; /* Used in the end? */
7254 /* Callback function for file_info comparison. We sort by looking at
7255 the directories in the path. */
7257 static int
7258 file_info_cmp (const void *p1, const void *p2)
7260 const struct file_info *s1 = p1;
7261 const struct file_info *s2 = p2;
7262 unsigned char *cp1;
7263 unsigned char *cp2;
7265 /* Take care of file names without directories. We need to make sure that
7266 we return consistent values to qsort since some will get confused if
7267 we return the same value when identical operands are passed in opposite
7268 orders. So if neither has a directory, return 0 and otherwise return
7269 1 or -1 depending on which one has the directory. */
7270 if ((s1->path == s1->fname || s2->path == s2->fname))
7271 return (s2->path == s2->fname) - (s1->path == s1->fname);
7273 cp1 = (unsigned char *) s1->path;
7274 cp2 = (unsigned char *) s2->path;
7276 while (1)
7278 ++cp1;
7279 ++cp2;
7280 /* Reached the end of the first path? If so, handle like above. */
7281 if ((cp1 == (unsigned char *) s1->fname)
7282 || (cp2 == (unsigned char *) s2->fname))
7283 return ((cp2 == (unsigned char *) s2->fname)
7284 - (cp1 == (unsigned char *) s1->fname));
7286 /* Character of current path component the same? */
7287 else if (*cp1 != *cp2)
7288 return *cp1 - *cp2;
7292 /* Output the directory table and the file name table. We try to minimize
7293 the total amount of memory needed. A heuristic is used to avoid large
7294 slowdowns with many input files. */
7296 static void
7297 output_file_names (void)
7299 struct file_info *files;
7300 struct dir_info *dirs;
7301 int *saved;
7302 int *savehere;
7303 int *backmap;
7304 size_t ndirs;
7305 int idx_offset;
7306 size_t i;
7307 int idx;
7309 /* Handle the case where file_table is empty. */
7310 if (VARRAY_ACTIVE_SIZE (file_table) <= 1)
7312 dw2_asm_output_data (1, 0, "End directory table");
7313 dw2_asm_output_data (1, 0, "End file name table");
7314 return;
7317 /* Allocate the various arrays we need. */
7318 files = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct file_info));
7319 dirs = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct dir_info));
7321 /* Sort the file names. */
7322 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7324 char *f;
7326 /* Skip all leading "./". */
7327 f = VARRAY_CHAR_PTR (file_table, i);
7328 while (f[0] == '.' && f[1] == '/')
7329 f += 2;
7331 /* Create a new array entry. */
7332 files[i].path = f;
7333 files[i].length = strlen (f);
7334 files[i].file_idx = i;
7336 /* Search for the file name part. */
7337 f = strrchr (f, '/');
7338 files[i].fname = f == NULL ? files[i].path : f + 1;
7341 qsort (files + 1, VARRAY_ACTIVE_SIZE (file_table) - 1,
7342 sizeof (files[0]), file_info_cmp);
7344 /* Find all the different directories used. */
7345 dirs[0].path = files[1].path;
7346 dirs[0].length = files[1].fname - files[1].path;
7347 dirs[0].prefix = -1;
7348 dirs[0].count = 1;
7349 dirs[0].dir_idx = 0;
7350 dirs[0].used = 0;
7351 files[1].dir_idx = 0;
7352 ndirs = 1;
7354 for (i = 2; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7355 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7356 && memcmp (dirs[ndirs - 1].path, files[i].path,
7357 dirs[ndirs - 1].length) == 0)
7359 /* Same directory as last entry. */
7360 files[i].dir_idx = ndirs - 1;
7361 ++dirs[ndirs - 1].count;
7363 else
7365 size_t j;
7367 /* This is a new directory. */
7368 dirs[ndirs].path = files[i].path;
7369 dirs[ndirs].length = files[i].fname - files[i].path;
7370 dirs[ndirs].count = 1;
7371 dirs[ndirs].dir_idx = ndirs;
7372 dirs[ndirs].used = 0;
7373 files[i].dir_idx = ndirs;
7375 /* Search for a prefix. */
7376 dirs[ndirs].prefix = -1;
7377 for (j = 0; j < ndirs; j++)
7378 if (dirs[j].length < dirs[ndirs].length
7379 && dirs[j].length > 1
7380 && (dirs[ndirs].prefix == -1
7381 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7382 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7383 dirs[ndirs].prefix = j;
7385 ++ndirs;
7388 /* Now to the actual work. We have to find a subset of the directories which
7389 allow expressing the file name using references to the directory table
7390 with the least amount of characters. We do not do an exhaustive search
7391 where we would have to check out every combination of every single
7392 possible prefix. Instead we use a heuristic which provides nearly optimal
7393 results in most cases and never is much off. */
7394 saved = alloca (ndirs * sizeof (int));
7395 savehere = alloca (ndirs * sizeof (int));
7397 memset (saved, '\0', ndirs * sizeof (saved[0]));
7398 for (i = 0; i < ndirs; i++)
7400 size_t j;
7401 int total;
7403 /* We can always save some space for the current directory. But this
7404 does not mean it will be enough to justify adding the directory. */
7405 savehere[i] = dirs[i].length;
7406 total = (savehere[i] - saved[i]) * dirs[i].count;
7408 for (j = i + 1; j < ndirs; j++)
7410 savehere[j] = 0;
7411 if (saved[j] < dirs[i].length)
7413 /* Determine whether the dirs[i] path is a prefix of the
7414 dirs[j] path. */
7415 int k;
7417 k = dirs[j].prefix;
7418 while (k != -1 && k != (int) i)
7419 k = dirs[k].prefix;
7421 if (k == (int) i)
7423 /* Yes it is. We can possibly safe some memory but
7424 writing the filenames in dirs[j] relative to
7425 dirs[i]. */
7426 savehere[j] = dirs[i].length;
7427 total += (savehere[j] - saved[j]) * dirs[j].count;
7432 /* Check whether we can safe enough to justify adding the dirs[i]
7433 directory. */
7434 if (total > dirs[i].length + 1)
7436 /* It's worthwhile adding. */
7437 for (j = i; j < ndirs; j++)
7438 if (savehere[j] > 0)
7440 /* Remember how much we saved for this directory so far. */
7441 saved[j] = savehere[j];
7443 /* Remember the prefix directory. */
7444 dirs[j].dir_idx = i;
7449 /* We have to emit them in the order they appear in the file_table array
7450 since the index is used in the debug info generation. To do this
7451 efficiently we generate a back-mapping of the indices first. */
7452 backmap = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (int));
7453 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7455 backmap[files[i].file_idx] = i;
7457 /* Mark this directory as used. */
7458 dirs[dirs[files[i].dir_idx].dir_idx].used = 1;
7461 /* That was it. We are ready to emit the information. First emit the
7462 directory name table. We have to make sure the first actually emitted
7463 directory name has index one; zero is reserved for the current working
7464 directory. Make sure we do not confuse these indices with the one for the
7465 constructed table (even though most of the time they are identical). */
7466 idx = 1;
7467 idx_offset = dirs[0].length > 0 ? 1 : 0;
7468 for (i = 1 - idx_offset; i < ndirs; i++)
7469 if (dirs[i].used != 0)
7471 dirs[i].used = idx++;
7472 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7473 "Directory Entry: 0x%x", dirs[i].used);
7476 dw2_asm_output_data (1, 0, "End directory table");
7478 /* Correct the index for the current working directory entry if it
7479 exists. */
7480 if (idx_offset == 0)
7481 dirs[0].used = 0;
7483 /* Now write all the file names. */
7484 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7486 int file_idx = backmap[i];
7487 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7489 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7490 "File Entry: 0x%lx", (unsigned long) i);
7492 /* Include directory index. */
7493 dw2_asm_output_data_uleb128 (dirs[dir_idx].used, NULL);
7495 /* Modification time. */
7496 dw2_asm_output_data_uleb128 (0, NULL);
7498 /* File length in bytes. */
7499 dw2_asm_output_data_uleb128 (0, NULL);
7502 dw2_asm_output_data (1, 0, "End file name table");
7506 /* Output the source line number correspondence information. This
7507 information goes into the .debug_line section. */
7509 static void
7510 output_line_info (void)
7512 char l1[20], l2[20], p1[20], p2[20];
7513 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7514 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7515 unsigned opc;
7516 unsigned n_op_args;
7517 unsigned long lt_index;
7518 unsigned long current_line;
7519 long line_offset;
7520 long line_delta;
7521 unsigned long current_file;
7522 unsigned long function;
7524 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7525 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7526 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7527 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7529 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7530 dw2_asm_output_data (4, 0xffffffff,
7531 "Initial length escape value indicating 64-bit DWARF extension");
7532 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7533 "Length of Source Line Info");
7534 ASM_OUTPUT_LABEL (asm_out_file, l1);
7536 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7537 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7538 ASM_OUTPUT_LABEL (asm_out_file, p1);
7540 /* Define the architecture-dependent minimum instruction length (in
7541 bytes). In this implementation of DWARF, this field is used for
7542 information purposes only. Since GCC generates assembly language,
7543 we have no a priori knowledge of how many instruction bytes are
7544 generated for each source line, and therefore can use only the
7545 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7546 commands. Accordingly, we fix this as `1', which is "correct
7547 enough" for all architectures, and don't let the target override. */
7548 dw2_asm_output_data (1, 1,
7549 "Minimum Instruction Length");
7551 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7552 "Default is_stmt_start flag");
7553 dw2_asm_output_data (1, DWARF_LINE_BASE,
7554 "Line Base Value (Special Opcodes)");
7555 dw2_asm_output_data (1, DWARF_LINE_RANGE,
7556 "Line Range Value (Special Opcodes)");
7557 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7558 "Special Opcode Base");
7560 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7562 switch (opc)
7564 case DW_LNS_advance_pc:
7565 case DW_LNS_advance_line:
7566 case DW_LNS_set_file:
7567 case DW_LNS_set_column:
7568 case DW_LNS_fixed_advance_pc:
7569 n_op_args = 1;
7570 break;
7571 default:
7572 n_op_args = 0;
7573 break;
7576 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
7577 opc, n_op_args);
7580 /* Write out the information about the files we use. */
7581 output_file_names ();
7582 ASM_OUTPUT_LABEL (asm_out_file, p2);
7584 /* We used to set the address register to the first location in the text
7585 section here, but that didn't accomplish anything since we already
7586 have a line note for the opening brace of the first function. */
7588 /* Generate the line number to PC correspondence table, encoded as
7589 a series of state machine operations. */
7590 current_file = 1;
7591 current_line = 1;
7592 strcpy (prev_line_label, text_section_label);
7593 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
7595 dw_line_info_ref line_info = &line_info_table[lt_index];
7597 #if 0
7598 /* Disable this optimization for now; GDB wants to see two line notes
7599 at the beginning of a function so it can find the end of the
7600 prologue. */
7602 /* Don't emit anything for redundant notes. Just updating the
7603 address doesn't accomplish anything, because we already assume
7604 that anything after the last address is this line. */
7605 if (line_info->dw_line_num == current_line
7606 && line_info->dw_file_num == current_file)
7607 continue;
7608 #endif
7610 /* Emit debug info for the address of the current line.
7612 Unfortunately, we have little choice here currently, and must always
7613 use the most general form. GCC does not know the address delta
7614 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
7615 attributes which will give an upper bound on the address range. We
7616 could perhaps use length attributes to determine when it is safe to
7617 use DW_LNS_fixed_advance_pc. */
7619 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
7620 if (0)
7622 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
7623 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7624 "DW_LNS_fixed_advance_pc");
7625 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7627 else
7629 /* This can handle any delta. This takes
7630 4+DWARF2_ADDR_SIZE bytes. */
7631 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7632 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7633 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7634 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7637 strcpy (prev_line_label, line_label);
7639 /* Emit debug info for the source file of the current line, if
7640 different from the previous line. */
7641 if (line_info->dw_file_num != current_file)
7643 current_file = line_info->dw_file_num;
7644 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7645 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7646 VARRAY_CHAR_PTR (file_table,
7647 current_file));
7650 /* Emit debug info for the current line number, choosing the encoding
7651 that uses the least amount of space. */
7652 if (line_info->dw_line_num != current_line)
7654 line_offset = line_info->dw_line_num - current_line;
7655 line_delta = line_offset - DWARF_LINE_BASE;
7656 current_line = line_info->dw_line_num;
7657 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7658 /* This can handle deltas from -10 to 234, using the current
7659 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
7660 takes 1 byte. */
7661 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7662 "line %lu", current_line);
7663 else
7665 /* This can handle any delta. This takes at least 4 bytes,
7666 depending on the value being encoded. */
7667 dw2_asm_output_data (1, DW_LNS_advance_line,
7668 "advance to line %lu", current_line);
7669 dw2_asm_output_data_sleb128 (line_offset, NULL);
7670 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7673 else
7674 /* We still need to start a new row, so output a copy insn. */
7675 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7678 /* Emit debug info for the address of the end of the function. */
7679 if (0)
7681 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7682 "DW_LNS_fixed_advance_pc");
7683 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
7685 else
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, text_end_label, NULL);
7693 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7694 dw2_asm_output_data_uleb128 (1, NULL);
7695 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7697 function = 0;
7698 current_file = 1;
7699 current_line = 1;
7700 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
7702 dw_separate_line_info_ref line_info
7703 = &separate_line_info_table[lt_index];
7705 #if 0
7706 /* Don't emit anything for redundant notes. */
7707 if (line_info->dw_line_num == current_line
7708 && line_info->dw_file_num == current_file
7709 && line_info->function == function)
7710 goto cont;
7711 #endif
7713 /* Emit debug info for the address of the current line. If this is
7714 a new function, or the first line of a function, then we need
7715 to handle it differently. */
7716 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
7717 lt_index);
7718 if (function != line_info->function)
7720 function = line_info->function;
7722 /* Set the address register to the first line in the function. */
7723 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7724 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7725 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7726 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7728 else
7730 /* ??? See the DW_LNS_advance_pc comment above. */
7731 if (0)
7733 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7734 "DW_LNS_fixed_advance_pc");
7735 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7737 else
7739 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7740 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7741 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7742 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7746 strcpy (prev_line_label, line_label);
7748 /* Emit debug info for the source file of the current line, if
7749 different from the previous line. */
7750 if (line_info->dw_file_num != current_file)
7752 current_file = line_info->dw_file_num;
7753 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7754 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7755 VARRAY_CHAR_PTR (file_table,
7756 current_file));
7759 /* Emit debug info for the current line number, choosing the encoding
7760 that uses the least amount of space. */
7761 if (line_info->dw_line_num != current_line)
7763 line_offset = line_info->dw_line_num - current_line;
7764 line_delta = line_offset - DWARF_LINE_BASE;
7765 current_line = line_info->dw_line_num;
7766 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7767 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7768 "line %lu", current_line);
7769 else
7771 dw2_asm_output_data (1, DW_LNS_advance_line,
7772 "advance to line %lu", current_line);
7773 dw2_asm_output_data_sleb128 (line_offset, NULL);
7774 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7777 else
7778 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7780 #if 0
7781 cont:
7782 #endif
7784 lt_index++;
7786 /* If we're done with a function, end its sequence. */
7787 if (lt_index == separate_line_info_table_in_use
7788 || separate_line_info_table[lt_index].function != function)
7790 current_file = 1;
7791 current_line = 1;
7793 /* Emit debug info for the address of the end of the function. */
7794 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
7795 if (0)
7797 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7798 "DW_LNS_fixed_advance_pc");
7799 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7801 else
7803 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7804 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7805 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7806 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7809 /* Output the marker for the end of this sequence. */
7810 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7811 dw2_asm_output_data_uleb128 (1, NULL);
7812 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7816 /* Output the marker for the end of the line number info. */
7817 ASM_OUTPUT_LABEL (asm_out_file, l2);
7820 /* Given a pointer to a tree node for some base type, return a pointer to
7821 a DIE that describes the given type.
7823 This routine must only be called for GCC type nodes that correspond to
7824 Dwarf base (fundamental) types. */
7826 static dw_die_ref
7827 base_type_die (tree type)
7829 dw_die_ref base_type_result;
7830 const char *type_name;
7831 enum dwarf_type encoding;
7832 tree name = TYPE_NAME (type);
7834 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
7835 return 0;
7837 if (name)
7839 if (TREE_CODE (name) == TYPE_DECL)
7840 name = DECL_NAME (name);
7842 type_name = IDENTIFIER_POINTER (name);
7844 else
7845 type_name = "__unknown__";
7847 switch (TREE_CODE (type))
7849 case INTEGER_TYPE:
7850 /* Carefully distinguish the C character types, without messing
7851 up if the language is not C. Note that we check only for the names
7852 that contain spaces; other names might occur by coincidence in other
7853 languages. */
7854 if (! (TYPE_PRECISION (type) == CHAR_TYPE_SIZE
7855 && (type == char_type_node
7856 || ! strcmp (type_name, "signed char")
7857 || ! strcmp (type_name, "unsigned char"))))
7859 if (TYPE_UNSIGNED (type))
7860 encoding = DW_ATE_unsigned;
7861 else
7862 encoding = DW_ATE_signed;
7863 break;
7865 /* else fall through. */
7867 case CHAR_TYPE:
7868 /* GNU Pascal/Ada CHAR type. Not used in C. */
7869 if (TYPE_UNSIGNED (type))
7870 encoding = DW_ATE_unsigned_char;
7871 else
7872 encoding = DW_ATE_signed_char;
7873 break;
7875 case REAL_TYPE:
7876 encoding = DW_ATE_float;
7877 break;
7879 /* Dwarf2 doesn't know anything about complex ints, so use
7880 a user defined type for it. */
7881 case COMPLEX_TYPE:
7882 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
7883 encoding = DW_ATE_complex_float;
7884 else
7885 encoding = DW_ATE_lo_user;
7886 break;
7888 case BOOLEAN_TYPE:
7889 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
7890 encoding = DW_ATE_boolean;
7891 break;
7893 default:
7894 /* No other TREE_CODEs are Dwarf fundamental types. */
7895 abort ();
7898 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
7899 if (demangle_name_func)
7900 type_name = (*demangle_name_func) (type_name);
7902 add_AT_string (base_type_result, DW_AT_name, type_name);
7903 add_AT_unsigned (base_type_result, DW_AT_byte_size,
7904 int_size_in_bytes (type));
7905 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
7907 return base_type_result;
7910 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
7911 the Dwarf "root" type for the given input type. The Dwarf "root" type of
7912 a given type is generally the same as the given type, except that if the
7913 given type is a pointer or reference type, then the root type of the given
7914 type is the root type of the "basis" type for the pointer or reference
7915 type. (This definition of the "root" type is recursive.) Also, the root
7916 type of a `const' qualified type or a `volatile' qualified type is the
7917 root type of the given type without the qualifiers. */
7919 static tree
7920 root_type (tree type)
7922 if (TREE_CODE (type) == ERROR_MARK)
7923 return error_mark_node;
7925 switch (TREE_CODE (type))
7927 case ERROR_MARK:
7928 return error_mark_node;
7930 case POINTER_TYPE:
7931 case REFERENCE_TYPE:
7932 return type_main_variant (root_type (TREE_TYPE (type)));
7934 default:
7935 return type_main_variant (type);
7939 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
7940 given input type is a Dwarf "fundamental" type. Otherwise return null. */
7942 static inline int
7943 is_base_type (tree type)
7945 switch (TREE_CODE (type))
7947 case ERROR_MARK:
7948 case VOID_TYPE:
7949 case INTEGER_TYPE:
7950 case REAL_TYPE:
7951 case COMPLEX_TYPE:
7952 case BOOLEAN_TYPE:
7953 case CHAR_TYPE:
7954 return 1;
7956 case SET_TYPE:
7957 case ARRAY_TYPE:
7958 case RECORD_TYPE:
7959 case UNION_TYPE:
7960 case QUAL_UNION_TYPE:
7961 case ENUMERAL_TYPE:
7962 case FUNCTION_TYPE:
7963 case METHOD_TYPE:
7964 case POINTER_TYPE:
7965 case REFERENCE_TYPE:
7966 case FILE_TYPE:
7967 case OFFSET_TYPE:
7968 case LANG_TYPE:
7969 case VECTOR_TYPE:
7970 return 0;
7972 default:
7973 abort ();
7976 return 0;
7979 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
7980 node, return the size in bits for the type if it is a constant, or else
7981 return the alignment for the type if the type's size is not constant, or
7982 else return BITS_PER_WORD if the type actually turns out to be an
7983 ERROR_MARK node. */
7985 static inline unsigned HOST_WIDE_INT
7986 simple_type_size_in_bits (tree type)
7988 if (TREE_CODE (type) == ERROR_MARK)
7989 return BITS_PER_WORD;
7990 else if (TYPE_SIZE (type) == NULL_TREE)
7991 return 0;
7992 else if (host_integerp (TYPE_SIZE (type), 1))
7993 return tree_low_cst (TYPE_SIZE (type), 1);
7994 else
7995 return TYPE_ALIGN (type);
7998 /* Return true if the debug information for the given type should be
7999 emitted as a subrange type. */
8001 static inline bool
8002 is_subrange_type (tree type)
8004 tree subtype = TREE_TYPE (type);
8006 /* Subrange types are identified by the fact that they are integer
8007 types, and that they have a subtype which is either an integer type
8008 or an enumeral type. */
8010 if (TREE_CODE (type) != INTEGER_TYPE
8011 || subtype == NULL_TREE)
8012 return false;
8014 if (TREE_CODE (subtype) != INTEGER_TYPE
8015 && TREE_CODE (subtype) != ENUMERAL_TYPE)
8016 return false;
8018 if (TREE_CODE (type) == TREE_CODE (subtype)
8019 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8020 && TYPE_MIN_VALUE (type) != NULL
8021 && TYPE_MIN_VALUE (subtype) != NULL
8022 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8023 && TYPE_MAX_VALUE (type) != NULL
8024 && TYPE_MAX_VALUE (subtype) != NULL
8025 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8027 /* The type and its subtype have the same representation. If in
8028 addition the two types also have the same name, then the given
8029 type is not a subrange type, but rather a plain base type. */
8030 /* FIXME: brobecker/2004-03-22:
8031 Sizetype INTEGER_CSTs nodes are canonicalized. It should
8032 therefore be sufficient to check the TYPE_SIZE node pointers
8033 rather than checking the actual size. Unfortunately, we have
8034 found some cases, such as in the Ada "integer" type, where
8035 this is not the case. Until this problem is solved, we need to
8036 keep checking the actual size. */
8037 tree type_name = TYPE_NAME (type);
8038 tree subtype_name = TYPE_NAME (subtype);
8040 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8041 type_name = DECL_NAME (type_name);
8043 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8044 subtype_name = DECL_NAME (subtype_name);
8046 if (type_name == subtype_name)
8047 return false;
8050 return true;
8053 /* Given a pointer to a tree node for a subrange type, return a pointer
8054 to a DIE that describes the given type. */
8056 static dw_die_ref
8057 subrange_type_die (tree type, dw_die_ref context_die)
8059 dw_die_ref subtype_die;
8060 dw_die_ref subrange_die;
8061 tree name = TYPE_NAME (type);
8062 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8063 tree subtype = TREE_TYPE (type);
8065 if (context_die == NULL)
8066 context_die = comp_unit_die;
8068 if (TREE_CODE (subtype) == ENUMERAL_TYPE)
8069 subtype_die = gen_enumeration_type_die (subtype, context_die);
8070 else
8071 subtype_die = base_type_die (subtype);
8073 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8075 if (name != NULL)
8077 if (TREE_CODE (name) == TYPE_DECL)
8078 name = DECL_NAME (name);
8079 add_name_attribute (subrange_die, IDENTIFIER_POINTER (name));
8082 if (int_size_in_bytes (subtype) != size_in_bytes)
8084 /* The size of the subrange type and its base type do not match,
8085 so we need to generate a size attribute for the subrange type. */
8086 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8089 if (TYPE_MIN_VALUE (type) != NULL)
8090 add_bound_info (subrange_die, DW_AT_lower_bound,
8091 TYPE_MIN_VALUE (type));
8092 if (TYPE_MAX_VALUE (type) != NULL)
8093 add_bound_info (subrange_die, DW_AT_upper_bound,
8094 TYPE_MAX_VALUE (type));
8095 add_AT_die_ref (subrange_die, DW_AT_type, subtype_die);
8097 return subrange_die;
8100 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8101 entry that chains various modifiers in front of the given type. */
8103 static dw_die_ref
8104 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8105 dw_die_ref context_die)
8107 enum tree_code code = TREE_CODE (type);
8108 dw_die_ref mod_type_die = NULL;
8109 dw_die_ref sub_die = NULL;
8110 tree item_type = NULL;
8112 if (code != ERROR_MARK)
8114 tree qualified_type;
8116 /* See if we already have the appropriately qualified variant of
8117 this type. */
8118 qualified_type
8119 = get_qualified_type (type,
8120 ((is_const_type ? TYPE_QUAL_CONST : 0)
8121 | (is_volatile_type
8122 ? TYPE_QUAL_VOLATILE : 0)));
8124 /* If we do, then we can just use its DIE, if it exists. */
8125 if (qualified_type)
8127 mod_type_die = lookup_type_die (qualified_type);
8128 if (mod_type_die)
8129 return mod_type_die;
8132 /* Handle C typedef types. */
8133 if (qualified_type && TYPE_NAME (qualified_type)
8134 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL
8135 && DECL_ORIGINAL_TYPE (TYPE_NAME (qualified_type)))
8137 tree type_name = TYPE_NAME (qualified_type);
8138 tree dtype = TREE_TYPE (type_name);
8140 if (qualified_type == dtype)
8142 /* For a named type, use the typedef. */
8143 gen_type_die (qualified_type, context_die);
8144 mod_type_die = lookup_type_die (qualified_type);
8146 else if (is_const_type < TYPE_READONLY (dtype)
8147 || is_volatile_type < TYPE_VOLATILE (dtype))
8148 /* cv-unqualified version of named type. Just use the unnamed
8149 type to which it refers. */
8150 mod_type_die
8151 = modified_type_die (DECL_ORIGINAL_TYPE (type_name),
8152 is_const_type, is_volatile_type,
8153 context_die);
8155 /* Else cv-qualified version of named type; fall through. */
8158 if (mod_type_die)
8159 /* OK. */
8161 else if (is_const_type)
8163 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8164 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8166 else if (is_volatile_type)
8168 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8169 sub_die = modified_type_die (type, 0, 0, context_die);
8171 else if (code == POINTER_TYPE)
8173 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8174 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8175 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8176 #if 0
8177 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8178 #endif
8179 item_type = TREE_TYPE (type);
8181 else if (code == REFERENCE_TYPE)
8183 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8184 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8185 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8186 #if 0
8187 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8188 #endif
8189 item_type = TREE_TYPE (type);
8191 else if (is_subrange_type (type))
8192 mod_type_die = subrange_type_die (type, context_die);
8193 else if (is_base_type (type))
8194 mod_type_die = base_type_die (type);
8195 else
8197 gen_type_die (type, context_die);
8199 /* We have to get the type_main_variant here (and pass that to the
8200 `lookup_type_die' routine) because the ..._TYPE node we have
8201 might simply be a *copy* of some original type node (where the
8202 copy was created to help us keep track of typedef names) and
8203 that copy might have a different TYPE_UID from the original
8204 ..._TYPE node. */
8205 if (TREE_CODE (type) != VECTOR_TYPE)
8206 mod_type_die = lookup_type_die (type_main_variant (type));
8207 else
8208 /* Vectors have the debugging information in the type,
8209 not the main variant. */
8210 mod_type_die = lookup_type_die (type);
8211 if (mod_type_die == NULL)
8212 abort ();
8215 /* We want to equate the qualified type to the die below. */
8216 type = qualified_type;
8219 if (type)
8220 equate_type_number_to_die (type, mod_type_die);
8221 if (item_type)
8222 /* We must do this after the equate_type_number_to_die call, in case
8223 this is a recursive type. This ensures that the modified_type_die
8224 recursion will terminate even if the type is recursive. Recursive
8225 types are possible in Ada. */
8226 sub_die = modified_type_die (item_type,
8227 TYPE_READONLY (item_type),
8228 TYPE_VOLATILE (item_type),
8229 context_die);
8231 if (sub_die != NULL)
8232 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8234 return mod_type_die;
8237 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8238 an enumerated type. */
8240 static inline int
8241 type_is_enum (tree type)
8243 return TREE_CODE (type) == ENUMERAL_TYPE;
8246 /* Return the DBX register number described by a given RTL node. */
8248 static unsigned int
8249 dbx_reg_number (rtx rtl)
8251 unsigned regno = REGNO (rtl);
8253 if (regno >= FIRST_PSEUDO_REGISTER)
8254 abort ();
8256 return DBX_REGISTER_NUMBER (regno);
8259 /* Return a location descriptor that designates a machine register or
8260 zero if there is none. */
8262 static dw_loc_descr_ref
8263 reg_loc_descriptor (rtx rtl)
8265 unsigned reg;
8266 rtx regs;
8268 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8269 return 0;
8271 reg = dbx_reg_number (rtl);
8272 regs = targetm.dwarf_register_span (rtl);
8274 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1
8275 || regs)
8276 return multiple_reg_loc_descriptor (rtl, regs);
8277 else
8278 return one_reg_loc_descriptor (reg);
8281 /* Return a location descriptor that designates a machine register for
8282 a given hard register number. */
8284 static dw_loc_descr_ref
8285 one_reg_loc_descriptor (unsigned int regno)
8287 if (regno <= 31)
8288 return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8289 else
8290 return new_loc_descr (DW_OP_regx, regno, 0);
8293 /* Given an RTL of a register, return a location descriptor that
8294 designates a value that spans more than one register. */
8296 static dw_loc_descr_ref
8297 multiple_reg_loc_descriptor (rtx rtl, rtx regs)
8299 int nregs, size, i;
8300 unsigned reg;
8301 dw_loc_descr_ref loc_result = NULL;
8303 reg = dbx_reg_number (rtl);
8304 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8306 /* Simple, contiguous registers. */
8307 if (regs == NULL_RTX)
8309 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8311 loc_result = NULL;
8312 while (nregs--)
8314 dw_loc_descr_ref t;
8316 t = one_reg_loc_descriptor (reg);
8317 add_loc_descr (&loc_result, t);
8318 add_loc_descr (&loc_result, new_loc_descr (DW_OP_piece, size, 0));
8319 ++reg;
8321 return loc_result;
8324 /* Now onto stupid register sets in non contiguous locations. */
8326 if (GET_CODE (regs) != PARALLEL)
8327 abort ();
8329 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8330 loc_result = NULL;
8332 for (i = 0; i < XVECLEN (regs, 0); ++i)
8334 dw_loc_descr_ref t;
8336 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8337 add_loc_descr (&loc_result, t);
8338 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8339 add_loc_descr (&loc_result, new_loc_descr (DW_OP_piece, size, 0));
8341 return loc_result;
8344 /* Return a location descriptor that designates a constant. */
8346 static dw_loc_descr_ref
8347 int_loc_descriptor (HOST_WIDE_INT i)
8349 enum dwarf_location_atom op;
8351 /* Pick the smallest representation of a constant, rather than just
8352 defaulting to the LEB encoding. */
8353 if (i >= 0)
8355 if (i <= 31)
8356 op = DW_OP_lit0 + i;
8357 else if (i <= 0xff)
8358 op = DW_OP_const1u;
8359 else if (i <= 0xffff)
8360 op = DW_OP_const2u;
8361 else if (HOST_BITS_PER_WIDE_INT == 32
8362 || i <= 0xffffffff)
8363 op = DW_OP_const4u;
8364 else
8365 op = DW_OP_constu;
8367 else
8369 if (i >= -0x80)
8370 op = DW_OP_const1s;
8371 else if (i >= -0x8000)
8372 op = DW_OP_const2s;
8373 else if (HOST_BITS_PER_WIDE_INT == 32
8374 || i >= -0x80000000)
8375 op = DW_OP_const4s;
8376 else
8377 op = DW_OP_consts;
8380 return new_loc_descr (op, i, 0);
8383 /* Return a location descriptor that designates a base+offset location. */
8385 static dw_loc_descr_ref
8386 based_loc_descr (unsigned int reg, HOST_WIDE_INT offset, bool can_use_fbreg)
8388 dw_loc_descr_ref loc_result;
8389 /* For the "frame base", we use the frame pointer or stack pointer
8390 registers, since the RTL for local variables is relative to one of
8391 them. */
8392 unsigned fp_reg = DBX_REGISTER_NUMBER (frame_pointer_needed
8393 ? HARD_FRAME_POINTER_REGNUM
8394 : STACK_POINTER_REGNUM);
8396 if (reg == fp_reg && can_use_fbreg)
8397 loc_result = new_loc_descr (DW_OP_fbreg, offset, 0);
8398 else if (reg <= 31)
8399 loc_result = new_loc_descr (DW_OP_breg0 + reg, offset, 0);
8400 else
8401 loc_result = new_loc_descr (DW_OP_bregx, reg, offset);
8403 return loc_result;
8406 /* Return true if this RTL expression describes a base+offset calculation. */
8408 static inline int
8409 is_based_loc (rtx rtl)
8411 return (GET_CODE (rtl) == PLUS
8412 && ((GET_CODE (XEXP (rtl, 0)) == REG
8413 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8414 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
8417 /* The following routine converts the RTL for a variable or parameter
8418 (resident in memory) into an equivalent Dwarf representation of a
8419 mechanism for getting the address of that same variable onto the top of a
8420 hypothetical "address evaluation" stack.
8422 When creating memory location descriptors, we are effectively transforming
8423 the RTL for a memory-resident object into its Dwarf postfix expression
8424 equivalent. This routine recursively descends an RTL tree, turning
8425 it into Dwarf postfix code as it goes.
8427 MODE is the mode of the memory reference, needed to handle some
8428 autoincrement addressing modes.
8430 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the location
8431 list for RTL. We can't use it when we are emitting location list for
8432 virtual variable frame_base_decl (i.e. a location list for DW_AT_frame_base)
8433 which describes how frame base changes when !frame_pointer_needed.
8435 Return 0 if we can't represent the location. */
8437 static dw_loc_descr_ref
8438 mem_loc_descriptor (rtx rtl, enum machine_mode mode, bool can_use_fbreg)
8440 dw_loc_descr_ref mem_loc_result = NULL;
8441 enum dwarf_location_atom op;
8443 /* Note that for a dynamically sized array, the location we will generate a
8444 description of here will be the lowest numbered location which is
8445 actually within the array. That's *not* necessarily the same as the
8446 zeroth element of the array. */
8448 rtl = targetm.delegitimize_address (rtl);
8450 switch (GET_CODE (rtl))
8452 case POST_INC:
8453 case POST_DEC:
8454 case POST_MODIFY:
8455 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
8456 just fall into the SUBREG code. */
8458 /* ... fall through ... */
8460 case SUBREG:
8461 /* The case of a subreg may arise when we have a local (register)
8462 variable or a formal (register) parameter which doesn't quite fill
8463 up an entire register. For now, just assume that it is
8464 legitimate to make the Dwarf info refer to the whole register which
8465 contains the given subreg. */
8466 rtl = SUBREG_REG (rtl);
8468 /* ... fall through ... */
8470 case REG:
8471 /* Whenever a register number forms a part of the description of the
8472 method for calculating the (dynamic) address of a memory resident
8473 object, DWARF rules require the register number be referred to as
8474 a "base register". This distinction is not based in any way upon
8475 what category of register the hardware believes the given register
8476 belongs to. This is strictly DWARF terminology we're dealing with
8477 here. Note that in cases where the location of a memory-resident
8478 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8479 OP_CONST (0)) the actual DWARF location descriptor that we generate
8480 may just be OP_BASEREG (basereg). This may look deceptively like
8481 the object in question was allocated to a register (rather than in
8482 memory) so DWARF consumers need to be aware of the subtle
8483 distinction between OP_REG and OP_BASEREG. */
8484 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8485 mem_loc_result = based_loc_descr (dbx_reg_number (rtl), 0,
8486 can_use_fbreg);
8487 break;
8489 case MEM:
8490 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
8491 can_use_fbreg);
8492 if (mem_loc_result != 0)
8493 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
8494 break;
8496 case LO_SUM:
8497 rtl = XEXP (rtl, 1);
8499 /* ... fall through ... */
8501 case LABEL_REF:
8502 /* Some ports can transform a symbol ref into a label ref, because
8503 the symbol ref is too far away and has to be dumped into a constant
8504 pool. */
8505 case CONST:
8506 case SYMBOL_REF:
8507 /* Alternatively, the symbol in the constant pool might be referenced
8508 by a different symbol. */
8509 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
8511 bool marked;
8512 rtx tmp = get_pool_constant_mark (rtl, &marked);
8514 if (GET_CODE (tmp) == SYMBOL_REF)
8516 rtl = tmp;
8517 if (CONSTANT_POOL_ADDRESS_P (tmp))
8518 get_pool_constant_mark (tmp, &marked);
8519 else
8520 marked = true;
8523 /* If all references to this pool constant were optimized away,
8524 it was not output and thus we can't represent it.
8525 FIXME: might try to use DW_OP_const_value here, though
8526 DW_OP_piece complicates it. */
8527 if (!marked)
8528 return 0;
8531 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8532 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
8533 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8534 VARRAY_PUSH_RTX (used_rtx_varray, rtl);
8535 break;
8537 case PRE_MODIFY:
8538 /* Extract the PLUS expression nested inside and fall into
8539 PLUS code below. */
8540 rtl = XEXP (rtl, 1);
8541 goto plus;
8543 case PRE_INC:
8544 case PRE_DEC:
8545 /* Turn these into a PLUS expression and fall into the PLUS code
8546 below. */
8547 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8548 GEN_INT (GET_CODE (rtl) == PRE_INC
8549 ? GET_MODE_UNIT_SIZE (mode)
8550 : -GET_MODE_UNIT_SIZE (mode)));
8552 /* ... fall through ... */
8554 case PLUS:
8555 plus:
8556 if (is_based_loc (rtl))
8557 mem_loc_result = based_loc_descr (dbx_reg_number (XEXP (rtl, 0)),
8558 INTVAL (XEXP (rtl, 1)),
8559 can_use_fbreg);
8560 else
8562 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
8563 can_use_fbreg);
8564 if (mem_loc_result == 0)
8565 break;
8567 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8568 && INTVAL (XEXP (rtl, 1)) >= 0)
8569 add_loc_descr (&mem_loc_result,
8570 new_loc_descr (DW_OP_plus_uconst,
8571 INTVAL (XEXP (rtl, 1)), 0));
8572 else
8574 add_loc_descr (&mem_loc_result,
8575 mem_loc_descriptor (XEXP (rtl, 1), mode,
8576 can_use_fbreg));
8577 add_loc_descr (&mem_loc_result,
8578 new_loc_descr (DW_OP_plus, 0, 0));
8581 break;
8583 /* If a pseudo-reg is optimized away, it is possible for it to
8584 be replaced with a MEM containing a multiply or shift. */
8585 case MULT:
8586 op = DW_OP_mul;
8587 goto do_binop;
8589 case ASHIFT:
8590 op = DW_OP_shl;
8591 goto do_binop;
8593 case ASHIFTRT:
8594 op = DW_OP_shra;
8595 goto do_binop;
8597 case LSHIFTRT:
8598 op = DW_OP_shr;
8599 goto do_binop;
8601 do_binop:
8603 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
8604 can_use_fbreg);
8605 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
8606 can_use_fbreg);
8608 if (op0 == 0 || op1 == 0)
8609 break;
8611 mem_loc_result = op0;
8612 add_loc_descr (&mem_loc_result, op1);
8613 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
8614 break;
8617 case CONST_INT:
8618 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
8619 break;
8621 case ADDRESSOF:
8622 /* If this is a MEM, return its address. Otherwise, we can't
8623 represent this. */
8624 if (GET_CODE (XEXP (rtl, 0)) == MEM)
8625 return mem_loc_descriptor (XEXP (XEXP (rtl, 0), 0), mode,
8626 can_use_fbreg);
8627 else
8628 return 0;
8630 default:
8631 abort ();
8634 return mem_loc_result;
8637 /* Return a descriptor that describes the concatenation of two locations.
8638 This is typically a complex variable. */
8640 static dw_loc_descr_ref
8641 concat_loc_descriptor (rtx x0, rtx x1)
8643 dw_loc_descr_ref cc_loc_result = NULL;
8644 dw_loc_descr_ref x0_ref = loc_descriptor (x0, true);
8645 dw_loc_descr_ref x1_ref = loc_descriptor (x1, true);
8647 if (x0_ref == 0 || x1_ref == 0)
8648 return 0;
8650 cc_loc_result = x0_ref;
8651 add_loc_descr (&cc_loc_result,
8652 new_loc_descr (DW_OP_piece,
8653 GET_MODE_SIZE (GET_MODE (x0)), 0));
8655 add_loc_descr (&cc_loc_result, x1_ref);
8656 add_loc_descr (&cc_loc_result,
8657 new_loc_descr (DW_OP_piece,
8658 GET_MODE_SIZE (GET_MODE (x1)), 0));
8660 return cc_loc_result;
8663 /* Output a proper Dwarf location descriptor for a variable or parameter
8664 which is either allocated in a register or in a memory location. For a
8665 register, we just generate an OP_REG and the register number. For a
8666 memory location we provide a Dwarf postfix expression describing how to
8667 generate the (dynamic) address of the object onto the address stack.
8669 If we don't know how to describe it, return 0. */
8671 static dw_loc_descr_ref
8672 loc_descriptor (rtx rtl, bool can_use_fbreg)
8674 dw_loc_descr_ref loc_result = NULL;
8676 switch (GET_CODE (rtl))
8678 case SUBREG:
8679 /* The case of a subreg may arise when we have a local (register)
8680 variable or a formal (register) parameter which doesn't quite fill
8681 up an entire register. For now, just assume that it is
8682 legitimate to make the Dwarf info refer to the whole register which
8683 contains the given subreg. */
8684 rtl = SUBREG_REG (rtl);
8686 /* ... fall through ... */
8688 case REG:
8689 loc_result = reg_loc_descriptor (rtl);
8690 break;
8692 case MEM:
8693 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
8694 can_use_fbreg);
8695 break;
8697 case CONCAT:
8698 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
8699 break;
8701 case VAR_LOCATION:
8702 /* Single part. */
8703 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
8705 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), can_use_fbreg);
8707 /* Multiple parts. */
8708 else
8710 rtvec par_elems = XVEC (XEXP (rtl, 1), 0);
8711 int num_elem = GET_NUM_ELEM (par_elems);
8712 enum machine_mode mode;
8713 int i;
8715 /* Create the first one, so we have something to add to. */
8716 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
8717 can_use_fbreg);
8718 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
8719 add_loc_descr (&loc_result,
8720 new_loc_descr (DW_OP_piece, GET_MODE_SIZE (mode), 0));
8721 for (i = 1; i < num_elem; i++)
8723 dw_loc_descr_ref temp;
8725 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
8726 can_use_fbreg);
8727 add_loc_descr (&loc_result, temp);
8728 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
8729 add_loc_descr (&loc_result,
8730 new_loc_descr (DW_OP_piece,
8731 GET_MODE_SIZE (mode), 0));
8734 break;
8736 default:
8737 abort ();
8740 return loc_result;
8743 /* Similar, but generate the descriptor from trees instead of rtl. This comes
8744 up particularly with variable length arrays. If ADDRESSP is nonzero, we are
8745 looking for an address. Otherwise, we return a value. If we can't make a
8746 descriptor, return 0. */
8748 static dw_loc_descr_ref
8749 loc_descriptor_from_tree (tree loc, int addressp)
8751 dw_loc_descr_ref ret, ret1;
8752 int indirect_p = 0;
8753 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
8754 enum dwarf_location_atom op;
8756 /* ??? Most of the time we do not take proper care for sign/zero
8757 extending the values properly. Hopefully this won't be a real
8758 problem... */
8760 switch (TREE_CODE (loc))
8762 case ERROR_MARK:
8763 return 0;
8765 case PLACEHOLDER_EXPR:
8766 /* This case involves extracting fields from an object to determine the
8767 position of other fields. We don't try to encode this here. The
8768 only user of this is Ada, which encodes the needed information using
8769 the names of types. */
8770 return 0;
8772 case CALL_EXPR:
8773 return 0;
8775 case PREINCREMENT_EXPR:
8776 case PREDECREMENT_EXPR:
8777 case POSTINCREMENT_EXPR:
8778 case POSTDECREMENT_EXPR:
8779 /* There are no opcodes for these operations. */
8780 return 0;
8782 case ADDR_EXPR:
8783 /* We can support this only if we can look through conversions and
8784 find an INDIRECT_EXPR. */
8785 for (loc = TREE_OPERAND (loc, 0);
8786 TREE_CODE (loc) == CONVERT_EXPR || TREE_CODE (loc) == NOP_EXPR
8787 || TREE_CODE (loc) == NON_LVALUE_EXPR
8788 || TREE_CODE (loc) == VIEW_CONVERT_EXPR
8789 || TREE_CODE (loc) == SAVE_EXPR;
8790 loc = TREE_OPERAND (loc, 0))
8793 return (TREE_CODE (loc) == INDIRECT_REF
8794 ? loc_descriptor_from_tree (TREE_OPERAND (loc, 0), addressp)
8795 : 0);
8797 case VAR_DECL:
8798 if (DECL_THREAD_LOCAL (loc))
8800 rtx rtl;
8802 #ifndef ASM_OUTPUT_DWARF_DTPREL
8803 /* If this is not defined, we have no way to emit the data. */
8804 return 0;
8805 #endif
8807 /* The way DW_OP_GNU_push_tls_address is specified, we can only
8808 look up addresses of objects in the current module. */
8809 if (DECL_EXTERNAL (loc))
8810 return 0;
8812 rtl = rtl_for_decl_location (loc);
8813 if (rtl == NULL_RTX)
8814 return 0;
8816 if (GET_CODE (rtl) != MEM)
8817 return 0;
8818 rtl = XEXP (rtl, 0);
8819 if (! CONSTANT_P (rtl))
8820 return 0;
8822 ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
8823 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8824 ret->dw_loc_oprnd1.v.val_addr = rtl;
8826 ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
8827 add_loc_descr (&ret, ret1);
8829 indirect_p = 1;
8830 break;
8832 /* Fall through. */
8834 case PARM_DECL:
8835 case RESULT_DECL:
8837 rtx rtl = rtl_for_decl_location (loc);
8839 if (rtl == NULL_RTX)
8840 return 0;
8841 else if (CONSTANT_P (rtl))
8843 ret = new_loc_descr (DW_OP_addr, 0, 0);
8844 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8845 ret->dw_loc_oprnd1.v.val_addr = rtl;
8846 indirect_p = 1;
8848 else
8850 enum machine_mode mode = GET_MODE (rtl);
8852 if (GET_CODE (rtl) == MEM)
8854 indirect_p = 1;
8855 rtl = XEXP (rtl, 0);
8858 ret = mem_loc_descriptor (rtl, mode, true);
8861 break;
8863 case INDIRECT_REF:
8864 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8865 indirect_p = 1;
8866 break;
8868 case COMPOUND_EXPR:
8869 return loc_descriptor_from_tree (TREE_OPERAND (loc, 1), addressp);
8871 case NOP_EXPR:
8872 case CONVERT_EXPR:
8873 case NON_LVALUE_EXPR:
8874 case VIEW_CONVERT_EXPR:
8875 case SAVE_EXPR:
8876 case MODIFY_EXPR:
8877 return loc_descriptor_from_tree (TREE_OPERAND (loc, 0), addressp);
8879 case COMPONENT_REF:
8880 case BIT_FIELD_REF:
8881 case ARRAY_REF:
8882 case ARRAY_RANGE_REF:
8884 tree obj, offset;
8885 HOST_WIDE_INT bitsize, bitpos, bytepos;
8886 enum machine_mode mode;
8887 int volatilep;
8889 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
8890 &unsignedp, &volatilep);
8892 if (obj == loc)
8893 return 0;
8895 ret = loc_descriptor_from_tree (obj, 1);
8896 if (ret == 0
8897 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
8898 return 0;
8900 if (offset != NULL_TREE)
8902 /* Variable offset. */
8903 add_loc_descr (&ret, loc_descriptor_from_tree (offset, 0));
8904 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
8907 if (!addressp)
8908 indirect_p = 1;
8910 bytepos = bitpos / BITS_PER_UNIT;
8911 if (bytepos > 0)
8912 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
8913 else if (bytepos < 0)
8915 add_loc_descr (&ret, int_loc_descriptor (bytepos));
8916 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
8918 break;
8921 case INTEGER_CST:
8922 if (host_integerp (loc, 0))
8923 ret = int_loc_descriptor (tree_low_cst (loc, 0));
8924 else
8925 return 0;
8926 break;
8928 case CONSTRUCTOR:
8930 /* Get an RTL for this, if something has been emitted. */
8931 rtx rtl = lookup_constant_def (loc);
8932 enum machine_mode mode;
8934 if (GET_CODE (rtl) != MEM)
8935 return 0;
8936 mode = GET_MODE (rtl);
8937 rtl = XEXP (rtl, 0);
8939 rtl = targetm.delegitimize_address (rtl);
8941 indirect_p = 1;
8942 ret = mem_loc_descriptor (rtl, mode, true);
8943 break;
8946 case TRUTH_AND_EXPR:
8947 case TRUTH_ANDIF_EXPR:
8948 case BIT_AND_EXPR:
8949 op = DW_OP_and;
8950 goto do_binop;
8952 case TRUTH_XOR_EXPR:
8953 case BIT_XOR_EXPR:
8954 op = DW_OP_xor;
8955 goto do_binop;
8957 case TRUTH_OR_EXPR:
8958 case TRUTH_ORIF_EXPR:
8959 case BIT_IOR_EXPR:
8960 op = DW_OP_or;
8961 goto do_binop;
8963 case FLOOR_DIV_EXPR:
8964 case CEIL_DIV_EXPR:
8965 case ROUND_DIV_EXPR:
8966 case TRUNC_DIV_EXPR:
8967 op = DW_OP_div;
8968 goto do_binop;
8970 case MINUS_EXPR:
8971 op = DW_OP_minus;
8972 goto do_binop;
8974 case FLOOR_MOD_EXPR:
8975 case CEIL_MOD_EXPR:
8976 case ROUND_MOD_EXPR:
8977 case TRUNC_MOD_EXPR:
8978 op = DW_OP_mod;
8979 goto do_binop;
8981 case MULT_EXPR:
8982 op = DW_OP_mul;
8983 goto do_binop;
8985 case LSHIFT_EXPR:
8986 op = DW_OP_shl;
8987 goto do_binop;
8989 case RSHIFT_EXPR:
8990 op = (unsignedp ? DW_OP_shr : DW_OP_shra);
8991 goto do_binop;
8993 case PLUS_EXPR:
8994 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
8995 && host_integerp (TREE_OPERAND (loc, 1), 0))
8997 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8998 if (ret == 0)
8999 return 0;
9001 add_loc_descr (&ret,
9002 new_loc_descr (DW_OP_plus_uconst,
9003 tree_low_cst (TREE_OPERAND (loc, 1),
9005 0));
9006 break;
9009 op = DW_OP_plus;
9010 goto do_binop;
9012 case LE_EXPR:
9013 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9014 return 0;
9016 op = DW_OP_le;
9017 goto do_binop;
9019 case GE_EXPR:
9020 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9021 return 0;
9023 op = DW_OP_ge;
9024 goto do_binop;
9026 case LT_EXPR:
9027 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9028 return 0;
9030 op = DW_OP_lt;
9031 goto do_binop;
9033 case GT_EXPR:
9034 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9035 return 0;
9037 op = DW_OP_gt;
9038 goto do_binop;
9040 case EQ_EXPR:
9041 op = DW_OP_eq;
9042 goto do_binop;
9044 case NE_EXPR:
9045 op = DW_OP_ne;
9046 goto do_binop;
9048 do_binop:
9049 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
9050 ret1 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
9051 if (ret == 0 || ret1 == 0)
9052 return 0;
9054 add_loc_descr (&ret, ret1);
9055 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9056 break;
9058 case TRUTH_NOT_EXPR:
9059 case BIT_NOT_EXPR:
9060 op = DW_OP_not;
9061 goto do_unop;
9063 case ABS_EXPR:
9064 op = DW_OP_abs;
9065 goto do_unop;
9067 case NEGATE_EXPR:
9068 op = DW_OP_neg;
9069 goto do_unop;
9071 do_unop:
9072 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
9073 if (ret == 0)
9074 return 0;
9076 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9077 break;
9079 case MIN_EXPR:
9080 case MAX_EXPR:
9082 const enum tree_code code =
9083 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9085 loc = build (COND_EXPR, TREE_TYPE (loc),
9086 build (code, integer_type_node,
9087 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9088 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9091 /* ... fall through ... */
9093 case COND_EXPR:
9095 dw_loc_descr_ref lhs
9096 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
9097 dw_loc_descr_ref rhs
9098 = loc_descriptor_from_tree (TREE_OPERAND (loc, 2), 0);
9099 dw_loc_descr_ref bra_node, jump_node, tmp;
9101 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
9102 if (ret == 0 || lhs == 0 || rhs == 0)
9103 return 0;
9105 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9106 add_loc_descr (&ret, bra_node);
9108 add_loc_descr (&ret, rhs);
9109 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9110 add_loc_descr (&ret, jump_node);
9112 add_loc_descr (&ret, lhs);
9113 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9114 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9116 /* ??? Need a node to point the skip at. Use a nop. */
9117 tmp = new_loc_descr (DW_OP_nop, 0, 0);
9118 add_loc_descr (&ret, tmp);
9119 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9120 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9122 break;
9124 default:
9125 /* Leave front-end specific codes as simply unknown. This comes
9126 up, for instance, with the C STMT_EXPR. */
9127 if ((unsigned int) TREE_CODE (loc)
9128 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9129 return 0;
9131 /* Otherwise this is a generic code; we should just lists all of
9132 these explicitly. Aborting means we forgot one. */
9133 abort ();
9136 /* Show if we can't fill the request for an address. */
9137 if (addressp && indirect_p == 0)
9138 return 0;
9140 /* If we've got an address and don't want one, dereference. */
9141 if (!addressp && indirect_p > 0)
9143 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9145 if (size > DWARF2_ADDR_SIZE || size == -1)
9146 return 0;
9147 else if (size == DWARF2_ADDR_SIZE)
9148 op = DW_OP_deref;
9149 else
9150 op = DW_OP_deref_size;
9152 add_loc_descr (&ret, new_loc_descr (op, size, 0));
9155 return ret;
9158 /* Given a value, round it up to the lowest multiple of `boundary'
9159 which is not less than the value itself. */
9161 static inline HOST_WIDE_INT
9162 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9164 return (((value + boundary - 1) / boundary) * boundary);
9167 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9168 pointer to the declared type for the relevant field variable, or return
9169 `integer_type_node' if the given node turns out to be an
9170 ERROR_MARK node. */
9172 static inline tree
9173 field_type (tree decl)
9175 tree type;
9177 if (TREE_CODE (decl) == ERROR_MARK)
9178 return integer_type_node;
9180 type = DECL_BIT_FIELD_TYPE (decl);
9181 if (type == NULL_TREE)
9182 type = TREE_TYPE (decl);
9184 return type;
9187 /* Given a pointer to a tree node, return the alignment in bits for
9188 it, or else return BITS_PER_WORD if the node actually turns out to
9189 be an ERROR_MARK node. */
9191 static inline unsigned
9192 simple_type_align_in_bits (tree type)
9194 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9197 static inline unsigned
9198 simple_decl_align_in_bits (tree decl)
9200 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9203 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9204 lowest addressed byte of the "containing object" for the given FIELD_DECL,
9205 or return 0 if we are unable to determine what that offset is, either
9206 because the argument turns out to be a pointer to an ERROR_MARK node, or
9207 because the offset is actually variable. (We can't handle the latter case
9208 just yet). */
9210 static HOST_WIDE_INT
9211 field_byte_offset (tree decl)
9213 unsigned int type_align_in_bits;
9214 unsigned int decl_align_in_bits;
9215 unsigned HOST_WIDE_INT type_size_in_bits;
9216 HOST_WIDE_INT object_offset_in_bits;
9217 tree type;
9218 tree field_size_tree;
9219 HOST_WIDE_INT bitpos_int;
9220 HOST_WIDE_INT deepest_bitpos;
9221 unsigned HOST_WIDE_INT field_size_in_bits;
9223 if (TREE_CODE (decl) == ERROR_MARK)
9224 return 0;
9225 else if (TREE_CODE (decl) != FIELD_DECL)
9226 abort ();
9228 type = field_type (decl);
9229 field_size_tree = DECL_SIZE (decl);
9231 /* The size could be unspecified if there was an error, or for
9232 a flexible array member. */
9233 if (! field_size_tree)
9234 field_size_tree = bitsize_zero_node;
9236 /* We cannot yet cope with fields whose positions are variable, so
9237 for now, when we see such things, we simply return 0. Someday, we may
9238 be able to handle such cases, but it will be damn difficult. */
9239 if (! host_integerp (bit_position (decl), 0))
9240 return 0;
9242 bitpos_int = int_bit_position (decl);
9244 /* If we don't know the size of the field, pretend it's a full word. */
9245 if (host_integerp (field_size_tree, 1))
9246 field_size_in_bits = tree_low_cst (field_size_tree, 1);
9247 else
9248 field_size_in_bits = BITS_PER_WORD;
9250 type_size_in_bits = simple_type_size_in_bits (type);
9251 type_align_in_bits = simple_type_align_in_bits (type);
9252 decl_align_in_bits = simple_decl_align_in_bits (decl);
9254 /* The GCC front-end doesn't make any attempt to keep track of the starting
9255 bit offset (relative to the start of the containing structure type) of the
9256 hypothetical "containing object" for a bit-field. Thus, when computing
9257 the byte offset value for the start of the "containing object" of a
9258 bit-field, we must deduce this information on our own. This can be rather
9259 tricky to do in some cases. For example, handling the following structure
9260 type definition when compiling for an i386/i486 target (which only aligns
9261 long long's to 32-bit boundaries) can be very tricky:
9263 struct S { int field1; long long field2:31; };
9265 Fortunately, there is a simple rule-of-thumb which can be used in such
9266 cases. When compiling for an i386/i486, GCC will allocate 8 bytes for the
9267 structure shown above. It decides to do this based upon one simple rule
9268 for bit-field allocation. GCC allocates each "containing object" for each
9269 bit-field at the first (i.e. lowest addressed) legitimate alignment
9270 boundary (based upon the required minimum alignment for the declared type
9271 of the field) which it can possibly use, subject to the condition that
9272 there is still enough available space remaining in the containing object
9273 (when allocated at the selected point) to fully accommodate all of the
9274 bits of the bit-field itself.
9276 This simple rule makes it obvious why GCC allocates 8 bytes for each
9277 object of the structure type shown above. When looking for a place to
9278 allocate the "containing object" for `field2', the compiler simply tries
9279 to allocate a 64-bit "containing object" at each successive 32-bit
9280 boundary (starting at zero) until it finds a place to allocate that 64-
9281 bit field such that at least 31 contiguous (and previously unallocated)
9282 bits remain within that selected 64 bit field. (As it turns out, for the
9283 example above, the compiler finds it is OK to allocate the "containing
9284 object" 64-bit field at bit-offset zero within the structure type.)
9286 Here we attempt to work backwards from the limited set of facts we're
9287 given, and we try to deduce from those facts, where GCC must have believed
9288 that the containing object started (within the structure type). The value
9289 we deduce is then used (by the callers of this routine) to generate
9290 DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
9291 and, in the case of DW_AT_location, regular fields as well). */
9293 /* Figure out the bit-distance from the start of the structure to the
9294 "deepest" bit of the bit-field. */
9295 deepest_bitpos = bitpos_int + field_size_in_bits;
9297 /* This is the tricky part. Use some fancy footwork to deduce where the
9298 lowest addressed bit of the containing object must be. */
9299 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9301 /* Round up to type_align by default. This works best for bitfields. */
9302 object_offset_in_bits += type_align_in_bits - 1;
9303 object_offset_in_bits /= type_align_in_bits;
9304 object_offset_in_bits *= type_align_in_bits;
9306 if (object_offset_in_bits > bitpos_int)
9308 /* Sigh, the decl must be packed. */
9309 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9311 /* Round up to decl_align instead. */
9312 object_offset_in_bits += decl_align_in_bits - 1;
9313 object_offset_in_bits /= decl_align_in_bits;
9314 object_offset_in_bits *= decl_align_in_bits;
9317 return object_offset_in_bits / BITS_PER_UNIT;
9320 /* The following routines define various Dwarf attributes and any data
9321 associated with them. */
9323 /* Add a location description attribute value to a DIE.
9325 This emits location attributes suitable for whole variables and
9326 whole parameters. Note that the location attributes for struct fields are
9327 generated by the routine `data_member_location_attribute' below. */
9329 static inline void
9330 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9331 dw_loc_descr_ref descr)
9333 if (descr != 0)
9334 add_AT_loc (die, attr_kind, descr);
9337 /* Attach the specialized form of location attribute used for data members of
9338 struct and union types. In the special case of a FIELD_DECL node which
9339 represents a bit-field, the "offset" part of this special location
9340 descriptor must indicate the distance in bytes from the lowest-addressed
9341 byte of the containing struct or union type to the lowest-addressed byte of
9342 the "containing object" for the bit-field. (See the `field_byte_offset'
9343 function above).
9345 For any given bit-field, the "containing object" is a hypothetical object
9346 (of some integral or enum type) within which the given bit-field lives. The
9347 type of this hypothetical "containing object" is always the same as the
9348 declared type of the individual bit-field itself (for GCC anyway... the
9349 DWARF spec doesn't actually mandate this). Note that it is the size (in
9350 bytes) of the hypothetical "containing object" which will be given in the
9351 DW_AT_byte_size attribute for this bit-field. (See the
9352 `byte_size_attribute' function below.) It is also used when calculating the
9353 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
9354 function below.) */
9356 static void
9357 add_data_member_location_attribute (dw_die_ref die, tree decl)
9359 HOST_WIDE_INT offset;
9360 dw_loc_descr_ref loc_descr = 0;
9362 if (TREE_CODE (decl) == TREE_VEC)
9364 /* We're working on the TAG_inheritance for a base class. */
9365 if (TREE_VIA_VIRTUAL (decl) && is_cxx ())
9367 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9368 aren't at a fixed offset from all (sub)objects of the same
9369 type. We need to extract the appropriate offset from our
9370 vtable. The following dwarf expression means
9372 BaseAddr = ObAddr + *((*ObAddr) - Offset)
9374 This is specific to the V3 ABI, of course. */
9376 dw_loc_descr_ref tmp;
9378 /* Make a copy of the object address. */
9379 tmp = new_loc_descr (DW_OP_dup, 0, 0);
9380 add_loc_descr (&loc_descr, tmp);
9382 /* Extract the vtable address. */
9383 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9384 add_loc_descr (&loc_descr, tmp);
9386 /* Calculate the address of the offset. */
9387 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9388 if (offset >= 0)
9389 abort ();
9391 tmp = int_loc_descriptor (-offset);
9392 add_loc_descr (&loc_descr, tmp);
9393 tmp = new_loc_descr (DW_OP_minus, 0, 0);
9394 add_loc_descr (&loc_descr, tmp);
9396 /* Extract the offset. */
9397 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9398 add_loc_descr (&loc_descr, tmp);
9400 /* Add it to the object address. */
9401 tmp = new_loc_descr (DW_OP_plus, 0, 0);
9402 add_loc_descr (&loc_descr, tmp);
9404 else
9405 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9407 else
9408 offset = field_byte_offset (decl);
9410 if (! loc_descr)
9412 enum dwarf_location_atom op;
9414 /* The DWARF2 standard says that we should assume that the structure
9415 address is already on the stack, so we can specify a structure field
9416 address by using DW_OP_plus_uconst. */
9418 #ifdef MIPS_DEBUGGING_INFO
9419 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9420 operator correctly. It works only if we leave the offset on the
9421 stack. */
9422 op = DW_OP_constu;
9423 #else
9424 op = DW_OP_plus_uconst;
9425 #endif
9427 loc_descr = new_loc_descr (op, offset, 0);
9430 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9433 /* Writes integer values to dw_vec_const array. */
9435 static void
9436 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9438 while (size != 0)
9440 *dest++ = val & 0xff;
9441 val >>= 8;
9442 --size;
9446 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
9448 static HOST_WIDE_INT
9449 extract_int (const unsigned char *src, unsigned int size)
9451 HOST_WIDE_INT val = 0;
9453 src += size;
9454 while (size != 0)
9456 val <<= 8;
9457 val |= *--src & 0xff;
9458 --size;
9460 return val;
9463 /* Writes floating point values to dw_vec_const array. */
9465 static void
9466 insert_float (rtx rtl, unsigned char *array)
9468 REAL_VALUE_TYPE rv;
9469 long val[4];
9470 int i;
9472 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9473 real_to_target (val, &rv, GET_MODE (rtl));
9475 /* real_to_target puts 32-bit pieces in each long. Pack them. */
9476 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9478 insert_int (val[i], 4, array);
9479 array += 4;
9483 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
9484 does not have a "location" either in memory or in a register. These
9485 things can arise in GNU C when a constant is passed as an actual parameter
9486 to an inlined function. They can also arise in C++ where declared
9487 constants do not necessarily get memory "homes". */
9489 static void
9490 add_const_value_attribute (dw_die_ref die, rtx rtl)
9492 switch (GET_CODE (rtl))
9494 case CONST_INT:
9496 HOST_WIDE_INT val = INTVAL (rtl);
9498 if (val < 0)
9499 add_AT_int (die, DW_AT_const_value, val);
9500 else
9501 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
9503 break;
9505 case CONST_DOUBLE:
9506 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9507 floating-point constant. A CONST_DOUBLE is used whenever the
9508 constant requires more than one word in order to be adequately
9509 represented. We output CONST_DOUBLEs as blocks. */
9511 enum machine_mode mode = GET_MODE (rtl);
9513 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
9515 unsigned int length = GET_MODE_SIZE (mode);
9516 unsigned char *array = ggc_alloc (length);
9518 insert_float (rtl, array);
9519 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
9521 else
9523 /* ??? We really should be using HOST_WIDE_INT throughout. */
9524 if (HOST_BITS_PER_LONG != HOST_BITS_PER_WIDE_INT)
9525 abort ();
9527 add_AT_long_long (die, DW_AT_const_value,
9528 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
9531 break;
9533 case CONST_VECTOR:
9535 enum machine_mode mode = GET_MODE (rtl);
9536 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9537 unsigned int length = CONST_VECTOR_NUNITS (rtl);
9538 unsigned char *array = ggc_alloc (length * elt_size);
9539 unsigned int i;
9540 unsigned char *p;
9542 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
9544 for (i = 0, p = array; i < length; i++, p += elt_size)
9546 rtx elt = CONST_VECTOR_ELT (rtl, i);
9547 HOST_WIDE_INT lo, hi;
9548 if (GET_CODE (elt) == CONST_INT)
9550 lo = INTVAL (elt);
9551 hi = -(lo < 0);
9553 else if (GET_CODE (elt) == CONST_DOUBLE)
9555 lo = CONST_DOUBLE_LOW (elt);
9556 hi = CONST_DOUBLE_HIGH (elt);
9558 else
9559 abort ();
9561 if (elt_size <= sizeof (HOST_WIDE_INT))
9562 insert_int (lo, elt_size, p);
9563 else if (elt_size == 2 * sizeof (HOST_WIDE_INT))
9565 unsigned char *p0 = p;
9566 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9568 if (WORDS_BIG_ENDIAN)
9570 p0 = p1;
9571 p1 = p;
9573 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9574 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9576 else
9577 abort ();
9580 else if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
9582 for (i = 0, p = array; i < length; i++, p += elt_size)
9584 rtx elt = CONST_VECTOR_ELT (rtl, i);
9585 insert_float (elt, p);
9588 else
9589 abort ();
9591 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
9593 break;
9595 case CONST_STRING:
9596 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
9597 break;
9599 case SYMBOL_REF:
9600 case LABEL_REF:
9601 case CONST:
9602 add_AT_addr (die, DW_AT_const_value, rtl);
9603 VARRAY_PUSH_RTX (used_rtx_varray, rtl);
9604 break;
9606 case PLUS:
9607 /* In cases where an inlined instance of an inline function is passed
9608 the address of an `auto' variable (which is local to the caller) we
9609 can get a situation where the DECL_RTL of the artificial local
9610 variable (for the inlining) which acts as a stand-in for the
9611 corresponding formal parameter (of the inline function) will look
9612 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
9613 exactly a compile-time constant expression, but it isn't the address
9614 of the (artificial) local variable either. Rather, it represents the
9615 *value* which the artificial local variable always has during its
9616 lifetime. We currently have no way to represent such quasi-constant
9617 values in Dwarf, so for now we just punt and generate nothing. */
9618 break;
9620 default:
9621 /* No other kinds of rtx should be possible here. */
9622 abort ();
9627 static rtx
9628 rtl_for_decl_location (tree decl)
9630 rtx rtl;
9632 /* Here we have to decide where we are going to say the parameter "lives"
9633 (as far as the debugger is concerned). We only have a couple of
9634 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
9636 DECL_RTL normally indicates where the parameter lives during most of the
9637 activation of the function. If optimization is enabled however, this
9638 could be either NULL or else a pseudo-reg. Both of those cases indicate
9639 that the parameter doesn't really live anywhere (as far as the code
9640 generation parts of GCC are concerned) during most of the function's
9641 activation. That will happen (for example) if the parameter is never
9642 referenced within the function.
9644 We could just generate a location descriptor here for all non-NULL
9645 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
9646 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
9647 where DECL_RTL is NULL or is a pseudo-reg.
9649 Note however that we can only get away with using DECL_INCOMING_RTL as
9650 a backup substitute for DECL_RTL in certain limited cases. In cases
9651 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
9652 we can be sure that the parameter was passed using the same type as it is
9653 declared to have within the function, and that its DECL_INCOMING_RTL
9654 points us to a place where a value of that type is passed.
9656 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
9657 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
9658 because in these cases DECL_INCOMING_RTL points us to a value of some
9659 type which is *different* from the type of the parameter itself. Thus,
9660 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
9661 such cases, the debugger would end up (for example) trying to fetch a
9662 `float' from a place which actually contains the first part of a
9663 `double'. That would lead to really incorrect and confusing
9664 output at debug-time.
9666 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
9667 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
9668 are a couple of exceptions however. On little-endian machines we can
9669 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
9670 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
9671 an integral type that is smaller than TREE_TYPE (decl). These cases arise
9672 when (on a little-endian machine) a non-prototyped function has a
9673 parameter declared to be of type `short' or `char'. In such cases,
9674 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
9675 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
9676 passed `int' value. If the debugger then uses that address to fetch
9677 a `short' or a `char' (on a little-endian machine) the result will be
9678 the correct data, so we allow for such exceptional cases below.
9680 Note that our goal here is to describe the place where the given formal
9681 parameter lives during most of the function's activation (i.e. between the
9682 end of the prologue and the start of the epilogue). We'll do that as best
9683 as we can. Note however that if the given formal parameter is modified
9684 sometime during the execution of the function, then a stack backtrace (at
9685 debug-time) will show the function as having been called with the *new*
9686 value rather than the value which was originally passed in. This happens
9687 rarely enough that it is not a major problem, but it *is* a problem, and
9688 I'd like to fix it.
9690 A future version of dwarf2out.c may generate two additional attributes for
9691 any given DW_TAG_formal_parameter DIE which will describe the "passed
9692 type" and the "passed location" for the given formal parameter in addition
9693 to the attributes we now generate to indicate the "declared type" and the
9694 "active location" for each parameter. This additional set of attributes
9695 could be used by debuggers for stack backtraces. Separately, note that
9696 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
9697 This happens (for example) for inlined-instances of inline function formal
9698 parameters which are never referenced. This really shouldn't be
9699 happening. All PARM_DECL nodes should get valid non-NULL
9700 DECL_INCOMING_RTL values. FIXME. */
9702 /* Use DECL_RTL as the "location" unless we find something better. */
9703 rtl = DECL_RTL_IF_SET (decl);
9705 /* When generating abstract instances, ignore everything except
9706 constants, symbols living in memory, and symbols living in
9707 fixed registers. */
9708 if (! reload_completed)
9710 if (rtl
9711 && (CONSTANT_P (rtl)
9712 || (GET_CODE (rtl) == MEM
9713 && CONSTANT_P (XEXP (rtl, 0)))
9714 || (GET_CODE (rtl) == REG
9715 && TREE_CODE (decl) == VAR_DECL
9716 && TREE_STATIC (decl))))
9718 rtl = targetm.delegitimize_address (rtl);
9719 return rtl;
9721 rtl = NULL_RTX;
9723 else if (TREE_CODE (decl) == PARM_DECL)
9725 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
9727 tree declared_type = type_main_variant (TREE_TYPE (decl));
9728 tree passed_type = type_main_variant (DECL_ARG_TYPE (decl));
9730 /* This decl represents a formal parameter which was optimized out.
9731 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
9732 all cases where (rtl == NULL_RTX) just below. */
9733 if (declared_type == passed_type)
9734 rtl = DECL_INCOMING_RTL (decl);
9735 else if (! BYTES_BIG_ENDIAN
9736 && TREE_CODE (declared_type) == INTEGER_TYPE
9737 && (GET_MODE_SIZE (TYPE_MODE (declared_type))
9738 <= GET_MODE_SIZE (TYPE_MODE (passed_type))))
9739 rtl = DECL_INCOMING_RTL (decl);
9742 /* If the parm was passed in registers, but lives on the stack, then
9743 make a big endian correction if the mode of the type of the
9744 parameter is not the same as the mode of the rtl. */
9745 /* ??? This is the same series of checks that are made in dbxout.c before
9746 we reach the big endian correction code there. It isn't clear if all
9747 of these checks are necessary here, but keeping them all is the safe
9748 thing to do. */
9749 else if (GET_CODE (rtl) == MEM
9750 && XEXP (rtl, 0) != const0_rtx
9751 && ! CONSTANT_P (XEXP (rtl, 0))
9752 /* Not passed in memory. */
9753 && GET_CODE (DECL_INCOMING_RTL (decl)) != MEM
9754 /* Not passed by invisible reference. */
9755 && (GET_CODE (XEXP (rtl, 0)) != REG
9756 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
9757 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
9758 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
9759 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
9760 #endif
9762 /* Big endian correction check. */
9763 && BYTES_BIG_ENDIAN
9764 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
9765 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
9766 < UNITS_PER_WORD))
9768 int offset = (UNITS_PER_WORD
9769 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
9771 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
9772 plus_constant (XEXP (rtl, 0), offset));
9775 else if (TREE_CODE (decl) == VAR_DECL
9776 && rtl
9777 && GET_CODE (rtl) == MEM
9778 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
9779 && BYTES_BIG_ENDIAN)
9781 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
9782 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
9784 /* If a variable is declared "register" yet is smaller than
9785 a register, then if we store the variable to memory, it
9786 looks like we're storing a register-sized value, when in
9787 fact we are not. We need to adjust the offset of the
9788 storage location to reflect the actual value's bytes,
9789 else gdb will not be able to display it. */
9790 if (rsize > dsize)
9791 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
9792 plus_constant (XEXP (rtl, 0), rsize-dsize));
9795 if (rtl != NULL_RTX)
9797 rtl = eliminate_regs (rtl, 0, NULL_RTX);
9798 #ifdef LEAF_REG_REMAP
9799 if (current_function_uses_only_leaf_regs)
9800 leaf_renumber_regs_insn (rtl);
9801 #endif
9804 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
9805 and will have been substituted directly into all expressions that use it.
9806 C does not have such a concept, but C++ and other languages do. */
9807 else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
9809 /* If a variable is initialized with a string constant without embedded
9810 zeros, build CONST_STRING. */
9811 if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
9812 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9814 tree arrtype = TREE_TYPE (decl);
9815 tree enttype = TREE_TYPE (arrtype);
9816 tree domain = TYPE_DOMAIN (arrtype);
9817 tree init = DECL_INITIAL (decl);
9818 enum machine_mode mode = TYPE_MODE (enttype);
9820 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
9821 && domain
9822 && integer_zerop (TYPE_MIN_VALUE (domain))
9823 && compare_tree_int (TYPE_MAX_VALUE (domain),
9824 TREE_STRING_LENGTH (init) - 1) == 0
9825 && ((size_t) TREE_STRING_LENGTH (init)
9826 == strlen (TREE_STRING_POINTER (init)) + 1))
9827 rtl = gen_rtx_CONST_STRING (VOIDmode, TREE_STRING_POINTER (init));
9829 /* If the initializer is something that we know will expand into an
9830 immediate RTL constant, expand it now. Expanding anything else
9831 tends to produce unresolved symbols; see debug/5770 and c++/6381. */
9832 else if (TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST
9833 || TREE_CODE (DECL_INITIAL (decl)) == REAL_CST)
9835 rtl = expand_expr (DECL_INITIAL (decl), NULL_RTX, VOIDmode,
9836 EXPAND_INITIALIZER);
9837 /* If expand_expr returns a MEM, it wasn't immediate. */
9838 if (rtl && GET_CODE (rtl) == MEM)
9839 abort ();
9843 if (rtl)
9844 rtl = targetm.delegitimize_address (rtl);
9846 /* If we don't look past the constant pool, we risk emitting a
9847 reference to a constant pool entry that isn't referenced from
9848 code, and thus is not emitted. */
9849 if (rtl)
9850 rtl = avoid_constant_pool_reference (rtl);
9852 return rtl;
9855 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
9856 data attribute for a variable or a parameter. We generate the
9857 DW_AT_const_value attribute only in those cases where the given variable
9858 or parameter does not have a true "location" either in memory or in a
9859 register. This can happen (for example) when a constant is passed as an
9860 actual argument in a call to an inline function. (It's possible that
9861 these things can crop up in other ways also.) Note that one type of
9862 constant value which can be passed into an inlined function is a constant
9863 pointer. This can happen for example if an actual argument in an inlined
9864 function call evaluates to a compile-time constant address. */
9866 static void
9867 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
9868 enum dwarf_attribute attr)
9870 rtx rtl;
9871 dw_loc_descr_ref descr;
9872 var_loc_list *loc_list;
9874 if (TREE_CODE (decl) == ERROR_MARK)
9875 return;
9876 else if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != PARM_DECL
9877 && TREE_CODE (decl) != RESULT_DECL)
9878 abort ();
9880 /* See if we possibly have multiple locations for this variable. */
9881 loc_list = lookup_decl_loc (decl);
9883 /* If it truly has multiple locations, the first and last node will
9884 differ. */
9885 if (loc_list && loc_list->first != loc_list->last)
9887 const char *secname;
9888 const char *endname;
9889 dw_loc_list_ref list;
9890 rtx varloc;
9891 struct var_loc_node *node;
9893 /* We need to figure out what section we should use as the base
9894 for the address ranges where a given location is valid.
9895 1. If this particular DECL has a section associated with it,
9896 use that.
9897 2. If this function has a section associated with it, use
9898 that.
9899 3. Otherwise, use the text section.
9900 XXX: If you split a variable across multiple sections, this
9901 won't notice. */
9903 if (DECL_SECTION_NAME (decl))
9905 tree sectree = DECL_SECTION_NAME (decl);
9906 secname = TREE_STRING_POINTER (sectree);
9908 else if (current_function_decl
9909 && DECL_SECTION_NAME (current_function_decl))
9911 tree sectree = DECL_SECTION_NAME (current_function_decl);
9912 secname = TREE_STRING_POINTER (sectree);
9914 else
9915 secname = text_section_label;
9917 /* Now that we know what section we are using for a base,
9918 actually construct the list of locations.
9919 The first location information is what is passed to the
9920 function that creates the location list, and the remaining
9921 locations just get added on to that list.
9922 Note that we only know the start address for a location
9923 (IE location changes), so to build the range, we use
9924 the range [current location start, next location start].
9925 This means we have to special case the last node, and generate
9926 a range of [last location start, end of function label]. */
9928 node = loc_list->first;
9929 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
9930 list = new_loc_list (loc_descriptor (varloc, attr != DW_AT_frame_base),
9931 node->label, node->next->label, secname, 1);
9932 node = node->next;
9934 for (; node->next; node = node->next)
9935 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
9937 /* The variable has a location between NODE->LABEL and
9938 NODE->NEXT->LABEL. */
9939 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
9940 add_loc_descr_to_loc_list (&list,
9941 loc_descriptor (varloc,
9942 attr != DW_AT_frame_base),
9943 node->label, node->next->label, secname);
9946 /* If the variable has a location at the last label
9947 it keeps its location until the end of function. */
9948 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
9950 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
9952 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
9953 if (!current_function_decl)
9954 endname = text_end_label;
9955 else
9957 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
9958 current_function_funcdef_no);
9959 endname = ggc_strdup (label_id);
9961 add_loc_descr_to_loc_list (&list,
9962 loc_descriptor (varloc,
9963 attr != DW_AT_frame_base),
9964 node->label, endname, secname);
9967 /* Finally, add the location list to the DIE, and we are done. */
9968 add_AT_loc_list (die, attr, list);
9969 return;
9972 rtl = rtl_for_decl_location (decl);
9973 if (rtl == NULL_RTX)
9974 return;
9976 switch (GET_CODE (rtl))
9978 case ADDRESSOF:
9979 /* The address of a variable that was optimized away;
9980 don't emit anything. */
9981 break;
9983 case CONST_INT:
9984 case CONST_DOUBLE:
9985 case CONST_VECTOR:
9986 case CONST_STRING:
9987 case SYMBOL_REF:
9988 case LABEL_REF:
9989 case CONST:
9990 case PLUS:
9991 /* DECL_RTL could be (plus (reg ...) (const_int ...)) */
9992 add_const_value_attribute (die, rtl);
9993 break;
9995 case MEM:
9996 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL (decl))
9998 /* Need loc_descriptor_from_tree since that's where we know
9999 how to handle TLS variables. Want the object's address
10000 since the top-level DW_AT_location assumes such. See
10001 the confusion in loc_descriptor for reference. */
10002 descr = loc_descriptor_from_tree (decl, 1);
10004 else
10006 case REG:
10007 case SUBREG:
10008 case CONCAT:
10009 descr = loc_descriptor (rtl, true);
10011 add_AT_location_description (die, attr, descr);
10012 break;
10014 case PARALLEL:
10016 rtvec par_elems = XVEC (rtl, 0);
10017 int num_elem = GET_NUM_ELEM (par_elems);
10018 enum machine_mode mode;
10019 int i;
10021 /* Create the first one, so we have something to add to. */
10022 descr = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0), true);
10023 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10024 add_loc_descr (&descr,
10025 new_loc_descr (DW_OP_piece, GET_MODE_SIZE (mode), 0));
10026 for (i = 1; i < num_elem; i++)
10028 dw_loc_descr_ref temp;
10030 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0), true);
10031 add_loc_descr (&descr, temp);
10032 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10033 add_loc_descr (&descr,
10034 new_loc_descr (DW_OP_piece,
10035 GET_MODE_SIZE (mode), 0));
10038 add_AT_location_description (die, DW_AT_location, descr);
10039 break;
10041 default:
10042 abort ();
10046 /* If we don't have a copy of this variable in memory for some reason (such
10047 as a C++ member constant that doesn't have an out-of-line definition),
10048 we should tell the debugger about the constant value. */
10050 static void
10051 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10053 tree init = DECL_INITIAL (decl);
10054 tree type = TREE_TYPE (decl);
10056 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init
10057 && initializer_constant_valid_p (init, type) == null_pointer_node)
10058 /* OK */;
10059 else
10060 return;
10062 switch (TREE_CODE (type))
10064 case INTEGER_TYPE:
10065 if (host_integerp (init, 0))
10066 add_AT_unsigned (var_die, DW_AT_const_value,
10067 tree_low_cst (init, 0));
10068 else
10069 add_AT_long_long (var_die, DW_AT_const_value,
10070 TREE_INT_CST_HIGH (init),
10071 TREE_INT_CST_LOW (init));
10072 break;
10074 default:;
10078 /* Generate a DW_AT_name attribute given some string value to be included as
10079 the value of the attribute. */
10081 static void
10082 add_name_attribute (dw_die_ref die, const char *name_string)
10084 if (name_string != NULL && *name_string != 0)
10086 if (demangle_name_func)
10087 name_string = (*demangle_name_func) (name_string);
10089 add_AT_string (die, DW_AT_name, name_string);
10093 /* Generate a DW_AT_comp_dir attribute for DIE. */
10095 static void
10096 add_comp_dir_attribute (dw_die_ref die)
10098 const char *wd = get_src_pwd ();
10099 if (wd != NULL)
10100 add_AT_string (die, DW_AT_comp_dir, wd);
10103 /* Given a tree node describing an array bound (either lower or upper) output
10104 a representation for that bound. */
10106 static void
10107 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10109 switch (TREE_CODE (bound))
10111 case ERROR_MARK:
10112 return;
10114 /* All fixed-bounds are represented by INTEGER_CST nodes. */
10115 case INTEGER_CST:
10116 if (! host_integerp (bound, 0)
10117 || (bound_attr == DW_AT_lower_bound
10118 && (((is_c_family () || is_java ()) && integer_zerop (bound))
10119 || (is_fortran () && integer_onep (bound)))))
10120 /* Use the default. */
10122 else
10123 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
10124 break;
10126 case CONVERT_EXPR:
10127 case NOP_EXPR:
10128 case NON_LVALUE_EXPR:
10129 case VIEW_CONVERT_EXPR:
10130 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10131 break;
10133 case SAVE_EXPR:
10134 /* If optimization is turned on, the SAVE_EXPRs that describe how to
10135 access the upper bound values may be bogus. If they refer to a
10136 register, they may only describe how to get at these values at the
10137 points in the generated code right after they have just been
10138 computed. Worse yet, in the typical case, the upper bound values
10139 will not even *be* computed in the optimized code (though the
10140 number of elements will), so these SAVE_EXPRs are entirely
10141 bogus. In order to compensate for this fact, we check here to see
10142 if optimization is enabled, and if so, we don't add an attribute
10143 for the (unknown and unknowable) upper bound. This should not
10144 cause too much trouble for existing (stupid?) debuggers because
10145 they have to deal with empty upper bounds location descriptions
10146 anyway in order to be able to deal with incomplete array types.
10147 Of course an intelligent debugger (GDB?) should be able to
10148 comprehend that a missing upper bound specification in an array
10149 type used for a storage class `auto' local array variable
10150 indicates that the upper bound is both unknown (at compile- time)
10151 and unknowable (at run-time) due to optimization.
10153 We assume that a MEM rtx is safe because gcc wouldn't put the
10154 value there unless it was going to be used repeatedly in the
10155 function, i.e. for cleanups. */
10156 if (SAVE_EXPR_RTL (bound)
10157 && (! optimize || GET_CODE (SAVE_EXPR_RTL (bound)) == MEM))
10159 dw_die_ref ctx = lookup_decl_die (current_function_decl);
10160 dw_die_ref decl_die = new_die (DW_TAG_variable, ctx, bound);
10161 rtx loc = SAVE_EXPR_RTL (bound);
10163 /* If the RTL for the SAVE_EXPR is memory, handle the case where
10164 it references an outer function's frame. */
10165 if (GET_CODE (loc) == MEM)
10167 rtx new_addr = fix_lexical_addr (XEXP (loc, 0), bound);
10169 if (XEXP (loc, 0) != new_addr)
10170 loc = gen_rtx_MEM (GET_MODE (loc), new_addr);
10173 add_AT_flag (decl_die, DW_AT_artificial, 1);
10174 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10175 add_AT_location_description (decl_die, DW_AT_location,
10176 loc_descriptor (loc, true));
10177 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10180 /* Else leave out the attribute. */
10181 break;
10183 case VAR_DECL:
10184 case PARM_DECL:
10185 case RESULT_DECL:
10187 dw_die_ref decl_die = lookup_decl_die (bound);
10189 /* ??? Can this happen, or should the variable have been bound
10190 first? Probably it can, since I imagine that we try to create
10191 the types of parameters in the order in which they exist in
10192 the list, and won't have created a forward reference to a
10193 later parameter. */
10194 if (decl_die != NULL)
10195 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10196 break;
10199 default:
10201 /* Otherwise try to create a stack operation procedure to
10202 evaluate the value of the array bound. */
10204 dw_die_ref ctx, decl_die;
10205 dw_loc_descr_ref loc;
10207 loc = loc_descriptor_from_tree (bound, 0);
10208 if (loc == NULL)
10209 break;
10211 if (current_function_decl == 0)
10212 ctx = comp_unit_die;
10213 else
10214 ctx = lookup_decl_die (current_function_decl);
10216 /* If we weren't able to find a context, it's most likely the case
10217 that we are processing the return type of the function. So
10218 make a SAVE_EXPR to point to it and have the limbo DIE code
10219 find the proper die. The save_expr function doesn't always
10220 make a SAVE_EXPR, so do it ourselves. */
10221 if (ctx == 0)
10222 bound = build (SAVE_EXPR, TREE_TYPE (bound), bound,
10223 current_function_decl, NULL_TREE);
10225 decl_die = new_die (DW_TAG_variable, ctx, bound);
10226 add_AT_flag (decl_die, DW_AT_artificial, 1);
10227 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10228 add_AT_loc (decl_die, DW_AT_location, loc);
10230 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10231 break;
10236 /* Note that the block of subscript information for an array type also
10237 includes information about the element type of type given array type. */
10239 static void
10240 add_subscript_info (dw_die_ref type_die, tree type)
10242 #ifndef MIPS_DEBUGGING_INFO
10243 unsigned dimension_number;
10244 #endif
10245 tree lower, upper;
10246 dw_die_ref subrange_die;
10248 /* The GNU compilers represent multidimensional array types as sequences of
10249 one dimensional array types whose element types are themselves array
10250 types. Here we squish that down, so that each multidimensional array
10251 type gets only one array_type DIE in the Dwarf debugging info. The draft
10252 Dwarf specification say that we are allowed to do this kind of
10253 compression in C (because there is no difference between an array or
10254 arrays and a multidimensional array in C) but for other source languages
10255 (e.g. Ada) we probably shouldn't do this. */
10257 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10258 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10259 We work around this by disabling this feature. See also
10260 gen_array_type_die. */
10261 #ifndef MIPS_DEBUGGING_INFO
10262 for (dimension_number = 0;
10263 TREE_CODE (type) == ARRAY_TYPE;
10264 type = TREE_TYPE (type), dimension_number++)
10265 #endif
10267 tree domain = TYPE_DOMAIN (type);
10269 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
10270 and (in GNU C only) variable bounds. Handle all three forms
10271 here. */
10272 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
10273 if (domain)
10275 /* We have an array type with specified bounds. */
10276 lower = TYPE_MIN_VALUE (domain);
10277 upper = TYPE_MAX_VALUE (domain);
10279 /* Define the index type. */
10280 if (TREE_TYPE (domain))
10282 /* ??? This is probably an Ada unnamed subrange type. Ignore the
10283 TREE_TYPE field. We can't emit debug info for this
10284 because it is an unnamed integral type. */
10285 if (TREE_CODE (domain) == INTEGER_TYPE
10286 && TYPE_NAME (domain) == NULL_TREE
10287 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10288 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
10290 else
10291 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10292 type_die);
10295 /* ??? If upper is NULL, the array has unspecified length,
10296 but it does have a lower bound. This happens with Fortran
10297 dimension arr(N:*)
10298 Since the debugger is definitely going to need to know N
10299 to produce useful results, go ahead and output the lower
10300 bound solo, and hope the debugger can cope. */
10302 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
10303 if (upper)
10304 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
10307 /* Otherwise we have an array type with an unspecified length. The
10308 DWARF-2 spec does not say how to handle this; let's just leave out the
10309 bounds. */
10313 static void
10314 add_byte_size_attribute (dw_die_ref die, tree tree_node)
10316 unsigned size;
10318 switch (TREE_CODE (tree_node))
10320 case ERROR_MARK:
10321 size = 0;
10322 break;
10323 case ENUMERAL_TYPE:
10324 case RECORD_TYPE:
10325 case UNION_TYPE:
10326 case QUAL_UNION_TYPE:
10327 size = int_size_in_bytes (tree_node);
10328 break;
10329 case FIELD_DECL:
10330 /* For a data member of a struct or union, the DW_AT_byte_size is
10331 generally given as the number of bytes normally allocated for an
10332 object of the *declared* type of the member itself. This is true
10333 even for bit-fields. */
10334 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10335 break;
10336 default:
10337 abort ();
10340 /* Note that `size' might be -1 when we get to this point. If it is, that
10341 indicates that the byte size of the entity in question is variable. We
10342 have no good way of expressing this fact in Dwarf at the present time,
10343 so just let the -1 pass on through. */
10344 add_AT_unsigned (die, DW_AT_byte_size, size);
10347 /* For a FIELD_DECL node which represents a bit-field, output an attribute
10348 which specifies the distance in bits from the highest order bit of the
10349 "containing object" for the bit-field to the highest order bit of the
10350 bit-field itself.
10352 For any given bit-field, the "containing object" is a hypothetical object
10353 (of some integral or enum type) within which the given bit-field lives. The
10354 type of this hypothetical "containing object" is always the same as the
10355 declared type of the individual bit-field itself. The determination of the
10356 exact location of the "containing object" for a bit-field is rather
10357 complicated. It's handled by the `field_byte_offset' function (above).
10359 Note that it is the size (in bytes) of the hypothetical "containing object"
10360 which will be given in the DW_AT_byte_size attribute for this bit-field.
10361 (See `byte_size_attribute' above). */
10363 static inline void
10364 add_bit_offset_attribute (dw_die_ref die, tree decl)
10366 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10367 tree type = DECL_BIT_FIELD_TYPE (decl);
10368 HOST_WIDE_INT bitpos_int;
10369 HOST_WIDE_INT highest_order_object_bit_offset;
10370 HOST_WIDE_INT highest_order_field_bit_offset;
10371 HOST_WIDE_INT unsigned bit_offset;
10373 /* Must be a field and a bit field. */
10374 if (!type
10375 || TREE_CODE (decl) != FIELD_DECL)
10376 abort ();
10378 /* We can't yet handle bit-fields whose offsets are variable, so if we
10379 encounter such things, just return without generating any attribute
10380 whatsoever. Likewise for variable or too large size. */
10381 if (! host_integerp (bit_position (decl), 0)
10382 || ! host_integerp (DECL_SIZE (decl), 1))
10383 return;
10385 bitpos_int = int_bit_position (decl);
10387 /* Note that the bit offset is always the distance (in bits) from the
10388 highest-order bit of the "containing object" to the highest-order bit of
10389 the bit-field itself. Since the "high-order end" of any object or field
10390 is different on big-endian and little-endian machines, the computation
10391 below must take account of these differences. */
10392 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10393 highest_order_field_bit_offset = bitpos_int;
10395 if (! BYTES_BIG_ENDIAN)
10397 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
10398 highest_order_object_bit_offset += simple_type_size_in_bits (type);
10401 bit_offset
10402 = (! BYTES_BIG_ENDIAN
10403 ? highest_order_object_bit_offset - highest_order_field_bit_offset
10404 : highest_order_field_bit_offset - highest_order_object_bit_offset);
10406 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10409 /* For a FIELD_DECL node which represents a bit field, output an attribute
10410 which specifies the length in bits of the given field. */
10412 static inline void
10413 add_bit_size_attribute (dw_die_ref die, tree decl)
10415 /* Must be a field and a bit field. */
10416 if (TREE_CODE (decl) != FIELD_DECL
10417 || ! DECL_BIT_FIELD_TYPE (decl))
10418 abort ();
10420 if (host_integerp (DECL_SIZE (decl), 1))
10421 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
10424 /* If the compiled language is ANSI C, then add a 'prototyped'
10425 attribute, if arg types are given for the parameters of a function. */
10427 static inline void
10428 add_prototyped_attribute (dw_die_ref die, tree func_type)
10430 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10431 && TYPE_ARG_TYPES (func_type) != NULL)
10432 add_AT_flag (die, DW_AT_prototyped, 1);
10435 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
10436 by looking in either the type declaration or object declaration
10437 equate table. */
10439 static inline void
10440 add_abstract_origin_attribute (dw_die_ref die, tree origin)
10442 dw_die_ref origin_die = NULL;
10444 if (TREE_CODE (origin) != FUNCTION_DECL)
10446 /* We may have gotten separated from the block for the inlined
10447 function, if we're in an exception handler or some such; make
10448 sure that the abstract function has been written out.
10450 Doing this for nested functions is wrong, however; functions are
10451 distinct units, and our context might not even be inline. */
10452 tree fn = origin;
10454 if (TYPE_P (fn))
10455 fn = TYPE_STUB_DECL (fn);
10457 fn = decl_function_context (fn);
10458 if (fn)
10459 dwarf2out_abstract_function (fn);
10462 if (DECL_P (origin))
10463 origin_die = lookup_decl_die (origin);
10464 else if (TYPE_P (origin))
10465 origin_die = lookup_type_die (origin);
10467 if (origin_die == NULL)
10468 abort ();
10470 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
10473 /* We do not currently support the pure_virtual attribute. */
10475 static inline void
10476 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
10478 if (DECL_VINDEX (func_decl))
10480 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10482 if (host_integerp (DECL_VINDEX (func_decl), 0))
10483 add_AT_loc (die, DW_AT_vtable_elem_location,
10484 new_loc_descr (DW_OP_constu,
10485 tree_low_cst (DECL_VINDEX (func_decl), 0),
10486 0));
10488 /* GNU extension: Record what type this method came from originally. */
10489 if (debug_info_level > DINFO_LEVEL_TERSE)
10490 add_AT_die_ref (die, DW_AT_containing_type,
10491 lookup_type_die (DECL_CONTEXT (func_decl)));
10495 /* Add source coordinate attributes for the given decl. */
10497 static void
10498 add_src_coords_attributes (dw_die_ref die, tree decl)
10500 unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
10502 add_AT_unsigned (die, DW_AT_decl_file, file_index);
10503 add_AT_unsigned (die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
10506 /* Add a DW_AT_name attribute and source coordinate attribute for the
10507 given decl, but only if it actually has a name. */
10509 static void
10510 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
10512 tree decl_name;
10514 decl_name = DECL_NAME (decl);
10515 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
10517 add_name_attribute (die, dwarf2_name (decl, 0));
10518 if (! DECL_ARTIFICIAL (decl))
10519 add_src_coords_attributes (die, decl);
10521 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
10522 && TREE_PUBLIC (decl)
10523 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
10524 && !DECL_ABSTRACT (decl))
10525 add_AT_string (die, DW_AT_MIPS_linkage_name,
10526 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
10529 #ifdef VMS_DEBUGGING_INFO
10530 /* Get the function's name, as described by its RTL. This may be different
10531 from the DECL_NAME name used in the source file. */
10532 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
10534 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
10535 XEXP (DECL_RTL (decl), 0));
10536 VARRAY_PUSH_RTX (used_rtx_varray, XEXP (DECL_RTL (decl), 0));
10538 #endif
10541 /* Push a new declaration scope. */
10543 static void
10544 push_decl_scope (tree scope)
10546 VARRAY_PUSH_TREE (decl_scope_table, scope);
10549 /* Pop a declaration scope. */
10551 static inline void
10552 pop_decl_scope (void)
10554 if (VARRAY_ACTIVE_SIZE (decl_scope_table) <= 0)
10555 abort ();
10557 VARRAY_POP (decl_scope_table);
10560 /* Return the DIE for the scope that immediately contains this type.
10561 Non-named types get global scope. Named types nested in other
10562 types get their containing scope if it's open, or global scope
10563 otherwise. All other types (i.e. function-local named types) get
10564 the current active scope. */
10566 static dw_die_ref
10567 scope_die_for (tree t, dw_die_ref context_die)
10569 dw_die_ref scope_die = NULL;
10570 tree containing_scope;
10571 int i;
10573 /* Non-types always go in the current scope. */
10574 if (! TYPE_P (t))
10575 abort ();
10577 containing_scope = TYPE_CONTEXT (t);
10579 /* Use the containing namespace if it was passed in (for a declaration). */
10580 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
10582 if (context_die == lookup_decl_die (containing_scope))
10583 /* OK */;
10584 else
10585 containing_scope = NULL_TREE;
10588 /* Ignore function type "scopes" from the C frontend. They mean that
10589 a tagged type is local to a parmlist of a function declarator, but
10590 that isn't useful to DWARF. */
10591 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
10592 containing_scope = NULL_TREE;
10594 if (containing_scope == NULL_TREE)
10595 scope_die = comp_unit_die;
10596 else if (TYPE_P (containing_scope))
10598 /* For types, we can just look up the appropriate DIE. But
10599 first we check to see if we're in the middle of emitting it
10600 so we know where the new DIE should go. */
10601 for (i = VARRAY_ACTIVE_SIZE (decl_scope_table) - 1; i >= 0; --i)
10602 if (VARRAY_TREE (decl_scope_table, i) == containing_scope)
10603 break;
10605 if (i < 0)
10607 if (debug_info_level > DINFO_LEVEL_TERSE
10608 && !TREE_ASM_WRITTEN (containing_scope))
10609 abort ();
10611 /* If none of the current dies are suitable, we get file scope. */
10612 scope_die = comp_unit_die;
10614 else
10615 scope_die = lookup_type_die (containing_scope);
10617 else
10618 scope_die = context_die;
10620 return scope_die;
10623 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
10625 static inline int
10626 local_scope_p (dw_die_ref context_die)
10628 for (; context_die; context_die = context_die->die_parent)
10629 if (context_die->die_tag == DW_TAG_inlined_subroutine
10630 || context_die->die_tag == DW_TAG_subprogram)
10631 return 1;
10633 return 0;
10636 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
10637 whether or not to treat a DIE in this context as a declaration. */
10639 static inline int
10640 class_or_namespace_scope_p (dw_die_ref context_die)
10642 return (context_die
10643 && (context_die->die_tag == DW_TAG_structure_type
10644 || context_die->die_tag == DW_TAG_union_type
10645 || context_die->die_tag == DW_TAG_namespace));
10648 /* Many forms of DIEs require a "type description" attribute. This
10649 routine locates the proper "type descriptor" die for the type given
10650 by 'type', and adds a DW_AT_type attribute below the given die. */
10652 static void
10653 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
10654 int decl_volatile, dw_die_ref context_die)
10656 enum tree_code code = TREE_CODE (type);
10657 dw_die_ref type_die = NULL;
10659 /* ??? If this type is an unnamed subrange type of an integral or
10660 floating-point type, use the inner type. This is because we have no
10661 support for unnamed types in base_type_die. This can happen if this is
10662 an Ada subrange type. Correct solution is emit a subrange type die. */
10663 if ((code == INTEGER_TYPE || code == REAL_TYPE)
10664 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
10665 type = TREE_TYPE (type), code = TREE_CODE (type);
10667 if (code == ERROR_MARK
10668 /* Handle a special case. For functions whose return type is void, we
10669 generate *no* type attribute. (Note that no object may have type
10670 `void', so this only applies to function return types). */
10671 || code == VOID_TYPE)
10672 return;
10674 type_die = modified_type_die (type,
10675 decl_const || TYPE_READONLY (type),
10676 decl_volatile || TYPE_VOLATILE (type),
10677 context_die);
10679 if (type_die != NULL)
10680 add_AT_die_ref (object_die, DW_AT_type, type_die);
10683 /* Given a tree pointer to a struct, class, union, or enum type node, return
10684 a pointer to the (string) tag name for the given type, or zero if the type
10685 was declared without a tag. */
10687 static const char *
10688 type_tag (tree type)
10690 const char *name = 0;
10692 if (TYPE_NAME (type) != 0)
10694 tree t = 0;
10696 /* Find the IDENTIFIER_NODE for the type name. */
10697 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
10698 t = TYPE_NAME (type);
10700 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
10701 a TYPE_DECL node, regardless of whether or not a `typedef' was
10702 involved. */
10703 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10704 && ! DECL_IGNORED_P (TYPE_NAME (type)))
10705 t = DECL_NAME (TYPE_NAME (type));
10707 /* Now get the name as a string, or invent one. */
10708 if (t != 0)
10709 name = IDENTIFIER_POINTER (t);
10712 return (name == 0 || *name == '\0') ? 0 : name;
10715 /* Return the type associated with a data member, make a special check
10716 for bit field types. */
10718 static inline tree
10719 member_declared_type (tree member)
10721 return (DECL_BIT_FIELD_TYPE (member)
10722 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
10725 /* Get the decl's label, as described by its RTL. This may be different
10726 from the DECL_NAME name used in the source file. */
10728 #if 0
10729 static const char *
10730 decl_start_label (tree decl)
10732 rtx x;
10733 const char *fnname;
10735 x = DECL_RTL (decl);
10736 if (GET_CODE (x) != MEM)
10737 abort ();
10739 x = XEXP (x, 0);
10740 if (GET_CODE (x) != SYMBOL_REF)
10741 abort ();
10743 fnname = XSTR (x, 0);
10744 return fnname;
10746 #endif
10748 /* These routines generate the internal representation of the DIE's for
10749 the compilation unit. Debugging information is collected by walking
10750 the declaration trees passed in from dwarf2out_decl(). */
10752 static void
10753 gen_array_type_die (tree type, dw_die_ref context_die)
10755 dw_die_ref scope_die = scope_die_for (type, context_die);
10756 dw_die_ref array_die;
10757 tree element_type;
10759 /* ??? The SGI dwarf reader fails for array of array of enum types unless
10760 the inner array type comes before the outer array type. Thus we must
10761 call gen_type_die before we call new_die. See below also. */
10762 #ifdef MIPS_DEBUGGING_INFO
10763 gen_type_die (TREE_TYPE (type), context_die);
10764 #endif
10766 array_die = new_die (DW_TAG_array_type, scope_die, type);
10767 add_name_attribute (array_die, type_tag (type));
10768 equate_type_number_to_die (type, array_die);
10770 if (TREE_CODE (type) == VECTOR_TYPE)
10772 /* The frontend feeds us a representation for the vector as a struct
10773 containing an array. Pull out the array type. */
10774 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
10775 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
10778 #if 0
10779 /* We default the array ordering. SDB will probably do
10780 the right things even if DW_AT_ordering is not present. It's not even
10781 an issue until we start to get into multidimensional arrays anyway. If
10782 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
10783 then we'll have to put the DW_AT_ordering attribute back in. (But if
10784 and when we find out that we need to put these in, we will only do so
10785 for multidimensional arrays. */
10786 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
10787 #endif
10789 #ifdef MIPS_DEBUGGING_INFO
10790 /* The SGI compilers handle arrays of unknown bound by setting
10791 AT_declaration and not emitting any subrange DIEs. */
10792 if (! TYPE_DOMAIN (type))
10793 add_AT_flag (array_die, DW_AT_declaration, 1);
10794 else
10795 #endif
10796 add_subscript_info (array_die, type);
10798 /* Add representation of the type of the elements of this array type. */
10799 element_type = TREE_TYPE (type);
10801 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10802 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10803 We work around this by disabling this feature. See also
10804 add_subscript_info. */
10805 #ifndef MIPS_DEBUGGING_INFO
10806 while (TREE_CODE (element_type) == ARRAY_TYPE)
10807 element_type = TREE_TYPE (element_type);
10809 gen_type_die (element_type, context_die);
10810 #endif
10812 add_type_attribute (array_die, element_type, 0, 0, context_die);
10815 static void
10816 gen_set_type_die (tree type, dw_die_ref context_die)
10818 dw_die_ref type_die
10819 = new_die (DW_TAG_set_type, scope_die_for (type, context_die), type);
10821 equate_type_number_to_die (type, type_die);
10822 add_type_attribute (type_die, TREE_TYPE (type), 0, 0, context_die);
10825 #if 0
10826 static void
10827 gen_entry_point_die (tree decl, dw_die_ref context_die)
10829 tree origin = decl_ultimate_origin (decl);
10830 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
10832 if (origin != NULL)
10833 add_abstract_origin_attribute (decl_die, origin);
10834 else
10836 add_name_and_src_coords_attributes (decl_die, decl);
10837 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
10838 0, 0, context_die);
10841 if (DECL_ABSTRACT (decl))
10842 equate_decl_number_to_die (decl, decl_die);
10843 else
10844 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
10846 #endif
10848 /* Walk through the list of incomplete types again, trying once more to
10849 emit full debugging info for them. */
10851 static void
10852 retry_incomplete_types (void)
10854 int i;
10856 for (i = VARRAY_ACTIVE_SIZE (incomplete_types) - 1; i >= 0; i--)
10857 gen_type_die (VARRAY_TREE (incomplete_types, i), comp_unit_die);
10860 /* Generate a DIE to represent an inlined instance of an enumeration type. */
10862 static void
10863 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
10865 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
10867 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10868 be incomplete and such types are not marked. */
10869 add_abstract_origin_attribute (type_die, type);
10872 /* Generate a DIE to represent an inlined instance of a structure type. */
10874 static void
10875 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
10877 dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
10879 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10880 be incomplete and such types are not marked. */
10881 add_abstract_origin_attribute (type_die, type);
10884 /* Generate a DIE to represent an inlined instance of a union type. */
10886 static void
10887 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
10889 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
10891 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10892 be incomplete and such types are not marked. */
10893 add_abstract_origin_attribute (type_die, type);
10896 /* Generate a DIE to represent an enumeration type. Note that these DIEs
10897 include all of the information about the enumeration values also. Each
10898 enumerated type name/value is listed as a child of the enumerated type
10899 DIE. */
10901 static dw_die_ref
10902 gen_enumeration_type_die (tree type, dw_die_ref context_die)
10904 dw_die_ref type_die = lookup_type_die (type);
10906 if (type_die == NULL)
10908 type_die = new_die (DW_TAG_enumeration_type,
10909 scope_die_for (type, context_die), type);
10910 equate_type_number_to_die (type, type_die);
10911 add_name_attribute (type_die, type_tag (type));
10913 else if (! TYPE_SIZE (type))
10914 return type_die;
10915 else
10916 remove_AT (type_die, DW_AT_declaration);
10918 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
10919 given enum type is incomplete, do not generate the DW_AT_byte_size
10920 attribute or the DW_AT_element_list attribute. */
10921 if (TYPE_SIZE (type))
10923 tree link;
10925 TREE_ASM_WRITTEN (type) = 1;
10926 add_byte_size_attribute (type_die, type);
10927 if (TYPE_STUB_DECL (type) != NULL_TREE)
10928 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
10930 /* If the first reference to this type was as the return type of an
10931 inline function, then it may not have a parent. Fix this now. */
10932 if (type_die->die_parent == NULL)
10933 add_child_die (scope_die_for (type, context_die), type_die);
10935 for (link = TYPE_VALUES (type);
10936 link != NULL; link = TREE_CHAIN (link))
10938 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
10939 tree value = TREE_VALUE (link);
10941 add_name_attribute (enum_die,
10942 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
10944 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
10945 /* DWARF2 does not provide a way of indicating whether or
10946 not enumeration constants are signed or unsigned. GDB
10947 always assumes the values are signed, so we output all
10948 values as if they were signed. That means that
10949 enumeration constants with very large unsigned values
10950 will appear to have negative values in the debugger. */
10951 add_AT_int (enum_die, DW_AT_const_value,
10952 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
10955 else
10956 add_AT_flag (type_die, DW_AT_declaration, 1);
10958 return type_die;
10961 /* Generate a DIE to represent either a real live formal parameter decl or to
10962 represent just the type of some formal parameter position in some function
10963 type.
10965 Note that this routine is a bit unusual because its argument may be a
10966 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
10967 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
10968 node. If it's the former then this function is being called to output a
10969 DIE to represent a formal parameter object (or some inlining thereof). If
10970 it's the latter, then this function is only being called to output a
10971 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
10972 argument type of some subprogram type. */
10974 static dw_die_ref
10975 gen_formal_parameter_die (tree node, dw_die_ref context_die)
10977 dw_die_ref parm_die
10978 = new_die (DW_TAG_formal_parameter, context_die, node);
10979 tree origin;
10981 switch (TREE_CODE_CLASS (TREE_CODE (node)))
10983 case 'd':
10984 origin = decl_ultimate_origin (node);
10985 if (origin != NULL)
10986 add_abstract_origin_attribute (parm_die, origin);
10987 else
10989 add_name_and_src_coords_attributes (parm_die, node);
10990 add_type_attribute (parm_die, TREE_TYPE (node),
10991 TREE_READONLY (node),
10992 TREE_THIS_VOLATILE (node),
10993 context_die);
10994 if (DECL_ARTIFICIAL (node))
10995 add_AT_flag (parm_die, DW_AT_artificial, 1);
10998 equate_decl_number_to_die (node, parm_die);
10999 if (! DECL_ABSTRACT (node))
11000 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
11002 break;
11004 case 't':
11005 /* We were called with some kind of a ..._TYPE node. */
11006 add_type_attribute (parm_die, node, 0, 0, context_die);
11007 break;
11009 default:
11010 abort ();
11013 return parm_die;
11016 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11017 at the end of an (ANSI prototyped) formal parameters list. */
11019 static void
11020 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11022 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11025 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11026 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11027 parameters as specified in some function type specification (except for
11028 those which appear as part of a function *definition*). */
11030 static void
11031 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11033 tree link;
11034 tree formal_type = NULL;
11035 tree first_parm_type;
11036 tree arg;
11038 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11040 arg = DECL_ARGUMENTS (function_or_method_type);
11041 function_or_method_type = TREE_TYPE (function_or_method_type);
11043 else
11044 arg = NULL_TREE;
11046 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11048 /* Make our first pass over the list of formal parameter types and output a
11049 DW_TAG_formal_parameter DIE for each one. */
11050 for (link = first_parm_type; link; )
11052 dw_die_ref parm_die;
11054 formal_type = TREE_VALUE (link);
11055 if (formal_type == void_type_node)
11056 break;
11058 /* Output a (nameless) DIE to represent the formal parameter itself. */
11059 parm_die = gen_formal_parameter_die (formal_type, context_die);
11060 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11061 && link == first_parm_type)
11062 || (arg && DECL_ARTIFICIAL (arg)))
11063 add_AT_flag (parm_die, DW_AT_artificial, 1);
11065 link = TREE_CHAIN (link);
11066 if (arg)
11067 arg = TREE_CHAIN (arg);
11070 /* If this function type has an ellipsis, add a
11071 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
11072 if (formal_type != void_type_node)
11073 gen_unspecified_parameters_die (function_or_method_type, context_die);
11075 /* Make our second (and final) pass over the list of formal parameter types
11076 and output DIEs to represent those types (as necessary). */
11077 for (link = TYPE_ARG_TYPES (function_or_method_type);
11078 link && TREE_VALUE (link);
11079 link = TREE_CHAIN (link))
11080 gen_type_die (TREE_VALUE (link), context_die);
11083 /* We want to generate the DIE for TYPE so that we can generate the
11084 die for MEMBER, which has been defined; we will need to refer back
11085 to the member declaration nested within TYPE. If we're trying to
11086 generate minimal debug info for TYPE, processing TYPE won't do the
11087 trick; we need to attach the member declaration by hand. */
11089 static void
11090 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11092 gen_type_die (type, context_die);
11094 /* If we're trying to avoid duplicate debug info, we may not have
11095 emitted the member decl for this function. Emit it now. */
11096 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11097 && ! lookup_decl_die (member))
11099 if (decl_ultimate_origin (member))
11100 abort ();
11102 push_decl_scope (type);
11103 if (TREE_CODE (member) == FUNCTION_DECL)
11104 gen_subprogram_die (member, lookup_type_die (type));
11105 else
11106 gen_variable_die (member, lookup_type_die (type));
11108 pop_decl_scope ();
11112 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11113 may later generate inlined and/or out-of-line instances of. */
11115 static void
11116 dwarf2out_abstract_function (tree decl)
11118 dw_die_ref old_die;
11119 tree save_fn;
11120 tree context;
11121 int was_abstract = DECL_ABSTRACT (decl);
11123 /* Make sure we have the actual abstract inline, not a clone. */
11124 decl = DECL_ORIGIN (decl);
11126 old_die = lookup_decl_die (decl);
11127 if (old_die && get_AT (old_die, DW_AT_inline))
11128 /* We've already generated the abstract instance. */
11129 return;
11131 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11132 we don't get confused by DECL_ABSTRACT. */
11133 if (debug_info_level > DINFO_LEVEL_TERSE)
11135 context = decl_class_context (decl);
11136 if (context)
11137 gen_type_die_for_member
11138 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11141 /* Pretend we've just finished compiling this function. */
11142 save_fn = current_function_decl;
11143 current_function_decl = decl;
11145 set_decl_abstract_flags (decl, 1);
11146 dwarf2out_decl (decl);
11147 if (! was_abstract)
11148 set_decl_abstract_flags (decl, 0);
11150 current_function_decl = save_fn;
11153 /* Generate a DIE to represent a declared function (either file-scope or
11154 block-local). */
11156 static void
11157 gen_subprogram_die (tree decl, dw_die_ref context_die)
11159 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11160 tree origin = decl_ultimate_origin (decl);
11161 dw_die_ref subr_die;
11162 rtx fp_reg;
11163 tree fn_arg_types;
11164 tree outer_scope;
11165 dw_die_ref old_die = lookup_decl_die (decl);
11166 int declaration = (current_function_decl != decl
11167 || class_or_namespace_scope_p (context_die));
11169 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11170 started to generate the abstract instance of an inline, decided to output
11171 its containing class, and proceeded to emit the declaration of the inline
11172 from the member list for the class. If so, DECLARATION takes priority;
11173 we'll get back to the abstract instance when done with the class. */
11175 /* The class-scope declaration DIE must be the primary DIE. */
11176 if (origin && declaration && class_or_namespace_scope_p (context_die))
11178 origin = NULL;
11179 if (old_die)
11180 abort ();
11183 if (origin != NULL)
11185 if (declaration && ! local_scope_p (context_die))
11186 abort ();
11188 /* Fixup die_parent for the abstract instance of a nested
11189 inline function. */
11190 if (old_die && old_die->die_parent == NULL)
11191 add_child_die (context_die, old_die);
11193 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11194 add_abstract_origin_attribute (subr_die, origin);
11196 else if (old_die)
11198 unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
11200 if (!get_AT_flag (old_die, DW_AT_declaration)
11201 /* We can have a normal definition following an inline one in the
11202 case of redefinition of GNU C extern inlines.
11203 It seems reasonable to use AT_specification in this case. */
11204 && !get_AT (old_die, DW_AT_inline))
11206 /* ??? This can happen if there is a bug in the program, for
11207 instance, if it has duplicate function definitions. Ideally,
11208 we should detect this case and ignore it. For now, if we have
11209 already reported an error, any error at all, then assume that
11210 we got here because of an input error, not a dwarf2 bug. */
11211 if (errorcount)
11212 return;
11213 abort ();
11216 /* If the definition comes from the same place as the declaration,
11217 maybe use the old DIE. We always want the DIE for this function
11218 that has the *_pc attributes to be under comp_unit_die so the
11219 debugger can find it. We also need to do this for abstract
11220 instances of inlines, since the spec requires the out-of-line copy
11221 to have the same parent. For local class methods, this doesn't
11222 apply; we just use the old DIE. */
11223 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
11224 && (DECL_ARTIFICIAL (decl)
11225 || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
11226 && (get_AT_unsigned (old_die, DW_AT_decl_line)
11227 == (unsigned) DECL_SOURCE_LINE (decl)))))
11229 subr_die = old_die;
11231 /* Clear out the declaration attribute and the formal parameters.
11232 Do not remove all children, because it is possible that this
11233 declaration die was forced using force_decl_die(). In such
11234 cases die that forced declaration die (e.g. TAG_imported_module)
11235 is one of the children that we do not want to remove. */
11236 remove_AT (subr_die, DW_AT_declaration);
11237 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
11239 else
11241 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11242 add_AT_specification (subr_die, old_die);
11243 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11244 add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
11245 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11246 != (unsigned) DECL_SOURCE_LINE (decl))
11247 add_AT_unsigned
11248 (subr_die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
11251 else
11253 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11255 if (TREE_PUBLIC (decl))
11256 add_AT_flag (subr_die, DW_AT_external, 1);
11258 add_name_and_src_coords_attributes (subr_die, decl);
11259 if (debug_info_level > DINFO_LEVEL_TERSE)
11261 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11262 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11263 0, 0, context_die);
11266 add_pure_or_virtual_attribute (subr_die, decl);
11267 if (DECL_ARTIFICIAL (decl))
11268 add_AT_flag (subr_die, DW_AT_artificial, 1);
11270 if (TREE_PROTECTED (decl))
11271 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11272 else if (TREE_PRIVATE (decl))
11273 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
11276 if (declaration)
11278 if (!old_die || !get_AT (old_die, DW_AT_inline))
11280 add_AT_flag (subr_die, DW_AT_declaration, 1);
11282 /* The first time we see a member function, it is in the context of
11283 the class to which it belongs. We make sure of this by emitting
11284 the class first. The next time is the definition, which is
11285 handled above. The two may come from the same source text.
11287 Note that force_decl_die() forces function declaration die. It is
11288 later reused to represent definition. */
11289 equate_decl_number_to_die (decl, subr_die);
11292 else if (DECL_ABSTRACT (decl))
11294 if (DECL_DECLARED_INLINE_P (decl))
11296 if (cgraph_function_possibly_inlined_p (decl))
11297 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11298 else
11299 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
11301 else
11303 if (cgraph_function_possibly_inlined_p (decl))
11304 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11305 else
11306 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11309 equate_decl_number_to_die (decl, subr_die);
11311 else if (!DECL_EXTERNAL (decl))
11313 if (!old_die || !get_AT (old_die, DW_AT_inline))
11314 equate_decl_number_to_die (decl, subr_die);
11316 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11317 current_function_funcdef_no);
11318 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11319 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11320 current_function_funcdef_no);
11321 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11323 add_pubname (decl, subr_die);
11324 add_arange (decl, subr_die);
11326 #ifdef MIPS_DEBUGGING_INFO
11327 /* Add a reference to the FDE for this routine. */
11328 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11329 #endif
11331 /* Define the "frame base" location for this routine. We use the
11332 frame pointer or stack pointer registers, since the RTL for local
11333 variables is relative to one of them. */
11334 if (frame_base_decl && lookup_decl_loc (frame_base_decl) != NULL)
11336 add_location_or_const_value_attribute (subr_die, frame_base_decl,
11337 DW_AT_frame_base);
11339 else
11341 fp_reg
11342 = frame_pointer_needed ? hard_frame_pointer_rtx : stack_pointer_rtx;
11343 add_AT_loc (subr_die, DW_AT_frame_base, reg_loc_descriptor (fp_reg));
11346 if (cfun->static_chain_decl)
11347 add_AT_location_description (subr_die, DW_AT_static_link,
11348 loc_descriptor_from_tree (cfun->static_chain_decl, 0));
11351 /* Now output descriptions of the arguments for this function. This gets
11352 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
11353 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
11354 `...' at the end of the formal parameter list. In order to find out if
11355 there was a trailing ellipsis or not, we must instead look at the type
11356 associated with the FUNCTION_DECL. This will be a node of type
11357 FUNCTION_TYPE. If the chain of type nodes hanging off of this
11358 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
11359 an ellipsis at the end. */
11361 /* In the case where we are describing a mere function declaration, all we
11362 need to do here (and all we *can* do here) is to describe the *types* of
11363 its formal parameters. */
11364 if (debug_info_level <= DINFO_LEVEL_TERSE)
11366 else if (declaration)
11367 gen_formal_types_die (decl, subr_die);
11368 else
11370 /* Generate DIEs to represent all known formal parameters. */
11371 tree arg_decls = DECL_ARGUMENTS (decl);
11372 tree parm;
11374 /* When generating DIEs, generate the unspecified_parameters DIE
11375 instead if we come across the arg "__builtin_va_alist" */
11376 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
11377 if (TREE_CODE (parm) == PARM_DECL)
11379 if (DECL_NAME (parm)
11380 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
11381 "__builtin_va_alist"))
11382 gen_unspecified_parameters_die (parm, subr_die);
11383 else
11384 gen_decl_die (parm, subr_die);
11387 /* Decide whether we need an unspecified_parameters DIE at the end.
11388 There are 2 more cases to do this for: 1) the ansi ... declaration -
11389 this is detectable when the end of the arg list is not a
11390 void_type_node 2) an unprototyped function declaration (not a
11391 definition). This just means that we have no info about the
11392 parameters at all. */
11393 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
11394 if (fn_arg_types != NULL)
11396 /* This is the prototyped case, check for.... */
11397 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
11398 gen_unspecified_parameters_die (decl, subr_die);
11400 else if (DECL_INITIAL (decl) == NULL_TREE)
11401 gen_unspecified_parameters_die (decl, subr_die);
11404 /* Output Dwarf info for all of the stuff within the body of the function
11405 (if it has one - it may be just a declaration). */
11406 outer_scope = DECL_INITIAL (decl);
11408 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
11409 a function. This BLOCK actually represents the outermost binding contour
11410 for the function, i.e. the contour in which the function's formal
11411 parameters and labels get declared. Curiously, it appears that the front
11412 end doesn't actually put the PARM_DECL nodes for the current function onto
11413 the BLOCK_VARS list for this outer scope, but are strung off of the
11414 DECL_ARGUMENTS list for the function instead.
11416 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
11417 the LABEL_DECL nodes for the function however, and we output DWARF info
11418 for those in decls_for_scope. Just within the `outer_scope' there will be
11419 a BLOCK node representing the function's outermost pair of curly braces,
11420 and any blocks used for the base and member initializers of a C++
11421 constructor function. */
11422 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
11424 /* Emit a DW_TAG_variable DIE for a named return value. */
11425 if (DECL_NAME (DECL_RESULT (decl)))
11426 gen_decl_die (DECL_RESULT (decl), subr_die);
11428 current_function_has_inlines = 0;
11429 decls_for_scope (outer_scope, subr_die, 0);
11431 #if 0 && defined (MIPS_DEBUGGING_INFO)
11432 if (current_function_has_inlines)
11434 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
11435 if (! comp_unit_has_inlines)
11437 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
11438 comp_unit_has_inlines = 1;
11441 #endif
11445 /* Generate a DIE to represent a declared data object. */
11447 static void
11448 gen_variable_die (tree decl, dw_die_ref context_die)
11450 tree origin = decl_ultimate_origin (decl);
11451 dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
11453 dw_die_ref old_die = lookup_decl_die (decl);
11454 int declaration = (DECL_EXTERNAL (decl)
11455 || class_or_namespace_scope_p (context_die));
11457 if (origin != NULL)
11458 add_abstract_origin_attribute (var_die, origin);
11460 /* Loop unrolling can create multiple blocks that refer to the same
11461 static variable, so we must test for the DW_AT_declaration flag.
11463 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
11464 copy decls and set the DECL_ABSTRACT flag on them instead of
11465 sharing them.
11467 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
11468 else if (old_die && TREE_STATIC (decl)
11469 && get_AT_flag (old_die, DW_AT_declaration) == 1)
11471 /* This is a definition of a C++ class level static. */
11472 add_AT_specification (var_die, old_die);
11473 if (DECL_NAME (decl))
11475 unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
11477 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11478 add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
11480 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11481 != (unsigned) DECL_SOURCE_LINE (decl))
11483 add_AT_unsigned (var_die, DW_AT_decl_line,
11484 DECL_SOURCE_LINE (decl));
11487 else
11489 add_name_and_src_coords_attributes (var_die, decl);
11490 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
11491 TREE_THIS_VOLATILE (decl), context_die);
11493 if (TREE_PUBLIC (decl))
11494 add_AT_flag (var_die, DW_AT_external, 1);
11496 if (DECL_ARTIFICIAL (decl))
11497 add_AT_flag (var_die, DW_AT_artificial, 1);
11499 if (TREE_PROTECTED (decl))
11500 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
11501 else if (TREE_PRIVATE (decl))
11502 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
11505 if (declaration)
11506 add_AT_flag (var_die, DW_AT_declaration, 1);
11508 if (DECL_ABSTRACT (decl) || declaration)
11509 equate_decl_number_to_die (decl, var_die);
11511 if (! declaration && ! DECL_ABSTRACT (decl))
11513 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
11514 add_pubname (decl, var_die);
11516 else
11517 tree_add_const_value_attribute (var_die, decl);
11520 /* Generate a DIE to represent a label identifier. */
11522 static void
11523 gen_label_die (tree decl, dw_die_ref context_die)
11525 tree origin = decl_ultimate_origin (decl);
11526 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
11527 rtx insn;
11528 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11530 if (origin != NULL)
11531 add_abstract_origin_attribute (lbl_die, origin);
11532 else
11533 add_name_and_src_coords_attributes (lbl_die, decl);
11535 if (DECL_ABSTRACT (decl))
11536 equate_decl_number_to_die (decl, lbl_die);
11537 else
11539 insn = DECL_RTL_IF_SET (decl);
11541 /* Deleted labels are programmer specified labels which have been
11542 eliminated because of various optimizations. We still emit them
11543 here so that it is possible to put breakpoints on them. */
11544 if (insn
11545 && (GET_CODE (insn) == CODE_LABEL
11546 || ((GET_CODE (insn) == NOTE
11547 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
11549 /* When optimization is enabled (via -O) some parts of the compiler
11550 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
11551 represent source-level labels which were explicitly declared by
11552 the user. This really shouldn't be happening though, so catch
11553 it if it ever does happen. */
11554 if (INSN_DELETED_P (insn))
11555 abort ();
11557 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
11558 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
11563 /* Generate a DIE for a lexical block. */
11565 static void
11566 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
11568 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
11569 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11571 if (! BLOCK_ABSTRACT (stmt))
11573 if (BLOCK_FRAGMENT_CHAIN (stmt))
11575 tree chain;
11577 add_AT_range_list (stmt_die, DW_AT_ranges, add_ranges (stmt));
11579 chain = BLOCK_FRAGMENT_CHAIN (stmt);
11582 add_ranges (chain);
11583 chain = BLOCK_FRAGMENT_CHAIN (chain);
11585 while (chain);
11586 add_ranges (NULL);
11588 else
11590 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
11591 BLOCK_NUMBER (stmt));
11592 add_AT_lbl_id (stmt_die, DW_AT_low_pc, label);
11593 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
11594 BLOCK_NUMBER (stmt));
11595 add_AT_lbl_id (stmt_die, DW_AT_high_pc, label);
11599 decls_for_scope (stmt, stmt_die, depth);
11602 /* Generate a DIE for an inlined subprogram. */
11604 static void
11605 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
11607 tree decl = block_ultimate_origin (stmt);
11609 /* Emit info for the abstract instance first, if we haven't yet. We
11610 must emit this even if the block is abstract, otherwise when we
11611 emit the block below (or elsewhere), we may end up trying to emit
11612 a die whose origin die hasn't been emitted, and crashing. */
11613 dwarf2out_abstract_function (decl);
11615 if (! BLOCK_ABSTRACT (stmt))
11617 dw_die_ref subr_die
11618 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
11619 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11621 add_abstract_origin_attribute (subr_die, decl);
11622 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
11623 BLOCK_NUMBER (stmt));
11624 add_AT_lbl_id (subr_die, DW_AT_low_pc, label);
11625 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
11626 BLOCK_NUMBER (stmt));
11627 add_AT_lbl_id (subr_die, DW_AT_high_pc, label);
11628 decls_for_scope (stmt, subr_die, depth);
11629 current_function_has_inlines = 1;
11631 else
11632 /* We may get here if we're the outer block of function A that was
11633 inlined into function B that was inlined into function C. When
11634 generating debugging info for C, dwarf2out_abstract_function(B)
11635 would mark all inlined blocks as abstract, including this one.
11636 So, we wouldn't (and shouldn't) expect labels to be generated
11637 for this one. Instead, just emit debugging info for
11638 declarations within the block. This is particularly important
11639 in the case of initializers of arguments passed from B to us:
11640 if they're statement expressions containing declarations, we
11641 wouldn't generate dies for their abstract variables, and then,
11642 when generating dies for the real variables, we'd die (pun
11643 intended :-) */
11644 gen_lexical_block_die (stmt, context_die, depth);
11647 /* Generate a DIE for a field in a record, or structure. */
11649 static void
11650 gen_field_die (tree decl, dw_die_ref context_die)
11652 dw_die_ref decl_die;
11654 if (TREE_TYPE (decl) == error_mark_node)
11655 return;
11657 decl_die = new_die (DW_TAG_member, context_die, decl);
11658 add_name_and_src_coords_attributes (decl_die, decl);
11659 add_type_attribute (decl_die, member_declared_type (decl),
11660 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
11661 context_die);
11663 if (DECL_BIT_FIELD_TYPE (decl))
11665 add_byte_size_attribute (decl_die, decl);
11666 add_bit_size_attribute (decl_die, decl);
11667 add_bit_offset_attribute (decl_die, decl);
11670 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
11671 add_data_member_location_attribute (decl_die, decl);
11673 if (DECL_ARTIFICIAL (decl))
11674 add_AT_flag (decl_die, DW_AT_artificial, 1);
11676 if (TREE_PROTECTED (decl))
11677 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
11678 else if (TREE_PRIVATE (decl))
11679 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
11682 #if 0
11683 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
11684 Use modified_type_die instead.
11685 We keep this code here just in case these types of DIEs may be needed to
11686 represent certain things in other languages (e.g. Pascal) someday. */
11688 static void
11689 gen_pointer_type_die (tree type, dw_die_ref context_die)
11691 dw_die_ref ptr_die
11692 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
11694 equate_type_number_to_die (type, ptr_die);
11695 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
11696 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
11699 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
11700 Use modified_type_die instead.
11701 We keep this code here just in case these types of DIEs may be needed to
11702 represent certain things in other languages (e.g. Pascal) someday. */
11704 static void
11705 gen_reference_type_die (tree type, dw_die_ref context_die)
11707 dw_die_ref ref_die
11708 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
11710 equate_type_number_to_die (type, ref_die);
11711 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
11712 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
11714 #endif
11716 /* Generate a DIE for a pointer to a member type. */
11718 static void
11719 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
11721 dw_die_ref ptr_die
11722 = new_die (DW_TAG_ptr_to_member_type,
11723 scope_die_for (type, context_die), type);
11725 equate_type_number_to_die (type, ptr_die);
11726 add_AT_die_ref (ptr_die, DW_AT_containing_type,
11727 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
11728 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
11731 /* Generate the DIE for the compilation unit. */
11733 static dw_die_ref
11734 gen_compile_unit_die (const char *filename)
11736 dw_die_ref die;
11737 char producer[250];
11738 const char *language_string = lang_hooks.name;
11739 int language;
11741 die = new_die (DW_TAG_compile_unit, NULL, NULL);
11743 if (filename)
11745 add_name_attribute (die, filename);
11746 /* Don't add cwd for <built-in>. */
11747 if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
11748 add_comp_dir_attribute (die);
11751 sprintf (producer, "%s %s", language_string, version_string);
11753 #ifdef MIPS_DEBUGGING_INFO
11754 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
11755 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
11756 not appear in the producer string, the debugger reaches the conclusion
11757 that the object file is stripped and has no debugging information.
11758 To get the MIPS/SGI debugger to believe that there is debugging
11759 information in the object file, we add a -g to the producer string. */
11760 if (debug_info_level > DINFO_LEVEL_TERSE)
11761 strcat (producer, " -g");
11762 #endif
11764 add_AT_string (die, DW_AT_producer, producer);
11766 if (strcmp (language_string, "GNU C++") == 0)
11767 language = DW_LANG_C_plus_plus;
11768 else if (strcmp (language_string, "GNU Ada") == 0)
11769 language = DW_LANG_Ada95;
11770 else if (strcmp (language_string, "GNU F77") == 0)
11771 language = DW_LANG_Fortran77;
11772 else if (strcmp (language_string, "GNU F95") == 0)
11773 language = DW_LANG_Fortran95;
11774 else if (strcmp (language_string, "GNU Pascal") == 0)
11775 language = DW_LANG_Pascal83;
11776 else if (strcmp (language_string, "GNU Java") == 0)
11777 language = DW_LANG_Java;
11778 else
11779 language = DW_LANG_C89;
11781 add_AT_unsigned (die, DW_AT_language, language);
11782 return die;
11785 /* Generate a DIE for a string type. */
11787 static void
11788 gen_string_type_die (tree type, dw_die_ref context_die)
11790 dw_die_ref type_die
11791 = new_die (DW_TAG_string_type, scope_die_for (type, context_die), type);
11793 equate_type_number_to_die (type, type_die);
11795 /* ??? Fudge the string length attribute for now.
11796 TODO: add string length info. */
11797 #if 0
11798 string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
11799 bound_representation (upper_bound, 0, 'u');
11800 #endif
11803 /* Generate the DIE for a base class. */
11805 static void
11806 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
11808 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
11810 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
11811 add_data_member_location_attribute (die, binfo);
11813 if (TREE_VIA_VIRTUAL (binfo))
11814 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11816 if (access == access_public_node)
11817 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
11818 else if (access == access_protected_node)
11819 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
11822 /* Generate a DIE for a class member. */
11824 static void
11825 gen_member_die (tree type, dw_die_ref context_die)
11827 tree member;
11828 tree binfo = TYPE_BINFO (type);
11829 dw_die_ref child;
11831 /* If this is not an incomplete type, output descriptions of each of its
11832 members. Note that as we output the DIEs necessary to represent the
11833 members of this record or union type, we will also be trying to output
11834 DIEs to represent the *types* of those members. However the `type'
11835 function (above) will specifically avoid generating type DIEs for member
11836 types *within* the list of member DIEs for this (containing) type except
11837 for those types (of members) which are explicitly marked as also being
11838 members of this (containing) type themselves. The g++ front- end can
11839 force any given type to be treated as a member of some other (containing)
11840 type by setting the TYPE_CONTEXT of the given (member) type to point to
11841 the TREE node representing the appropriate (containing) type. */
11843 /* First output info about the base classes. */
11844 if (binfo && BINFO_BASETYPES (binfo))
11846 tree bases = BINFO_BASETYPES (binfo);
11847 tree accesses = BINFO_BASEACCESSES (binfo);
11848 int n_bases = TREE_VEC_LENGTH (bases);
11849 int i;
11851 for (i = 0; i < n_bases; i++)
11852 gen_inheritance_die (TREE_VEC_ELT (bases, i),
11853 (accesses ? TREE_VEC_ELT (accesses, i)
11854 : access_public_node), context_die);
11857 /* Now output info about the data members and type members. */
11858 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
11860 /* If we thought we were generating minimal debug info for TYPE
11861 and then changed our minds, some of the member declarations
11862 may have already been defined. Don't define them again, but
11863 do put them in the right order. */
11865 child = lookup_decl_die (member);
11866 if (child)
11867 splice_child_die (context_die, child);
11868 else
11869 gen_decl_die (member, context_die);
11872 /* Now output info about the function members (if any). */
11873 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
11875 /* Don't include clones in the member list. */
11876 if (DECL_ABSTRACT_ORIGIN (member))
11877 continue;
11879 child = lookup_decl_die (member);
11880 if (child)
11881 splice_child_die (context_die, child);
11882 else
11883 gen_decl_die (member, context_die);
11887 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
11888 is set, we pretend that the type was never defined, so we only get the
11889 member DIEs needed by later specification DIEs. */
11891 static void
11892 gen_struct_or_union_type_die (tree type, dw_die_ref context_die)
11894 dw_die_ref type_die = lookup_type_die (type);
11895 dw_die_ref scope_die = 0;
11896 int nested = 0;
11897 int complete = (TYPE_SIZE (type)
11898 && (! TYPE_STUB_DECL (type)
11899 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
11900 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
11902 if (type_die && ! complete)
11903 return;
11905 if (TYPE_CONTEXT (type) != NULL_TREE
11906 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
11907 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
11908 nested = 1;
11910 scope_die = scope_die_for (type, context_die);
11912 if (! type_die || (nested && scope_die == comp_unit_die))
11913 /* First occurrence of type or toplevel definition of nested class. */
11915 dw_die_ref old_die = type_die;
11917 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
11918 ? DW_TAG_structure_type : DW_TAG_union_type,
11919 scope_die, type);
11920 equate_type_number_to_die (type, type_die);
11921 if (old_die)
11922 add_AT_specification (type_die, old_die);
11923 else
11924 add_name_attribute (type_die, type_tag (type));
11926 else
11927 remove_AT (type_die, DW_AT_declaration);
11929 /* If this type has been completed, then give it a byte_size attribute and
11930 then give a list of members. */
11931 if (complete && !ns_decl)
11933 /* Prevent infinite recursion in cases where the type of some member of
11934 this type is expressed in terms of this type itself. */
11935 TREE_ASM_WRITTEN (type) = 1;
11936 add_byte_size_attribute (type_die, type);
11937 if (TYPE_STUB_DECL (type) != NULL_TREE)
11938 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11940 /* If the first reference to this type was as the return type of an
11941 inline function, then it may not have a parent. Fix this now. */
11942 if (type_die->die_parent == NULL)
11943 add_child_die (scope_die, type_die);
11945 push_decl_scope (type);
11946 gen_member_die (type, type_die);
11947 pop_decl_scope ();
11949 /* GNU extension: Record what type our vtable lives in. */
11950 if (TYPE_VFIELD (type))
11952 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
11954 gen_type_die (vtype, context_die);
11955 add_AT_die_ref (type_die, DW_AT_containing_type,
11956 lookup_type_die (vtype));
11959 else
11961 add_AT_flag (type_die, DW_AT_declaration, 1);
11963 /* We don't need to do this for function-local types. */
11964 if (TYPE_STUB_DECL (type)
11965 && ! decl_function_context (TYPE_STUB_DECL (type)))
11966 VARRAY_PUSH_TREE (incomplete_types, type);
11970 /* Generate a DIE for a subroutine _type_. */
11972 static void
11973 gen_subroutine_type_die (tree type, dw_die_ref context_die)
11975 tree return_type = TREE_TYPE (type);
11976 dw_die_ref subr_die
11977 = new_die (DW_TAG_subroutine_type,
11978 scope_die_for (type, context_die), type);
11980 equate_type_number_to_die (type, subr_die);
11981 add_prototyped_attribute (subr_die, type);
11982 add_type_attribute (subr_die, return_type, 0, 0, context_die);
11983 gen_formal_types_die (type, subr_die);
11986 /* Generate a DIE for a type definition. */
11988 static void
11989 gen_typedef_die (tree decl, dw_die_ref context_die)
11991 dw_die_ref type_die;
11992 tree origin;
11994 if (TREE_ASM_WRITTEN (decl))
11995 return;
11997 TREE_ASM_WRITTEN (decl) = 1;
11998 type_die = new_die (DW_TAG_typedef, context_die, decl);
11999 origin = decl_ultimate_origin (decl);
12000 if (origin != NULL)
12001 add_abstract_origin_attribute (type_die, origin);
12002 else
12004 tree type;
12006 add_name_and_src_coords_attributes (type_die, decl);
12007 if (DECL_ORIGINAL_TYPE (decl))
12009 type = DECL_ORIGINAL_TYPE (decl);
12011 if (type == TREE_TYPE (decl))
12012 abort ();
12013 else
12014 equate_type_number_to_die (TREE_TYPE (decl), type_die);
12016 else
12017 type = TREE_TYPE (decl);
12019 add_type_attribute (type_die, type, TREE_READONLY (decl),
12020 TREE_THIS_VOLATILE (decl), context_die);
12023 if (DECL_ABSTRACT (decl))
12024 equate_decl_number_to_die (decl, type_die);
12027 /* Generate a type description DIE. */
12029 static void
12030 gen_type_die (tree type, dw_die_ref context_die)
12032 int need_pop;
12034 if (type == NULL_TREE || type == error_mark_node)
12035 return;
12037 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12038 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
12040 if (TREE_ASM_WRITTEN (type))
12041 return;
12043 /* Prevent broken recursion; we can't hand off to the same type. */
12044 if (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) == type)
12045 abort ();
12047 TREE_ASM_WRITTEN (type) = 1;
12048 gen_decl_die (TYPE_NAME (type), context_die);
12049 return;
12052 /* We are going to output a DIE to represent the unqualified version
12053 of this type (i.e. without any const or volatile qualifiers) so
12054 get the main variant (i.e. the unqualified version) of this type
12055 now. (Vectors are special because the debugging info is in the
12056 cloned type itself). */
12057 if (TREE_CODE (type) != VECTOR_TYPE)
12058 type = type_main_variant (type);
12060 if (TREE_ASM_WRITTEN (type))
12061 return;
12063 switch (TREE_CODE (type))
12065 case ERROR_MARK:
12066 break;
12068 case POINTER_TYPE:
12069 case REFERENCE_TYPE:
12070 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
12071 ensures that the gen_type_die recursion will terminate even if the
12072 type is recursive. Recursive types are possible in Ada. */
12073 /* ??? We could perhaps do this for all types before the switch
12074 statement. */
12075 TREE_ASM_WRITTEN (type) = 1;
12077 /* For these types, all that is required is that we output a DIE (or a
12078 set of DIEs) to represent the "basis" type. */
12079 gen_type_die (TREE_TYPE (type), context_die);
12080 break;
12082 case OFFSET_TYPE:
12083 /* This code is used for C++ pointer-to-data-member types.
12084 Output a description of the relevant class type. */
12085 gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
12087 /* Output a description of the type of the object pointed to. */
12088 gen_type_die (TREE_TYPE (type), context_die);
12090 /* Now output a DIE to represent this pointer-to-data-member type
12091 itself. */
12092 gen_ptr_to_mbr_type_die (type, context_die);
12093 break;
12095 case SET_TYPE:
12096 gen_type_die (TYPE_DOMAIN (type), context_die);
12097 gen_set_type_die (type, context_die);
12098 break;
12100 case FILE_TYPE:
12101 gen_type_die (TREE_TYPE (type), context_die);
12102 abort (); /* No way to represent these in Dwarf yet! */
12103 break;
12105 case FUNCTION_TYPE:
12106 /* Force out return type (in case it wasn't forced out already). */
12107 gen_type_die (TREE_TYPE (type), context_die);
12108 gen_subroutine_type_die (type, context_die);
12109 break;
12111 case METHOD_TYPE:
12112 /* Force out return type (in case it wasn't forced out already). */
12113 gen_type_die (TREE_TYPE (type), context_die);
12114 gen_subroutine_type_die (type, context_die);
12115 break;
12117 case ARRAY_TYPE:
12118 if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
12120 gen_type_die (TREE_TYPE (type), context_die);
12121 gen_string_type_die (type, context_die);
12123 else
12124 gen_array_type_die (type, context_die);
12125 break;
12127 case VECTOR_TYPE:
12128 gen_array_type_die (type, context_die);
12129 break;
12131 case ENUMERAL_TYPE:
12132 case RECORD_TYPE:
12133 case UNION_TYPE:
12134 case QUAL_UNION_TYPE:
12135 /* If this is a nested type whose containing class hasn't been written
12136 out yet, writing it out will cover this one, too. This does not apply
12137 to instantiations of member class templates; they need to be added to
12138 the containing class as they are generated. FIXME: This hurts the
12139 idea of combining type decls from multiple TUs, since we can't predict
12140 what set of template instantiations we'll get. */
12141 if (TYPE_CONTEXT (type)
12142 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12143 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
12145 gen_type_die (TYPE_CONTEXT (type), context_die);
12147 if (TREE_ASM_WRITTEN (type))
12148 return;
12150 /* If that failed, attach ourselves to the stub. */
12151 push_decl_scope (TYPE_CONTEXT (type));
12152 context_die = lookup_type_die (TYPE_CONTEXT (type));
12153 need_pop = 1;
12155 else
12157 declare_in_namespace (type, context_die);
12158 need_pop = 0;
12161 if (TREE_CODE (type) == ENUMERAL_TYPE)
12162 gen_enumeration_type_die (type, context_die);
12163 else
12164 gen_struct_or_union_type_die (type, context_die);
12166 if (need_pop)
12167 pop_decl_scope ();
12169 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
12170 it up if it is ever completed. gen_*_type_die will set it for us
12171 when appropriate. */
12172 return;
12174 case VOID_TYPE:
12175 case INTEGER_TYPE:
12176 case REAL_TYPE:
12177 case COMPLEX_TYPE:
12178 case BOOLEAN_TYPE:
12179 case CHAR_TYPE:
12180 /* No DIEs needed for fundamental types. */
12181 break;
12183 case LANG_TYPE:
12184 /* No Dwarf representation currently defined. */
12185 break;
12187 default:
12188 abort ();
12191 TREE_ASM_WRITTEN (type) = 1;
12194 /* Generate a DIE for a tagged type instantiation. */
12196 static void
12197 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
12199 if (type == NULL_TREE || type == error_mark_node)
12200 return;
12202 /* We are going to output a DIE to represent the unqualified version of
12203 this type (i.e. without any const or volatile qualifiers) so make sure
12204 that we have the main variant (i.e. the unqualified version) of this
12205 type now. */
12206 if (type != type_main_variant (type))
12207 abort ();
12209 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
12210 an instance of an unresolved type. */
12212 switch (TREE_CODE (type))
12214 case ERROR_MARK:
12215 break;
12217 case ENUMERAL_TYPE:
12218 gen_inlined_enumeration_type_die (type, context_die);
12219 break;
12221 case RECORD_TYPE:
12222 gen_inlined_structure_type_die (type, context_die);
12223 break;
12225 case UNION_TYPE:
12226 case QUAL_UNION_TYPE:
12227 gen_inlined_union_type_die (type, context_die);
12228 break;
12230 default:
12231 abort ();
12235 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12236 things which are local to the given block. */
12238 static void
12239 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
12241 int must_output_die = 0;
12242 tree origin;
12243 tree decl;
12244 enum tree_code origin_code;
12246 /* Ignore blocks never really used to make RTL. */
12247 if (stmt == NULL_TREE || !TREE_USED (stmt)
12248 || (!TREE_ASM_WRITTEN (stmt) && !BLOCK_ABSTRACT (stmt)))
12249 return;
12251 /* If the block is one fragment of a non-contiguous block, do not
12252 process the variables, since they will have been done by the
12253 origin block. Do process subblocks. */
12254 if (BLOCK_FRAGMENT_ORIGIN (stmt))
12256 tree sub;
12258 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
12259 gen_block_die (sub, context_die, depth + 1);
12261 return;
12264 /* Determine the "ultimate origin" of this block. This block may be an
12265 inlined instance of an inlined instance of inline function, so we have
12266 to trace all of the way back through the origin chain to find out what
12267 sort of node actually served as the original seed for the creation of
12268 the current block. */
12269 origin = block_ultimate_origin (stmt);
12270 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
12272 /* Determine if we need to output any Dwarf DIEs at all to represent this
12273 block. */
12274 if (origin_code == FUNCTION_DECL)
12275 /* The outer scopes for inlinings *must* always be represented. We
12276 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
12277 must_output_die = 1;
12278 else
12280 /* In the case where the current block represents an inlining of the
12281 "body block" of an inline function, we must *NOT* output any DIE for
12282 this block because we have already output a DIE to represent the whole
12283 inlined function scope and the "body block" of any function doesn't
12284 really represent a different scope according to ANSI C rules. So we
12285 check here to make sure that this block does not represent a "body
12286 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
12287 if (! is_body_block (origin ? origin : stmt))
12289 /* Determine if this block directly contains any "significant"
12290 local declarations which we will need to output DIEs for. */
12291 if (debug_info_level > DINFO_LEVEL_TERSE)
12292 /* We are not in terse mode so *any* local declaration counts
12293 as being a "significant" one. */
12294 must_output_die = (BLOCK_VARS (stmt) != NULL);
12295 else
12296 /* We are in terse mode, so only local (nested) function
12297 definitions count as "significant" local declarations. */
12298 for (decl = BLOCK_VARS (stmt);
12299 decl != NULL; decl = TREE_CHAIN (decl))
12300 if (TREE_CODE (decl) == FUNCTION_DECL
12301 && DECL_INITIAL (decl))
12303 must_output_die = 1;
12304 break;
12309 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
12310 DIE for any block which contains no significant local declarations at
12311 all. Rather, in such cases we just call `decls_for_scope' so that any
12312 needed Dwarf info for any sub-blocks will get properly generated. Note
12313 that in terse mode, our definition of what constitutes a "significant"
12314 local declaration gets restricted to include only inlined function
12315 instances and local (nested) function definitions. */
12316 if (must_output_die)
12318 if (origin_code == FUNCTION_DECL)
12319 gen_inlined_subroutine_die (stmt, context_die, depth);
12320 else
12321 gen_lexical_block_die (stmt, context_die, depth);
12323 else
12324 decls_for_scope (stmt, context_die, depth);
12327 /* Generate all of the decls declared within a given scope and (recursively)
12328 all of its sub-blocks. */
12330 static void
12331 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
12333 tree decl;
12334 tree subblocks;
12336 /* Ignore blocks never really used to make RTL. */
12337 if (stmt == NULL_TREE || ! TREE_USED (stmt))
12338 return;
12340 /* Output the DIEs to represent all of the data objects and typedefs
12341 declared directly within this block but not within any nested
12342 sub-blocks. Also, nested function and tag DIEs have been
12343 generated with a parent of NULL; fix that up now. */
12344 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
12346 dw_die_ref die;
12348 if (TREE_CODE (decl) == FUNCTION_DECL)
12349 die = lookup_decl_die (decl);
12350 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
12351 die = lookup_type_die (TREE_TYPE (decl));
12352 else
12353 die = NULL;
12355 if (die != NULL && die->die_parent == NULL)
12356 add_child_die (context_die, die);
12357 else
12358 gen_decl_die (decl, context_die);
12361 /* If we're at -g1, we're not interested in subblocks. */
12362 if (debug_info_level <= DINFO_LEVEL_TERSE)
12363 return;
12365 /* Output the DIEs to represent all sub-blocks (and the items declared
12366 therein) of this block. */
12367 for (subblocks = BLOCK_SUBBLOCKS (stmt);
12368 subblocks != NULL;
12369 subblocks = BLOCK_CHAIN (subblocks))
12370 gen_block_die (subblocks, context_die, depth + 1);
12373 /* Is this a typedef we can avoid emitting? */
12375 static inline int
12376 is_redundant_typedef (tree decl)
12378 if (TYPE_DECL_IS_STUB (decl))
12379 return 1;
12381 if (DECL_ARTIFICIAL (decl)
12382 && DECL_CONTEXT (decl)
12383 && is_tagged_type (DECL_CONTEXT (decl))
12384 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
12385 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
12386 /* Also ignore the artificial member typedef for the class name. */
12387 return 1;
12389 return 0;
12392 /* Returns the DIE for decl or aborts. */
12394 static dw_die_ref
12395 force_decl_die (tree decl)
12397 dw_die_ref decl_die;
12398 unsigned saved_external_flag;
12399 tree save_fn = NULL_TREE;
12400 decl_die = lookup_decl_die (decl);
12401 if (!decl_die)
12403 dw_die_ref context_die;
12404 tree decl_context = DECL_CONTEXT (decl);
12405 if (decl_context)
12407 /* Find die that represents this context. */
12408 if (TYPE_P (decl_context))
12409 context_die = force_type_die (decl_context);
12410 else
12411 context_die = force_decl_die (decl_context);
12413 else
12414 context_die = comp_unit_die;
12416 switch (TREE_CODE (decl))
12418 case FUNCTION_DECL:
12419 /* Clear current_function_decl, so that gen_subprogram_die thinks
12420 that this is a declaration. At this point, we just want to force
12421 declaration die. */
12422 save_fn = current_function_decl;
12423 current_function_decl = NULL_TREE;
12424 gen_subprogram_die (decl, context_die);
12425 current_function_decl = save_fn;
12426 break;
12428 case VAR_DECL:
12429 /* Set external flag to force declaration die. Restore it after
12430 gen_decl_die() call. */
12431 saved_external_flag = DECL_EXTERNAL (decl);
12432 DECL_EXTERNAL (decl) = 1;
12433 gen_decl_die (decl, context_die);
12434 DECL_EXTERNAL (decl) = saved_external_flag;
12435 break;
12437 case NAMESPACE_DECL:
12438 dwarf2out_decl (decl);
12439 break;
12441 default:
12442 abort ();
12445 /* See if we can find the die for this deci now.
12446 If not then abort. */
12447 if (!decl_die)
12448 decl_die = lookup_decl_die (decl);
12449 if (!decl_die)
12450 abort ();
12453 return decl_die;
12456 /* Returns the DIE for decl or aborts. */
12458 static dw_die_ref
12459 force_type_die (tree type)
12461 dw_die_ref type_die;
12463 type_die = lookup_type_die (type);
12464 if (!type_die)
12466 dw_die_ref context_die;
12467 if (TYPE_CONTEXT (type))
12468 if (TYPE_P (TYPE_CONTEXT (type)))
12469 context_die = force_type_die (TYPE_CONTEXT (type));
12470 else
12471 context_die = force_decl_die (TYPE_CONTEXT (type));
12472 else
12473 context_die = comp_unit_die;
12475 gen_type_die (type, context_die);
12476 type_die = lookup_type_die (type);
12477 if (!type_die)
12478 abort();
12480 return type_die;
12483 /* Force out any required namespaces to be able to output DECL,
12484 and return the new context_die for it, if it's changed. */
12486 static dw_die_ref
12487 setup_namespace_context (tree thing, dw_die_ref context_die)
12489 tree context = DECL_P (thing) ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing);
12490 if (context && TREE_CODE (context) == NAMESPACE_DECL)
12491 /* Force out the namespace. */
12492 context_die = force_decl_die (context);
12494 return context_die;
12497 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
12498 type) within its namespace, if appropriate.
12500 For compatibility with older debuggers, namespace DIEs only contain
12501 declarations; all definitions are emitted at CU scope. */
12503 static void
12504 declare_in_namespace (tree thing, dw_die_ref context_die)
12506 dw_die_ref ns_context;
12508 if (debug_info_level <= DINFO_LEVEL_TERSE)
12509 return;
12511 ns_context = setup_namespace_context (thing, context_die);
12513 if (ns_context != context_die)
12515 if (DECL_P (thing))
12516 gen_decl_die (thing, ns_context);
12517 else
12518 gen_type_die (thing, ns_context);
12522 /* Generate a DIE for a namespace or namespace alias. */
12524 static void
12525 gen_namespace_die (tree decl)
12527 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
12529 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
12530 they are an alias of. */
12531 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
12533 /* Output a real namespace. */
12534 dw_die_ref namespace_die
12535 = new_die (DW_TAG_namespace, context_die, decl);
12536 add_name_and_src_coords_attributes (namespace_die, decl);
12537 equate_decl_number_to_die (decl, namespace_die);
12539 else
12541 /* Output a namespace alias. */
12543 /* Force out the namespace we are an alias of, if necessary. */
12544 dw_die_ref origin_die
12545 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
12547 /* Now create the namespace alias DIE. */
12548 dw_die_ref namespace_die
12549 = new_die (DW_TAG_imported_declaration, context_die, decl);
12550 add_name_and_src_coords_attributes (namespace_die, decl);
12551 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
12552 equate_decl_number_to_die (decl, namespace_die);
12556 /* Generate Dwarf debug information for a decl described by DECL. */
12558 static void
12559 gen_decl_die (tree decl, dw_die_ref context_die)
12561 tree origin;
12563 if (DECL_P (decl) && DECL_IGNORED_P (decl))
12564 return;
12566 switch (TREE_CODE (decl))
12568 case ERROR_MARK:
12569 break;
12571 case CONST_DECL:
12572 /* The individual enumerators of an enum type get output when we output
12573 the Dwarf representation of the relevant enum type itself. */
12574 break;
12576 case FUNCTION_DECL:
12577 /* Don't output any DIEs to represent mere function declarations,
12578 unless they are class members or explicit block externs. */
12579 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
12580 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
12581 break;
12583 #if 0
12584 /* FIXME */
12585 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
12586 on local redeclarations of global functions. That seems broken. */
12587 if (current_function_decl != decl)
12588 /* This is only a declaration. */;
12589 #endif
12591 /* If we're emitting a clone, emit info for the abstract instance. */
12592 if (DECL_ORIGIN (decl) != decl)
12593 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
12595 /* If we're emitting an out-of-line copy of an inline function,
12596 emit info for the abstract instance and set up to refer to it. */
12597 else if (cgraph_function_possibly_inlined_p (decl)
12598 && ! DECL_ABSTRACT (decl)
12599 && ! class_or_namespace_scope_p (context_die)
12600 /* dwarf2out_abstract_function won't emit a die if this is just
12601 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
12602 that case, because that works only if we have a die. */
12603 && DECL_INITIAL (decl) != NULL_TREE)
12605 dwarf2out_abstract_function (decl);
12606 set_decl_origin_self (decl);
12609 /* Otherwise we're emitting the primary DIE for this decl. */
12610 else if (debug_info_level > DINFO_LEVEL_TERSE)
12612 /* Before we describe the FUNCTION_DECL itself, make sure that we
12613 have described its return type. */
12614 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
12616 /* And its virtual context. */
12617 if (DECL_VINDEX (decl) != NULL_TREE)
12618 gen_type_die (DECL_CONTEXT (decl), context_die);
12620 /* And its containing type. */
12621 origin = decl_class_context (decl);
12622 if (origin != NULL_TREE)
12623 gen_type_die_for_member (origin, decl, context_die);
12625 /* And its containing namespace. */
12626 declare_in_namespace (decl, context_die);
12629 /* Now output a DIE to represent the function itself. */
12630 gen_subprogram_die (decl, context_die);
12631 break;
12633 case TYPE_DECL:
12634 /* If we are in terse mode, don't generate any DIEs to represent any
12635 actual typedefs. */
12636 if (debug_info_level <= DINFO_LEVEL_TERSE)
12637 break;
12639 /* In the special case of a TYPE_DECL node representing the declaration
12640 of some type tag, if the given TYPE_DECL is marked as having been
12641 instantiated from some other (original) TYPE_DECL node (e.g. one which
12642 was generated within the original definition of an inline function) we
12643 have to generate a special (abbreviated) DW_TAG_structure_type,
12644 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
12645 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
12647 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
12648 break;
12651 if (is_redundant_typedef (decl))
12652 gen_type_die (TREE_TYPE (decl), context_die);
12653 else
12654 /* Output a DIE to represent the typedef itself. */
12655 gen_typedef_die (decl, context_die);
12656 break;
12658 case LABEL_DECL:
12659 if (debug_info_level >= DINFO_LEVEL_NORMAL)
12660 gen_label_die (decl, context_die);
12661 break;
12663 case VAR_DECL:
12664 case RESULT_DECL:
12665 /* If we are in terse mode, don't generate any DIEs to represent any
12666 variable declarations or definitions. */
12667 if (debug_info_level <= DINFO_LEVEL_TERSE)
12668 break;
12670 /* Output any DIEs that are needed to specify the type of this data
12671 object. */
12672 gen_type_die (TREE_TYPE (decl), context_die);
12674 /* And its containing type. */
12675 origin = decl_class_context (decl);
12676 if (origin != NULL_TREE)
12677 gen_type_die_for_member (origin, decl, context_die);
12679 /* And its containing namespace. */
12680 declare_in_namespace (decl, context_die);
12682 /* Now output the DIE to represent the data object itself. This gets
12683 complicated because of the possibility that the VAR_DECL really
12684 represents an inlined instance of a formal parameter for an inline
12685 function. */
12686 origin = decl_ultimate_origin (decl);
12687 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
12688 gen_formal_parameter_die (decl, context_die);
12689 else
12690 gen_variable_die (decl, context_die);
12691 break;
12693 case FIELD_DECL:
12694 /* Ignore the nameless fields that are used to skip bits but handle C++
12695 anonymous unions and structs. */
12696 if (DECL_NAME (decl) != NULL_TREE
12697 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
12698 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
12700 gen_type_die (member_declared_type (decl), context_die);
12701 gen_field_die (decl, context_die);
12703 break;
12705 case PARM_DECL:
12706 gen_type_die (TREE_TYPE (decl), context_die);
12707 gen_formal_parameter_die (decl, context_die);
12708 break;
12710 case NAMESPACE_DECL:
12711 gen_namespace_die (decl);
12712 break;
12714 default:
12715 if ((int)TREE_CODE (decl) > NUM_TREE_CODES)
12716 /* Probably some frontend-internal decl. Assume we don't care. */
12717 break;
12718 abort ();
12722 /* Add Ada "use" clause information for SGI Workshop debugger. */
12724 void
12725 dwarf2out_add_library_unit_info (const char *filename, const char *context_list)
12727 unsigned int file_index;
12729 if (filename != NULL)
12731 dw_die_ref unit_die = new_die (DW_TAG_module, comp_unit_die, NULL);
12732 tree context_list_decl
12733 = build_decl (LABEL_DECL, get_identifier (context_list),
12734 void_type_node);
12736 TREE_PUBLIC (context_list_decl) = TRUE;
12737 add_name_attribute (unit_die, context_list);
12738 file_index = lookup_filename (filename);
12739 add_AT_unsigned (unit_die, DW_AT_decl_file, file_index);
12740 add_pubname (context_list_decl, unit_die);
12744 /* Output debug information for global decl DECL. Called from toplev.c after
12745 compilation proper has finished. */
12747 static void
12748 dwarf2out_global_decl (tree decl)
12750 /* Output DWARF2 information for file-scope tentative data object
12751 declarations, file-scope (extern) function declarations (which had no
12752 corresponding body) and file-scope tagged type declarations and
12753 definitions which have not yet been forced out. */
12754 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
12755 dwarf2out_decl (decl);
12758 /* Output debug information for type decl DECL. Called from toplev.c
12759 and from language front ends (to record built-in types). */
12760 static void
12761 dwarf2out_type_decl (tree decl, int local)
12763 if (!local)
12764 dwarf2out_decl (decl);
12767 /* Output debug information for imported module or decl. */
12769 static void
12770 dwarf2out_imported_module_or_decl (tree decl, tree context)
12772 dw_die_ref imported_die, at_import_die;
12773 dw_die_ref scope_die;
12774 unsigned file_index;
12776 if (debug_info_level <= DINFO_LEVEL_TERSE)
12777 return;
12779 if (!decl)
12780 abort ();
12782 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
12783 We need decl DIE for reference and scope die. First, get DIE for the decl
12784 itself. */
12786 /* Get the scope die for decl context. Use comp_unit_die for global module
12787 or decl. If die is not found for non globals, force new die. */
12788 if (!context)
12789 scope_die = comp_unit_die;
12790 else if (TYPE_P (context))
12791 scope_die = force_type_die (context);
12792 else
12793 scope_die = force_decl_die (context);
12795 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
12796 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
12797 at_import_die = force_type_die (TREE_TYPE (decl));
12798 else
12799 at_import_die = force_decl_die (decl);
12801 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
12802 if (TREE_CODE (decl) == NAMESPACE_DECL)
12803 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
12804 else
12805 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
12807 file_index = lookup_filename (input_filename);
12808 add_AT_unsigned (imported_die, DW_AT_decl_file, file_index);
12809 add_AT_unsigned (imported_die, DW_AT_decl_line, input_line);
12810 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
12813 /* Write the debugging output for DECL. */
12815 void
12816 dwarf2out_decl (tree decl)
12818 dw_die_ref context_die = comp_unit_die;
12820 switch (TREE_CODE (decl))
12822 case ERROR_MARK:
12823 return;
12825 case FUNCTION_DECL:
12826 /* What we would really like to do here is to filter out all mere
12827 file-scope declarations of file-scope functions which are never
12828 referenced later within this translation unit (and keep all of ones
12829 that *are* referenced later on) but we aren't clairvoyant, so we have
12830 no idea which functions will be referenced in the future (i.e. later
12831 on within the current translation unit). So here we just ignore all
12832 file-scope function declarations which are not also definitions. If
12833 and when the debugger needs to know something about these functions,
12834 it will have to hunt around and find the DWARF information associated
12835 with the definition of the function.
12837 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
12838 nodes represent definitions and which ones represent mere
12839 declarations. We have to check DECL_INITIAL instead. That's because
12840 the C front-end supports some weird semantics for "extern inline"
12841 function definitions. These can get inlined within the current
12842 translation unit (an thus, we need to generate Dwarf info for their
12843 abstract instances so that the Dwarf info for the concrete inlined
12844 instances can have something to refer to) but the compiler never
12845 generates any out-of-lines instances of such things (despite the fact
12846 that they *are* definitions).
12848 The important point is that the C front-end marks these "extern
12849 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
12850 them anyway. Note that the C++ front-end also plays some similar games
12851 for inline function definitions appearing within include files which
12852 also contain `#pragma interface' pragmas. */
12853 if (DECL_INITIAL (decl) == NULL_TREE)
12854 return;
12856 /* If we're a nested function, initially use a parent of NULL; if we're
12857 a plain function, this will be fixed up in decls_for_scope. If
12858 we're a method, it will be ignored, since we already have a DIE. */
12859 if (decl_function_context (decl)
12860 /* But if we're in terse mode, we don't care about scope. */
12861 && debug_info_level > DINFO_LEVEL_TERSE)
12862 context_die = NULL;
12863 break;
12865 case VAR_DECL:
12866 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
12867 declaration and if the declaration was never even referenced from
12868 within this entire compilation unit. We suppress these DIEs in
12869 order to save space in the .debug section (by eliminating entries
12870 which are probably useless). Note that we must not suppress
12871 block-local extern declarations (whether used or not) because that
12872 would screw-up the debugger's name lookup mechanism and cause it to
12873 miss things which really ought to be in scope at a given point. */
12874 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
12875 return;
12877 /* If we are in terse mode, don't generate any DIEs to represent any
12878 variable declarations or definitions. */
12879 if (debug_info_level <= DINFO_LEVEL_TERSE)
12880 return;
12881 break;
12883 case NAMESPACE_DECL:
12884 if (debug_info_level <= DINFO_LEVEL_TERSE)
12885 return;
12886 if (lookup_decl_die (decl) != NULL)
12887 return;
12888 break;
12890 case TYPE_DECL:
12891 /* Don't emit stubs for types unless they are needed by other DIEs. */
12892 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
12893 return;
12895 /* Don't bother trying to generate any DIEs to represent any of the
12896 normal built-in types for the language we are compiling. */
12897 if (DECL_SOURCE_LINE (decl) == 0)
12899 /* OK, we need to generate one for `bool' so GDB knows what type
12900 comparisons have. */
12901 if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
12902 == DW_LANG_C_plus_plus)
12903 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
12904 && ! DECL_IGNORED_P (decl))
12905 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
12907 return;
12910 /* If we are in terse mode, don't generate any DIEs for types. */
12911 if (debug_info_level <= DINFO_LEVEL_TERSE)
12912 return;
12914 /* If we're a function-scope tag, initially use a parent of NULL;
12915 this will be fixed up in decls_for_scope. */
12916 if (decl_function_context (decl))
12917 context_die = NULL;
12919 break;
12921 default:
12922 return;
12925 gen_decl_die (decl, context_die);
12928 /* Output a marker (i.e. a label) for the beginning of the generated code for
12929 a lexical block. */
12931 static void
12932 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
12933 unsigned int blocknum)
12935 function_section (current_function_decl);
12936 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
12939 /* Output a marker (i.e. a label) for the end of the generated code for a
12940 lexical block. */
12942 static void
12943 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
12945 function_section (current_function_decl);
12946 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
12949 /* Returns nonzero if it is appropriate not to emit any debugging
12950 information for BLOCK, because it doesn't contain any instructions.
12952 Don't allow this for blocks with nested functions or local classes
12953 as we would end up with orphans, and in the presence of scheduling
12954 we may end up calling them anyway. */
12956 static bool
12957 dwarf2out_ignore_block (tree block)
12959 tree decl;
12961 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
12962 if (TREE_CODE (decl) == FUNCTION_DECL
12963 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
12964 return 0;
12966 return 1;
12969 /* Lookup FILE_NAME (in the list of filenames that we know about here in
12970 dwarf2out.c) and return its "index". The index of each (known) filename is
12971 just a unique number which is associated with only that one filename. We
12972 need such numbers for the sake of generating labels (in the .debug_sfnames
12973 section) and references to those files numbers (in the .debug_srcinfo
12974 and.debug_macinfo sections). If the filename given as an argument is not
12975 found in our current list, add it to the list and assign it the next
12976 available unique index number. In order to speed up searches, we remember
12977 the index of the filename was looked up last. This handles the majority of
12978 all searches. */
12980 static unsigned
12981 lookup_filename (const char *file_name)
12983 size_t i, n;
12984 char *save_file_name;
12986 /* Check to see if the file name that was searched on the previous
12987 call matches this file name. If so, return the index. */
12988 if (file_table_last_lookup_index != 0)
12990 const char *last
12991 = VARRAY_CHAR_PTR (file_table, file_table_last_lookup_index);
12992 if (strcmp (file_name, last) == 0)
12993 return file_table_last_lookup_index;
12996 /* Didn't match the previous lookup, search the table */
12997 n = VARRAY_ACTIVE_SIZE (file_table);
12998 for (i = 1; i < n; i++)
12999 if (strcmp (file_name, VARRAY_CHAR_PTR (file_table, i)) == 0)
13001 file_table_last_lookup_index = i;
13002 return i;
13005 /* Add the new entry to the end of the filename table. */
13006 file_table_last_lookup_index = n;
13007 save_file_name = (char *) ggc_strdup (file_name);
13008 VARRAY_PUSH_CHAR_PTR (file_table, save_file_name);
13009 VARRAY_PUSH_UINT (file_table_emitted, 0);
13011 return i;
13014 static int
13015 maybe_emit_file (int fileno)
13017 if (DWARF2_ASM_LINE_DEBUG_INFO && fileno > 0)
13019 if (!VARRAY_UINT (file_table_emitted, fileno))
13021 VARRAY_UINT (file_table_emitted, fileno) = ++emitcount;
13022 fprintf (asm_out_file, "\t.file %u ",
13023 VARRAY_UINT (file_table_emitted, fileno));
13024 output_quoted_string (asm_out_file,
13025 VARRAY_CHAR_PTR (file_table, fileno));
13026 fputc ('\n', asm_out_file);
13028 return VARRAY_UINT (file_table_emitted, fileno);
13030 else
13031 return fileno;
13034 static void
13035 init_file_table (void)
13037 /* Allocate the initial hunk of the file_table. */
13038 VARRAY_CHAR_PTR_INIT (file_table, 64, "file_table");
13039 VARRAY_UINT_INIT (file_table_emitted, 64, "file_table_emitted");
13041 /* Skip the first entry - file numbers begin at 1. */
13042 VARRAY_PUSH_CHAR_PTR (file_table, NULL);
13043 VARRAY_PUSH_UINT (file_table_emitted, 0);
13044 file_table_last_lookup_index = 0;
13047 /* Called by the final INSN scan whenever we see a var location. We
13048 use it to drop labels in the right places, and throw the location in
13049 our lookup table. */
13051 static void
13052 dwarf2out_var_location (rtx loc_note)
13054 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13055 struct var_loc_node *newloc;
13056 rtx prev_insn;
13057 static rtx last_insn;
13058 static const char *last_label;
13060 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13061 return;
13062 prev_insn = PREV_INSN (loc_note);
13064 newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13065 /* If the insn we processed last time is the previous insn
13066 and it is also a var location note, use the label we emitted
13067 last time. */
13068 if (last_insn != NULL_RTX
13069 && last_insn == prev_insn
13070 && GET_CODE (prev_insn) == NOTE
13071 && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13073 newloc->label = last_label;
13075 else
13077 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13078 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13079 loclabel_num++;
13080 newloc->label = ggc_strdup (loclabel);
13082 newloc->var_loc_note = loc_note;
13083 newloc->next = NULL;
13085 last_insn = loc_note;
13086 last_label = newloc->label;
13088 add_var_loc_to_decl (NOTE_VAR_LOCATION_DECL (loc_note), newloc);
13091 /* We need to reset the locations at the beginning of each
13092 function. We can't do this in the end_function hook, because the
13093 declarations that use the locations won't have been outputted when
13094 that hook is called. */
13096 static void
13097 dwarf2out_begin_function (tree unused ATTRIBUTE_UNUSED)
13099 htab_empty (decl_loc_table);
13102 /* Output a label to mark the beginning of a source code line entry
13103 and record information relating to this source line, in
13104 'line_info_table' for later output of the .debug_line section. */
13106 static void
13107 dwarf2out_source_line (unsigned int line, const char *filename)
13109 if (debug_info_level >= DINFO_LEVEL_NORMAL
13110 && line != 0)
13112 function_section (current_function_decl);
13114 /* If requested, emit something human-readable. */
13115 if (flag_debug_asm)
13116 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13117 filename, line);
13119 if (DWARF2_ASM_LINE_DEBUG_INFO)
13121 unsigned file_num = lookup_filename (filename);
13123 file_num = maybe_emit_file (file_num);
13125 /* Emit the .loc directive understood by GNU as. */
13126 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
13128 /* Indicate that line number info exists. */
13129 line_info_table_in_use++;
13131 /* Indicate that multiple line number tables exist. */
13132 if (DECL_SECTION_NAME (current_function_decl))
13133 separate_line_info_table_in_use++;
13135 else if (DECL_SECTION_NAME (current_function_decl))
13137 dw_separate_line_info_ref line_info;
13138 targetm.asm_out.internal_label (asm_out_file, SEPARATE_LINE_CODE_LABEL,
13139 separate_line_info_table_in_use);
13141 /* Expand the line info table if necessary. */
13142 if (separate_line_info_table_in_use
13143 == separate_line_info_table_allocated)
13145 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13146 separate_line_info_table
13147 = ggc_realloc (separate_line_info_table,
13148 separate_line_info_table_allocated
13149 * sizeof (dw_separate_line_info_entry));
13150 memset (separate_line_info_table
13151 + separate_line_info_table_in_use,
13153 (LINE_INFO_TABLE_INCREMENT
13154 * sizeof (dw_separate_line_info_entry)));
13157 /* Add the new entry at the end of the line_info_table. */
13158 line_info
13159 = &separate_line_info_table[separate_line_info_table_in_use++];
13160 line_info->dw_file_num = lookup_filename (filename);
13161 line_info->dw_line_num = line;
13162 line_info->function = current_function_funcdef_no;
13164 else
13166 dw_line_info_ref line_info;
13168 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
13169 line_info_table_in_use);
13171 /* Expand the line info table if necessary. */
13172 if (line_info_table_in_use == line_info_table_allocated)
13174 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13175 line_info_table
13176 = ggc_realloc (line_info_table,
13177 (line_info_table_allocated
13178 * sizeof (dw_line_info_entry)));
13179 memset (line_info_table + line_info_table_in_use, 0,
13180 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
13183 /* Add the new entry at the end of the line_info_table. */
13184 line_info = &line_info_table[line_info_table_in_use++];
13185 line_info->dw_file_num = lookup_filename (filename);
13186 line_info->dw_line_num = line;
13191 /* Record the beginning of a new source file. */
13193 static void
13194 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
13196 if (flag_eliminate_dwarf2_dups)
13198 /* Record the beginning of the file for break_out_includes. */
13199 dw_die_ref bincl_die;
13201 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
13202 add_AT_string (bincl_die, DW_AT_name, filename);
13205 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13207 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13208 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
13209 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13210 lineno);
13211 maybe_emit_file (lookup_filename (filename));
13212 dw2_asm_output_data_uleb128 (lookup_filename (filename),
13213 "Filename we just started");
13217 /* Record the end of a source file. */
13219 static void
13220 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
13222 if (flag_eliminate_dwarf2_dups)
13223 /* Record the end of the file for break_out_includes. */
13224 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
13226 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13228 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13229 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13233 /* Called from debug_define in toplev.c. The `buffer' parameter contains
13234 the tail part of the directive line, i.e. the part which is past the
13235 initial whitespace, #, whitespace, directive-name, whitespace part. */
13237 static void
13238 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13239 const char *buffer ATTRIBUTE_UNUSED)
13241 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13243 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13244 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
13245 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13246 dw2_asm_output_nstring (buffer, -1, "The macro");
13250 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
13251 the tail part of the directive line, i.e. the part which is past the
13252 initial whitespace, #, whitespace, directive-name, whitespace part. */
13254 static void
13255 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13256 const char *buffer ATTRIBUTE_UNUSED)
13258 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13260 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13261 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
13262 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13263 dw2_asm_output_nstring (buffer, -1, "The macro");
13267 /* Set up for Dwarf output at the start of compilation. */
13269 static void
13270 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
13272 init_file_table ();
13274 /* Allocate the decl_die_table. */
13275 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13276 decl_die_table_eq, NULL);
13278 /* Allocate the decl_loc_table. */
13279 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13280 decl_loc_table_eq, NULL);
13282 /* Allocate the initial hunk of the decl_scope_table. */
13283 VARRAY_TREE_INIT (decl_scope_table, 256, "decl_scope_table");
13285 /* Allocate the initial hunk of the abbrev_die_table. */
13286 abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
13287 * sizeof (dw_die_ref));
13288 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
13289 /* Zero-th entry is allocated, but unused */
13290 abbrev_die_table_in_use = 1;
13292 /* Allocate the initial hunk of the line_info_table. */
13293 line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
13294 * sizeof (dw_line_info_entry));
13295 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
13297 /* Zero-th entry is allocated, but unused */
13298 line_info_table_in_use = 1;
13300 /* Generate the initial DIE for the .debug section. Note that the (string)
13301 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
13302 will (typically) be a relative pathname and that this pathname should be
13303 taken as being relative to the directory from which the compiler was
13304 invoked when the given (base) source file was compiled. We will fill
13305 in this value in dwarf2out_finish. */
13306 comp_unit_die = gen_compile_unit_die (NULL);
13308 VARRAY_TREE_INIT (incomplete_types, 64, "incomplete_types");
13310 VARRAY_RTX_INIT (used_rtx_varray, 32, "used_rtx_varray");
13312 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
13313 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
13314 DEBUG_ABBREV_SECTION_LABEL, 0);
13315 if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
13316 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
13317 else
13318 strcpy (text_section_label, stripattributes (TEXT_SECTION_NAME));
13320 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
13321 DEBUG_INFO_SECTION_LABEL, 0);
13322 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
13323 DEBUG_LINE_SECTION_LABEL, 0);
13324 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
13325 DEBUG_RANGES_SECTION_LABEL, 0);
13326 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13327 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
13328 named_section_flags (DEBUG_INFO_SECTION, SECTION_DEBUG);
13329 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
13330 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13331 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
13333 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13335 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13336 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
13337 DEBUG_MACINFO_SECTION_LABEL, 0);
13338 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
13341 if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
13343 text_section ();
13344 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
13348 /* A helper function for dwarf2out_finish called through
13349 ht_forall. Emit one queued .debug_str string. */
13351 static int
13352 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
13354 struct indirect_string_node *node = (struct indirect_string_node *) *h;
13356 if (node->form == DW_FORM_strp)
13358 named_section_flags (DEBUG_STR_SECTION, DEBUG_STR_SECTION_FLAGS);
13359 ASM_OUTPUT_LABEL (asm_out_file, node->label);
13360 assemble_string (node->str, strlen (node->str) + 1);
13363 return 1;
13368 /* Clear the marks for a die and its children.
13369 Be cool if the mark isn't set. */
13371 static void
13372 prune_unmark_dies (dw_die_ref die)
13374 dw_die_ref c;
13375 die->die_mark = 0;
13376 for (c = die->die_child; c; c = c->die_sib)
13377 prune_unmark_dies (c);
13381 /* Given DIE that we're marking as used, find any other dies
13382 it references as attributes and mark them as used. */
13384 static void
13385 prune_unused_types_walk_attribs (dw_die_ref die)
13387 dw_attr_ref a;
13389 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
13391 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
13393 /* A reference to another DIE.
13394 Make sure that it will get emitted. */
13395 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
13397 else if (a->dw_attr == DW_AT_decl_file)
13399 /* A reference to a file. Make sure the file name is emitted. */
13400 a->dw_attr_val.v.val_unsigned =
13401 maybe_emit_file (a->dw_attr_val.v.val_unsigned);
13407 /* Mark DIE as being used. If DOKIDS is true, then walk down
13408 to DIE's children. */
13410 static void
13411 prune_unused_types_mark (dw_die_ref die, int dokids)
13413 dw_die_ref c;
13415 if (die->die_mark == 0)
13417 /* We haven't done this node yet. Mark it as used. */
13418 die->die_mark = 1;
13420 /* We also have to mark its parents as used.
13421 (But we don't want to mark our parents' kids due to this.) */
13422 if (die->die_parent)
13423 prune_unused_types_mark (die->die_parent, 0);
13425 /* Mark any referenced nodes. */
13426 prune_unused_types_walk_attribs (die);
13428 /* If this node is a specification,
13429 also mark the definition, if it exists. */
13430 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
13431 prune_unused_types_mark (die->die_definition, 1);
13434 if (dokids && die->die_mark != 2)
13436 /* We need to walk the children, but haven't done so yet.
13437 Remember that we've walked the kids. */
13438 die->die_mark = 2;
13440 /* Walk them. */
13441 for (c = die->die_child; c; c = c->die_sib)
13443 /* If this is an array type, we need to make sure our
13444 kids get marked, even if they're types. */
13445 if (die->die_tag == DW_TAG_array_type)
13446 prune_unused_types_mark (c, 1);
13447 else
13448 prune_unused_types_walk (c);
13454 /* Walk the tree DIE and mark types that we actually use. */
13456 static void
13457 prune_unused_types_walk (dw_die_ref die)
13459 dw_die_ref c;
13461 /* Don't do anything if this node is already marked. */
13462 if (die->die_mark)
13463 return;
13465 switch (die->die_tag) {
13466 case DW_TAG_const_type:
13467 case DW_TAG_packed_type:
13468 case DW_TAG_pointer_type:
13469 case DW_TAG_reference_type:
13470 case DW_TAG_volatile_type:
13471 case DW_TAG_typedef:
13472 case DW_TAG_array_type:
13473 case DW_TAG_structure_type:
13474 case DW_TAG_union_type:
13475 case DW_TAG_class_type:
13476 case DW_TAG_friend:
13477 case DW_TAG_variant_part:
13478 case DW_TAG_enumeration_type:
13479 case DW_TAG_subroutine_type:
13480 case DW_TAG_string_type:
13481 case DW_TAG_set_type:
13482 case DW_TAG_subrange_type:
13483 case DW_TAG_ptr_to_member_type:
13484 case DW_TAG_file_type:
13485 /* It's a type node --- don't mark it. */
13486 return;
13488 default:
13489 /* Mark everything else. */
13490 break;
13493 die->die_mark = 1;
13495 /* Now, mark any dies referenced from here. */
13496 prune_unused_types_walk_attribs (die);
13498 /* Mark children. */
13499 for (c = die->die_child; c; c = c->die_sib)
13500 prune_unused_types_walk (c);
13504 /* Remove from the tree DIE any dies that aren't marked. */
13506 static void
13507 prune_unused_types_prune (dw_die_ref die)
13509 dw_die_ref c, p, n;
13510 if (!die->die_mark)
13511 abort();
13513 p = NULL;
13514 for (c = die->die_child; c; c = n)
13516 n = c->die_sib;
13517 if (c->die_mark)
13519 prune_unused_types_prune (c);
13520 p = c;
13522 else
13524 if (p)
13525 p->die_sib = n;
13526 else
13527 die->die_child = n;
13528 free_die (c);
13534 /* Remove dies representing declarations that we never use. */
13536 static void
13537 prune_unused_types (void)
13539 unsigned int i;
13540 limbo_die_node *node;
13542 /* Clear all the marks. */
13543 prune_unmark_dies (comp_unit_die);
13544 for (node = limbo_die_list; node; node = node->next)
13545 prune_unmark_dies (node->die);
13547 /* Set the mark on nodes that are actually used. */
13548 prune_unused_types_walk (comp_unit_die);
13549 for (node = limbo_die_list; node; node = node->next)
13550 prune_unused_types_walk (node->die);
13552 /* Also set the mark on nodes referenced from the
13553 pubname_table or arange_table. */
13554 for (i = 0; i < pubname_table_in_use; i++)
13555 prune_unused_types_mark (pubname_table[i].die, 1);
13556 for (i = 0; i < arange_table_in_use; i++)
13557 prune_unused_types_mark (arange_table[i], 1);
13559 /* Get rid of nodes that aren't marked. */
13560 prune_unused_types_prune (comp_unit_die);
13561 for (node = limbo_die_list; node; node = node->next)
13562 prune_unused_types_prune (node->die);
13564 /* Leave the marks clear. */
13565 prune_unmark_dies (comp_unit_die);
13566 for (node = limbo_die_list; node; node = node->next)
13567 prune_unmark_dies (node->die);
13570 /* Output stuff that dwarf requires at the end of every file,
13571 and generate the DWARF-2 debugging info. */
13573 static void
13574 dwarf2out_finish (const char *filename)
13576 limbo_die_node *node, *next_node;
13577 dw_die_ref die = 0;
13579 /* Add the name for the main input file now. We delayed this from
13580 dwarf2out_init to avoid complications with PCH. */
13581 add_name_attribute (comp_unit_die, filename);
13582 if (filename[0] != DIR_SEPARATOR)
13583 add_comp_dir_attribute (comp_unit_die);
13584 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
13586 size_t i;
13587 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
13588 if (VARRAY_CHAR_PTR (file_table, i)[0] != DIR_SEPARATOR
13589 /* Don't add cwd for <built-in>. */
13590 && VARRAY_CHAR_PTR (file_table, i)[0] != '<')
13592 add_comp_dir_attribute (comp_unit_die);
13593 break;
13597 /* Traverse the limbo die list, and add parent/child links. The only
13598 dies without parents that should be here are concrete instances of
13599 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
13600 For concrete instances, we can get the parent die from the abstract
13601 instance. */
13602 for (node = limbo_die_list; node; node = next_node)
13604 next_node = node->next;
13605 die = node->die;
13607 if (die->die_parent == NULL)
13609 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
13610 tree context;
13612 if (origin)
13613 add_child_die (origin->die_parent, die);
13614 else if (die == comp_unit_die)
13616 /* If this was an expression for a bound involved in a function
13617 return type, it may be a SAVE_EXPR for which we weren't able
13618 to find a DIE previously. So try now. */
13619 else if (node->created_for
13620 && TREE_CODE (node->created_for) == SAVE_EXPR
13621 && 0 != (origin = (lookup_decl_die
13622 (SAVE_EXPR_CONTEXT
13623 (node->created_for)))))
13624 add_child_die (origin, die);
13625 else if (errorcount > 0 || sorrycount > 0)
13626 /* It's OK to be confused by errors in the input. */
13627 add_child_die (comp_unit_die, die);
13628 else if (node->created_for
13629 && ((DECL_P (node->created_for)
13630 && (context = DECL_CONTEXT (node->created_for)))
13631 || (TYPE_P (node->created_for)
13632 && (context = TYPE_CONTEXT (node->created_for))))
13633 && TREE_CODE (context) == FUNCTION_DECL)
13635 /* In certain situations, the lexical block containing a
13636 nested function can be optimized away, which results
13637 in the nested function die being orphaned. Likewise
13638 with the return type of that nested function. Force
13639 this to be a child of the containing function. */
13640 origin = lookup_decl_die (context);
13641 if (! origin)
13642 abort ();
13643 add_child_die (origin, die);
13645 else
13646 abort ();
13650 limbo_die_list = NULL;
13652 /* Walk through the list of incomplete types again, trying once more to
13653 emit full debugging info for them. */
13654 retry_incomplete_types ();
13656 /* We need to reverse all the dies before break_out_includes, or
13657 we'll see the end of an include file before the beginning. */
13658 reverse_all_dies (comp_unit_die);
13660 if (flag_eliminate_unused_debug_types)
13661 prune_unused_types ();
13663 /* Generate separate CUs for each of the include files we've seen.
13664 They will go into limbo_die_list. */
13665 if (flag_eliminate_dwarf2_dups)
13666 break_out_includes (comp_unit_die);
13668 /* Traverse the DIE's and add add sibling attributes to those DIE's
13669 that have children. */
13670 add_sibling_attributes (comp_unit_die);
13671 for (node = limbo_die_list; node; node = node->next)
13672 add_sibling_attributes (node->die);
13674 /* Output a terminator label for the .text section. */
13675 text_section ();
13676 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
13678 /* Output the source line correspondence table. We must do this
13679 even if there is no line information. Otherwise, on an empty
13680 translation unit, we will generate a present, but empty,
13681 .debug_info section. IRIX 6.5 `nm' will then complain when
13682 examining the file. */
13683 if (! DWARF2_ASM_LINE_DEBUG_INFO)
13685 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13686 output_line_info ();
13689 /* Output location list section if necessary. */
13690 if (have_location_lists)
13692 /* Output the location lists info. */
13693 named_section_flags (DEBUG_LOC_SECTION, SECTION_DEBUG);
13694 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
13695 DEBUG_LOC_SECTION_LABEL, 0);
13696 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
13697 output_location_lists (die);
13698 have_location_lists = 0;
13701 /* We can only use the low/high_pc attributes if all of the code was
13702 in .text. */
13703 if (separate_line_info_table_in_use == 0)
13705 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
13706 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
13709 /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
13710 "base address". Use zero so that these addresses become absolute. */
13711 else if (have_location_lists || ranges_table_in_use)
13712 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
13714 if (debug_info_level >= DINFO_LEVEL_NORMAL)
13715 add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
13716 debug_line_section_label);
13718 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13719 add_AT_lbl_offset (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
13721 /* Output all of the compilation units. We put the main one last so that
13722 the offsets are available to output_pubnames. */
13723 for (node = limbo_die_list; node; node = node->next)
13724 output_comp_unit (node->die, 0);
13726 output_comp_unit (comp_unit_die, 0);
13728 /* Output the abbreviation table. */
13729 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13730 output_abbrev_section ();
13732 /* Output public names table if necessary. */
13733 if (pubname_table_in_use)
13735 named_section_flags (DEBUG_PUBNAMES_SECTION, SECTION_DEBUG);
13736 output_pubnames ();
13739 /* Output the address range information. We only put functions in the arange
13740 table, so don't write it out if we don't have any. */
13741 if (fde_table_in_use)
13743 named_section_flags (DEBUG_ARANGES_SECTION, SECTION_DEBUG);
13744 output_aranges ();
13747 /* Output ranges section if necessary. */
13748 if (ranges_table_in_use)
13750 named_section_flags (DEBUG_RANGES_SECTION, SECTION_DEBUG);
13751 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
13752 output_ranges ();
13755 /* Have to end the primary source file. */
13756 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13758 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13759 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13760 dw2_asm_output_data (1, 0, "End compilation unit");
13763 /* If we emitted any DW_FORM_strp form attribute, output the string
13764 table too. */
13765 if (debug_str_hash)
13766 htab_traverse (debug_str_hash, output_indirect_string, NULL);
13768 #else
13770 /* This should never be used, but its address is needed for comparisons. */
13771 const struct gcc_debug_hooks dwarf2_debug_hooks;
13773 #endif /* DWARF2_DEBUGGING_INFO */
13775 #include "gt-dwarf2out.h"