* decl.c: Remove calls to add_decl_expr, pushdecl, rest_of_compilation,
[official-gcc.git] / gcc / dwarf2out.c
blob93afca759ede5406d09108b57ef5de6febb5eedf
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 (REG_P (XEXP (src, 0))
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 (REG_P (XEXP (src, 0))
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 (REG_P (XEXP (src, 0))
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 (!REG_P (XEXP (src, 0))
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 (!REG_P (src))
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 (!REG_P (x))
1655 x = XEXP (x, 0);
1656 if (!REG_P (x))
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");
2086 if (DW_CIE_VERSION == 1)
2087 dw2_asm_output_data (1, DWARF_FRAME_RETURN_COLUMN, "CIE RA Column");
2088 else
2089 dw2_asm_output_data_uleb128 (DWARF_FRAME_RETURN_COLUMN, "CIE RA Column");
2091 if (augmentation[0])
2093 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2094 if (eh_personality_libfunc)
2096 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2097 eh_data_format_name (per_encoding));
2098 dw2_asm_output_encoded_addr_rtx (per_encoding,
2099 eh_personality_libfunc, NULL);
2102 if (any_lsda_needed)
2103 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2104 eh_data_format_name (lsda_encoding));
2106 if (fde_encoding != DW_EH_PE_absptr)
2107 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2108 eh_data_format_name (fde_encoding));
2111 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2112 output_cfi (cfi, NULL, for_eh);
2114 /* Pad the CIE out to an address sized boundary. */
2115 ASM_OUTPUT_ALIGN (asm_out_file,
2116 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2117 ASM_OUTPUT_LABEL (asm_out_file, l2);
2119 /* Loop through all of the FDE's. */
2120 for (i = 0; i < fde_table_in_use; i++)
2122 fde = &fde_table[i];
2124 /* Don't emit EH unwind info for leaf functions that don't need it. */
2125 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2126 && (fde->nothrow || fde->all_throwers_are_sibcalls)
2127 && (! TARGET_USES_WEAK_UNWIND_INFO || ! DECL_ONE_ONLY (fde->decl))
2128 && !fde->uses_eh_lsda)
2129 continue;
2131 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2132 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2133 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2134 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2135 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2136 "FDE Length");
2137 ASM_OUTPUT_LABEL (asm_out_file, l1);
2139 if (for_eh)
2140 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2141 else
2142 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2143 "FDE CIE offset");
2145 if (for_eh)
2147 if (TARGET_USES_WEAK_UNWIND_INFO
2148 && DECL_ONE_ONLY (fde->decl))
2149 dw2_asm_output_encoded_addr_rtx (fde_encoding,
2150 gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER
2151 (DECL_ASSEMBLER_NAME (fde->decl))),
2152 "FDE initial location");
2153 else
2154 dw2_asm_output_encoded_addr_rtx (fde_encoding,
2155 gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin),
2156 "FDE initial location");
2157 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2158 fde->dw_fde_end, fde->dw_fde_begin,
2159 "FDE address range");
2161 else
2163 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2164 "FDE initial location");
2165 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2166 fde->dw_fde_end, fde->dw_fde_begin,
2167 "FDE address range");
2170 if (augmentation[0])
2172 if (any_lsda_needed)
2174 int size = size_of_encoded_value (lsda_encoding);
2176 if (lsda_encoding == DW_EH_PE_aligned)
2178 int offset = ( 4 /* Length */
2179 + 4 /* CIE offset */
2180 + 2 * size_of_encoded_value (fde_encoding)
2181 + 1 /* Augmentation size */ );
2182 int pad = -offset & (PTR_SIZE - 1);
2184 size += pad;
2185 if (size_of_uleb128 (size) != 1)
2186 abort ();
2189 dw2_asm_output_data_uleb128 (size, "Augmentation size");
2191 if (fde->uses_eh_lsda)
2193 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2194 fde->funcdef_number);
2195 dw2_asm_output_encoded_addr_rtx (
2196 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2197 "Language Specific Data Area");
2199 else
2201 if (lsda_encoding == DW_EH_PE_aligned)
2202 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2203 dw2_asm_output_data
2204 (size_of_encoded_value (lsda_encoding), 0,
2205 "Language Specific Data Area (none)");
2208 else
2209 dw2_asm_output_data_uleb128 (0, "Augmentation size");
2212 /* Loop through the Call Frame Instructions associated with
2213 this FDE. */
2214 fde->dw_fde_current_label = fde->dw_fde_begin;
2215 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2216 output_cfi (cfi, fde, for_eh);
2218 /* Pad the FDE out to an address sized boundary. */
2219 ASM_OUTPUT_ALIGN (asm_out_file,
2220 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2221 ASM_OUTPUT_LABEL (asm_out_file, l2);
2224 if (for_eh && targetm.terminate_dw2_eh_frame_info)
2225 dw2_asm_output_data (4, 0, "End of Table");
2226 #ifdef MIPS_DEBUGGING_INFO
2227 /* Work around Irix 6 assembler bug whereby labels at the end of a section
2228 get a value of 0. Putting .align 0 after the label fixes it. */
2229 ASM_OUTPUT_ALIGN (asm_out_file, 0);
2230 #endif
2232 /* Turn off app to make assembly quicker. */
2233 if (flag_debug_asm)
2234 app_disable ();
2237 /* Output a marker (i.e. a label) for the beginning of a function, before
2238 the prologue. */
2240 void
2241 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2242 const char *file ATTRIBUTE_UNUSED)
2244 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2245 dw_fde_ref fde;
2247 current_function_func_begin_label = 0;
2249 #ifdef IA64_UNWIND_INFO
2250 /* ??? current_function_func_begin_label is also used by except.c
2251 for call-site information. We must emit this label if it might
2252 be used. */
2253 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2254 && ! dwarf2out_do_frame ())
2255 return;
2256 #else
2257 if (! dwarf2out_do_frame ())
2258 return;
2259 #endif
2261 function_section (current_function_decl);
2262 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2263 current_function_funcdef_no);
2264 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2265 current_function_funcdef_no);
2266 current_function_func_begin_label = get_identifier (label);
2268 #ifdef IA64_UNWIND_INFO
2269 /* We can elide the fde allocation if we're not emitting debug info. */
2270 if (! dwarf2out_do_frame ())
2271 return;
2272 #endif
2274 /* Expand the fde table if necessary. */
2275 if (fde_table_in_use == fde_table_allocated)
2277 fde_table_allocated += FDE_TABLE_INCREMENT;
2278 fde_table = ggc_realloc (fde_table,
2279 fde_table_allocated * sizeof (dw_fde_node));
2280 memset (fde_table + fde_table_in_use, 0,
2281 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2284 /* Record the FDE associated with this function. */
2285 current_funcdef_fde = fde_table_in_use;
2287 /* Add the new FDE at the end of the fde_table. */
2288 fde = &fde_table[fde_table_in_use++];
2289 fde->decl = current_function_decl;
2290 fde->dw_fde_begin = xstrdup (label);
2291 fde->dw_fde_current_label = NULL;
2292 fde->dw_fde_end = NULL;
2293 fde->dw_fde_cfi = NULL;
2294 fde->funcdef_number = current_function_funcdef_no;
2295 fde->nothrow = current_function_nothrow;
2296 fde->uses_eh_lsda = cfun->uses_eh_lsda;
2297 fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2299 args_size = old_args_size = 0;
2301 /* We only want to output line number information for the genuine dwarf2
2302 prologue case, not the eh frame case. */
2303 #ifdef DWARF2_DEBUGGING_INFO
2304 if (file)
2305 dwarf2out_source_line (line, file);
2306 #endif
2309 /* Output a marker (i.e. a label) for the absolute end of the generated code
2310 for a function definition. This gets called *after* the epilogue code has
2311 been generated. */
2313 void
2314 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2315 const char *file ATTRIBUTE_UNUSED)
2317 dw_fde_ref fde;
2318 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2320 /* Output a label to mark the endpoint of the code generated for this
2321 function. */
2322 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2323 current_function_funcdef_no);
2324 ASM_OUTPUT_LABEL (asm_out_file, label);
2325 fde = &fde_table[fde_table_in_use - 1];
2326 fde->dw_fde_end = xstrdup (label);
2329 void
2330 dwarf2out_frame_init (void)
2332 /* Allocate the initial hunk of the fde_table. */
2333 fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2334 fde_table_allocated = FDE_TABLE_INCREMENT;
2335 fde_table_in_use = 0;
2337 /* Generate the CFA instructions common to all FDE's. Do it now for the
2338 sake of lookup_cfa. */
2340 #ifdef DWARF2_UNWIND_INFO
2341 /* On entry, the Canonical Frame Address is at SP. */
2342 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2343 initial_return_save (INCOMING_RETURN_ADDR_RTX);
2344 #endif
2347 void
2348 dwarf2out_frame_finish (void)
2350 /* Output call frame information. */
2351 if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
2352 output_call_frame_info (0);
2354 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2355 output_call_frame_info (1);
2357 #endif
2359 /* And now, the subset of the debugging information support code necessary
2360 for emitting location expressions. */
2362 /* We need some way to distinguish DW_OP_addr with a direct symbol
2363 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
2364 #define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
2367 typedef struct dw_val_struct *dw_val_ref;
2368 typedef struct die_struct *dw_die_ref;
2369 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2370 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2372 /* Each DIE may have a series of attribute/value pairs. Values
2373 can take on several forms. The forms that are used in this
2374 implementation are listed below. */
2376 enum dw_val_class
2378 dw_val_class_addr,
2379 dw_val_class_offset,
2380 dw_val_class_loc,
2381 dw_val_class_loc_list,
2382 dw_val_class_range_list,
2383 dw_val_class_const,
2384 dw_val_class_unsigned_const,
2385 dw_val_class_long_long,
2386 dw_val_class_vec,
2387 dw_val_class_flag,
2388 dw_val_class_die_ref,
2389 dw_val_class_fde_ref,
2390 dw_val_class_lbl_id,
2391 dw_val_class_lbl_offset,
2392 dw_val_class_str
2395 /* Describe a double word constant value. */
2396 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
2398 typedef struct dw_long_long_struct GTY(())
2400 unsigned long hi;
2401 unsigned long low;
2403 dw_long_long_const;
2405 /* Describe a floating point constant value, or a vector constant value. */
2407 typedef struct dw_vec_struct GTY(())
2409 unsigned char * GTY((length ("%h.length"))) array;
2410 unsigned length;
2411 unsigned elt_size;
2413 dw_vec_const;
2415 /* The dw_val_node describes an attribute's value, as it is
2416 represented internally. */
2418 typedef struct dw_val_struct GTY(())
2420 enum dw_val_class val_class;
2421 union dw_val_struct_union
2423 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2424 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2425 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2426 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2427 HOST_WIDE_INT GTY ((default)) val_int;
2428 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2429 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2430 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2431 struct dw_val_die_union
2433 dw_die_ref die;
2434 int external;
2435 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2436 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2437 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2438 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2439 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2441 GTY ((desc ("%1.val_class"))) v;
2443 dw_val_node;
2445 /* Locations in memory are described using a sequence of stack machine
2446 operations. */
2448 typedef struct dw_loc_descr_struct GTY(())
2450 dw_loc_descr_ref dw_loc_next;
2451 enum dwarf_location_atom dw_loc_opc;
2452 dw_val_node dw_loc_oprnd1;
2453 dw_val_node dw_loc_oprnd2;
2454 int dw_loc_addr;
2456 dw_loc_descr_node;
2458 /* Location lists are ranges + location descriptions for that range,
2459 so you can track variables that are in different places over
2460 their entire life. */
2461 typedef struct dw_loc_list_struct GTY(())
2463 dw_loc_list_ref dw_loc_next;
2464 const char *begin; /* Label for begin address of range */
2465 const char *end; /* Label for end address of range */
2466 char *ll_symbol; /* Label for beginning of location list.
2467 Only on head of list */
2468 const char *section; /* Section this loclist is relative to */
2469 dw_loc_descr_ref expr;
2470 } dw_loc_list_node;
2472 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2474 static const char *dwarf_stack_op_name (unsigned);
2475 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2476 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2477 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2478 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2479 static unsigned long size_of_locs (dw_loc_descr_ref);
2480 static void output_loc_operands (dw_loc_descr_ref);
2481 static void output_loc_sequence (dw_loc_descr_ref);
2483 /* Convert a DWARF stack opcode into its string name. */
2485 static const char *
2486 dwarf_stack_op_name (unsigned int op)
2488 switch (op)
2490 case DW_OP_addr:
2491 case INTERNAL_DW_OP_tls_addr:
2492 return "DW_OP_addr";
2493 case DW_OP_deref:
2494 return "DW_OP_deref";
2495 case DW_OP_const1u:
2496 return "DW_OP_const1u";
2497 case DW_OP_const1s:
2498 return "DW_OP_const1s";
2499 case DW_OP_const2u:
2500 return "DW_OP_const2u";
2501 case DW_OP_const2s:
2502 return "DW_OP_const2s";
2503 case DW_OP_const4u:
2504 return "DW_OP_const4u";
2505 case DW_OP_const4s:
2506 return "DW_OP_const4s";
2507 case DW_OP_const8u:
2508 return "DW_OP_const8u";
2509 case DW_OP_const8s:
2510 return "DW_OP_const8s";
2511 case DW_OP_constu:
2512 return "DW_OP_constu";
2513 case DW_OP_consts:
2514 return "DW_OP_consts";
2515 case DW_OP_dup:
2516 return "DW_OP_dup";
2517 case DW_OP_drop:
2518 return "DW_OP_drop";
2519 case DW_OP_over:
2520 return "DW_OP_over";
2521 case DW_OP_pick:
2522 return "DW_OP_pick";
2523 case DW_OP_swap:
2524 return "DW_OP_swap";
2525 case DW_OP_rot:
2526 return "DW_OP_rot";
2527 case DW_OP_xderef:
2528 return "DW_OP_xderef";
2529 case DW_OP_abs:
2530 return "DW_OP_abs";
2531 case DW_OP_and:
2532 return "DW_OP_and";
2533 case DW_OP_div:
2534 return "DW_OP_div";
2535 case DW_OP_minus:
2536 return "DW_OP_minus";
2537 case DW_OP_mod:
2538 return "DW_OP_mod";
2539 case DW_OP_mul:
2540 return "DW_OP_mul";
2541 case DW_OP_neg:
2542 return "DW_OP_neg";
2543 case DW_OP_not:
2544 return "DW_OP_not";
2545 case DW_OP_or:
2546 return "DW_OP_or";
2547 case DW_OP_plus:
2548 return "DW_OP_plus";
2549 case DW_OP_plus_uconst:
2550 return "DW_OP_plus_uconst";
2551 case DW_OP_shl:
2552 return "DW_OP_shl";
2553 case DW_OP_shr:
2554 return "DW_OP_shr";
2555 case DW_OP_shra:
2556 return "DW_OP_shra";
2557 case DW_OP_xor:
2558 return "DW_OP_xor";
2559 case DW_OP_bra:
2560 return "DW_OP_bra";
2561 case DW_OP_eq:
2562 return "DW_OP_eq";
2563 case DW_OP_ge:
2564 return "DW_OP_ge";
2565 case DW_OP_gt:
2566 return "DW_OP_gt";
2567 case DW_OP_le:
2568 return "DW_OP_le";
2569 case DW_OP_lt:
2570 return "DW_OP_lt";
2571 case DW_OP_ne:
2572 return "DW_OP_ne";
2573 case DW_OP_skip:
2574 return "DW_OP_skip";
2575 case DW_OP_lit0:
2576 return "DW_OP_lit0";
2577 case DW_OP_lit1:
2578 return "DW_OP_lit1";
2579 case DW_OP_lit2:
2580 return "DW_OP_lit2";
2581 case DW_OP_lit3:
2582 return "DW_OP_lit3";
2583 case DW_OP_lit4:
2584 return "DW_OP_lit4";
2585 case DW_OP_lit5:
2586 return "DW_OP_lit5";
2587 case DW_OP_lit6:
2588 return "DW_OP_lit6";
2589 case DW_OP_lit7:
2590 return "DW_OP_lit7";
2591 case DW_OP_lit8:
2592 return "DW_OP_lit8";
2593 case DW_OP_lit9:
2594 return "DW_OP_lit9";
2595 case DW_OP_lit10:
2596 return "DW_OP_lit10";
2597 case DW_OP_lit11:
2598 return "DW_OP_lit11";
2599 case DW_OP_lit12:
2600 return "DW_OP_lit12";
2601 case DW_OP_lit13:
2602 return "DW_OP_lit13";
2603 case DW_OP_lit14:
2604 return "DW_OP_lit14";
2605 case DW_OP_lit15:
2606 return "DW_OP_lit15";
2607 case DW_OP_lit16:
2608 return "DW_OP_lit16";
2609 case DW_OP_lit17:
2610 return "DW_OP_lit17";
2611 case DW_OP_lit18:
2612 return "DW_OP_lit18";
2613 case DW_OP_lit19:
2614 return "DW_OP_lit19";
2615 case DW_OP_lit20:
2616 return "DW_OP_lit20";
2617 case DW_OP_lit21:
2618 return "DW_OP_lit21";
2619 case DW_OP_lit22:
2620 return "DW_OP_lit22";
2621 case DW_OP_lit23:
2622 return "DW_OP_lit23";
2623 case DW_OP_lit24:
2624 return "DW_OP_lit24";
2625 case DW_OP_lit25:
2626 return "DW_OP_lit25";
2627 case DW_OP_lit26:
2628 return "DW_OP_lit26";
2629 case DW_OP_lit27:
2630 return "DW_OP_lit27";
2631 case DW_OP_lit28:
2632 return "DW_OP_lit28";
2633 case DW_OP_lit29:
2634 return "DW_OP_lit29";
2635 case DW_OP_lit30:
2636 return "DW_OP_lit30";
2637 case DW_OP_lit31:
2638 return "DW_OP_lit31";
2639 case DW_OP_reg0:
2640 return "DW_OP_reg0";
2641 case DW_OP_reg1:
2642 return "DW_OP_reg1";
2643 case DW_OP_reg2:
2644 return "DW_OP_reg2";
2645 case DW_OP_reg3:
2646 return "DW_OP_reg3";
2647 case DW_OP_reg4:
2648 return "DW_OP_reg4";
2649 case DW_OP_reg5:
2650 return "DW_OP_reg5";
2651 case DW_OP_reg6:
2652 return "DW_OP_reg6";
2653 case DW_OP_reg7:
2654 return "DW_OP_reg7";
2655 case DW_OP_reg8:
2656 return "DW_OP_reg8";
2657 case DW_OP_reg9:
2658 return "DW_OP_reg9";
2659 case DW_OP_reg10:
2660 return "DW_OP_reg10";
2661 case DW_OP_reg11:
2662 return "DW_OP_reg11";
2663 case DW_OP_reg12:
2664 return "DW_OP_reg12";
2665 case DW_OP_reg13:
2666 return "DW_OP_reg13";
2667 case DW_OP_reg14:
2668 return "DW_OP_reg14";
2669 case DW_OP_reg15:
2670 return "DW_OP_reg15";
2671 case DW_OP_reg16:
2672 return "DW_OP_reg16";
2673 case DW_OP_reg17:
2674 return "DW_OP_reg17";
2675 case DW_OP_reg18:
2676 return "DW_OP_reg18";
2677 case DW_OP_reg19:
2678 return "DW_OP_reg19";
2679 case DW_OP_reg20:
2680 return "DW_OP_reg20";
2681 case DW_OP_reg21:
2682 return "DW_OP_reg21";
2683 case DW_OP_reg22:
2684 return "DW_OP_reg22";
2685 case DW_OP_reg23:
2686 return "DW_OP_reg23";
2687 case DW_OP_reg24:
2688 return "DW_OP_reg24";
2689 case DW_OP_reg25:
2690 return "DW_OP_reg25";
2691 case DW_OP_reg26:
2692 return "DW_OP_reg26";
2693 case DW_OP_reg27:
2694 return "DW_OP_reg27";
2695 case DW_OP_reg28:
2696 return "DW_OP_reg28";
2697 case DW_OP_reg29:
2698 return "DW_OP_reg29";
2699 case DW_OP_reg30:
2700 return "DW_OP_reg30";
2701 case DW_OP_reg31:
2702 return "DW_OP_reg31";
2703 case DW_OP_breg0:
2704 return "DW_OP_breg0";
2705 case DW_OP_breg1:
2706 return "DW_OP_breg1";
2707 case DW_OP_breg2:
2708 return "DW_OP_breg2";
2709 case DW_OP_breg3:
2710 return "DW_OP_breg3";
2711 case DW_OP_breg4:
2712 return "DW_OP_breg4";
2713 case DW_OP_breg5:
2714 return "DW_OP_breg5";
2715 case DW_OP_breg6:
2716 return "DW_OP_breg6";
2717 case DW_OP_breg7:
2718 return "DW_OP_breg7";
2719 case DW_OP_breg8:
2720 return "DW_OP_breg8";
2721 case DW_OP_breg9:
2722 return "DW_OP_breg9";
2723 case DW_OP_breg10:
2724 return "DW_OP_breg10";
2725 case DW_OP_breg11:
2726 return "DW_OP_breg11";
2727 case DW_OP_breg12:
2728 return "DW_OP_breg12";
2729 case DW_OP_breg13:
2730 return "DW_OP_breg13";
2731 case DW_OP_breg14:
2732 return "DW_OP_breg14";
2733 case DW_OP_breg15:
2734 return "DW_OP_breg15";
2735 case DW_OP_breg16:
2736 return "DW_OP_breg16";
2737 case DW_OP_breg17:
2738 return "DW_OP_breg17";
2739 case DW_OP_breg18:
2740 return "DW_OP_breg18";
2741 case DW_OP_breg19:
2742 return "DW_OP_breg19";
2743 case DW_OP_breg20:
2744 return "DW_OP_breg20";
2745 case DW_OP_breg21:
2746 return "DW_OP_breg21";
2747 case DW_OP_breg22:
2748 return "DW_OP_breg22";
2749 case DW_OP_breg23:
2750 return "DW_OP_breg23";
2751 case DW_OP_breg24:
2752 return "DW_OP_breg24";
2753 case DW_OP_breg25:
2754 return "DW_OP_breg25";
2755 case DW_OP_breg26:
2756 return "DW_OP_breg26";
2757 case DW_OP_breg27:
2758 return "DW_OP_breg27";
2759 case DW_OP_breg28:
2760 return "DW_OP_breg28";
2761 case DW_OP_breg29:
2762 return "DW_OP_breg29";
2763 case DW_OP_breg30:
2764 return "DW_OP_breg30";
2765 case DW_OP_breg31:
2766 return "DW_OP_breg31";
2767 case DW_OP_regx:
2768 return "DW_OP_regx";
2769 case DW_OP_fbreg:
2770 return "DW_OP_fbreg";
2771 case DW_OP_bregx:
2772 return "DW_OP_bregx";
2773 case DW_OP_piece:
2774 return "DW_OP_piece";
2775 case DW_OP_deref_size:
2776 return "DW_OP_deref_size";
2777 case DW_OP_xderef_size:
2778 return "DW_OP_xderef_size";
2779 case DW_OP_nop:
2780 return "DW_OP_nop";
2781 case DW_OP_push_object_address:
2782 return "DW_OP_push_object_address";
2783 case DW_OP_call2:
2784 return "DW_OP_call2";
2785 case DW_OP_call4:
2786 return "DW_OP_call4";
2787 case DW_OP_call_ref:
2788 return "DW_OP_call_ref";
2789 case DW_OP_GNU_push_tls_address:
2790 return "DW_OP_GNU_push_tls_address";
2791 default:
2792 return "OP_<unknown>";
2796 /* Return a pointer to a newly allocated location description. Location
2797 descriptions are simple expression terms that can be strung
2798 together to form more complicated location (address) descriptions. */
2800 static inline dw_loc_descr_ref
2801 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
2802 unsigned HOST_WIDE_INT oprnd2)
2804 dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
2806 descr->dw_loc_opc = op;
2807 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
2808 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
2809 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
2810 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
2812 return descr;
2816 /* Add a location description term to a location description expression. */
2818 static inline void
2819 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
2821 dw_loc_descr_ref *d;
2823 /* Find the end of the chain. */
2824 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
2827 *d = descr;
2830 /* Return the size of a location descriptor. */
2832 static unsigned long
2833 size_of_loc_descr (dw_loc_descr_ref loc)
2835 unsigned long size = 1;
2837 switch (loc->dw_loc_opc)
2839 case DW_OP_addr:
2840 case INTERNAL_DW_OP_tls_addr:
2841 size += DWARF2_ADDR_SIZE;
2842 break;
2843 case DW_OP_const1u:
2844 case DW_OP_const1s:
2845 size += 1;
2846 break;
2847 case DW_OP_const2u:
2848 case DW_OP_const2s:
2849 size += 2;
2850 break;
2851 case DW_OP_const4u:
2852 case DW_OP_const4s:
2853 size += 4;
2854 break;
2855 case DW_OP_const8u:
2856 case DW_OP_const8s:
2857 size += 8;
2858 break;
2859 case DW_OP_constu:
2860 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2861 break;
2862 case DW_OP_consts:
2863 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2864 break;
2865 case DW_OP_pick:
2866 size += 1;
2867 break;
2868 case DW_OP_plus_uconst:
2869 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2870 break;
2871 case DW_OP_skip:
2872 case DW_OP_bra:
2873 size += 2;
2874 break;
2875 case DW_OP_breg0:
2876 case DW_OP_breg1:
2877 case DW_OP_breg2:
2878 case DW_OP_breg3:
2879 case DW_OP_breg4:
2880 case DW_OP_breg5:
2881 case DW_OP_breg6:
2882 case DW_OP_breg7:
2883 case DW_OP_breg8:
2884 case DW_OP_breg9:
2885 case DW_OP_breg10:
2886 case DW_OP_breg11:
2887 case DW_OP_breg12:
2888 case DW_OP_breg13:
2889 case DW_OP_breg14:
2890 case DW_OP_breg15:
2891 case DW_OP_breg16:
2892 case DW_OP_breg17:
2893 case DW_OP_breg18:
2894 case DW_OP_breg19:
2895 case DW_OP_breg20:
2896 case DW_OP_breg21:
2897 case DW_OP_breg22:
2898 case DW_OP_breg23:
2899 case DW_OP_breg24:
2900 case DW_OP_breg25:
2901 case DW_OP_breg26:
2902 case DW_OP_breg27:
2903 case DW_OP_breg28:
2904 case DW_OP_breg29:
2905 case DW_OP_breg30:
2906 case DW_OP_breg31:
2907 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2908 break;
2909 case DW_OP_regx:
2910 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2911 break;
2912 case DW_OP_fbreg:
2913 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2914 break;
2915 case DW_OP_bregx:
2916 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2917 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
2918 break;
2919 case DW_OP_piece:
2920 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2921 break;
2922 case DW_OP_deref_size:
2923 case DW_OP_xderef_size:
2924 size += 1;
2925 break;
2926 case DW_OP_call2:
2927 size += 2;
2928 break;
2929 case DW_OP_call4:
2930 size += 4;
2931 break;
2932 case DW_OP_call_ref:
2933 size += DWARF2_ADDR_SIZE;
2934 break;
2935 default:
2936 break;
2939 return size;
2942 /* Return the size of a series of location descriptors. */
2944 static unsigned long
2945 size_of_locs (dw_loc_descr_ref loc)
2947 unsigned long size;
2949 for (size = 0; loc != NULL; loc = loc->dw_loc_next)
2951 loc->dw_loc_addr = size;
2952 size += size_of_loc_descr (loc);
2955 return size;
2958 /* Output location description stack opcode's operands (if any). */
2960 static void
2961 output_loc_operands (dw_loc_descr_ref loc)
2963 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2964 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2966 switch (loc->dw_loc_opc)
2968 #ifdef DWARF2_DEBUGGING_INFO
2969 case DW_OP_addr:
2970 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2971 break;
2972 case DW_OP_const2u:
2973 case DW_OP_const2s:
2974 dw2_asm_output_data (2, val1->v.val_int, NULL);
2975 break;
2976 case DW_OP_const4u:
2977 case DW_OP_const4s:
2978 dw2_asm_output_data (4, val1->v.val_int, NULL);
2979 break;
2980 case DW_OP_const8u:
2981 case DW_OP_const8s:
2982 if (HOST_BITS_PER_LONG < 64)
2983 abort ();
2984 dw2_asm_output_data (8, val1->v.val_int, NULL);
2985 break;
2986 case DW_OP_skip:
2987 case DW_OP_bra:
2989 int offset;
2991 if (val1->val_class == dw_val_class_loc)
2992 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2993 else
2994 abort ();
2996 dw2_asm_output_data (2, offset, NULL);
2998 break;
2999 #else
3000 case DW_OP_addr:
3001 case DW_OP_const2u:
3002 case DW_OP_const2s:
3003 case DW_OP_const4u:
3004 case DW_OP_const4s:
3005 case DW_OP_const8u:
3006 case DW_OP_const8s:
3007 case DW_OP_skip:
3008 case DW_OP_bra:
3009 /* We currently don't make any attempt to make sure these are
3010 aligned properly like we do for the main unwind info, so
3011 don't support emitting things larger than a byte if we're
3012 only doing unwinding. */
3013 abort ();
3014 #endif
3015 case DW_OP_const1u:
3016 case DW_OP_const1s:
3017 dw2_asm_output_data (1, val1->v.val_int, NULL);
3018 break;
3019 case DW_OP_constu:
3020 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3021 break;
3022 case DW_OP_consts:
3023 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3024 break;
3025 case DW_OP_pick:
3026 dw2_asm_output_data (1, val1->v.val_int, NULL);
3027 break;
3028 case DW_OP_plus_uconst:
3029 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3030 break;
3031 case DW_OP_breg0:
3032 case DW_OP_breg1:
3033 case DW_OP_breg2:
3034 case DW_OP_breg3:
3035 case DW_OP_breg4:
3036 case DW_OP_breg5:
3037 case DW_OP_breg6:
3038 case DW_OP_breg7:
3039 case DW_OP_breg8:
3040 case DW_OP_breg9:
3041 case DW_OP_breg10:
3042 case DW_OP_breg11:
3043 case DW_OP_breg12:
3044 case DW_OP_breg13:
3045 case DW_OP_breg14:
3046 case DW_OP_breg15:
3047 case DW_OP_breg16:
3048 case DW_OP_breg17:
3049 case DW_OP_breg18:
3050 case DW_OP_breg19:
3051 case DW_OP_breg20:
3052 case DW_OP_breg21:
3053 case DW_OP_breg22:
3054 case DW_OP_breg23:
3055 case DW_OP_breg24:
3056 case DW_OP_breg25:
3057 case DW_OP_breg26:
3058 case DW_OP_breg27:
3059 case DW_OP_breg28:
3060 case DW_OP_breg29:
3061 case DW_OP_breg30:
3062 case DW_OP_breg31:
3063 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3064 break;
3065 case DW_OP_regx:
3066 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3067 break;
3068 case DW_OP_fbreg:
3069 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3070 break;
3071 case DW_OP_bregx:
3072 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3073 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3074 break;
3075 case DW_OP_piece:
3076 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3077 break;
3078 case DW_OP_deref_size:
3079 case DW_OP_xderef_size:
3080 dw2_asm_output_data (1, val1->v.val_int, NULL);
3081 break;
3083 case INTERNAL_DW_OP_tls_addr:
3084 #ifdef ASM_OUTPUT_DWARF_DTPREL
3085 ASM_OUTPUT_DWARF_DTPREL (asm_out_file, DWARF2_ADDR_SIZE,
3086 val1->v.val_addr);
3087 fputc ('\n', asm_out_file);
3088 #else
3089 abort ();
3090 #endif
3091 break;
3093 default:
3094 /* Other codes have no operands. */
3095 break;
3099 /* Output a sequence of location operations. */
3101 static void
3102 output_loc_sequence (dw_loc_descr_ref loc)
3104 for (; loc != NULL; loc = loc->dw_loc_next)
3106 /* Output the opcode. */
3107 dw2_asm_output_data (1, loc->dw_loc_opc,
3108 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3110 /* Output the operand(s) (if any). */
3111 output_loc_operands (loc);
3115 /* This routine will generate the correct assembly data for a location
3116 description based on a cfi entry with a complex address. */
3118 static void
3119 output_cfa_loc (dw_cfi_ref cfi)
3121 dw_loc_descr_ref loc;
3122 unsigned long size;
3124 /* Output the size of the block. */
3125 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3126 size = size_of_locs (loc);
3127 dw2_asm_output_data_uleb128 (size, NULL);
3129 /* Now output the operations themselves. */
3130 output_loc_sequence (loc);
3133 /* This function builds a dwarf location descriptor sequence from
3134 a dw_cfa_location. */
3136 static struct dw_loc_descr_struct *
3137 build_cfa_loc (dw_cfa_location *cfa)
3139 struct dw_loc_descr_struct *head, *tmp;
3141 if (cfa->indirect == 0)
3142 abort ();
3144 if (cfa->base_offset)
3146 if (cfa->reg <= 31)
3147 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3148 else
3149 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3151 else if (cfa->reg <= 31)
3152 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3153 else
3154 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3156 head->dw_loc_oprnd1.val_class = dw_val_class_const;
3157 tmp = new_loc_descr (DW_OP_deref, 0, 0);
3158 add_loc_descr (&head, tmp);
3159 if (cfa->offset != 0)
3161 tmp = new_loc_descr (DW_OP_plus_uconst, cfa->offset, 0);
3162 add_loc_descr (&head, tmp);
3165 return head;
3168 /* This function fills in aa dw_cfa_location structure from a dwarf location
3169 descriptor sequence. */
3171 static void
3172 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3174 struct dw_loc_descr_struct *ptr;
3175 cfa->offset = 0;
3176 cfa->base_offset = 0;
3177 cfa->indirect = 0;
3178 cfa->reg = -1;
3180 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3182 enum dwarf_location_atom op = ptr->dw_loc_opc;
3184 switch (op)
3186 case DW_OP_reg0:
3187 case DW_OP_reg1:
3188 case DW_OP_reg2:
3189 case DW_OP_reg3:
3190 case DW_OP_reg4:
3191 case DW_OP_reg5:
3192 case DW_OP_reg6:
3193 case DW_OP_reg7:
3194 case DW_OP_reg8:
3195 case DW_OP_reg9:
3196 case DW_OP_reg10:
3197 case DW_OP_reg11:
3198 case DW_OP_reg12:
3199 case DW_OP_reg13:
3200 case DW_OP_reg14:
3201 case DW_OP_reg15:
3202 case DW_OP_reg16:
3203 case DW_OP_reg17:
3204 case DW_OP_reg18:
3205 case DW_OP_reg19:
3206 case DW_OP_reg20:
3207 case DW_OP_reg21:
3208 case DW_OP_reg22:
3209 case DW_OP_reg23:
3210 case DW_OP_reg24:
3211 case DW_OP_reg25:
3212 case DW_OP_reg26:
3213 case DW_OP_reg27:
3214 case DW_OP_reg28:
3215 case DW_OP_reg29:
3216 case DW_OP_reg30:
3217 case DW_OP_reg31:
3218 cfa->reg = op - DW_OP_reg0;
3219 break;
3220 case DW_OP_regx:
3221 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3222 break;
3223 case DW_OP_breg0:
3224 case DW_OP_breg1:
3225 case DW_OP_breg2:
3226 case DW_OP_breg3:
3227 case DW_OP_breg4:
3228 case DW_OP_breg5:
3229 case DW_OP_breg6:
3230 case DW_OP_breg7:
3231 case DW_OP_breg8:
3232 case DW_OP_breg9:
3233 case DW_OP_breg10:
3234 case DW_OP_breg11:
3235 case DW_OP_breg12:
3236 case DW_OP_breg13:
3237 case DW_OP_breg14:
3238 case DW_OP_breg15:
3239 case DW_OP_breg16:
3240 case DW_OP_breg17:
3241 case DW_OP_breg18:
3242 case DW_OP_breg19:
3243 case DW_OP_breg20:
3244 case DW_OP_breg21:
3245 case DW_OP_breg22:
3246 case DW_OP_breg23:
3247 case DW_OP_breg24:
3248 case DW_OP_breg25:
3249 case DW_OP_breg26:
3250 case DW_OP_breg27:
3251 case DW_OP_breg28:
3252 case DW_OP_breg29:
3253 case DW_OP_breg30:
3254 case DW_OP_breg31:
3255 cfa->reg = op - DW_OP_breg0;
3256 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3257 break;
3258 case DW_OP_bregx:
3259 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3260 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3261 break;
3262 case DW_OP_deref:
3263 cfa->indirect = 1;
3264 break;
3265 case DW_OP_plus_uconst:
3266 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3267 break;
3268 default:
3269 internal_error ("DW_LOC_OP %s not implemented\n",
3270 dwarf_stack_op_name (ptr->dw_loc_opc));
3274 #endif /* .debug_frame support */
3276 /* And now, the support for symbolic debugging information. */
3277 #ifdef DWARF2_DEBUGGING_INFO
3279 /* .debug_str support. */
3280 static int output_indirect_string (void **, void *);
3282 static void dwarf2out_init (const char *);
3283 static void dwarf2out_finish (const char *);
3284 static void dwarf2out_define (unsigned int, const char *);
3285 static void dwarf2out_undef (unsigned int, const char *);
3286 static void dwarf2out_start_source_file (unsigned, const char *);
3287 static void dwarf2out_end_source_file (unsigned);
3288 static void dwarf2out_begin_block (unsigned, unsigned);
3289 static void dwarf2out_end_block (unsigned, unsigned);
3290 static bool dwarf2out_ignore_block (tree);
3291 static void dwarf2out_global_decl (tree);
3292 static void dwarf2out_type_decl (tree, int);
3293 static void dwarf2out_imported_module_or_decl (tree, tree);
3294 static void dwarf2out_abstract_function (tree);
3295 static void dwarf2out_var_location (rtx);
3296 static void dwarf2out_begin_function (tree);
3298 /* The debug hooks structure. */
3300 const struct gcc_debug_hooks dwarf2_debug_hooks =
3302 dwarf2out_init,
3303 dwarf2out_finish,
3304 dwarf2out_define,
3305 dwarf2out_undef,
3306 dwarf2out_start_source_file,
3307 dwarf2out_end_source_file,
3308 dwarf2out_begin_block,
3309 dwarf2out_end_block,
3310 dwarf2out_ignore_block,
3311 dwarf2out_source_line,
3312 dwarf2out_begin_prologue,
3313 debug_nothing_int_charstar, /* end_prologue */
3314 dwarf2out_end_epilogue,
3315 dwarf2out_begin_function,
3316 debug_nothing_int, /* end_function */
3317 dwarf2out_decl, /* function_decl */
3318 dwarf2out_global_decl,
3319 dwarf2out_type_decl, /* type_decl */
3320 dwarf2out_imported_module_or_decl,
3321 debug_nothing_tree, /* deferred_inline_function */
3322 /* The DWARF 2 backend tries to reduce debugging bloat by not
3323 emitting the abstract description of inline functions until
3324 something tries to reference them. */
3325 dwarf2out_abstract_function, /* outlining_inline_function */
3326 debug_nothing_rtx, /* label */
3327 debug_nothing_int, /* handle_pch */
3328 dwarf2out_var_location
3330 #endif
3332 /* NOTE: In the comments in this file, many references are made to
3333 "Debugging Information Entries". This term is abbreviated as `DIE'
3334 throughout the remainder of this file. */
3336 /* An internal representation of the DWARF output is built, and then
3337 walked to generate the DWARF debugging info. The walk of the internal
3338 representation is done after the entire program has been compiled.
3339 The types below are used to describe the internal representation. */
3341 /* Various DIE's use offsets relative to the beginning of the
3342 .debug_info section to refer to each other. */
3344 typedef long int dw_offset;
3346 /* Define typedefs here to avoid circular dependencies. */
3348 typedef struct dw_attr_struct *dw_attr_ref;
3349 typedef struct dw_line_info_struct *dw_line_info_ref;
3350 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3351 typedef struct pubname_struct *pubname_ref;
3352 typedef struct dw_ranges_struct *dw_ranges_ref;
3354 /* Each entry in the line_info_table maintains the file and
3355 line number associated with the label generated for that
3356 entry. The label gives the PC value associated with
3357 the line number entry. */
3359 typedef struct dw_line_info_struct GTY(())
3361 unsigned long dw_file_num;
3362 unsigned long dw_line_num;
3364 dw_line_info_entry;
3366 /* Line information for functions in separate sections; each one gets its
3367 own sequence. */
3368 typedef struct dw_separate_line_info_struct GTY(())
3370 unsigned long dw_file_num;
3371 unsigned long dw_line_num;
3372 unsigned long function;
3374 dw_separate_line_info_entry;
3376 /* Each DIE attribute has a field specifying the attribute kind,
3377 a link to the next attribute in the chain, and an attribute value.
3378 Attributes are typically linked below the DIE they modify. */
3380 typedef struct dw_attr_struct GTY(())
3382 enum dwarf_attribute dw_attr;
3383 dw_attr_ref dw_attr_next;
3384 dw_val_node dw_attr_val;
3386 dw_attr_node;
3388 /* The Debugging Information Entry (DIE) structure */
3390 typedef struct die_struct GTY(())
3392 enum dwarf_tag die_tag;
3393 char *die_symbol;
3394 dw_attr_ref die_attr;
3395 dw_die_ref die_parent;
3396 dw_die_ref die_child;
3397 dw_die_ref die_sib;
3398 dw_die_ref die_definition; /* ref from a specification to its definition */
3399 dw_offset die_offset;
3400 unsigned long die_abbrev;
3401 int die_mark;
3402 unsigned int decl_id;
3404 die_node;
3406 /* The pubname structure */
3408 typedef struct pubname_struct GTY(())
3410 dw_die_ref die;
3411 char *name;
3413 pubname_entry;
3415 struct dw_ranges_struct GTY(())
3417 int block_num;
3420 /* The limbo die list structure. */
3421 typedef struct limbo_die_struct GTY(())
3423 dw_die_ref die;
3424 tree created_for;
3425 struct limbo_die_struct *next;
3427 limbo_die_node;
3429 /* How to start an assembler comment. */
3430 #ifndef ASM_COMMENT_START
3431 #define ASM_COMMENT_START ";#"
3432 #endif
3434 /* Define a macro which returns nonzero for a TYPE_DECL which was
3435 implicitly generated for a tagged type.
3437 Note that unlike the gcc front end (which generates a NULL named
3438 TYPE_DECL node for each complete tagged type, each array type, and
3439 each function type node created) the g++ front end generates a
3440 _named_ TYPE_DECL node for each tagged type node created.
3441 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3442 generate a DW_TAG_typedef DIE for them. */
3444 #define TYPE_DECL_IS_STUB(decl) \
3445 (DECL_NAME (decl) == NULL_TREE \
3446 || (DECL_ARTIFICIAL (decl) \
3447 && is_tagged_type (TREE_TYPE (decl)) \
3448 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3449 /* This is necessary for stub decls that \
3450 appear in nested inline functions. */ \
3451 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3452 && (decl_ultimate_origin (decl) \
3453 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3455 /* Information concerning the compilation unit's programming
3456 language, and compiler version. */
3458 /* Fixed size portion of the DWARF compilation unit header. */
3459 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3460 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3462 /* Fixed size portion of public names info. */
3463 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3465 /* Fixed size portion of the address range info. */
3466 #define DWARF_ARANGES_HEADER_SIZE \
3467 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3468 DWARF2_ADDR_SIZE * 2) \
3469 - DWARF_INITIAL_LENGTH_SIZE)
3471 /* Size of padding portion in the address range info. It must be
3472 aligned to twice the pointer size. */
3473 #define DWARF_ARANGES_PAD_SIZE \
3474 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3475 DWARF2_ADDR_SIZE * 2) \
3476 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3478 /* Use assembler line directives if available. */
3479 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3480 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3481 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3482 #else
3483 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3484 #endif
3485 #endif
3487 /* Minimum line offset in a special line info. opcode.
3488 This value was chosen to give a reasonable range of values. */
3489 #define DWARF_LINE_BASE -10
3491 /* First special line opcode - leave room for the standard opcodes. */
3492 #define DWARF_LINE_OPCODE_BASE 10
3494 /* Range of line offsets in a special line info. opcode. */
3495 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3497 /* Flag that indicates the initial value of the is_stmt_start flag.
3498 In the present implementation, we do not mark any lines as
3499 the beginning of a source statement, because that information
3500 is not made available by the GCC front-end. */
3501 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3503 #ifdef DWARF2_DEBUGGING_INFO
3504 /* This location is used by calc_die_sizes() to keep track
3505 the offset of each DIE within the .debug_info section. */
3506 static unsigned long next_die_offset;
3507 #endif
3509 /* Record the root of the DIE's built for the current compilation unit. */
3510 static GTY(()) dw_die_ref comp_unit_die;
3512 /* A list of DIEs with a NULL parent waiting to be relocated. */
3513 static GTY(()) limbo_die_node *limbo_die_list;
3515 /* Filenames referenced by this compilation unit. */
3516 static GTY(()) varray_type file_table;
3517 static GTY(()) varray_type file_table_emitted;
3518 static GTY(()) size_t file_table_last_lookup_index;
3520 /* A hash table of references to DIE's that describe declarations.
3521 The key is a DECL_UID() which is a unique number identifying each decl. */
3522 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3524 /* Node of the variable location list. */
3525 struct var_loc_node GTY ((chain_next ("%h.next")))
3527 rtx GTY (()) var_loc_note;
3528 const char * GTY (()) label;
3529 struct var_loc_node * GTY (()) next;
3532 /* Variable location list. */
3533 struct var_loc_list_def GTY (())
3535 struct var_loc_node * GTY (()) first;
3537 /* Do not mark the last element of the chained list because
3538 it is marked through the chain. */
3539 struct var_loc_node * GTY ((skip ("%h"))) last;
3541 /* DECL_UID of the variable decl. */
3542 unsigned int decl_id;
3544 typedef struct var_loc_list_def var_loc_list;
3547 /* Table of decl location linked lists. */
3548 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3550 /* A pointer to the base of a list of references to DIE's that
3551 are uniquely identified by their tag, presence/absence of
3552 children DIE's, and list of attribute/value pairs. */
3553 static GTY((length ("abbrev_die_table_allocated")))
3554 dw_die_ref *abbrev_die_table;
3556 /* Number of elements currently allocated for abbrev_die_table. */
3557 static GTY(()) unsigned abbrev_die_table_allocated;
3559 /* Number of elements in type_die_table currently in use. */
3560 static GTY(()) unsigned abbrev_die_table_in_use;
3562 /* Size (in elements) of increments by which we may expand the
3563 abbrev_die_table. */
3564 #define ABBREV_DIE_TABLE_INCREMENT 256
3566 /* A pointer to the base of a table that contains line information
3567 for each source code line in .text in the compilation unit. */
3568 static GTY((length ("line_info_table_allocated")))
3569 dw_line_info_ref line_info_table;
3571 /* Number of elements currently allocated for line_info_table. */
3572 static GTY(()) unsigned line_info_table_allocated;
3574 /* Number of elements in line_info_table currently in use. */
3575 static GTY(()) unsigned line_info_table_in_use;
3577 /* A pointer to the base of a table that contains line information
3578 for each source code line outside of .text in the compilation unit. */
3579 static GTY ((length ("separate_line_info_table_allocated")))
3580 dw_separate_line_info_ref separate_line_info_table;
3582 /* Number of elements currently allocated for separate_line_info_table. */
3583 static GTY(()) unsigned separate_line_info_table_allocated;
3585 /* Number of elements in separate_line_info_table currently in use. */
3586 static GTY(()) unsigned separate_line_info_table_in_use;
3588 /* Size (in elements) of increments by which we may expand the
3589 line_info_table. */
3590 #define LINE_INFO_TABLE_INCREMENT 1024
3592 /* A pointer to the base of a table that contains a list of publicly
3593 accessible names. */
3594 static GTY ((length ("pubname_table_allocated"))) pubname_ref pubname_table;
3596 /* Number of elements currently allocated for pubname_table. */
3597 static GTY(()) unsigned pubname_table_allocated;
3599 /* Number of elements in pubname_table currently in use. */
3600 static GTY(()) unsigned pubname_table_in_use;
3602 /* Size (in elements) of increments by which we may expand the
3603 pubname_table. */
3604 #define PUBNAME_TABLE_INCREMENT 64
3606 /* Array of dies for which we should generate .debug_arange info. */
3607 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3609 /* Number of elements currently allocated for arange_table. */
3610 static GTY(()) unsigned arange_table_allocated;
3612 /* Number of elements in arange_table currently in use. */
3613 static GTY(()) unsigned arange_table_in_use;
3615 /* Size (in elements) of increments by which we may expand the
3616 arange_table. */
3617 #define ARANGE_TABLE_INCREMENT 64
3619 /* Array of dies for which we should generate .debug_ranges info. */
3620 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3622 /* Number of elements currently allocated for ranges_table. */
3623 static GTY(()) unsigned ranges_table_allocated;
3625 /* Number of elements in ranges_table currently in use. */
3626 static GTY(()) unsigned ranges_table_in_use;
3628 /* Size (in elements) of increments by which we may expand the
3629 ranges_table. */
3630 #define RANGES_TABLE_INCREMENT 64
3632 /* Whether we have location lists that need outputting */
3633 static GTY(()) unsigned have_location_lists;
3635 /* Unique label counter. */
3636 static GTY(()) unsigned int loclabel_num;
3638 #ifdef DWARF2_DEBUGGING_INFO
3639 /* Record whether the function being analyzed contains inlined functions. */
3640 static int current_function_has_inlines;
3641 #endif
3642 #if 0 && defined (MIPS_DEBUGGING_INFO)
3643 static int comp_unit_has_inlines;
3644 #endif
3646 /* Number of file tables emitted in maybe_emit_file(). */
3647 static GTY(()) int emitcount = 0;
3649 /* Number of internal labels generated by gen_internal_sym(). */
3650 static GTY(()) int label_num;
3652 #ifdef DWARF2_DEBUGGING_INFO
3654 /* Forward declarations for functions defined in this file. */
3656 static int is_pseudo_reg (rtx);
3657 static tree type_main_variant (tree);
3658 static int is_tagged_type (tree);
3659 static const char *dwarf_tag_name (unsigned);
3660 static const char *dwarf_attr_name (unsigned);
3661 static const char *dwarf_form_name (unsigned);
3662 #if 0
3663 static const char *dwarf_type_encoding_name (unsigned);
3664 #endif
3665 static tree decl_ultimate_origin (tree);
3666 static tree block_ultimate_origin (tree);
3667 static tree decl_class_context (tree);
3668 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3669 static inline enum dw_val_class AT_class (dw_attr_ref);
3670 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3671 static inline unsigned AT_flag (dw_attr_ref);
3672 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3673 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3674 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3675 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3676 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
3677 unsigned long);
3678 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3679 unsigned int, unsigned char *);
3680 static hashval_t debug_str_do_hash (const void *);
3681 static int debug_str_eq (const void *, const void *);
3682 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3683 static inline const char *AT_string (dw_attr_ref);
3684 static int AT_string_form (dw_attr_ref);
3685 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3686 static void add_AT_specification (dw_die_ref, dw_die_ref);
3687 static inline dw_die_ref AT_ref (dw_attr_ref);
3688 static inline int AT_ref_external (dw_attr_ref);
3689 static inline void set_AT_ref_external (dw_attr_ref, int);
3690 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3691 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3692 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3693 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3694 dw_loc_list_ref);
3695 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3696 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3697 static inline rtx AT_addr (dw_attr_ref);
3698 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3699 static void add_AT_lbl_offset (dw_die_ref, enum dwarf_attribute, const char *);
3700 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3701 unsigned HOST_WIDE_INT);
3702 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3703 unsigned long);
3704 static inline const char *AT_lbl (dw_attr_ref);
3705 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3706 static const char *get_AT_low_pc (dw_die_ref);
3707 static const char *get_AT_hi_pc (dw_die_ref);
3708 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3709 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3710 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3711 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3712 static bool is_c_family (void);
3713 static bool is_cxx (void);
3714 static bool is_java (void);
3715 static bool is_fortran (void);
3716 static bool is_ada (void);
3717 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3718 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3719 static inline void free_die (dw_die_ref);
3720 static void remove_children (dw_die_ref);
3721 static void add_child_die (dw_die_ref, dw_die_ref);
3722 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3723 static dw_die_ref lookup_type_die (tree);
3724 static void equate_type_number_to_die (tree, dw_die_ref);
3725 static hashval_t decl_die_table_hash (const void *);
3726 static int decl_die_table_eq (const void *, const void *);
3727 static dw_die_ref lookup_decl_die (tree);
3728 static hashval_t decl_loc_table_hash (const void *);
3729 static int decl_loc_table_eq (const void *, const void *);
3730 static var_loc_list *lookup_decl_loc (tree);
3731 static void equate_decl_number_to_die (tree, dw_die_ref);
3732 static void add_var_loc_to_decl (tree, struct var_loc_node *);
3733 static void print_spaces (FILE *);
3734 static void print_die (dw_die_ref, FILE *);
3735 static void print_dwarf_line_table (FILE *);
3736 static void reverse_die_lists (dw_die_ref);
3737 static void reverse_all_dies (dw_die_ref);
3738 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3739 static dw_die_ref pop_compile_unit (dw_die_ref);
3740 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3741 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3742 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3743 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3744 static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
3745 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3746 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3747 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3748 static void compute_section_prefix (dw_die_ref);
3749 static int is_type_die (dw_die_ref);
3750 static int is_comdat_die (dw_die_ref);
3751 static int is_symbol_die (dw_die_ref);
3752 static void assign_symbol_names (dw_die_ref);
3753 static void break_out_includes (dw_die_ref);
3754 static hashval_t htab_cu_hash (const void *);
3755 static int htab_cu_eq (const void *, const void *);
3756 static void htab_cu_del (void *);
3757 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3758 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3759 static void add_sibling_attributes (dw_die_ref);
3760 static void build_abbrev_table (dw_die_ref);
3761 static void output_location_lists (dw_die_ref);
3762 static int constant_size (long unsigned);
3763 static unsigned long size_of_die (dw_die_ref);
3764 static void calc_die_sizes (dw_die_ref);
3765 static void mark_dies (dw_die_ref);
3766 static void unmark_dies (dw_die_ref);
3767 static void unmark_all_dies (dw_die_ref);
3768 static unsigned long size_of_pubnames (void);
3769 static unsigned long size_of_aranges (void);
3770 static enum dwarf_form value_format (dw_attr_ref);
3771 static void output_value_format (dw_attr_ref);
3772 static void output_abbrev_section (void);
3773 static void output_die_symbol (dw_die_ref);
3774 static void output_die (dw_die_ref);
3775 static void output_compilation_unit_header (void);
3776 static void output_comp_unit (dw_die_ref, int);
3777 static const char *dwarf2_name (tree, int);
3778 static void add_pubname (tree, dw_die_ref);
3779 static void output_pubnames (void);
3780 static void add_arange (tree, dw_die_ref);
3781 static void output_aranges (void);
3782 static unsigned int add_ranges (tree);
3783 static void output_ranges (void);
3784 static void output_line_info (void);
3785 static void output_file_names (void);
3786 static dw_die_ref base_type_die (tree);
3787 static tree root_type (tree);
3788 static int is_base_type (tree);
3789 static bool is_subrange_type (tree);
3790 static dw_die_ref subrange_type_die (tree, dw_die_ref);
3791 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3792 static int type_is_enum (tree);
3793 static unsigned int dbx_reg_number (rtx);
3794 static dw_loc_descr_ref reg_loc_descriptor (rtx);
3795 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
3796 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
3797 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3798 static dw_loc_descr_ref based_loc_descr (unsigned, HOST_WIDE_INT, bool);
3799 static int is_based_loc (rtx);
3800 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode, bool);
3801 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
3802 static dw_loc_descr_ref loc_descriptor (rtx, bool);
3803 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3804 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3805 static tree field_type (tree);
3806 static unsigned int simple_type_align_in_bits (tree);
3807 static unsigned int simple_decl_align_in_bits (tree);
3808 static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
3809 static HOST_WIDE_INT field_byte_offset (tree);
3810 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3811 dw_loc_descr_ref);
3812 static void add_data_member_location_attribute (dw_die_ref, tree);
3813 static void add_const_value_attribute (dw_die_ref, rtx);
3814 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3815 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
3816 static void insert_float (rtx, unsigned char *);
3817 static rtx rtl_for_decl_location (tree);
3818 static void add_location_or_const_value_attribute (dw_die_ref, tree,
3819 enum dwarf_attribute);
3820 static void tree_add_const_value_attribute (dw_die_ref, tree);
3821 static void add_name_attribute (dw_die_ref, const char *);
3822 static void add_comp_dir_attribute (dw_die_ref);
3823 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3824 static void add_subscript_info (dw_die_ref, tree);
3825 static void add_byte_size_attribute (dw_die_ref, tree);
3826 static void add_bit_offset_attribute (dw_die_ref, tree);
3827 static void add_bit_size_attribute (dw_die_ref, tree);
3828 static void add_prototyped_attribute (dw_die_ref, tree);
3829 static void add_abstract_origin_attribute (dw_die_ref, tree);
3830 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3831 static void add_src_coords_attributes (dw_die_ref, tree);
3832 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3833 static void push_decl_scope (tree);
3834 static void pop_decl_scope (void);
3835 static dw_die_ref scope_die_for (tree, dw_die_ref);
3836 static inline int local_scope_p (dw_die_ref);
3837 static inline int class_or_namespace_scope_p (dw_die_ref);
3838 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3839 static const char *type_tag (tree);
3840 static tree member_declared_type (tree);
3841 #if 0
3842 static const char *decl_start_label (tree);
3843 #endif
3844 static void gen_array_type_die (tree, dw_die_ref);
3845 static void gen_set_type_die (tree, dw_die_ref);
3846 #if 0
3847 static void gen_entry_point_die (tree, dw_die_ref);
3848 #endif
3849 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
3850 static void gen_inlined_structure_type_die (tree, dw_die_ref);
3851 static void gen_inlined_union_type_die (tree, dw_die_ref);
3852 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3853 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
3854 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3855 static void gen_formal_types_die (tree, dw_die_ref);
3856 static void gen_subprogram_die (tree, dw_die_ref);
3857 static void gen_variable_die (tree, dw_die_ref);
3858 static void gen_label_die (tree, dw_die_ref);
3859 static void gen_lexical_block_die (tree, dw_die_ref, int);
3860 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3861 static void gen_field_die (tree, dw_die_ref);
3862 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3863 static dw_die_ref gen_compile_unit_die (const char *);
3864 static void gen_string_type_die (tree, dw_die_ref);
3865 static void gen_inheritance_die (tree, tree, dw_die_ref);
3866 static void gen_member_die (tree, dw_die_ref);
3867 static void gen_struct_or_union_type_die (tree, dw_die_ref);
3868 static void gen_subroutine_type_die (tree, dw_die_ref);
3869 static void gen_typedef_die (tree, dw_die_ref);
3870 static void gen_type_die (tree, dw_die_ref);
3871 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
3872 static void gen_block_die (tree, dw_die_ref, int);
3873 static void decls_for_scope (tree, dw_die_ref, int);
3874 static int is_redundant_typedef (tree);
3875 static void gen_namespace_die (tree);
3876 static void gen_decl_die (tree, dw_die_ref);
3877 static dw_die_ref force_decl_die (tree);
3878 static dw_die_ref force_type_die (tree);
3879 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3880 static void declare_in_namespace (tree, dw_die_ref);
3881 static unsigned lookup_filename (const char *);
3882 static void init_file_table (void);
3883 static void retry_incomplete_types (void);
3884 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3885 static void splice_child_die (dw_die_ref, dw_die_ref);
3886 static int file_info_cmp (const void *, const void *);
3887 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3888 const char *, const char *, unsigned);
3889 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
3890 const char *, const char *,
3891 const char *);
3892 static void output_loc_list (dw_loc_list_ref);
3893 static char *gen_internal_sym (const char *);
3895 static void prune_unmark_dies (dw_die_ref);
3896 static void prune_unused_types_mark (dw_die_ref, int);
3897 static void prune_unused_types_walk (dw_die_ref);
3898 static void prune_unused_types_walk_attribs (dw_die_ref);
3899 static void prune_unused_types_prune (dw_die_ref);
3900 static void prune_unused_types (void);
3901 static int maybe_emit_file (int);
3903 /* Section names used to hold DWARF debugging information. */
3904 #ifndef DEBUG_INFO_SECTION
3905 #define DEBUG_INFO_SECTION ".debug_info"
3906 #endif
3907 #ifndef DEBUG_ABBREV_SECTION
3908 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3909 #endif
3910 #ifndef DEBUG_ARANGES_SECTION
3911 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3912 #endif
3913 #ifndef DEBUG_MACINFO_SECTION
3914 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3915 #endif
3916 #ifndef DEBUG_LINE_SECTION
3917 #define DEBUG_LINE_SECTION ".debug_line"
3918 #endif
3919 #ifndef DEBUG_LOC_SECTION
3920 #define DEBUG_LOC_SECTION ".debug_loc"
3921 #endif
3922 #ifndef DEBUG_PUBNAMES_SECTION
3923 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
3924 #endif
3925 #ifndef DEBUG_STR_SECTION
3926 #define DEBUG_STR_SECTION ".debug_str"
3927 #endif
3928 #ifndef DEBUG_RANGES_SECTION
3929 #define DEBUG_RANGES_SECTION ".debug_ranges"
3930 #endif
3932 /* Standard ELF section names for compiled code and data. */
3933 #ifndef TEXT_SECTION_NAME
3934 #define TEXT_SECTION_NAME ".text"
3935 #endif
3937 /* Section flags for .debug_str section. */
3938 #define DEBUG_STR_SECTION_FLAGS \
3939 (HAVE_GAS_SHF_MERGE && flag_merge_constants \
3940 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3941 : SECTION_DEBUG)
3943 /* Labels we insert at beginning sections we can reference instead of
3944 the section names themselves. */
3946 #ifndef TEXT_SECTION_LABEL
3947 #define TEXT_SECTION_LABEL "Ltext"
3948 #endif
3949 #ifndef DEBUG_LINE_SECTION_LABEL
3950 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3951 #endif
3952 #ifndef DEBUG_INFO_SECTION_LABEL
3953 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3954 #endif
3955 #ifndef DEBUG_ABBREV_SECTION_LABEL
3956 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3957 #endif
3958 #ifndef DEBUG_LOC_SECTION_LABEL
3959 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3960 #endif
3961 #ifndef DEBUG_RANGES_SECTION_LABEL
3962 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3963 #endif
3964 #ifndef DEBUG_MACINFO_SECTION_LABEL
3965 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3966 #endif
3968 /* Definitions of defaults for formats and names of various special
3969 (artificial) labels which may be generated within this file (when the -g
3970 options is used and DWARF2_DEBUGGING_INFO is in effect.
3971 If necessary, these may be overridden from within the tm.h file, but
3972 typically, overriding these defaults is unnecessary. */
3974 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3975 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3976 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3977 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3978 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3979 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3980 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3981 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3983 #ifndef TEXT_END_LABEL
3984 #define TEXT_END_LABEL "Letext"
3985 #endif
3986 #ifndef BLOCK_BEGIN_LABEL
3987 #define BLOCK_BEGIN_LABEL "LBB"
3988 #endif
3989 #ifndef BLOCK_END_LABEL
3990 #define BLOCK_END_LABEL "LBE"
3991 #endif
3992 #ifndef LINE_CODE_LABEL
3993 #define LINE_CODE_LABEL "LM"
3994 #endif
3995 #ifndef SEPARATE_LINE_CODE_LABEL
3996 #define SEPARATE_LINE_CODE_LABEL "LSM"
3997 #endif
3999 /* We allow a language front-end to designate a function that is to be
4000 called to "demangle" any name before it it put into a DIE. */
4002 static const char *(*demangle_name_func) (const char *);
4004 void
4005 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4007 demangle_name_func = func;
4010 /* Test if rtl node points to a pseudo register. */
4012 static inline int
4013 is_pseudo_reg (rtx rtl)
4015 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4016 || (GET_CODE (rtl) == SUBREG
4017 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4020 /* Return a reference to a type, with its const and volatile qualifiers
4021 removed. */
4023 static inline tree
4024 type_main_variant (tree type)
4026 type = TYPE_MAIN_VARIANT (type);
4028 /* ??? There really should be only one main variant among any group of
4029 variants of a given type (and all of the MAIN_VARIANT values for all
4030 members of the group should point to that one type) but sometimes the C
4031 front-end messes this up for array types, so we work around that bug
4032 here. */
4033 if (TREE_CODE (type) == ARRAY_TYPE)
4034 while (type != TYPE_MAIN_VARIANT (type))
4035 type = TYPE_MAIN_VARIANT (type);
4037 return type;
4040 /* Return nonzero if the given type node represents a tagged type. */
4042 static inline int
4043 is_tagged_type (tree type)
4045 enum tree_code code = TREE_CODE (type);
4047 return (code == RECORD_TYPE || code == UNION_TYPE
4048 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4051 /* Convert a DIE tag into its string name. */
4053 static const char *
4054 dwarf_tag_name (unsigned int tag)
4056 switch (tag)
4058 case DW_TAG_padding:
4059 return "DW_TAG_padding";
4060 case DW_TAG_array_type:
4061 return "DW_TAG_array_type";
4062 case DW_TAG_class_type:
4063 return "DW_TAG_class_type";
4064 case DW_TAG_entry_point:
4065 return "DW_TAG_entry_point";
4066 case DW_TAG_enumeration_type:
4067 return "DW_TAG_enumeration_type";
4068 case DW_TAG_formal_parameter:
4069 return "DW_TAG_formal_parameter";
4070 case DW_TAG_imported_declaration:
4071 return "DW_TAG_imported_declaration";
4072 case DW_TAG_label:
4073 return "DW_TAG_label";
4074 case DW_TAG_lexical_block:
4075 return "DW_TAG_lexical_block";
4076 case DW_TAG_member:
4077 return "DW_TAG_member";
4078 case DW_TAG_pointer_type:
4079 return "DW_TAG_pointer_type";
4080 case DW_TAG_reference_type:
4081 return "DW_TAG_reference_type";
4082 case DW_TAG_compile_unit:
4083 return "DW_TAG_compile_unit";
4084 case DW_TAG_string_type:
4085 return "DW_TAG_string_type";
4086 case DW_TAG_structure_type:
4087 return "DW_TAG_structure_type";
4088 case DW_TAG_subroutine_type:
4089 return "DW_TAG_subroutine_type";
4090 case DW_TAG_typedef:
4091 return "DW_TAG_typedef";
4092 case DW_TAG_union_type:
4093 return "DW_TAG_union_type";
4094 case DW_TAG_unspecified_parameters:
4095 return "DW_TAG_unspecified_parameters";
4096 case DW_TAG_variant:
4097 return "DW_TAG_variant";
4098 case DW_TAG_common_block:
4099 return "DW_TAG_common_block";
4100 case DW_TAG_common_inclusion:
4101 return "DW_TAG_common_inclusion";
4102 case DW_TAG_inheritance:
4103 return "DW_TAG_inheritance";
4104 case DW_TAG_inlined_subroutine:
4105 return "DW_TAG_inlined_subroutine";
4106 case DW_TAG_module:
4107 return "DW_TAG_module";
4108 case DW_TAG_ptr_to_member_type:
4109 return "DW_TAG_ptr_to_member_type";
4110 case DW_TAG_set_type:
4111 return "DW_TAG_set_type";
4112 case DW_TAG_subrange_type:
4113 return "DW_TAG_subrange_type";
4114 case DW_TAG_with_stmt:
4115 return "DW_TAG_with_stmt";
4116 case DW_TAG_access_declaration:
4117 return "DW_TAG_access_declaration";
4118 case DW_TAG_base_type:
4119 return "DW_TAG_base_type";
4120 case DW_TAG_catch_block:
4121 return "DW_TAG_catch_block";
4122 case DW_TAG_const_type:
4123 return "DW_TAG_const_type";
4124 case DW_TAG_constant:
4125 return "DW_TAG_constant";
4126 case DW_TAG_enumerator:
4127 return "DW_TAG_enumerator";
4128 case DW_TAG_file_type:
4129 return "DW_TAG_file_type";
4130 case DW_TAG_friend:
4131 return "DW_TAG_friend";
4132 case DW_TAG_namelist:
4133 return "DW_TAG_namelist";
4134 case DW_TAG_namelist_item:
4135 return "DW_TAG_namelist_item";
4136 case DW_TAG_namespace:
4137 return "DW_TAG_namespace";
4138 case DW_TAG_packed_type:
4139 return "DW_TAG_packed_type";
4140 case DW_TAG_subprogram:
4141 return "DW_TAG_subprogram";
4142 case DW_TAG_template_type_param:
4143 return "DW_TAG_template_type_param";
4144 case DW_TAG_template_value_param:
4145 return "DW_TAG_template_value_param";
4146 case DW_TAG_thrown_type:
4147 return "DW_TAG_thrown_type";
4148 case DW_TAG_try_block:
4149 return "DW_TAG_try_block";
4150 case DW_TAG_variant_part:
4151 return "DW_TAG_variant_part";
4152 case DW_TAG_variable:
4153 return "DW_TAG_variable";
4154 case DW_TAG_volatile_type:
4155 return "DW_TAG_volatile_type";
4156 case DW_TAG_imported_module:
4157 return "DW_TAG_imported_module";
4158 case DW_TAG_MIPS_loop:
4159 return "DW_TAG_MIPS_loop";
4160 case DW_TAG_format_label:
4161 return "DW_TAG_format_label";
4162 case DW_TAG_function_template:
4163 return "DW_TAG_function_template";
4164 case DW_TAG_class_template:
4165 return "DW_TAG_class_template";
4166 case DW_TAG_GNU_BINCL:
4167 return "DW_TAG_GNU_BINCL";
4168 case DW_TAG_GNU_EINCL:
4169 return "DW_TAG_GNU_EINCL";
4170 default:
4171 return "DW_TAG_<unknown>";
4175 /* Convert a DWARF attribute code into its string name. */
4177 static const char *
4178 dwarf_attr_name (unsigned int attr)
4180 switch (attr)
4182 case DW_AT_sibling:
4183 return "DW_AT_sibling";
4184 case DW_AT_location:
4185 return "DW_AT_location";
4186 case DW_AT_name:
4187 return "DW_AT_name";
4188 case DW_AT_ordering:
4189 return "DW_AT_ordering";
4190 case DW_AT_subscr_data:
4191 return "DW_AT_subscr_data";
4192 case DW_AT_byte_size:
4193 return "DW_AT_byte_size";
4194 case DW_AT_bit_offset:
4195 return "DW_AT_bit_offset";
4196 case DW_AT_bit_size:
4197 return "DW_AT_bit_size";
4198 case DW_AT_element_list:
4199 return "DW_AT_element_list";
4200 case DW_AT_stmt_list:
4201 return "DW_AT_stmt_list";
4202 case DW_AT_low_pc:
4203 return "DW_AT_low_pc";
4204 case DW_AT_high_pc:
4205 return "DW_AT_high_pc";
4206 case DW_AT_language:
4207 return "DW_AT_language";
4208 case DW_AT_member:
4209 return "DW_AT_member";
4210 case DW_AT_discr:
4211 return "DW_AT_discr";
4212 case DW_AT_discr_value:
4213 return "DW_AT_discr_value";
4214 case DW_AT_visibility:
4215 return "DW_AT_visibility";
4216 case DW_AT_import:
4217 return "DW_AT_import";
4218 case DW_AT_string_length:
4219 return "DW_AT_string_length";
4220 case DW_AT_common_reference:
4221 return "DW_AT_common_reference";
4222 case DW_AT_comp_dir:
4223 return "DW_AT_comp_dir";
4224 case DW_AT_const_value:
4225 return "DW_AT_const_value";
4226 case DW_AT_containing_type:
4227 return "DW_AT_containing_type";
4228 case DW_AT_default_value:
4229 return "DW_AT_default_value";
4230 case DW_AT_inline:
4231 return "DW_AT_inline";
4232 case DW_AT_is_optional:
4233 return "DW_AT_is_optional";
4234 case DW_AT_lower_bound:
4235 return "DW_AT_lower_bound";
4236 case DW_AT_producer:
4237 return "DW_AT_producer";
4238 case DW_AT_prototyped:
4239 return "DW_AT_prototyped";
4240 case DW_AT_return_addr:
4241 return "DW_AT_return_addr";
4242 case DW_AT_start_scope:
4243 return "DW_AT_start_scope";
4244 case DW_AT_stride_size:
4245 return "DW_AT_stride_size";
4246 case DW_AT_upper_bound:
4247 return "DW_AT_upper_bound";
4248 case DW_AT_abstract_origin:
4249 return "DW_AT_abstract_origin";
4250 case DW_AT_accessibility:
4251 return "DW_AT_accessibility";
4252 case DW_AT_address_class:
4253 return "DW_AT_address_class";
4254 case DW_AT_artificial:
4255 return "DW_AT_artificial";
4256 case DW_AT_base_types:
4257 return "DW_AT_base_types";
4258 case DW_AT_calling_convention:
4259 return "DW_AT_calling_convention";
4260 case DW_AT_count:
4261 return "DW_AT_count";
4262 case DW_AT_data_member_location:
4263 return "DW_AT_data_member_location";
4264 case DW_AT_decl_column:
4265 return "DW_AT_decl_column";
4266 case DW_AT_decl_file:
4267 return "DW_AT_decl_file";
4268 case DW_AT_decl_line:
4269 return "DW_AT_decl_line";
4270 case DW_AT_declaration:
4271 return "DW_AT_declaration";
4272 case DW_AT_discr_list:
4273 return "DW_AT_discr_list";
4274 case DW_AT_encoding:
4275 return "DW_AT_encoding";
4276 case DW_AT_external:
4277 return "DW_AT_external";
4278 case DW_AT_frame_base:
4279 return "DW_AT_frame_base";
4280 case DW_AT_friend:
4281 return "DW_AT_friend";
4282 case DW_AT_identifier_case:
4283 return "DW_AT_identifier_case";
4284 case DW_AT_macro_info:
4285 return "DW_AT_macro_info";
4286 case DW_AT_namelist_items:
4287 return "DW_AT_namelist_items";
4288 case DW_AT_priority:
4289 return "DW_AT_priority";
4290 case DW_AT_segment:
4291 return "DW_AT_segment";
4292 case DW_AT_specification:
4293 return "DW_AT_specification";
4294 case DW_AT_static_link:
4295 return "DW_AT_static_link";
4296 case DW_AT_type:
4297 return "DW_AT_type";
4298 case DW_AT_use_location:
4299 return "DW_AT_use_location";
4300 case DW_AT_variable_parameter:
4301 return "DW_AT_variable_parameter";
4302 case DW_AT_virtuality:
4303 return "DW_AT_virtuality";
4304 case DW_AT_vtable_elem_location:
4305 return "DW_AT_vtable_elem_location";
4307 case DW_AT_allocated:
4308 return "DW_AT_allocated";
4309 case DW_AT_associated:
4310 return "DW_AT_associated";
4311 case DW_AT_data_location:
4312 return "DW_AT_data_location";
4313 case DW_AT_stride:
4314 return "DW_AT_stride";
4315 case DW_AT_entry_pc:
4316 return "DW_AT_entry_pc";
4317 case DW_AT_use_UTF8:
4318 return "DW_AT_use_UTF8";
4319 case DW_AT_extension:
4320 return "DW_AT_extension";
4321 case DW_AT_ranges:
4322 return "DW_AT_ranges";
4323 case DW_AT_trampoline:
4324 return "DW_AT_trampoline";
4325 case DW_AT_call_column:
4326 return "DW_AT_call_column";
4327 case DW_AT_call_file:
4328 return "DW_AT_call_file";
4329 case DW_AT_call_line:
4330 return "DW_AT_call_line";
4332 case DW_AT_MIPS_fde:
4333 return "DW_AT_MIPS_fde";
4334 case DW_AT_MIPS_loop_begin:
4335 return "DW_AT_MIPS_loop_begin";
4336 case DW_AT_MIPS_tail_loop_begin:
4337 return "DW_AT_MIPS_tail_loop_begin";
4338 case DW_AT_MIPS_epilog_begin:
4339 return "DW_AT_MIPS_epilog_begin";
4340 case DW_AT_MIPS_loop_unroll_factor:
4341 return "DW_AT_MIPS_loop_unroll_factor";
4342 case DW_AT_MIPS_software_pipeline_depth:
4343 return "DW_AT_MIPS_software_pipeline_depth";
4344 case DW_AT_MIPS_linkage_name:
4345 return "DW_AT_MIPS_linkage_name";
4346 case DW_AT_MIPS_stride:
4347 return "DW_AT_MIPS_stride";
4348 case DW_AT_MIPS_abstract_name:
4349 return "DW_AT_MIPS_abstract_name";
4350 case DW_AT_MIPS_clone_origin:
4351 return "DW_AT_MIPS_clone_origin";
4352 case DW_AT_MIPS_has_inlines:
4353 return "DW_AT_MIPS_has_inlines";
4355 case DW_AT_sf_names:
4356 return "DW_AT_sf_names";
4357 case DW_AT_src_info:
4358 return "DW_AT_src_info";
4359 case DW_AT_mac_info:
4360 return "DW_AT_mac_info";
4361 case DW_AT_src_coords:
4362 return "DW_AT_src_coords";
4363 case DW_AT_body_begin:
4364 return "DW_AT_body_begin";
4365 case DW_AT_body_end:
4366 return "DW_AT_body_end";
4367 case DW_AT_GNU_vector:
4368 return "DW_AT_GNU_vector";
4370 case DW_AT_VMS_rtnbeg_pd_address:
4371 return "DW_AT_VMS_rtnbeg_pd_address";
4373 default:
4374 return "DW_AT_<unknown>";
4378 /* Convert a DWARF value form code into its string name. */
4380 static const char *
4381 dwarf_form_name (unsigned int form)
4383 switch (form)
4385 case DW_FORM_addr:
4386 return "DW_FORM_addr";
4387 case DW_FORM_block2:
4388 return "DW_FORM_block2";
4389 case DW_FORM_block4:
4390 return "DW_FORM_block4";
4391 case DW_FORM_data2:
4392 return "DW_FORM_data2";
4393 case DW_FORM_data4:
4394 return "DW_FORM_data4";
4395 case DW_FORM_data8:
4396 return "DW_FORM_data8";
4397 case DW_FORM_string:
4398 return "DW_FORM_string";
4399 case DW_FORM_block:
4400 return "DW_FORM_block";
4401 case DW_FORM_block1:
4402 return "DW_FORM_block1";
4403 case DW_FORM_data1:
4404 return "DW_FORM_data1";
4405 case DW_FORM_flag:
4406 return "DW_FORM_flag";
4407 case DW_FORM_sdata:
4408 return "DW_FORM_sdata";
4409 case DW_FORM_strp:
4410 return "DW_FORM_strp";
4411 case DW_FORM_udata:
4412 return "DW_FORM_udata";
4413 case DW_FORM_ref_addr:
4414 return "DW_FORM_ref_addr";
4415 case DW_FORM_ref1:
4416 return "DW_FORM_ref1";
4417 case DW_FORM_ref2:
4418 return "DW_FORM_ref2";
4419 case DW_FORM_ref4:
4420 return "DW_FORM_ref4";
4421 case DW_FORM_ref8:
4422 return "DW_FORM_ref8";
4423 case DW_FORM_ref_udata:
4424 return "DW_FORM_ref_udata";
4425 case DW_FORM_indirect:
4426 return "DW_FORM_indirect";
4427 default:
4428 return "DW_FORM_<unknown>";
4432 /* Convert a DWARF type code into its string name. */
4434 #if 0
4435 static const char *
4436 dwarf_type_encoding_name (unsigned enc)
4438 switch (enc)
4440 case DW_ATE_address:
4441 return "DW_ATE_address";
4442 case DW_ATE_boolean:
4443 return "DW_ATE_boolean";
4444 case DW_ATE_complex_float:
4445 return "DW_ATE_complex_float";
4446 case DW_ATE_float:
4447 return "DW_ATE_float";
4448 case DW_ATE_signed:
4449 return "DW_ATE_signed";
4450 case DW_ATE_signed_char:
4451 return "DW_ATE_signed_char";
4452 case DW_ATE_unsigned:
4453 return "DW_ATE_unsigned";
4454 case DW_ATE_unsigned_char:
4455 return "DW_ATE_unsigned_char";
4456 default:
4457 return "DW_ATE_<unknown>";
4460 #endif
4462 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4463 instance of an inlined instance of a decl which is local to an inline
4464 function, so we have to trace all of the way back through the origin chain
4465 to find out what sort of node actually served as the original seed for the
4466 given block. */
4468 static tree
4469 decl_ultimate_origin (tree decl)
4471 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4472 nodes in the function to point to themselves; ignore that if
4473 we're trying to output the abstract instance of this function. */
4474 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4475 return NULL_TREE;
4477 #ifdef ENABLE_CHECKING
4478 if (DECL_FROM_INLINE (DECL_ORIGIN (decl)))
4479 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4480 most distant ancestor, this should never happen. */
4481 abort ();
4482 #endif
4484 return DECL_ABSTRACT_ORIGIN (decl);
4487 /* Determine the "ultimate origin" of a block. The block may be an inlined
4488 instance of an inlined instance of a block which is local to an inline
4489 function, so we have to trace all of the way back through the origin chain
4490 to find out what sort of node actually served as the original seed for the
4491 given block. */
4493 static tree
4494 block_ultimate_origin (tree block)
4496 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4498 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4499 nodes in the function to point to themselves; ignore that if
4500 we're trying to output the abstract instance of this function. */
4501 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4502 return NULL_TREE;
4504 if (immediate_origin == NULL_TREE)
4505 return NULL_TREE;
4506 else
4508 tree ret_val;
4509 tree lookahead = immediate_origin;
4513 ret_val = lookahead;
4514 lookahead = (TREE_CODE (ret_val) == BLOCK
4515 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4517 while (lookahead != NULL && lookahead != ret_val);
4519 return ret_val;
4523 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4524 of a virtual function may refer to a base class, so we check the 'this'
4525 parameter. */
4527 static tree
4528 decl_class_context (tree decl)
4530 tree context = NULL_TREE;
4532 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4533 context = DECL_CONTEXT (decl);
4534 else
4535 context = TYPE_MAIN_VARIANT
4536 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4538 if (context && !TYPE_P (context))
4539 context = NULL_TREE;
4541 return context;
4544 /* Add an attribute/value pair to a DIE. We build the lists up in reverse
4545 addition order, and correct that in reverse_all_dies. */
4547 static inline void
4548 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4550 if (die != NULL && attr != NULL)
4552 attr->dw_attr_next = die->die_attr;
4553 die->die_attr = attr;
4557 static inline enum dw_val_class
4558 AT_class (dw_attr_ref a)
4560 return a->dw_attr_val.val_class;
4563 /* Add a flag value attribute to a DIE. */
4565 static inline void
4566 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4568 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4570 attr->dw_attr_next = NULL;
4571 attr->dw_attr = attr_kind;
4572 attr->dw_attr_val.val_class = dw_val_class_flag;
4573 attr->dw_attr_val.v.val_flag = flag;
4574 add_dwarf_attr (die, attr);
4577 static inline unsigned
4578 AT_flag (dw_attr_ref a)
4580 if (a && AT_class (a) == dw_val_class_flag)
4581 return a->dw_attr_val.v.val_flag;
4583 abort ();
4586 /* Add a signed integer attribute value to a DIE. */
4588 static inline void
4589 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4591 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4593 attr->dw_attr_next = NULL;
4594 attr->dw_attr = attr_kind;
4595 attr->dw_attr_val.val_class = dw_val_class_const;
4596 attr->dw_attr_val.v.val_int = int_val;
4597 add_dwarf_attr (die, attr);
4600 static inline HOST_WIDE_INT
4601 AT_int (dw_attr_ref a)
4603 if (a && AT_class (a) == dw_val_class_const)
4604 return a->dw_attr_val.v.val_int;
4606 abort ();
4609 /* Add an unsigned integer attribute value to a DIE. */
4611 static inline void
4612 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4613 unsigned HOST_WIDE_INT unsigned_val)
4615 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4617 attr->dw_attr_next = NULL;
4618 attr->dw_attr = attr_kind;
4619 attr->dw_attr_val.val_class = dw_val_class_unsigned_const;
4620 attr->dw_attr_val.v.val_unsigned = unsigned_val;
4621 add_dwarf_attr (die, attr);
4624 static inline unsigned HOST_WIDE_INT
4625 AT_unsigned (dw_attr_ref a)
4627 if (a && AT_class (a) == dw_val_class_unsigned_const)
4628 return a->dw_attr_val.v.val_unsigned;
4630 abort ();
4633 /* Add an unsigned double integer attribute value to a DIE. */
4635 static inline void
4636 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4637 long unsigned int val_hi, long unsigned int val_low)
4639 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4641 attr->dw_attr_next = NULL;
4642 attr->dw_attr = attr_kind;
4643 attr->dw_attr_val.val_class = dw_val_class_long_long;
4644 attr->dw_attr_val.v.val_long_long.hi = val_hi;
4645 attr->dw_attr_val.v.val_long_long.low = val_low;
4646 add_dwarf_attr (die, attr);
4649 /* Add a floating point attribute value to a DIE and return it. */
4651 static inline void
4652 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4653 unsigned int length, unsigned int elt_size, unsigned char *array)
4655 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4657 attr->dw_attr_next = NULL;
4658 attr->dw_attr = attr_kind;
4659 attr->dw_attr_val.val_class = dw_val_class_vec;
4660 attr->dw_attr_val.v.val_vec.length = length;
4661 attr->dw_attr_val.v.val_vec.elt_size = elt_size;
4662 attr->dw_attr_val.v.val_vec.array = array;
4663 add_dwarf_attr (die, attr);
4666 /* Hash and equality functions for debug_str_hash. */
4668 static hashval_t
4669 debug_str_do_hash (const void *x)
4671 return htab_hash_string (((const struct indirect_string_node *)x)->str);
4674 static int
4675 debug_str_eq (const void *x1, const void *x2)
4677 return strcmp ((((const struct indirect_string_node *)x1)->str),
4678 (const char *)x2) == 0;
4681 /* Add a string attribute value to a DIE. */
4683 static inline void
4684 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4686 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4687 struct indirect_string_node *node;
4688 void **slot;
4690 if (! debug_str_hash)
4691 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4692 debug_str_eq, NULL);
4694 slot = htab_find_slot_with_hash (debug_str_hash, str,
4695 htab_hash_string (str), INSERT);
4696 if (*slot == NULL)
4697 *slot = ggc_alloc_cleared (sizeof (struct indirect_string_node));
4698 node = (struct indirect_string_node *) *slot;
4699 node->str = ggc_strdup (str);
4700 node->refcount++;
4702 attr->dw_attr_next = NULL;
4703 attr->dw_attr = attr_kind;
4704 attr->dw_attr_val.val_class = dw_val_class_str;
4705 attr->dw_attr_val.v.val_str = node;
4706 add_dwarf_attr (die, attr);
4709 static inline const char *
4710 AT_string (dw_attr_ref a)
4712 if (a && AT_class (a) == dw_val_class_str)
4713 return a->dw_attr_val.v.val_str->str;
4715 abort ();
4718 /* Find out whether a string should be output inline in DIE
4719 or out-of-line in .debug_str section. */
4721 static int
4722 AT_string_form (dw_attr_ref a)
4724 if (a && AT_class (a) == dw_val_class_str)
4726 struct indirect_string_node *node;
4727 unsigned int len;
4728 char label[32];
4730 node = a->dw_attr_val.v.val_str;
4731 if (node->form)
4732 return node->form;
4734 len = strlen (node->str) + 1;
4736 /* If the string is shorter or equal to the size of the reference, it is
4737 always better to put it inline. */
4738 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4739 return node->form = DW_FORM_string;
4741 /* If we cannot expect the linker to merge strings in .debug_str
4742 section, only put it into .debug_str if it is worth even in this
4743 single module. */
4744 if ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) == 0
4745 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
4746 return node->form = DW_FORM_string;
4748 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4749 ++dw2_string_counter;
4750 node->label = xstrdup (label);
4752 return node->form = DW_FORM_strp;
4755 abort ();
4758 /* Add a DIE reference attribute value to a DIE. */
4760 static inline void
4761 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4763 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4765 attr->dw_attr_next = NULL;
4766 attr->dw_attr = attr_kind;
4767 attr->dw_attr_val.val_class = dw_val_class_die_ref;
4768 attr->dw_attr_val.v.val_die_ref.die = targ_die;
4769 attr->dw_attr_val.v.val_die_ref.external = 0;
4770 add_dwarf_attr (die, attr);
4773 /* Add an AT_specification attribute to a DIE, and also make the back
4774 pointer from the specification to the definition. */
4776 static inline void
4777 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4779 add_AT_die_ref (die, DW_AT_specification, targ_die);
4780 if (targ_die->die_definition)
4781 abort ();
4782 targ_die->die_definition = die;
4785 static inline dw_die_ref
4786 AT_ref (dw_attr_ref a)
4788 if (a && AT_class (a) == dw_val_class_die_ref)
4789 return a->dw_attr_val.v.val_die_ref.die;
4791 abort ();
4794 static inline int
4795 AT_ref_external (dw_attr_ref a)
4797 if (a && AT_class (a) == dw_val_class_die_ref)
4798 return a->dw_attr_val.v.val_die_ref.external;
4800 return 0;
4803 static inline void
4804 set_AT_ref_external (dw_attr_ref a, int i)
4806 if (a && AT_class (a) == dw_val_class_die_ref)
4807 a->dw_attr_val.v.val_die_ref.external = i;
4808 else
4809 abort ();
4812 /* Add an FDE reference attribute value to a DIE. */
4814 static inline void
4815 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4817 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4819 attr->dw_attr_next = NULL;
4820 attr->dw_attr = attr_kind;
4821 attr->dw_attr_val.val_class = dw_val_class_fde_ref;
4822 attr->dw_attr_val.v.val_fde_index = targ_fde;
4823 add_dwarf_attr (die, attr);
4826 /* Add a location description attribute value to a DIE. */
4828 static inline void
4829 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4831 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4833 attr->dw_attr_next = NULL;
4834 attr->dw_attr = attr_kind;
4835 attr->dw_attr_val.val_class = dw_val_class_loc;
4836 attr->dw_attr_val.v.val_loc = loc;
4837 add_dwarf_attr (die, attr);
4840 static inline dw_loc_descr_ref
4841 AT_loc (dw_attr_ref a)
4843 if (a && AT_class (a) == dw_val_class_loc)
4844 return a->dw_attr_val.v.val_loc;
4846 abort ();
4849 static inline void
4850 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4852 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4854 attr->dw_attr_next = NULL;
4855 attr->dw_attr = attr_kind;
4856 attr->dw_attr_val.val_class = dw_val_class_loc_list;
4857 attr->dw_attr_val.v.val_loc_list = loc_list;
4858 add_dwarf_attr (die, attr);
4859 have_location_lists = 1;
4862 static inline dw_loc_list_ref
4863 AT_loc_list (dw_attr_ref a)
4865 if (a && AT_class (a) == dw_val_class_loc_list)
4866 return a->dw_attr_val.v.val_loc_list;
4868 abort ();
4871 /* Add an address constant attribute value to a DIE. */
4873 static inline void
4874 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
4876 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4878 attr->dw_attr_next = NULL;
4879 attr->dw_attr = attr_kind;
4880 attr->dw_attr_val.val_class = dw_val_class_addr;
4881 attr->dw_attr_val.v.val_addr = addr;
4882 add_dwarf_attr (die, attr);
4885 static inline rtx
4886 AT_addr (dw_attr_ref a)
4888 if (a && AT_class (a) == dw_val_class_addr)
4889 return a->dw_attr_val.v.val_addr;
4891 abort ();
4894 /* Add a label identifier attribute value to a DIE. */
4896 static inline void
4897 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
4899 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4901 attr->dw_attr_next = NULL;
4902 attr->dw_attr = attr_kind;
4903 attr->dw_attr_val.val_class = dw_val_class_lbl_id;
4904 attr->dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4905 add_dwarf_attr (die, attr);
4908 /* Add a section offset attribute value to a DIE. */
4910 static inline void
4911 add_AT_lbl_offset (dw_die_ref die, enum dwarf_attribute attr_kind, const char *label)
4913 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4915 attr->dw_attr_next = NULL;
4916 attr->dw_attr = attr_kind;
4917 attr->dw_attr_val.val_class = dw_val_class_lbl_offset;
4918 attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
4919 add_dwarf_attr (die, attr);
4922 /* Add an offset attribute value to a DIE. */
4924 static inline void
4925 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4926 unsigned HOST_WIDE_INT offset)
4928 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4930 attr->dw_attr_next = NULL;
4931 attr->dw_attr = attr_kind;
4932 attr->dw_attr_val.val_class = dw_val_class_offset;
4933 attr->dw_attr_val.v.val_offset = offset;
4934 add_dwarf_attr (die, attr);
4937 /* Add an range_list attribute value to a DIE. */
4939 static void
4940 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4941 long unsigned int offset)
4943 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4945 attr->dw_attr_next = NULL;
4946 attr->dw_attr = attr_kind;
4947 attr->dw_attr_val.val_class = dw_val_class_range_list;
4948 attr->dw_attr_val.v.val_offset = offset;
4949 add_dwarf_attr (die, attr);
4952 static inline const char *
4953 AT_lbl (dw_attr_ref a)
4955 if (a && (AT_class (a) == dw_val_class_lbl_id
4956 || AT_class (a) == dw_val_class_lbl_offset))
4957 return a->dw_attr_val.v.val_lbl_id;
4959 abort ();
4962 /* Get the attribute of type attr_kind. */
4964 static dw_attr_ref
4965 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4967 dw_attr_ref a;
4968 dw_die_ref spec = NULL;
4970 if (die != NULL)
4972 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
4973 if (a->dw_attr == attr_kind)
4974 return a;
4975 else if (a->dw_attr == DW_AT_specification
4976 || a->dw_attr == DW_AT_abstract_origin)
4977 spec = AT_ref (a);
4979 if (spec)
4980 return get_AT (spec, attr_kind);
4983 return NULL;
4986 /* Return the "low pc" attribute value, typically associated with a subprogram
4987 DIE. Return null if the "low pc" attribute is either not present, or if it
4988 cannot be represented as an assembler label identifier. */
4990 static inline const char *
4991 get_AT_low_pc (dw_die_ref die)
4993 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4995 return a ? AT_lbl (a) : NULL;
4998 /* Return the "high pc" attribute value, typically associated with a subprogram
4999 DIE. Return null if the "high pc" attribute is either not present, or if it
5000 cannot be represented as an assembler label identifier. */
5002 static inline const char *
5003 get_AT_hi_pc (dw_die_ref die)
5005 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5007 return a ? AT_lbl (a) : NULL;
5010 /* Return the value of the string attribute designated by ATTR_KIND, or
5011 NULL if it is not present. */
5013 static inline const char *
5014 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5016 dw_attr_ref a = get_AT (die, attr_kind);
5018 return a ? AT_string (a) : NULL;
5021 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5022 if it is not present. */
5024 static inline int
5025 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5027 dw_attr_ref a = get_AT (die, attr_kind);
5029 return a ? AT_flag (a) : 0;
5032 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5033 if it is not present. */
5035 static inline unsigned
5036 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5038 dw_attr_ref a = get_AT (die, attr_kind);
5040 return a ? AT_unsigned (a) : 0;
5043 static inline dw_die_ref
5044 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5046 dw_attr_ref a = get_AT (die, attr_kind);
5048 return a ? AT_ref (a) : NULL;
5051 /* Return TRUE if the language is C or C++. */
5053 static inline bool
5054 is_c_family (void)
5056 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5058 return (lang == DW_LANG_C || lang == DW_LANG_C89
5059 || lang == DW_LANG_C_plus_plus);
5062 /* Return TRUE if the language is C++. */
5064 static inline bool
5065 is_cxx (void)
5067 return (get_AT_unsigned (comp_unit_die, DW_AT_language)
5068 == DW_LANG_C_plus_plus);
5071 /* Return TRUE if the language is Fortran. */
5073 static inline bool
5074 is_fortran (void)
5076 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5078 return (lang == DW_LANG_Fortran77
5079 || lang == DW_LANG_Fortran90
5080 || lang == DW_LANG_Fortran95);
5083 /* Return TRUE if the language is Java. */
5085 static inline bool
5086 is_java (void)
5088 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5090 return lang == DW_LANG_Java;
5093 /* Return TRUE if the language is Ada. */
5095 static inline bool
5096 is_ada (void)
5098 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5100 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5103 /* Free up the memory used by A. */
5105 static inline void free_AT (dw_attr_ref);
5106 static inline void
5107 free_AT (dw_attr_ref a)
5109 if (AT_class (a) == dw_val_class_str)
5110 if (a->dw_attr_val.v.val_str->refcount)
5111 a->dw_attr_val.v.val_str->refcount--;
5114 /* Remove the specified attribute if present. */
5116 static void
5117 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5119 dw_attr_ref *p;
5120 dw_attr_ref removed = NULL;
5122 if (die != NULL)
5124 for (p = &(die->die_attr); *p; p = &((*p)->dw_attr_next))
5125 if ((*p)->dw_attr == attr_kind)
5127 removed = *p;
5128 *p = (*p)->dw_attr_next;
5129 break;
5132 if (removed != 0)
5133 free_AT (removed);
5137 /* Remove child die whose die_tag is specified tag. */
5139 static void
5140 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5142 dw_die_ref current, prev, next;
5143 current = die->die_child;
5144 prev = NULL;
5145 while (current != NULL)
5147 if (current->die_tag == tag)
5149 next = current->die_sib;
5150 if (prev == NULL)
5151 die->die_child = next;
5152 else
5153 prev->die_sib = next;
5154 free_die (current);
5155 current = next;
5157 else
5159 prev = current;
5160 current = current->die_sib;
5165 /* Free up the memory used by DIE. */
5167 static inline void
5168 free_die (dw_die_ref die)
5170 remove_children (die);
5173 /* Discard the children of this DIE. */
5175 static void
5176 remove_children (dw_die_ref die)
5178 dw_die_ref child_die = die->die_child;
5180 die->die_child = NULL;
5182 while (child_die != NULL)
5184 dw_die_ref tmp_die = child_die;
5185 dw_attr_ref a;
5187 child_die = child_die->die_sib;
5189 for (a = tmp_die->die_attr; a != NULL;)
5191 dw_attr_ref tmp_a = a;
5193 a = a->dw_attr_next;
5194 free_AT (tmp_a);
5197 free_die (tmp_die);
5201 /* Add a child DIE below its parent. We build the lists up in reverse
5202 addition order, and correct that in reverse_all_dies. */
5204 static inline void
5205 add_child_die (dw_die_ref die, dw_die_ref child_die)
5207 if (die != NULL && child_die != NULL)
5209 if (die == child_die)
5210 abort ();
5212 child_die->die_parent = die;
5213 child_die->die_sib = die->die_child;
5214 die->die_child = child_die;
5218 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5219 is the specification, to the front of PARENT's list of children. */
5221 static void
5222 splice_child_die (dw_die_ref parent, dw_die_ref child)
5224 dw_die_ref *p;
5226 /* We want the declaration DIE from inside the class, not the
5227 specification DIE at toplevel. */
5228 if (child->die_parent != parent)
5230 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5232 if (tmp)
5233 child = tmp;
5236 if (child->die_parent != parent
5237 && child->die_parent != get_AT_ref (parent, DW_AT_specification))
5238 abort ();
5240 for (p = &(child->die_parent->die_child); *p; p = &((*p)->die_sib))
5241 if (*p == child)
5243 *p = child->die_sib;
5244 break;
5247 child->die_parent = parent;
5248 child->die_sib = parent->die_child;
5249 parent->die_child = child;
5252 /* Return a pointer to a newly created DIE node. */
5254 static inline dw_die_ref
5255 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5257 dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5259 die->die_tag = tag_value;
5261 if (parent_die != NULL)
5262 add_child_die (parent_die, die);
5263 else
5265 limbo_die_node *limbo_node;
5267 limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5268 limbo_node->die = die;
5269 limbo_node->created_for = t;
5270 limbo_node->next = limbo_die_list;
5271 limbo_die_list = limbo_node;
5274 return die;
5277 /* Return the DIE associated with the given type specifier. */
5279 static inline dw_die_ref
5280 lookup_type_die (tree type)
5282 return TYPE_SYMTAB_DIE (type);
5285 /* Equate a DIE to a given type specifier. */
5287 static inline void
5288 equate_type_number_to_die (tree type, dw_die_ref type_die)
5290 TYPE_SYMTAB_DIE (type) = type_die;
5293 /* Returns a hash value for X (which really is a die_struct). */
5295 static hashval_t
5296 decl_die_table_hash (const void *x)
5298 return (hashval_t) ((const dw_die_ref) x)->decl_id;
5301 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5303 static int
5304 decl_die_table_eq (const void *x, const void *y)
5306 return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5309 /* Return the DIE associated with a given declaration. */
5311 static inline dw_die_ref
5312 lookup_decl_die (tree decl)
5314 return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5317 /* Returns a hash value for X (which really is a var_loc_list). */
5319 static hashval_t
5320 decl_loc_table_hash (const void *x)
5322 return (hashval_t) ((const var_loc_list *) x)->decl_id;
5325 /* Return nonzero if decl_id of var_loc_list X is the same as
5326 UID of decl *Y. */
5328 static int
5329 decl_loc_table_eq (const void *x, const void *y)
5331 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5334 /* Return the var_loc list associated with a given declaration. */
5336 static inline var_loc_list *
5337 lookup_decl_loc (tree decl)
5339 return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5342 /* Equate a DIE to a particular declaration. */
5344 static void
5345 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5347 unsigned int decl_id = DECL_UID (decl);
5348 void **slot;
5350 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5351 *slot = decl_die;
5352 decl_die->decl_id = decl_id;
5355 /* Add a variable location node to the linked list for DECL. */
5357 static void
5358 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5360 unsigned int decl_id = DECL_UID (decl);
5361 var_loc_list *temp;
5362 void **slot;
5364 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5365 if (*slot == NULL)
5367 temp = ggc_alloc_cleared (sizeof (var_loc_list));
5368 temp->decl_id = decl_id;
5369 *slot = temp;
5371 else
5372 temp = *slot;
5374 if (temp->last)
5376 /* If the current location is the same as the end of the list,
5377 we have nothing to do. */
5378 if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5379 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5381 /* Add LOC to the end of list and update LAST. */
5382 temp->last->next = loc;
5383 temp->last = loc;
5386 /* Do not add empty location to the beginning of the list. */
5387 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5389 temp->first = loc;
5390 temp->last = loc;
5394 /* Keep track of the number of spaces used to indent the
5395 output of the debugging routines that print the structure of
5396 the DIE internal representation. */
5397 static int print_indent;
5399 /* Indent the line the number of spaces given by print_indent. */
5401 static inline void
5402 print_spaces (FILE *outfile)
5404 fprintf (outfile, "%*s", print_indent, "");
5407 /* Print the information associated with a given DIE, and its children.
5408 This routine is a debugging aid only. */
5410 static void
5411 print_die (dw_die_ref die, FILE *outfile)
5413 dw_attr_ref a;
5414 dw_die_ref c;
5416 print_spaces (outfile);
5417 fprintf (outfile, "DIE %4lu: %s\n",
5418 die->die_offset, dwarf_tag_name (die->die_tag));
5419 print_spaces (outfile);
5420 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5421 fprintf (outfile, " offset: %lu\n", die->die_offset);
5423 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5425 print_spaces (outfile);
5426 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5428 switch (AT_class (a))
5430 case dw_val_class_addr:
5431 fprintf (outfile, "address");
5432 break;
5433 case dw_val_class_offset:
5434 fprintf (outfile, "offset");
5435 break;
5436 case dw_val_class_loc:
5437 fprintf (outfile, "location descriptor");
5438 break;
5439 case dw_val_class_loc_list:
5440 fprintf (outfile, "location list -> label:%s",
5441 AT_loc_list (a)->ll_symbol);
5442 break;
5443 case dw_val_class_range_list:
5444 fprintf (outfile, "range list");
5445 break;
5446 case dw_val_class_const:
5447 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5448 break;
5449 case dw_val_class_unsigned_const:
5450 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5451 break;
5452 case dw_val_class_long_long:
5453 fprintf (outfile, "constant (%lu,%lu)",
5454 a->dw_attr_val.v.val_long_long.hi,
5455 a->dw_attr_val.v.val_long_long.low);
5456 break;
5457 case dw_val_class_vec:
5458 fprintf (outfile, "floating-point or vector constant");
5459 break;
5460 case dw_val_class_flag:
5461 fprintf (outfile, "%u", AT_flag (a));
5462 break;
5463 case dw_val_class_die_ref:
5464 if (AT_ref (a) != NULL)
5466 if (AT_ref (a)->die_symbol)
5467 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5468 else
5469 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5471 else
5472 fprintf (outfile, "die -> <null>");
5473 break;
5474 case dw_val_class_lbl_id:
5475 case dw_val_class_lbl_offset:
5476 fprintf (outfile, "label: %s", AT_lbl (a));
5477 break;
5478 case dw_val_class_str:
5479 if (AT_string (a) != NULL)
5480 fprintf (outfile, "\"%s\"", AT_string (a));
5481 else
5482 fprintf (outfile, "<null>");
5483 break;
5484 default:
5485 break;
5488 fprintf (outfile, "\n");
5491 if (die->die_child != NULL)
5493 print_indent += 4;
5494 for (c = die->die_child; c != NULL; c = c->die_sib)
5495 print_die (c, outfile);
5497 print_indent -= 4;
5499 if (print_indent == 0)
5500 fprintf (outfile, "\n");
5503 /* Print the contents of the source code line number correspondence table.
5504 This routine is a debugging aid only. */
5506 static void
5507 print_dwarf_line_table (FILE *outfile)
5509 unsigned i;
5510 dw_line_info_ref line_info;
5512 fprintf (outfile, "\n\nDWARF source line information\n");
5513 for (i = 1; i < line_info_table_in_use; i++)
5515 line_info = &line_info_table[i];
5516 fprintf (outfile, "%5d: ", i);
5517 fprintf (outfile, "%-20s",
5518 VARRAY_CHAR_PTR (file_table, line_info->dw_file_num));
5519 fprintf (outfile, "%6ld", line_info->dw_line_num);
5520 fprintf (outfile, "\n");
5523 fprintf (outfile, "\n\n");
5526 /* Print the information collected for a given DIE. */
5528 void
5529 debug_dwarf_die (dw_die_ref die)
5531 print_die (die, stderr);
5534 /* Print all DWARF information collected for the compilation unit.
5535 This routine is a debugging aid only. */
5537 void
5538 debug_dwarf (void)
5540 print_indent = 0;
5541 print_die (comp_unit_die, stderr);
5542 if (! DWARF2_ASM_LINE_DEBUG_INFO)
5543 print_dwarf_line_table (stderr);
5546 /* We build up the lists of children and attributes by pushing new ones
5547 onto the beginning of the list. Reverse the lists for DIE so that
5548 they are in order of addition. */
5550 static void
5551 reverse_die_lists (dw_die_ref die)
5553 dw_die_ref c, cp, cn;
5554 dw_attr_ref a, ap, an;
5556 for (a = die->die_attr, ap = 0; a; a = an)
5558 an = a->dw_attr_next;
5559 a->dw_attr_next = ap;
5560 ap = a;
5563 die->die_attr = ap;
5565 for (c = die->die_child, cp = 0; c; c = cn)
5567 cn = c->die_sib;
5568 c->die_sib = cp;
5569 cp = c;
5572 die->die_child = cp;
5575 /* reverse_die_lists only reverses the single die you pass it. Since we used to
5576 reverse all dies in add_sibling_attributes, which runs through all the dies,
5577 it would reverse all the dies. Now, however, since we don't call
5578 reverse_die_lists in add_sibling_attributes, we need a routine to
5579 recursively reverse all the dies. This is that routine. */
5581 static void
5582 reverse_all_dies (dw_die_ref die)
5584 dw_die_ref c;
5586 reverse_die_lists (die);
5588 for (c = die->die_child; c; c = c->die_sib)
5589 reverse_all_dies (c);
5592 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5593 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5594 DIE that marks the start of the DIEs for this include file. */
5596 static dw_die_ref
5597 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5599 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5600 dw_die_ref new_unit = gen_compile_unit_die (filename);
5602 new_unit->die_sib = old_unit;
5603 return new_unit;
5606 /* Close an include-file CU and reopen the enclosing one. */
5608 static dw_die_ref
5609 pop_compile_unit (dw_die_ref old_unit)
5611 dw_die_ref new_unit = old_unit->die_sib;
5613 old_unit->die_sib = NULL;
5614 return new_unit;
5617 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5618 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5620 /* Calculate the checksum of a location expression. */
5622 static inline void
5623 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5625 CHECKSUM (loc->dw_loc_opc);
5626 CHECKSUM (loc->dw_loc_oprnd1);
5627 CHECKSUM (loc->dw_loc_oprnd2);
5630 /* Calculate the checksum of an attribute. */
5632 static void
5633 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5635 dw_loc_descr_ref loc;
5636 rtx r;
5638 CHECKSUM (at->dw_attr);
5640 /* We don't care about differences in file numbering. */
5641 if (at->dw_attr == DW_AT_decl_file
5642 /* Or that this was compiled with a different compiler snapshot; if
5643 the output is the same, that's what matters. */
5644 || at->dw_attr == DW_AT_producer)
5645 return;
5647 switch (AT_class (at))
5649 case dw_val_class_const:
5650 CHECKSUM (at->dw_attr_val.v.val_int);
5651 break;
5652 case dw_val_class_unsigned_const:
5653 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5654 break;
5655 case dw_val_class_long_long:
5656 CHECKSUM (at->dw_attr_val.v.val_long_long);
5657 break;
5658 case dw_val_class_vec:
5659 CHECKSUM (at->dw_attr_val.v.val_vec);
5660 break;
5661 case dw_val_class_flag:
5662 CHECKSUM (at->dw_attr_val.v.val_flag);
5663 break;
5664 case dw_val_class_str:
5665 CHECKSUM_STRING (AT_string (at));
5666 break;
5668 case dw_val_class_addr:
5669 r = AT_addr (at);
5670 switch (GET_CODE (r))
5672 case SYMBOL_REF:
5673 CHECKSUM_STRING (XSTR (r, 0));
5674 break;
5676 default:
5677 abort ();
5679 break;
5681 case dw_val_class_offset:
5682 CHECKSUM (at->dw_attr_val.v.val_offset);
5683 break;
5685 case dw_val_class_loc:
5686 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5687 loc_checksum (loc, ctx);
5688 break;
5690 case dw_val_class_die_ref:
5691 die_checksum (AT_ref (at), ctx, mark);
5692 break;
5694 case dw_val_class_fde_ref:
5695 case dw_val_class_lbl_id:
5696 case dw_val_class_lbl_offset:
5697 break;
5699 default:
5700 break;
5704 /* Calculate the checksum of a DIE. */
5706 static void
5707 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5709 dw_die_ref c;
5710 dw_attr_ref a;
5712 /* To avoid infinite recursion. */
5713 if (die->die_mark)
5715 CHECKSUM (die->die_mark);
5716 return;
5718 die->die_mark = ++(*mark);
5720 CHECKSUM (die->die_tag);
5722 for (a = die->die_attr; a; a = a->dw_attr_next)
5723 attr_checksum (a, ctx, mark);
5725 for (c = die->die_child; c; c = c->die_sib)
5726 die_checksum (c, ctx, mark);
5729 #undef CHECKSUM
5730 #undef CHECKSUM_STRING
5732 /* Do the location expressions look same? */
5733 static inline int
5734 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
5736 return loc1->dw_loc_opc == loc2->dw_loc_opc
5737 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
5738 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
5741 /* Do the values look the same? */
5742 static int
5743 same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
5745 dw_loc_descr_ref loc1, loc2;
5746 rtx r1, r2;
5748 if (v1->val_class != v2->val_class)
5749 return 0;
5751 switch (v1->val_class)
5753 case dw_val_class_const:
5754 return v1->v.val_int == v2->v.val_int;
5755 case dw_val_class_unsigned_const:
5756 return v1->v.val_unsigned == v2->v.val_unsigned;
5757 case dw_val_class_long_long:
5758 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
5759 && v1->v.val_long_long.low == v2->v.val_long_long.low;
5760 case dw_val_class_vec:
5761 if (v1->v.val_vec.length != v2->v.val_vec.length
5762 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
5763 return 0;
5764 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
5765 v1->v.val_vec.length * v1->v.val_vec.elt_size))
5766 return 0;
5767 return 1;
5768 case dw_val_class_flag:
5769 return v1->v.val_flag == v2->v.val_flag;
5770 case dw_val_class_str:
5771 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
5773 case dw_val_class_addr:
5774 r1 = v1->v.val_addr;
5775 r2 = v2->v.val_addr;
5776 if (GET_CODE (r1) != GET_CODE (r2))
5777 return 0;
5778 switch (GET_CODE (r1))
5780 case SYMBOL_REF:
5781 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
5783 default:
5784 abort ();
5787 case dw_val_class_offset:
5788 return v1->v.val_offset == v2->v.val_offset;
5790 case dw_val_class_loc:
5791 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
5792 loc1 && loc2;
5793 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
5794 if (!same_loc_p (loc1, loc2, mark))
5795 return 0;
5796 return !loc1 && !loc2;
5798 case dw_val_class_die_ref:
5799 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
5801 case dw_val_class_fde_ref:
5802 case dw_val_class_lbl_id:
5803 case dw_val_class_lbl_offset:
5804 return 1;
5806 default:
5807 return 1;
5811 /* Do the attributes look the same? */
5813 static int
5814 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
5816 if (at1->dw_attr != at2->dw_attr)
5817 return 0;
5819 /* We don't care about differences in file numbering. */
5820 if (at1->dw_attr == DW_AT_decl_file
5821 /* Or that this was compiled with a different compiler snapshot; if
5822 the output is the same, that's what matters. */
5823 || at1->dw_attr == DW_AT_producer)
5824 return 1;
5826 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
5829 /* Do the dies look the same? */
5831 static int
5832 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
5834 dw_die_ref c1, c2;
5835 dw_attr_ref a1, a2;
5837 /* To avoid infinite recursion. */
5838 if (die1->die_mark)
5839 return die1->die_mark == die2->die_mark;
5840 die1->die_mark = die2->die_mark = ++(*mark);
5842 if (die1->die_tag != die2->die_tag)
5843 return 0;
5845 for (a1 = die1->die_attr, a2 = die2->die_attr;
5846 a1 && a2;
5847 a1 = a1->dw_attr_next, a2 = a2->dw_attr_next)
5848 if (!same_attr_p (a1, a2, mark))
5849 return 0;
5850 if (a1 || a2)
5851 return 0;
5853 for (c1 = die1->die_child, c2 = die2->die_child;
5854 c1 && c2;
5855 c1 = c1->die_sib, c2 = c2->die_sib)
5856 if (!same_die_p (c1, c2, mark))
5857 return 0;
5858 if (c1 || c2)
5859 return 0;
5861 return 1;
5864 /* Do the dies look the same? Wrapper around same_die_p. */
5866 static int
5867 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
5869 int mark = 0;
5870 int ret = same_die_p (die1, die2, &mark);
5872 unmark_all_dies (die1);
5873 unmark_all_dies (die2);
5875 return ret;
5878 /* The prefix to attach to symbols on DIEs in the current comdat debug
5879 info section. */
5880 static char *comdat_symbol_id;
5882 /* The index of the current symbol within the current comdat CU. */
5883 static unsigned int comdat_symbol_number;
5885 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
5886 children, and set comdat_symbol_id accordingly. */
5888 static void
5889 compute_section_prefix (dw_die_ref unit_die)
5891 const char *die_name = get_AT_string (unit_die, DW_AT_name);
5892 const char *base = die_name ? lbasename (die_name) : "anonymous";
5893 char *name = alloca (strlen (base) + 64);
5894 char *p;
5895 int i, mark;
5896 unsigned char checksum[16];
5897 struct md5_ctx ctx;
5899 /* Compute the checksum of the DIE, then append part of it as hex digits to
5900 the name filename of the unit. */
5902 md5_init_ctx (&ctx);
5903 mark = 0;
5904 die_checksum (unit_die, &ctx, &mark);
5905 unmark_all_dies (unit_die);
5906 md5_finish_ctx (&ctx, checksum);
5908 sprintf (name, "%s.", base);
5909 clean_symbol_name (name);
5911 p = name + strlen (name);
5912 for (i = 0; i < 4; i++)
5914 sprintf (p, "%.2x", checksum[i]);
5915 p += 2;
5918 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
5919 comdat_symbol_number = 0;
5922 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
5924 static int
5925 is_type_die (dw_die_ref die)
5927 switch (die->die_tag)
5929 case DW_TAG_array_type:
5930 case DW_TAG_class_type:
5931 case DW_TAG_enumeration_type:
5932 case DW_TAG_pointer_type:
5933 case DW_TAG_reference_type:
5934 case DW_TAG_string_type:
5935 case DW_TAG_structure_type:
5936 case DW_TAG_subroutine_type:
5937 case DW_TAG_union_type:
5938 case DW_TAG_ptr_to_member_type:
5939 case DW_TAG_set_type:
5940 case DW_TAG_subrange_type:
5941 case DW_TAG_base_type:
5942 case DW_TAG_const_type:
5943 case DW_TAG_file_type:
5944 case DW_TAG_packed_type:
5945 case DW_TAG_volatile_type:
5946 case DW_TAG_typedef:
5947 return 1;
5948 default:
5949 return 0;
5953 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
5954 Basically, we want to choose the bits that are likely to be shared between
5955 compilations (types) and leave out the bits that are specific to individual
5956 compilations (functions). */
5958 static int
5959 is_comdat_die (dw_die_ref c)
5961 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
5962 we do for stabs. The advantage is a greater likelihood of sharing between
5963 objects that don't include headers in the same order (and therefore would
5964 put the base types in a different comdat). jason 8/28/00 */
5966 if (c->die_tag == DW_TAG_base_type)
5967 return 0;
5969 if (c->die_tag == DW_TAG_pointer_type
5970 || c->die_tag == DW_TAG_reference_type
5971 || c->die_tag == DW_TAG_const_type
5972 || c->die_tag == DW_TAG_volatile_type)
5974 dw_die_ref t = get_AT_ref (c, DW_AT_type);
5976 return t ? is_comdat_die (t) : 0;
5979 return is_type_die (c);
5982 /* Returns 1 iff C is the sort of DIE that might be referred to from another
5983 compilation unit. */
5985 static int
5986 is_symbol_die (dw_die_ref c)
5988 return (is_type_die (c)
5989 || (get_AT (c, DW_AT_declaration)
5990 && !get_AT (c, DW_AT_specification)));
5993 static char *
5994 gen_internal_sym (const char *prefix)
5996 char buf[256];
5998 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
5999 return xstrdup (buf);
6002 /* Assign symbols to all worthy DIEs under DIE. */
6004 static void
6005 assign_symbol_names (dw_die_ref die)
6007 dw_die_ref c;
6009 if (is_symbol_die (die))
6011 if (comdat_symbol_id)
6013 char *p = alloca (strlen (comdat_symbol_id) + 64);
6015 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6016 comdat_symbol_id, comdat_symbol_number++);
6017 die->die_symbol = xstrdup (p);
6019 else
6020 die->die_symbol = gen_internal_sym ("LDIE");
6023 for (c = die->die_child; c != NULL; c = c->die_sib)
6024 assign_symbol_names (c);
6027 struct cu_hash_table_entry
6029 dw_die_ref cu;
6030 unsigned min_comdat_num, max_comdat_num;
6031 struct cu_hash_table_entry *next;
6034 /* Routines to manipulate hash table of CUs. */
6035 static hashval_t
6036 htab_cu_hash (const void *of)
6038 const struct cu_hash_table_entry *entry = of;
6040 return htab_hash_string (entry->cu->die_symbol);
6043 static int
6044 htab_cu_eq (const void *of1, const void *of2)
6046 const struct cu_hash_table_entry *entry1 = of1;
6047 const struct die_struct *entry2 = of2;
6049 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6052 static void
6053 htab_cu_del (void *what)
6055 struct cu_hash_table_entry *next, *entry = what;
6057 while (entry)
6059 next = entry->next;
6060 free (entry);
6061 entry = next;
6065 /* Check whether we have already seen this CU and set up SYM_NUM
6066 accordingly. */
6067 static int
6068 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6070 struct cu_hash_table_entry dummy;
6071 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6073 dummy.max_comdat_num = 0;
6075 slot = (struct cu_hash_table_entry **)
6076 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6077 INSERT);
6078 entry = *slot;
6080 for (; entry; last = entry, entry = entry->next)
6082 if (same_die_p_wrap (cu, entry->cu))
6083 break;
6086 if (entry)
6088 *sym_num = entry->min_comdat_num;
6089 return 1;
6092 entry = xcalloc (1, sizeof (struct cu_hash_table_entry));
6093 entry->cu = cu;
6094 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6095 entry->next = *slot;
6096 *slot = entry;
6098 return 0;
6101 /* Record SYM_NUM to record of CU in HTABLE. */
6102 static void
6103 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6105 struct cu_hash_table_entry **slot, *entry;
6107 slot = (struct cu_hash_table_entry **)
6108 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6109 NO_INSERT);
6110 entry = *slot;
6112 entry->max_comdat_num = sym_num;
6115 /* Traverse the DIE (which is always comp_unit_die), and set up
6116 additional compilation units for each of the include files we see
6117 bracketed by BINCL/EINCL. */
6119 static void
6120 break_out_includes (dw_die_ref die)
6122 dw_die_ref *ptr;
6123 dw_die_ref unit = NULL;
6124 limbo_die_node *node, **pnode;
6125 htab_t cu_hash_table;
6127 for (ptr = &(die->die_child); *ptr;)
6129 dw_die_ref c = *ptr;
6131 if (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6132 || (unit && is_comdat_die (c)))
6134 /* This DIE is for a secondary CU; remove it from the main one. */
6135 *ptr = c->die_sib;
6137 if (c->die_tag == DW_TAG_GNU_BINCL)
6139 unit = push_new_compile_unit (unit, c);
6140 free_die (c);
6142 else if (c->die_tag == DW_TAG_GNU_EINCL)
6144 unit = pop_compile_unit (unit);
6145 free_die (c);
6147 else
6148 add_child_die (unit, c);
6150 else
6152 /* Leave this DIE in the main CU. */
6153 ptr = &(c->die_sib);
6154 continue;
6158 #if 0
6159 /* We can only use this in debugging, since the frontend doesn't check
6160 to make sure that we leave every include file we enter. */
6161 if (unit != NULL)
6162 abort ();
6163 #endif
6165 assign_symbol_names (die);
6166 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6167 for (node = limbo_die_list, pnode = &limbo_die_list;
6168 node;
6169 node = node->next)
6171 int is_dupl;
6173 compute_section_prefix (node->die);
6174 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6175 &comdat_symbol_number);
6176 assign_symbol_names (node->die);
6177 if (is_dupl)
6178 *pnode = node->next;
6179 else
6181 pnode = &node->next;
6182 record_comdat_symbol_number (node->die, cu_hash_table,
6183 comdat_symbol_number);
6186 htab_delete (cu_hash_table);
6189 /* Traverse the DIE and add a sibling attribute if it may have the
6190 effect of speeding up access to siblings. To save some space,
6191 avoid generating sibling attributes for DIE's without children. */
6193 static void
6194 add_sibling_attributes (dw_die_ref die)
6196 dw_die_ref c;
6198 if (die->die_tag != DW_TAG_compile_unit
6199 && die->die_sib && die->die_child != NULL)
6200 /* Add the sibling link to the front of the attribute list. */
6201 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6203 for (c = die->die_child; c != NULL; c = c->die_sib)
6204 add_sibling_attributes (c);
6207 /* Output all location lists for the DIE and its children. */
6209 static void
6210 output_location_lists (dw_die_ref die)
6212 dw_die_ref c;
6213 dw_attr_ref d_attr;
6215 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6216 if (AT_class (d_attr) == dw_val_class_loc_list)
6217 output_loc_list (AT_loc_list (d_attr));
6219 for (c = die->die_child; c != NULL; c = c->die_sib)
6220 output_location_lists (c);
6224 /* The format of each DIE (and its attribute value pairs) is encoded in an
6225 abbreviation table. This routine builds the abbreviation table and assigns
6226 a unique abbreviation id for each abbreviation entry. The children of each
6227 die are visited recursively. */
6229 static void
6230 build_abbrev_table (dw_die_ref die)
6232 unsigned long abbrev_id;
6233 unsigned int n_alloc;
6234 dw_die_ref c;
6235 dw_attr_ref d_attr, a_attr;
6237 /* Scan the DIE references, and mark as external any that refer to
6238 DIEs from other CUs (i.e. those which are not marked). */
6239 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6240 if (AT_class (d_attr) == dw_val_class_die_ref
6241 && AT_ref (d_attr)->die_mark == 0)
6243 if (AT_ref (d_attr)->die_symbol == 0)
6244 abort ();
6246 set_AT_ref_external (d_attr, 1);
6249 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6251 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6253 if (abbrev->die_tag == die->die_tag)
6255 if ((abbrev->die_child != NULL) == (die->die_child != NULL))
6257 a_attr = abbrev->die_attr;
6258 d_attr = die->die_attr;
6260 while (a_attr != NULL && d_attr != NULL)
6262 if ((a_attr->dw_attr != d_attr->dw_attr)
6263 || (value_format (a_attr) != value_format (d_attr)))
6264 break;
6266 a_attr = a_attr->dw_attr_next;
6267 d_attr = d_attr->dw_attr_next;
6270 if (a_attr == NULL && d_attr == NULL)
6271 break;
6276 if (abbrev_id >= abbrev_die_table_in_use)
6278 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6280 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6281 abbrev_die_table = ggc_realloc (abbrev_die_table,
6282 sizeof (dw_die_ref) * n_alloc);
6284 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6285 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6286 abbrev_die_table_allocated = n_alloc;
6289 ++abbrev_die_table_in_use;
6290 abbrev_die_table[abbrev_id] = die;
6293 die->die_abbrev = abbrev_id;
6294 for (c = die->die_child; c != NULL; c = c->die_sib)
6295 build_abbrev_table (c);
6298 /* Return the power-of-two number of bytes necessary to represent VALUE. */
6300 static int
6301 constant_size (long unsigned int value)
6303 int log;
6305 if (value == 0)
6306 log = 0;
6307 else
6308 log = floor_log2 (value);
6310 log = log / 8;
6311 log = 1 << (floor_log2 (log) + 1);
6313 return log;
6316 /* Return the size of a DIE as it is represented in the
6317 .debug_info section. */
6319 static unsigned long
6320 size_of_die (dw_die_ref die)
6322 unsigned long size = 0;
6323 dw_attr_ref a;
6325 size += size_of_uleb128 (die->die_abbrev);
6326 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6328 switch (AT_class (a))
6330 case dw_val_class_addr:
6331 size += DWARF2_ADDR_SIZE;
6332 break;
6333 case dw_val_class_offset:
6334 size += DWARF_OFFSET_SIZE;
6335 break;
6336 case dw_val_class_loc:
6338 unsigned long lsize = size_of_locs (AT_loc (a));
6340 /* Block length. */
6341 size += constant_size (lsize);
6342 size += lsize;
6344 break;
6345 case dw_val_class_loc_list:
6346 size += DWARF_OFFSET_SIZE;
6347 break;
6348 case dw_val_class_range_list:
6349 size += DWARF_OFFSET_SIZE;
6350 break;
6351 case dw_val_class_const:
6352 size += size_of_sleb128 (AT_int (a));
6353 break;
6354 case dw_val_class_unsigned_const:
6355 size += constant_size (AT_unsigned (a));
6356 break;
6357 case dw_val_class_long_long:
6358 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6359 break;
6360 case dw_val_class_vec:
6361 size += 1 + (a->dw_attr_val.v.val_vec.length
6362 * a->dw_attr_val.v.val_vec.elt_size); /* block */
6363 break;
6364 case dw_val_class_flag:
6365 size += 1;
6366 break;
6367 case dw_val_class_die_ref:
6368 if (AT_ref_external (a))
6369 size += DWARF2_ADDR_SIZE;
6370 else
6371 size += DWARF_OFFSET_SIZE;
6372 break;
6373 case dw_val_class_fde_ref:
6374 size += DWARF_OFFSET_SIZE;
6375 break;
6376 case dw_val_class_lbl_id:
6377 size += DWARF2_ADDR_SIZE;
6378 break;
6379 case dw_val_class_lbl_offset:
6380 size += DWARF_OFFSET_SIZE;
6381 break;
6382 case dw_val_class_str:
6383 if (AT_string_form (a) == DW_FORM_strp)
6384 size += DWARF_OFFSET_SIZE;
6385 else
6386 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6387 break;
6388 default:
6389 abort ();
6393 return size;
6396 /* Size the debugging information associated with a given DIE. Visits the
6397 DIE's children recursively. Updates the global variable next_die_offset, on
6398 each time through. Uses the current value of next_die_offset to update the
6399 die_offset field in each DIE. */
6401 static void
6402 calc_die_sizes (dw_die_ref die)
6404 dw_die_ref c;
6406 die->die_offset = next_die_offset;
6407 next_die_offset += size_of_die (die);
6409 for (c = die->die_child; c != NULL; c = c->die_sib)
6410 calc_die_sizes (c);
6412 if (die->die_child != NULL)
6413 /* Count the null byte used to terminate sibling lists. */
6414 next_die_offset += 1;
6417 /* Set the marks for a die and its children. We do this so
6418 that we know whether or not a reference needs to use FORM_ref_addr; only
6419 DIEs in the same CU will be marked. We used to clear out the offset
6420 and use that as the flag, but ran into ordering problems. */
6422 static void
6423 mark_dies (dw_die_ref die)
6425 dw_die_ref c;
6427 if (die->die_mark)
6428 abort ();
6430 die->die_mark = 1;
6431 for (c = die->die_child; c; c = c->die_sib)
6432 mark_dies (c);
6435 /* Clear the marks for a die and its children. */
6437 static void
6438 unmark_dies (dw_die_ref die)
6440 dw_die_ref c;
6442 if (!die->die_mark)
6443 abort ();
6445 die->die_mark = 0;
6446 for (c = die->die_child; c; c = c->die_sib)
6447 unmark_dies (c);
6450 /* Clear the marks for a die, its children and referred dies. */
6452 static void
6453 unmark_all_dies (dw_die_ref die)
6455 dw_die_ref c;
6456 dw_attr_ref a;
6458 if (!die->die_mark)
6459 return;
6460 die->die_mark = 0;
6462 for (c = die->die_child; c; c = c->die_sib)
6463 unmark_all_dies (c);
6465 for (a = die->die_attr; a; a = a->dw_attr_next)
6466 if (AT_class (a) == dw_val_class_die_ref)
6467 unmark_all_dies (AT_ref (a));
6470 /* Return the size of the .debug_pubnames table generated for the
6471 compilation unit. */
6473 static unsigned long
6474 size_of_pubnames (void)
6476 unsigned long size;
6477 unsigned i;
6479 size = DWARF_PUBNAMES_HEADER_SIZE;
6480 for (i = 0; i < pubname_table_in_use; i++)
6482 pubname_ref p = &pubname_table[i];
6483 size += DWARF_OFFSET_SIZE + strlen (p->name) + 1;
6486 size += DWARF_OFFSET_SIZE;
6487 return size;
6490 /* Return the size of the information in the .debug_aranges section. */
6492 static unsigned long
6493 size_of_aranges (void)
6495 unsigned long size;
6497 size = DWARF_ARANGES_HEADER_SIZE;
6499 /* Count the address/length pair for this compilation unit. */
6500 size += 2 * DWARF2_ADDR_SIZE;
6501 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6503 /* Count the two zero words used to terminated the address range table. */
6504 size += 2 * DWARF2_ADDR_SIZE;
6505 return size;
6508 /* Select the encoding of an attribute value. */
6510 static enum dwarf_form
6511 value_format (dw_attr_ref a)
6513 switch (a->dw_attr_val.val_class)
6515 case dw_val_class_addr:
6516 return DW_FORM_addr;
6517 case dw_val_class_range_list:
6518 case dw_val_class_offset:
6519 if (DWARF_OFFSET_SIZE == 4)
6520 return DW_FORM_data4;
6521 if (DWARF_OFFSET_SIZE == 8)
6522 return DW_FORM_data8;
6523 abort ();
6524 case dw_val_class_loc_list:
6525 /* FIXME: Could be DW_FORM_data8, with a > 32 bit size
6526 .debug_loc section */
6527 return DW_FORM_data4;
6528 case dw_val_class_loc:
6529 switch (constant_size (size_of_locs (AT_loc (a))))
6531 case 1:
6532 return DW_FORM_block1;
6533 case 2:
6534 return DW_FORM_block2;
6535 default:
6536 abort ();
6538 case dw_val_class_const:
6539 return DW_FORM_sdata;
6540 case dw_val_class_unsigned_const:
6541 switch (constant_size (AT_unsigned (a)))
6543 case 1:
6544 return DW_FORM_data1;
6545 case 2:
6546 return DW_FORM_data2;
6547 case 4:
6548 return DW_FORM_data4;
6549 case 8:
6550 return DW_FORM_data8;
6551 default:
6552 abort ();
6554 case dw_val_class_long_long:
6555 return DW_FORM_block1;
6556 case dw_val_class_vec:
6557 return DW_FORM_block1;
6558 case dw_val_class_flag:
6559 return DW_FORM_flag;
6560 case dw_val_class_die_ref:
6561 if (AT_ref_external (a))
6562 return DW_FORM_ref_addr;
6563 else
6564 return DW_FORM_ref;
6565 case dw_val_class_fde_ref:
6566 return DW_FORM_data;
6567 case dw_val_class_lbl_id:
6568 return DW_FORM_addr;
6569 case dw_val_class_lbl_offset:
6570 return DW_FORM_data;
6571 case dw_val_class_str:
6572 return AT_string_form (a);
6574 default:
6575 abort ();
6579 /* Output the encoding of an attribute value. */
6581 static void
6582 output_value_format (dw_attr_ref a)
6584 enum dwarf_form form = value_format (a);
6586 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6589 /* Output the .debug_abbrev section which defines the DIE abbreviation
6590 table. */
6592 static void
6593 output_abbrev_section (void)
6595 unsigned long abbrev_id;
6597 dw_attr_ref a_attr;
6599 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6601 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6603 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6604 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6605 dwarf_tag_name (abbrev->die_tag));
6607 if (abbrev->die_child != NULL)
6608 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6609 else
6610 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6612 for (a_attr = abbrev->die_attr; a_attr != NULL;
6613 a_attr = a_attr->dw_attr_next)
6615 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6616 dwarf_attr_name (a_attr->dw_attr));
6617 output_value_format (a_attr);
6620 dw2_asm_output_data (1, 0, NULL);
6621 dw2_asm_output_data (1, 0, NULL);
6624 /* Terminate the table. */
6625 dw2_asm_output_data (1, 0, NULL);
6628 /* Output a symbol we can use to refer to this DIE from another CU. */
6630 static inline void
6631 output_die_symbol (dw_die_ref die)
6633 char *sym = die->die_symbol;
6635 if (sym == 0)
6636 return;
6638 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6639 /* We make these global, not weak; if the target doesn't support
6640 .linkonce, it doesn't support combining the sections, so debugging
6641 will break. */
6642 targetm.asm_out.globalize_label (asm_out_file, sym);
6644 ASM_OUTPUT_LABEL (asm_out_file, sym);
6647 /* Return a new location list, given the begin and end range, and the
6648 expression. gensym tells us whether to generate a new internal symbol for
6649 this location list node, which is done for the head of the list only. */
6651 static inline dw_loc_list_ref
6652 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6653 const char *section, unsigned int gensym)
6655 dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
6657 retlist->begin = begin;
6658 retlist->end = end;
6659 retlist->expr = expr;
6660 retlist->section = section;
6661 if (gensym)
6662 retlist->ll_symbol = gen_internal_sym ("LLST");
6664 return retlist;
6667 /* Add a location description expression to a location list. */
6669 static inline void
6670 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6671 const char *begin, const char *end,
6672 const char *section)
6674 dw_loc_list_ref *d;
6676 /* Find the end of the chain. */
6677 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
6680 /* Add a new location list node to the list. */
6681 *d = new_loc_list (descr, begin, end, section, 0);
6684 /* Output the location list given to us. */
6686 static void
6687 output_loc_list (dw_loc_list_ref list_head)
6689 dw_loc_list_ref curr = list_head;
6691 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
6693 /* Walk the location list, and output each range + expression. */
6694 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
6696 unsigned long size;
6697 if (separate_line_info_table_in_use == 0)
6699 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
6700 "Location list begin address (%s)",
6701 list_head->ll_symbol);
6702 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
6703 "Location list end address (%s)",
6704 list_head->ll_symbol);
6706 else
6708 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
6709 "Location list begin address (%s)",
6710 list_head->ll_symbol);
6711 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
6712 "Location list end address (%s)",
6713 list_head->ll_symbol);
6715 size = size_of_locs (curr->expr);
6717 /* Output the block length for this list of location operations. */
6718 if (size > 0xffff)
6719 abort ();
6720 dw2_asm_output_data (2, size, "%s", "Location expression size");
6722 output_loc_sequence (curr->expr);
6725 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6726 "Location list terminator begin (%s)",
6727 list_head->ll_symbol);
6728 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6729 "Location list terminator end (%s)",
6730 list_head->ll_symbol);
6733 /* Output the DIE and its attributes. Called recursively to generate
6734 the definitions of each child DIE. */
6736 static void
6737 output_die (dw_die_ref die)
6739 dw_attr_ref a;
6740 dw_die_ref c;
6741 unsigned long size;
6743 /* If someone in another CU might refer to us, set up a symbol for
6744 them to point to. */
6745 if (die->die_symbol)
6746 output_die_symbol (die);
6748 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
6749 die->die_offset, dwarf_tag_name (die->die_tag));
6751 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6753 const char *name = dwarf_attr_name (a->dw_attr);
6755 switch (AT_class (a))
6757 case dw_val_class_addr:
6758 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
6759 break;
6761 case dw_val_class_offset:
6762 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
6763 "%s", name);
6764 break;
6766 case dw_val_class_range_list:
6768 char *p = strchr (ranges_section_label, '\0');
6770 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
6771 a->dw_attr_val.v.val_offset);
6772 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
6773 "%s", name);
6774 *p = '\0';
6776 break;
6778 case dw_val_class_loc:
6779 size = size_of_locs (AT_loc (a));
6781 /* Output the block length for this list of location operations. */
6782 dw2_asm_output_data (constant_size (size), size, "%s", name);
6784 output_loc_sequence (AT_loc (a));
6785 break;
6787 case dw_val_class_const:
6788 /* ??? It would be slightly more efficient to use a scheme like is
6789 used for unsigned constants below, but gdb 4.x does not sign
6790 extend. Gdb 5.x does sign extend. */
6791 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
6792 break;
6794 case dw_val_class_unsigned_const:
6795 dw2_asm_output_data (constant_size (AT_unsigned (a)),
6796 AT_unsigned (a), "%s", name);
6797 break;
6799 case dw_val_class_long_long:
6801 unsigned HOST_WIDE_INT first, second;
6803 dw2_asm_output_data (1,
6804 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6805 "%s", name);
6807 if (WORDS_BIG_ENDIAN)
6809 first = a->dw_attr_val.v.val_long_long.hi;
6810 second = a->dw_attr_val.v.val_long_long.low;
6812 else
6814 first = a->dw_attr_val.v.val_long_long.low;
6815 second = a->dw_attr_val.v.val_long_long.hi;
6818 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6819 first, "long long constant");
6820 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6821 second, NULL);
6823 break;
6825 case dw_val_class_vec:
6827 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
6828 unsigned int len = a->dw_attr_val.v.val_vec.length;
6829 unsigned int i;
6830 unsigned char *p;
6832 dw2_asm_output_data (1, len * elt_size, "%s", name);
6833 if (elt_size > sizeof (HOST_WIDE_INT))
6835 elt_size /= 2;
6836 len *= 2;
6838 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
6839 i < len;
6840 i++, p += elt_size)
6841 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
6842 "fp or vector constant word %u", i);
6843 break;
6846 case dw_val_class_flag:
6847 dw2_asm_output_data (1, AT_flag (a), "%s", name);
6848 break;
6850 case dw_val_class_loc_list:
6852 char *sym = AT_loc_list (a)->ll_symbol;
6854 if (sym == 0)
6855 abort ();
6856 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, "%s", name);
6858 break;
6860 case dw_val_class_die_ref:
6861 if (AT_ref_external (a))
6863 char *sym = AT_ref (a)->die_symbol;
6865 if (sym == 0)
6866 abort ();
6867 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, "%s", name);
6869 else if (AT_ref (a)->die_offset == 0)
6870 abort ();
6871 else
6872 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
6873 "%s", name);
6874 break;
6876 case dw_val_class_fde_ref:
6878 char l1[20];
6880 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
6881 a->dw_attr_val.v.val_fde_index * 2);
6882 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, "%s", name);
6884 break;
6886 case dw_val_class_lbl_id:
6887 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
6888 break;
6890 case dw_val_class_lbl_offset:
6891 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a), "%s", name);
6892 break;
6894 case dw_val_class_str:
6895 if (AT_string_form (a) == DW_FORM_strp)
6896 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
6897 a->dw_attr_val.v.val_str->label,
6898 "%s: \"%s\"", name, AT_string (a));
6899 else
6900 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
6901 break;
6903 default:
6904 abort ();
6908 for (c = die->die_child; c != NULL; c = c->die_sib)
6909 output_die (c);
6911 /* Add null byte to terminate sibling list. */
6912 if (die->die_child != NULL)
6913 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
6914 die->die_offset);
6917 /* Output the compilation unit that appears at the beginning of the
6918 .debug_info section, and precedes the DIE descriptions. */
6920 static void
6921 output_compilation_unit_header (void)
6923 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
6924 dw2_asm_output_data (4, 0xffffffff,
6925 "Initial length escape value indicating 64-bit DWARF extension");
6926 dw2_asm_output_data (DWARF_OFFSET_SIZE,
6927 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
6928 "Length of Compilation Unit Info");
6929 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
6930 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
6931 "Offset Into Abbrev. Section");
6932 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
6935 /* Output the compilation unit DIE and its children. */
6937 static void
6938 output_comp_unit (dw_die_ref die, int output_if_empty)
6940 const char *secname;
6941 char *oldsym, *tmp;
6943 /* Unless we are outputting main CU, we may throw away empty ones. */
6944 if (!output_if_empty && die->die_child == NULL)
6945 return;
6947 /* Even if there are no children of this DIE, we must output the information
6948 about the compilation unit. Otherwise, on an empty translation unit, we
6949 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
6950 will then complain when examining the file. First mark all the DIEs in
6951 this CU so we know which get local refs. */
6952 mark_dies (die);
6954 build_abbrev_table (die);
6956 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
6957 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
6958 calc_die_sizes (die);
6960 oldsym = die->die_symbol;
6961 if (oldsym)
6963 tmp = alloca (strlen (oldsym) + 24);
6965 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
6966 secname = tmp;
6967 die->die_symbol = NULL;
6969 else
6970 secname = (const char *) DEBUG_INFO_SECTION;
6972 /* Output debugging information. */
6973 named_section_flags (secname, SECTION_DEBUG);
6974 output_compilation_unit_header ();
6975 output_die (die);
6977 /* Leave the marks on the main CU, so we can check them in
6978 output_pubnames. */
6979 if (oldsym)
6981 unmark_dies (die);
6982 die->die_symbol = oldsym;
6986 /* The DWARF2 pubname for a nested thingy looks like "A::f". The
6987 output of lang_hooks.decl_printable_name for C++ looks like
6988 "A::f(int)". Let's drop the argument list, and maybe the scope. */
6990 static const char *
6991 dwarf2_name (tree decl, int scope)
6993 return lang_hooks.decl_printable_name (decl, scope ? 1 : 0);
6996 /* Add a new entry to .debug_pubnames if appropriate. */
6998 static void
6999 add_pubname (tree decl, dw_die_ref die)
7001 pubname_ref p;
7003 if (! TREE_PUBLIC (decl))
7004 return;
7006 if (pubname_table_in_use == pubname_table_allocated)
7008 pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
7009 pubname_table
7010 = ggc_realloc (pubname_table,
7011 (pubname_table_allocated * sizeof (pubname_entry)));
7012 memset (pubname_table + pubname_table_in_use, 0,
7013 PUBNAME_TABLE_INCREMENT * sizeof (pubname_entry));
7016 p = &pubname_table[pubname_table_in_use++];
7017 p->die = die;
7018 p->name = xstrdup (dwarf2_name (decl, 1));
7021 /* Output the public names table used to speed up access to externally
7022 visible names. For now, only generate entries for externally
7023 visible procedures. */
7025 static void
7026 output_pubnames (void)
7028 unsigned i;
7029 unsigned long pubnames_length = size_of_pubnames ();
7031 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7032 dw2_asm_output_data (4, 0xffffffff,
7033 "Initial length escape value indicating 64-bit DWARF extension");
7034 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7035 "Length of Public Names Info");
7036 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7037 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7038 "Offset of Compilation Unit Info");
7039 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7040 "Compilation Unit Length");
7042 for (i = 0; i < pubname_table_in_use; i++)
7044 pubname_ref pub = &pubname_table[i];
7046 /* We shouldn't see pubnames for DIEs outside of the main CU. */
7047 if (pub->die->die_mark == 0)
7048 abort ();
7050 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7051 "DIE offset");
7053 dw2_asm_output_nstring (pub->name, -1, "external name");
7056 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7059 /* Add a new entry to .debug_aranges if appropriate. */
7061 static void
7062 add_arange (tree decl, dw_die_ref die)
7064 if (! DECL_SECTION_NAME (decl))
7065 return;
7067 if (arange_table_in_use == arange_table_allocated)
7069 arange_table_allocated += ARANGE_TABLE_INCREMENT;
7070 arange_table = ggc_realloc (arange_table,
7071 (arange_table_allocated
7072 * sizeof (dw_die_ref)));
7073 memset (arange_table + arange_table_in_use, 0,
7074 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7077 arange_table[arange_table_in_use++] = die;
7080 /* Output the information that goes into the .debug_aranges table.
7081 Namely, define the beginning and ending address range of the
7082 text section generated for this compilation unit. */
7084 static void
7085 output_aranges (void)
7087 unsigned i;
7088 unsigned long aranges_length = size_of_aranges ();
7090 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7091 dw2_asm_output_data (4, 0xffffffff,
7092 "Initial length escape value indicating 64-bit DWARF extension");
7093 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7094 "Length of Address Ranges Info");
7095 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7096 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7097 "Offset of Compilation Unit Info");
7098 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7099 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7101 /* We need to align to twice the pointer size here. */
7102 if (DWARF_ARANGES_PAD_SIZE)
7104 /* Pad using a 2 byte words so that padding is correct for any
7105 pointer size. */
7106 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7107 2 * DWARF2_ADDR_SIZE);
7108 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7109 dw2_asm_output_data (2, 0, NULL);
7112 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7113 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7114 text_section_label, "Length");
7116 for (i = 0; i < arange_table_in_use; i++)
7118 dw_die_ref die = arange_table[i];
7120 /* We shouldn't see aranges for DIEs outside of the main CU. */
7121 if (die->die_mark == 0)
7122 abort ();
7124 if (die->die_tag == DW_TAG_subprogram)
7126 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7127 "Address");
7128 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7129 get_AT_low_pc (die), "Length");
7131 else
7133 /* A static variable; extract the symbol from DW_AT_location.
7134 Note that this code isn't currently hit, as we only emit
7135 aranges for functions (jason 9/23/99). */
7136 dw_attr_ref a = get_AT (die, DW_AT_location);
7137 dw_loc_descr_ref loc;
7139 if (! a || AT_class (a) != dw_val_class_loc)
7140 abort ();
7142 loc = AT_loc (a);
7143 if (loc->dw_loc_opc != DW_OP_addr)
7144 abort ();
7146 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7147 loc->dw_loc_oprnd1.v.val_addr, "Address");
7148 dw2_asm_output_data (DWARF2_ADDR_SIZE,
7149 get_AT_unsigned (die, DW_AT_byte_size),
7150 "Length");
7154 /* Output the terminator words. */
7155 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7156 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7159 /* Add a new entry to .debug_ranges. Return the offset at which it
7160 was placed. */
7162 static unsigned int
7163 add_ranges (tree block)
7165 unsigned int in_use = ranges_table_in_use;
7167 if (in_use == ranges_table_allocated)
7169 ranges_table_allocated += RANGES_TABLE_INCREMENT;
7170 ranges_table
7171 = ggc_realloc (ranges_table, (ranges_table_allocated
7172 * sizeof (struct dw_ranges_struct)));
7173 memset (ranges_table + ranges_table_in_use, 0,
7174 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7177 ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7178 ranges_table_in_use = in_use + 1;
7180 return in_use * 2 * DWARF2_ADDR_SIZE;
7183 static void
7184 output_ranges (void)
7186 unsigned i;
7187 static const char *const start_fmt = "Offset 0x%x";
7188 const char *fmt = start_fmt;
7190 for (i = 0; i < ranges_table_in_use; i++)
7192 int block_num = ranges_table[i].block_num;
7194 if (block_num)
7196 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7197 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7199 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7200 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7202 /* If all code is in the text section, then the compilation
7203 unit base address defaults to DW_AT_low_pc, which is the
7204 base of the text section. */
7205 if (separate_line_info_table_in_use == 0)
7207 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7208 text_section_label,
7209 fmt, i * 2 * DWARF2_ADDR_SIZE);
7210 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7211 text_section_label, NULL);
7214 /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7215 compilation unit base address to zero, which allows us to
7216 use absolute addresses, and not worry about whether the
7217 target supports cross-section arithmetic. */
7218 else
7220 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7221 fmt, i * 2 * DWARF2_ADDR_SIZE);
7222 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7225 fmt = NULL;
7227 else
7229 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7230 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7231 fmt = start_fmt;
7236 /* Data structure containing information about input files. */
7237 struct file_info
7239 char *path; /* Complete file name. */
7240 char *fname; /* File name part. */
7241 int length; /* Length of entire string. */
7242 int file_idx; /* Index in input file table. */
7243 int dir_idx; /* Index in directory table. */
7246 /* Data structure containing information about directories with source
7247 files. */
7248 struct dir_info
7250 char *path; /* Path including directory name. */
7251 int length; /* Path length. */
7252 int prefix; /* Index of directory entry which is a prefix. */
7253 int count; /* Number of files in this directory. */
7254 int dir_idx; /* Index of directory used as base. */
7255 int used; /* Used in the end? */
7258 /* Callback function for file_info comparison. We sort by looking at
7259 the directories in the path. */
7261 static int
7262 file_info_cmp (const void *p1, const void *p2)
7264 const struct file_info *s1 = p1;
7265 const struct file_info *s2 = p2;
7266 unsigned char *cp1;
7267 unsigned char *cp2;
7269 /* Take care of file names without directories. We need to make sure that
7270 we return consistent values to qsort since some will get confused if
7271 we return the same value when identical operands are passed in opposite
7272 orders. So if neither has a directory, return 0 and otherwise return
7273 1 or -1 depending on which one has the directory. */
7274 if ((s1->path == s1->fname || s2->path == s2->fname))
7275 return (s2->path == s2->fname) - (s1->path == s1->fname);
7277 cp1 = (unsigned char *) s1->path;
7278 cp2 = (unsigned char *) s2->path;
7280 while (1)
7282 ++cp1;
7283 ++cp2;
7284 /* Reached the end of the first path? If so, handle like above. */
7285 if ((cp1 == (unsigned char *) s1->fname)
7286 || (cp2 == (unsigned char *) s2->fname))
7287 return ((cp2 == (unsigned char *) s2->fname)
7288 - (cp1 == (unsigned char *) s1->fname));
7290 /* Character of current path component the same? */
7291 else if (*cp1 != *cp2)
7292 return *cp1 - *cp2;
7296 /* Output the directory table and the file name table. We try to minimize
7297 the total amount of memory needed. A heuristic is used to avoid large
7298 slowdowns with many input files. */
7300 static void
7301 output_file_names (void)
7303 struct file_info *files;
7304 struct dir_info *dirs;
7305 int *saved;
7306 int *savehere;
7307 int *backmap;
7308 size_t ndirs;
7309 int idx_offset;
7310 size_t i;
7311 int idx;
7313 /* Handle the case where file_table is empty. */
7314 if (VARRAY_ACTIVE_SIZE (file_table) <= 1)
7316 dw2_asm_output_data (1, 0, "End directory table");
7317 dw2_asm_output_data (1, 0, "End file name table");
7318 return;
7321 /* Allocate the various arrays we need. */
7322 files = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct file_info));
7323 dirs = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct dir_info));
7325 /* Sort the file names. */
7326 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7328 char *f;
7330 /* Skip all leading "./". */
7331 f = VARRAY_CHAR_PTR (file_table, i);
7332 while (f[0] == '.' && f[1] == '/')
7333 f += 2;
7335 /* Create a new array entry. */
7336 files[i].path = f;
7337 files[i].length = strlen (f);
7338 files[i].file_idx = i;
7340 /* Search for the file name part. */
7341 f = strrchr (f, '/');
7342 files[i].fname = f == NULL ? files[i].path : f + 1;
7345 qsort (files + 1, VARRAY_ACTIVE_SIZE (file_table) - 1,
7346 sizeof (files[0]), file_info_cmp);
7348 /* Find all the different directories used. */
7349 dirs[0].path = files[1].path;
7350 dirs[0].length = files[1].fname - files[1].path;
7351 dirs[0].prefix = -1;
7352 dirs[0].count = 1;
7353 dirs[0].dir_idx = 0;
7354 dirs[0].used = 0;
7355 files[1].dir_idx = 0;
7356 ndirs = 1;
7358 for (i = 2; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7359 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7360 && memcmp (dirs[ndirs - 1].path, files[i].path,
7361 dirs[ndirs - 1].length) == 0)
7363 /* Same directory as last entry. */
7364 files[i].dir_idx = ndirs - 1;
7365 ++dirs[ndirs - 1].count;
7367 else
7369 size_t j;
7371 /* This is a new directory. */
7372 dirs[ndirs].path = files[i].path;
7373 dirs[ndirs].length = files[i].fname - files[i].path;
7374 dirs[ndirs].count = 1;
7375 dirs[ndirs].dir_idx = ndirs;
7376 dirs[ndirs].used = 0;
7377 files[i].dir_idx = ndirs;
7379 /* Search for a prefix. */
7380 dirs[ndirs].prefix = -1;
7381 for (j = 0; j < ndirs; j++)
7382 if (dirs[j].length < dirs[ndirs].length
7383 && dirs[j].length > 1
7384 && (dirs[ndirs].prefix == -1
7385 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7386 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7387 dirs[ndirs].prefix = j;
7389 ++ndirs;
7392 /* Now to the actual work. We have to find a subset of the directories which
7393 allow expressing the file name using references to the directory table
7394 with the least amount of characters. We do not do an exhaustive search
7395 where we would have to check out every combination of every single
7396 possible prefix. Instead we use a heuristic which provides nearly optimal
7397 results in most cases and never is much off. */
7398 saved = alloca (ndirs * sizeof (int));
7399 savehere = alloca (ndirs * sizeof (int));
7401 memset (saved, '\0', ndirs * sizeof (saved[0]));
7402 for (i = 0; i < ndirs; i++)
7404 size_t j;
7405 int total;
7407 /* We can always save some space for the current directory. But this
7408 does not mean it will be enough to justify adding the directory. */
7409 savehere[i] = dirs[i].length;
7410 total = (savehere[i] - saved[i]) * dirs[i].count;
7412 for (j = i + 1; j < ndirs; j++)
7414 savehere[j] = 0;
7415 if (saved[j] < dirs[i].length)
7417 /* Determine whether the dirs[i] path is a prefix of the
7418 dirs[j] path. */
7419 int k;
7421 k = dirs[j].prefix;
7422 while (k != -1 && k != (int) i)
7423 k = dirs[k].prefix;
7425 if (k == (int) i)
7427 /* Yes it is. We can possibly safe some memory but
7428 writing the filenames in dirs[j] relative to
7429 dirs[i]. */
7430 savehere[j] = dirs[i].length;
7431 total += (savehere[j] - saved[j]) * dirs[j].count;
7436 /* Check whether we can safe enough to justify adding the dirs[i]
7437 directory. */
7438 if (total > dirs[i].length + 1)
7440 /* It's worthwhile adding. */
7441 for (j = i; j < ndirs; j++)
7442 if (savehere[j] > 0)
7444 /* Remember how much we saved for this directory so far. */
7445 saved[j] = savehere[j];
7447 /* Remember the prefix directory. */
7448 dirs[j].dir_idx = i;
7453 /* We have to emit them in the order they appear in the file_table array
7454 since the index is used in the debug info generation. To do this
7455 efficiently we generate a back-mapping of the indices first. */
7456 backmap = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (int));
7457 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7459 backmap[files[i].file_idx] = i;
7461 /* Mark this directory as used. */
7462 dirs[dirs[files[i].dir_idx].dir_idx].used = 1;
7465 /* That was it. We are ready to emit the information. First emit the
7466 directory name table. We have to make sure the first actually emitted
7467 directory name has index one; zero is reserved for the current working
7468 directory. Make sure we do not confuse these indices with the one for the
7469 constructed table (even though most of the time they are identical). */
7470 idx = 1;
7471 idx_offset = dirs[0].length > 0 ? 1 : 0;
7472 for (i = 1 - idx_offset; i < ndirs; i++)
7473 if (dirs[i].used != 0)
7475 dirs[i].used = idx++;
7476 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7477 "Directory Entry: 0x%x", dirs[i].used);
7480 dw2_asm_output_data (1, 0, "End directory table");
7482 /* Correct the index for the current working directory entry if it
7483 exists. */
7484 if (idx_offset == 0)
7485 dirs[0].used = 0;
7487 /* Now write all the file names. */
7488 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7490 int file_idx = backmap[i];
7491 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7493 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7494 "File Entry: 0x%lx", (unsigned long) i);
7496 /* Include directory index. */
7497 dw2_asm_output_data_uleb128 (dirs[dir_idx].used, NULL);
7499 /* Modification time. */
7500 dw2_asm_output_data_uleb128 (0, NULL);
7502 /* File length in bytes. */
7503 dw2_asm_output_data_uleb128 (0, NULL);
7506 dw2_asm_output_data (1, 0, "End file name table");
7510 /* Output the source line number correspondence information. This
7511 information goes into the .debug_line section. */
7513 static void
7514 output_line_info (void)
7516 char l1[20], l2[20], p1[20], p2[20];
7517 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7518 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7519 unsigned opc;
7520 unsigned n_op_args;
7521 unsigned long lt_index;
7522 unsigned long current_line;
7523 long line_offset;
7524 long line_delta;
7525 unsigned long current_file;
7526 unsigned long function;
7528 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7529 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7530 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7531 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7533 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7534 dw2_asm_output_data (4, 0xffffffff,
7535 "Initial length escape value indicating 64-bit DWARF extension");
7536 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7537 "Length of Source Line Info");
7538 ASM_OUTPUT_LABEL (asm_out_file, l1);
7540 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7541 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7542 ASM_OUTPUT_LABEL (asm_out_file, p1);
7544 /* Define the architecture-dependent minimum instruction length (in
7545 bytes). In this implementation of DWARF, this field is used for
7546 information purposes only. Since GCC generates assembly language,
7547 we have no a priori knowledge of how many instruction bytes are
7548 generated for each source line, and therefore can use only the
7549 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7550 commands. Accordingly, we fix this as `1', which is "correct
7551 enough" for all architectures, and don't let the target override. */
7552 dw2_asm_output_data (1, 1,
7553 "Minimum Instruction Length");
7555 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7556 "Default is_stmt_start flag");
7557 dw2_asm_output_data (1, DWARF_LINE_BASE,
7558 "Line Base Value (Special Opcodes)");
7559 dw2_asm_output_data (1, DWARF_LINE_RANGE,
7560 "Line Range Value (Special Opcodes)");
7561 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7562 "Special Opcode Base");
7564 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7566 switch (opc)
7568 case DW_LNS_advance_pc:
7569 case DW_LNS_advance_line:
7570 case DW_LNS_set_file:
7571 case DW_LNS_set_column:
7572 case DW_LNS_fixed_advance_pc:
7573 n_op_args = 1;
7574 break;
7575 default:
7576 n_op_args = 0;
7577 break;
7580 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
7581 opc, n_op_args);
7584 /* Write out the information about the files we use. */
7585 output_file_names ();
7586 ASM_OUTPUT_LABEL (asm_out_file, p2);
7588 /* We used to set the address register to the first location in the text
7589 section here, but that didn't accomplish anything since we already
7590 have a line note for the opening brace of the first function. */
7592 /* Generate the line number to PC correspondence table, encoded as
7593 a series of state machine operations. */
7594 current_file = 1;
7595 current_line = 1;
7596 strcpy (prev_line_label, text_section_label);
7597 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
7599 dw_line_info_ref line_info = &line_info_table[lt_index];
7601 #if 0
7602 /* Disable this optimization for now; GDB wants to see two line notes
7603 at the beginning of a function so it can find the end of the
7604 prologue. */
7606 /* Don't emit anything for redundant notes. Just updating the
7607 address doesn't accomplish anything, because we already assume
7608 that anything after the last address is this line. */
7609 if (line_info->dw_line_num == current_line
7610 && line_info->dw_file_num == current_file)
7611 continue;
7612 #endif
7614 /* Emit debug info for the address of the current line.
7616 Unfortunately, we have little choice here currently, and must always
7617 use the most general form. GCC does not know the address delta
7618 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
7619 attributes which will give an upper bound on the address range. We
7620 could perhaps use length attributes to determine when it is safe to
7621 use DW_LNS_fixed_advance_pc. */
7623 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
7624 if (0)
7626 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
7627 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7628 "DW_LNS_fixed_advance_pc");
7629 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7631 else
7633 /* This can handle any delta. This takes
7634 4+DWARF2_ADDR_SIZE bytes. */
7635 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7636 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7637 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7638 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7641 strcpy (prev_line_label, line_label);
7643 /* Emit debug info for the source file of the current line, if
7644 different from the previous line. */
7645 if (line_info->dw_file_num != current_file)
7647 current_file = line_info->dw_file_num;
7648 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7649 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7650 VARRAY_CHAR_PTR (file_table,
7651 current_file));
7654 /* Emit debug info for the current line number, choosing the encoding
7655 that uses the least amount of space. */
7656 if (line_info->dw_line_num != current_line)
7658 line_offset = line_info->dw_line_num - current_line;
7659 line_delta = line_offset - DWARF_LINE_BASE;
7660 current_line = line_info->dw_line_num;
7661 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7662 /* This can handle deltas from -10 to 234, using the current
7663 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
7664 takes 1 byte. */
7665 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7666 "line %lu", current_line);
7667 else
7669 /* This can handle any delta. This takes at least 4 bytes,
7670 depending on the value being encoded. */
7671 dw2_asm_output_data (1, DW_LNS_advance_line,
7672 "advance to line %lu", current_line);
7673 dw2_asm_output_data_sleb128 (line_offset, NULL);
7674 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7677 else
7678 /* We still need to start a new row, so output a copy insn. */
7679 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7682 /* Emit debug info for the address of the end of the function. */
7683 if (0)
7685 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7686 "DW_LNS_fixed_advance_pc");
7687 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
7689 else
7691 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7692 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7693 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7694 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
7697 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7698 dw2_asm_output_data_uleb128 (1, NULL);
7699 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7701 function = 0;
7702 current_file = 1;
7703 current_line = 1;
7704 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
7706 dw_separate_line_info_ref line_info
7707 = &separate_line_info_table[lt_index];
7709 #if 0
7710 /* Don't emit anything for redundant notes. */
7711 if (line_info->dw_line_num == current_line
7712 && line_info->dw_file_num == current_file
7713 && line_info->function == function)
7714 goto cont;
7715 #endif
7717 /* Emit debug info for the address of the current line. If this is
7718 a new function, or the first line of a function, then we need
7719 to handle it differently. */
7720 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
7721 lt_index);
7722 if (function != line_info->function)
7724 function = line_info->function;
7726 /* Set the address register to the first line in the function. */
7727 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7728 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7729 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7730 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7732 else
7734 /* ??? See the DW_LNS_advance_pc comment above. */
7735 if (0)
7737 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7738 "DW_LNS_fixed_advance_pc");
7739 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7741 else
7743 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7744 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7745 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7746 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7750 strcpy (prev_line_label, line_label);
7752 /* Emit debug info for the source file of the current line, if
7753 different from the previous line. */
7754 if (line_info->dw_file_num != current_file)
7756 current_file = line_info->dw_file_num;
7757 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7758 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7759 VARRAY_CHAR_PTR (file_table,
7760 current_file));
7763 /* Emit debug info for the current line number, choosing the encoding
7764 that uses the least amount of space. */
7765 if (line_info->dw_line_num != current_line)
7767 line_offset = line_info->dw_line_num - current_line;
7768 line_delta = line_offset - DWARF_LINE_BASE;
7769 current_line = line_info->dw_line_num;
7770 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7771 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7772 "line %lu", current_line);
7773 else
7775 dw2_asm_output_data (1, DW_LNS_advance_line,
7776 "advance to line %lu", current_line);
7777 dw2_asm_output_data_sleb128 (line_offset, NULL);
7778 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7781 else
7782 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7784 #if 0
7785 cont:
7786 #endif
7788 lt_index++;
7790 /* If we're done with a function, end its sequence. */
7791 if (lt_index == separate_line_info_table_in_use
7792 || separate_line_info_table[lt_index].function != function)
7794 current_file = 1;
7795 current_line = 1;
7797 /* Emit debug info for the address of the end of the function. */
7798 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
7799 if (0)
7801 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7802 "DW_LNS_fixed_advance_pc");
7803 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7805 else
7807 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7808 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7809 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7810 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7813 /* Output the marker for the end of this sequence. */
7814 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7815 dw2_asm_output_data_uleb128 (1, NULL);
7816 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7820 /* Output the marker for the end of the line number info. */
7821 ASM_OUTPUT_LABEL (asm_out_file, l2);
7824 /* Given a pointer to a tree node for some base type, return a pointer to
7825 a DIE that describes the given type.
7827 This routine must only be called for GCC type nodes that correspond to
7828 Dwarf base (fundamental) types. */
7830 static dw_die_ref
7831 base_type_die (tree type)
7833 dw_die_ref base_type_result;
7834 const char *type_name;
7835 enum dwarf_type encoding;
7836 tree name = TYPE_NAME (type);
7838 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
7839 return 0;
7841 if (name)
7843 if (TREE_CODE (name) == TYPE_DECL)
7844 name = DECL_NAME (name);
7846 type_name = IDENTIFIER_POINTER (name);
7848 else
7849 type_name = "__unknown__";
7851 switch (TREE_CODE (type))
7853 case INTEGER_TYPE:
7854 /* Carefully distinguish the C character types, without messing
7855 up if the language is not C. Note that we check only for the names
7856 that contain spaces; other names might occur by coincidence in other
7857 languages. */
7858 if (! (TYPE_PRECISION (type) == CHAR_TYPE_SIZE
7859 && (type == char_type_node
7860 || ! strcmp (type_name, "signed char")
7861 || ! strcmp (type_name, "unsigned char"))))
7863 if (TYPE_UNSIGNED (type))
7864 encoding = DW_ATE_unsigned;
7865 else
7866 encoding = DW_ATE_signed;
7867 break;
7869 /* else fall through. */
7871 case CHAR_TYPE:
7872 /* GNU Pascal/Ada CHAR type. Not used in C. */
7873 if (TYPE_UNSIGNED (type))
7874 encoding = DW_ATE_unsigned_char;
7875 else
7876 encoding = DW_ATE_signed_char;
7877 break;
7879 case REAL_TYPE:
7880 encoding = DW_ATE_float;
7881 break;
7883 /* Dwarf2 doesn't know anything about complex ints, so use
7884 a user defined type for it. */
7885 case COMPLEX_TYPE:
7886 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
7887 encoding = DW_ATE_complex_float;
7888 else
7889 encoding = DW_ATE_lo_user;
7890 break;
7892 case BOOLEAN_TYPE:
7893 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
7894 encoding = DW_ATE_boolean;
7895 break;
7897 default:
7898 /* No other TREE_CODEs are Dwarf fundamental types. */
7899 abort ();
7902 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
7903 if (demangle_name_func)
7904 type_name = (*demangle_name_func) (type_name);
7906 add_AT_string (base_type_result, DW_AT_name, type_name);
7907 add_AT_unsigned (base_type_result, DW_AT_byte_size,
7908 int_size_in_bytes (type));
7909 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
7911 return base_type_result;
7914 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
7915 the Dwarf "root" type for the given input type. The Dwarf "root" type of
7916 a given type is generally the same as the given type, except that if the
7917 given type is a pointer or reference type, then the root type of the given
7918 type is the root type of the "basis" type for the pointer or reference
7919 type. (This definition of the "root" type is recursive.) Also, the root
7920 type of a `const' qualified type or a `volatile' qualified type is the
7921 root type of the given type without the qualifiers. */
7923 static tree
7924 root_type (tree type)
7926 if (TREE_CODE (type) == ERROR_MARK)
7927 return error_mark_node;
7929 switch (TREE_CODE (type))
7931 case ERROR_MARK:
7932 return error_mark_node;
7934 case POINTER_TYPE:
7935 case REFERENCE_TYPE:
7936 return type_main_variant (root_type (TREE_TYPE (type)));
7938 default:
7939 return type_main_variant (type);
7943 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
7944 given input type is a Dwarf "fundamental" type. Otherwise return null. */
7946 static inline int
7947 is_base_type (tree type)
7949 switch (TREE_CODE (type))
7951 case ERROR_MARK:
7952 case VOID_TYPE:
7953 case INTEGER_TYPE:
7954 case REAL_TYPE:
7955 case COMPLEX_TYPE:
7956 case BOOLEAN_TYPE:
7957 case CHAR_TYPE:
7958 return 1;
7960 case SET_TYPE:
7961 case ARRAY_TYPE:
7962 case RECORD_TYPE:
7963 case UNION_TYPE:
7964 case QUAL_UNION_TYPE:
7965 case ENUMERAL_TYPE:
7966 case FUNCTION_TYPE:
7967 case METHOD_TYPE:
7968 case POINTER_TYPE:
7969 case REFERENCE_TYPE:
7970 case FILE_TYPE:
7971 case OFFSET_TYPE:
7972 case LANG_TYPE:
7973 case VECTOR_TYPE:
7974 return 0;
7976 default:
7977 abort ();
7980 return 0;
7983 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
7984 node, return the size in bits for the type if it is a constant, or else
7985 return the alignment for the type if the type's size is not constant, or
7986 else return BITS_PER_WORD if the type actually turns out to be an
7987 ERROR_MARK node. */
7989 static inline unsigned HOST_WIDE_INT
7990 simple_type_size_in_bits (tree type)
7992 if (TREE_CODE (type) == ERROR_MARK)
7993 return BITS_PER_WORD;
7994 else if (TYPE_SIZE (type) == NULL_TREE)
7995 return 0;
7996 else if (host_integerp (TYPE_SIZE (type), 1))
7997 return tree_low_cst (TYPE_SIZE (type), 1);
7998 else
7999 return TYPE_ALIGN (type);
8002 /* Return true if the debug information for the given type should be
8003 emitted as a subrange type. */
8005 static inline bool
8006 is_subrange_type (tree type)
8008 tree subtype = TREE_TYPE (type);
8010 /* Subrange types are identified by the fact that they are integer
8011 types, and that they have a subtype which is either an integer type
8012 or an enumeral type. */
8014 if (TREE_CODE (type) != INTEGER_TYPE
8015 || subtype == NULL_TREE)
8016 return false;
8018 if (TREE_CODE (subtype) != INTEGER_TYPE
8019 && TREE_CODE (subtype) != ENUMERAL_TYPE)
8020 return false;
8022 if (TREE_CODE (type) == TREE_CODE (subtype)
8023 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8024 && TYPE_MIN_VALUE (type) != NULL
8025 && TYPE_MIN_VALUE (subtype) != NULL
8026 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8027 && TYPE_MAX_VALUE (type) != NULL
8028 && TYPE_MAX_VALUE (subtype) != NULL
8029 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8031 /* The type and its subtype have the same representation. If in
8032 addition the two types also have the same name, then the given
8033 type is not a subrange type, but rather a plain base type. */
8034 /* FIXME: brobecker/2004-03-22:
8035 Sizetype INTEGER_CSTs nodes are canonicalized. It should
8036 therefore be sufficient to check the TYPE_SIZE node pointers
8037 rather than checking the actual size. Unfortunately, we have
8038 found some cases, such as in the Ada "integer" type, where
8039 this is not the case. Until this problem is solved, we need to
8040 keep checking the actual size. */
8041 tree type_name = TYPE_NAME (type);
8042 tree subtype_name = TYPE_NAME (subtype);
8044 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8045 type_name = DECL_NAME (type_name);
8047 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8048 subtype_name = DECL_NAME (subtype_name);
8050 if (type_name == subtype_name)
8051 return false;
8054 return true;
8057 /* Given a pointer to a tree node for a subrange type, return a pointer
8058 to a DIE that describes the given type. */
8060 static dw_die_ref
8061 subrange_type_die (tree type, dw_die_ref context_die)
8063 dw_die_ref subtype_die;
8064 dw_die_ref subrange_die;
8065 tree name = TYPE_NAME (type);
8066 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8067 tree subtype = TREE_TYPE (type);
8069 if (context_die == NULL)
8070 context_die = comp_unit_die;
8072 if (TREE_CODE (subtype) == ENUMERAL_TYPE)
8073 subtype_die = gen_enumeration_type_die (subtype, context_die);
8074 else
8075 subtype_die = base_type_die (subtype);
8077 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8079 if (name != NULL)
8081 if (TREE_CODE (name) == TYPE_DECL)
8082 name = DECL_NAME (name);
8083 add_name_attribute (subrange_die, IDENTIFIER_POINTER (name));
8086 if (int_size_in_bytes (subtype) != size_in_bytes)
8088 /* The size of the subrange type and its base type do not match,
8089 so we need to generate a size attribute for the subrange type. */
8090 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8093 if (TYPE_MIN_VALUE (type) != NULL)
8094 add_bound_info (subrange_die, DW_AT_lower_bound,
8095 TYPE_MIN_VALUE (type));
8096 if (TYPE_MAX_VALUE (type) != NULL)
8097 add_bound_info (subrange_die, DW_AT_upper_bound,
8098 TYPE_MAX_VALUE (type));
8099 add_AT_die_ref (subrange_die, DW_AT_type, subtype_die);
8101 return subrange_die;
8104 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8105 entry that chains various modifiers in front of the given type. */
8107 static dw_die_ref
8108 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8109 dw_die_ref context_die)
8111 enum tree_code code = TREE_CODE (type);
8112 dw_die_ref mod_type_die = NULL;
8113 dw_die_ref sub_die = NULL;
8114 tree item_type = NULL;
8116 if (code != ERROR_MARK)
8118 tree qualified_type;
8120 /* See if we already have the appropriately qualified variant of
8121 this type. */
8122 qualified_type
8123 = get_qualified_type (type,
8124 ((is_const_type ? TYPE_QUAL_CONST : 0)
8125 | (is_volatile_type
8126 ? TYPE_QUAL_VOLATILE : 0)));
8128 /* If we do, then we can just use its DIE, if it exists. */
8129 if (qualified_type)
8131 mod_type_die = lookup_type_die (qualified_type);
8132 if (mod_type_die)
8133 return mod_type_die;
8136 /* Handle C typedef types. */
8137 if (qualified_type && TYPE_NAME (qualified_type)
8138 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL
8139 && DECL_ORIGINAL_TYPE (TYPE_NAME (qualified_type)))
8141 tree type_name = TYPE_NAME (qualified_type);
8142 tree dtype = TREE_TYPE (type_name);
8144 if (qualified_type == dtype)
8146 /* For a named type, use the typedef. */
8147 gen_type_die (qualified_type, context_die);
8148 mod_type_die = lookup_type_die (qualified_type);
8150 else if (is_const_type < TYPE_READONLY (dtype)
8151 || is_volatile_type < TYPE_VOLATILE (dtype))
8152 /* cv-unqualified version of named type. Just use the unnamed
8153 type to which it refers. */
8154 mod_type_die
8155 = modified_type_die (DECL_ORIGINAL_TYPE (type_name),
8156 is_const_type, is_volatile_type,
8157 context_die);
8159 /* Else cv-qualified version of named type; fall through. */
8162 if (mod_type_die)
8163 /* OK. */
8165 else if (is_const_type)
8167 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8168 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8170 else if (is_volatile_type)
8172 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8173 sub_die = modified_type_die (type, 0, 0, context_die);
8175 else if (code == POINTER_TYPE)
8177 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8178 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8179 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8180 #if 0
8181 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8182 #endif
8183 item_type = TREE_TYPE (type);
8185 else if (code == REFERENCE_TYPE)
8187 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8188 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8189 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8190 #if 0
8191 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8192 #endif
8193 item_type = TREE_TYPE (type);
8195 else if (is_subrange_type (type))
8196 mod_type_die = subrange_type_die (type, context_die);
8197 else if (is_base_type (type))
8198 mod_type_die = base_type_die (type);
8199 else
8201 gen_type_die (type, context_die);
8203 /* We have to get the type_main_variant here (and pass that to the
8204 `lookup_type_die' routine) because the ..._TYPE node we have
8205 might simply be a *copy* of some original type node (where the
8206 copy was created to help us keep track of typedef names) and
8207 that copy might have a different TYPE_UID from the original
8208 ..._TYPE node. */
8209 if (TREE_CODE (type) != VECTOR_TYPE)
8210 mod_type_die = lookup_type_die (type_main_variant (type));
8211 else
8212 /* Vectors have the debugging information in the type,
8213 not the main variant. */
8214 mod_type_die = lookup_type_die (type);
8215 if (mod_type_die == NULL)
8216 abort ();
8219 /* We want to equate the qualified type to the die below. */
8220 type = qualified_type;
8223 if (type)
8224 equate_type_number_to_die (type, mod_type_die);
8225 if (item_type)
8226 /* We must do this after the equate_type_number_to_die call, in case
8227 this is a recursive type. This ensures that the modified_type_die
8228 recursion will terminate even if the type is recursive. Recursive
8229 types are possible in Ada. */
8230 sub_die = modified_type_die (item_type,
8231 TYPE_READONLY (item_type),
8232 TYPE_VOLATILE (item_type),
8233 context_die);
8235 if (sub_die != NULL)
8236 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8238 return mod_type_die;
8241 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8242 an enumerated type. */
8244 static inline int
8245 type_is_enum (tree type)
8247 return TREE_CODE (type) == ENUMERAL_TYPE;
8250 /* Return the DBX register number described by a given RTL node. */
8252 static unsigned int
8253 dbx_reg_number (rtx rtl)
8255 unsigned regno = REGNO (rtl);
8257 if (regno >= FIRST_PSEUDO_REGISTER)
8258 abort ();
8260 return DBX_REGISTER_NUMBER (regno);
8263 /* Return a location descriptor that designates a machine register or
8264 zero if there is none. */
8266 static dw_loc_descr_ref
8267 reg_loc_descriptor (rtx rtl)
8269 unsigned reg;
8270 rtx regs;
8272 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8273 return 0;
8275 reg = dbx_reg_number (rtl);
8276 regs = targetm.dwarf_register_span (rtl);
8278 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1
8279 || regs)
8280 return multiple_reg_loc_descriptor (rtl, regs);
8281 else
8282 return one_reg_loc_descriptor (reg);
8285 /* Return a location descriptor that designates a machine register for
8286 a given hard register number. */
8288 static dw_loc_descr_ref
8289 one_reg_loc_descriptor (unsigned int regno)
8291 if (regno <= 31)
8292 return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8293 else
8294 return new_loc_descr (DW_OP_regx, regno, 0);
8297 /* Given an RTL of a register, return a location descriptor that
8298 designates a value that spans more than one register. */
8300 static dw_loc_descr_ref
8301 multiple_reg_loc_descriptor (rtx rtl, rtx regs)
8303 int nregs, size, i;
8304 unsigned reg;
8305 dw_loc_descr_ref loc_result = NULL;
8307 reg = dbx_reg_number (rtl);
8308 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8310 /* Simple, contiguous registers. */
8311 if (regs == NULL_RTX)
8313 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8315 loc_result = NULL;
8316 while (nregs--)
8318 dw_loc_descr_ref t;
8320 t = one_reg_loc_descriptor (reg);
8321 add_loc_descr (&loc_result, t);
8322 add_loc_descr (&loc_result, new_loc_descr (DW_OP_piece, size, 0));
8323 ++reg;
8325 return loc_result;
8328 /* Now onto stupid register sets in non contiguous locations. */
8330 if (GET_CODE (regs) != PARALLEL)
8331 abort ();
8333 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8334 loc_result = NULL;
8336 for (i = 0; i < XVECLEN (regs, 0); ++i)
8338 dw_loc_descr_ref t;
8340 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8341 add_loc_descr (&loc_result, t);
8342 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8343 add_loc_descr (&loc_result, new_loc_descr (DW_OP_piece, size, 0));
8345 return loc_result;
8348 /* Return a location descriptor that designates a constant. */
8350 static dw_loc_descr_ref
8351 int_loc_descriptor (HOST_WIDE_INT i)
8353 enum dwarf_location_atom op;
8355 /* Pick the smallest representation of a constant, rather than just
8356 defaulting to the LEB encoding. */
8357 if (i >= 0)
8359 if (i <= 31)
8360 op = DW_OP_lit0 + i;
8361 else if (i <= 0xff)
8362 op = DW_OP_const1u;
8363 else if (i <= 0xffff)
8364 op = DW_OP_const2u;
8365 else if (HOST_BITS_PER_WIDE_INT == 32
8366 || i <= 0xffffffff)
8367 op = DW_OP_const4u;
8368 else
8369 op = DW_OP_constu;
8371 else
8373 if (i >= -0x80)
8374 op = DW_OP_const1s;
8375 else if (i >= -0x8000)
8376 op = DW_OP_const2s;
8377 else if (HOST_BITS_PER_WIDE_INT == 32
8378 || i >= -0x80000000)
8379 op = DW_OP_const4s;
8380 else
8381 op = DW_OP_consts;
8384 return new_loc_descr (op, i, 0);
8387 /* Return a location descriptor that designates a base+offset location. */
8389 static dw_loc_descr_ref
8390 based_loc_descr (unsigned int reg, HOST_WIDE_INT offset, bool can_use_fbreg)
8392 dw_loc_descr_ref loc_result;
8393 /* For the "frame base", we use the frame pointer or stack pointer
8394 registers, since the RTL for local variables is relative to one of
8395 them. */
8396 unsigned fp_reg = DBX_REGISTER_NUMBER (frame_pointer_needed
8397 ? HARD_FRAME_POINTER_REGNUM
8398 : STACK_POINTER_REGNUM);
8400 if (reg == fp_reg && can_use_fbreg)
8401 loc_result = new_loc_descr (DW_OP_fbreg, offset, 0);
8402 else if (reg <= 31)
8403 loc_result = new_loc_descr (DW_OP_breg0 + reg, offset, 0);
8404 else
8405 loc_result = new_loc_descr (DW_OP_bregx, reg, offset);
8407 return loc_result;
8410 /* Return true if this RTL expression describes a base+offset calculation. */
8412 static inline int
8413 is_based_loc (rtx rtl)
8415 return (GET_CODE (rtl) == PLUS
8416 && ((REG_P (XEXP (rtl, 0))
8417 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8418 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
8421 /* The following routine converts the RTL for a variable or parameter
8422 (resident in memory) into an equivalent Dwarf representation of a
8423 mechanism for getting the address of that same variable onto the top of a
8424 hypothetical "address evaluation" stack.
8426 When creating memory location descriptors, we are effectively transforming
8427 the RTL for a memory-resident object into its Dwarf postfix expression
8428 equivalent. This routine recursively descends an RTL tree, turning
8429 it into Dwarf postfix code as it goes.
8431 MODE is the mode of the memory reference, needed to handle some
8432 autoincrement addressing modes.
8434 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the location
8435 list for RTL. We can't use it when we are emitting location list for
8436 virtual variable frame_base_decl (i.e. a location list for DW_AT_frame_base)
8437 which describes how frame base changes when !frame_pointer_needed.
8439 Return 0 if we can't represent the location. */
8441 static dw_loc_descr_ref
8442 mem_loc_descriptor (rtx rtl, enum machine_mode mode, bool can_use_fbreg)
8444 dw_loc_descr_ref mem_loc_result = NULL;
8445 enum dwarf_location_atom op;
8447 /* Note that for a dynamically sized array, the location we will generate a
8448 description of here will be the lowest numbered location which is
8449 actually within the array. That's *not* necessarily the same as the
8450 zeroth element of the array. */
8452 rtl = targetm.delegitimize_address (rtl);
8454 switch (GET_CODE (rtl))
8456 case POST_INC:
8457 case POST_DEC:
8458 case POST_MODIFY:
8459 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
8460 just fall into the SUBREG code. */
8462 /* ... fall through ... */
8464 case SUBREG:
8465 /* The case of a subreg may arise when we have a local (register)
8466 variable or a formal (register) parameter which doesn't quite fill
8467 up an entire register. For now, just assume that it is
8468 legitimate to make the Dwarf info refer to the whole register which
8469 contains the given subreg. */
8470 rtl = SUBREG_REG (rtl);
8472 /* ... fall through ... */
8474 case REG:
8475 /* Whenever a register number forms a part of the description of the
8476 method for calculating the (dynamic) address of a memory resident
8477 object, DWARF rules require the register number be referred to as
8478 a "base register". This distinction is not based in any way upon
8479 what category of register the hardware believes the given register
8480 belongs to. This is strictly DWARF terminology we're dealing with
8481 here. Note that in cases where the location of a memory-resident
8482 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8483 OP_CONST (0)) the actual DWARF location descriptor that we generate
8484 may just be OP_BASEREG (basereg). This may look deceptively like
8485 the object in question was allocated to a register (rather than in
8486 memory) so DWARF consumers need to be aware of the subtle
8487 distinction between OP_REG and OP_BASEREG. */
8488 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8489 mem_loc_result = based_loc_descr (dbx_reg_number (rtl), 0,
8490 can_use_fbreg);
8491 break;
8493 case MEM:
8494 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
8495 can_use_fbreg);
8496 if (mem_loc_result != 0)
8497 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
8498 break;
8500 case LO_SUM:
8501 rtl = XEXP (rtl, 1);
8503 /* ... fall through ... */
8505 case LABEL_REF:
8506 /* Some ports can transform a symbol ref into a label ref, because
8507 the symbol ref is too far away and has to be dumped into a constant
8508 pool. */
8509 case CONST:
8510 case SYMBOL_REF:
8511 /* Alternatively, the symbol in the constant pool might be referenced
8512 by a different symbol. */
8513 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
8515 bool marked;
8516 rtx tmp = get_pool_constant_mark (rtl, &marked);
8518 if (GET_CODE (tmp) == SYMBOL_REF)
8520 rtl = tmp;
8521 if (CONSTANT_POOL_ADDRESS_P (tmp))
8522 get_pool_constant_mark (tmp, &marked);
8523 else
8524 marked = true;
8527 /* If all references to this pool constant were optimized away,
8528 it was not output and thus we can't represent it.
8529 FIXME: might try to use DW_OP_const_value here, though
8530 DW_OP_piece complicates it. */
8531 if (!marked)
8532 return 0;
8535 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8536 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
8537 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8538 VARRAY_PUSH_RTX (used_rtx_varray, rtl);
8539 break;
8541 case PRE_MODIFY:
8542 /* Extract the PLUS expression nested inside and fall into
8543 PLUS code below. */
8544 rtl = XEXP (rtl, 1);
8545 goto plus;
8547 case PRE_INC:
8548 case PRE_DEC:
8549 /* Turn these into a PLUS expression and fall into the PLUS code
8550 below. */
8551 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8552 GEN_INT (GET_CODE (rtl) == PRE_INC
8553 ? GET_MODE_UNIT_SIZE (mode)
8554 : -GET_MODE_UNIT_SIZE (mode)));
8556 /* ... fall through ... */
8558 case PLUS:
8559 plus:
8560 if (is_based_loc (rtl))
8561 mem_loc_result = based_loc_descr (dbx_reg_number (XEXP (rtl, 0)),
8562 INTVAL (XEXP (rtl, 1)),
8563 can_use_fbreg);
8564 else
8566 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
8567 can_use_fbreg);
8568 if (mem_loc_result == 0)
8569 break;
8571 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8572 && INTVAL (XEXP (rtl, 1)) >= 0)
8573 add_loc_descr (&mem_loc_result,
8574 new_loc_descr (DW_OP_plus_uconst,
8575 INTVAL (XEXP (rtl, 1)), 0));
8576 else
8578 add_loc_descr (&mem_loc_result,
8579 mem_loc_descriptor (XEXP (rtl, 1), mode,
8580 can_use_fbreg));
8581 add_loc_descr (&mem_loc_result,
8582 new_loc_descr (DW_OP_plus, 0, 0));
8585 break;
8587 /* If a pseudo-reg is optimized away, it is possible for it to
8588 be replaced with a MEM containing a multiply or shift. */
8589 case MULT:
8590 op = DW_OP_mul;
8591 goto do_binop;
8593 case ASHIFT:
8594 op = DW_OP_shl;
8595 goto do_binop;
8597 case ASHIFTRT:
8598 op = DW_OP_shra;
8599 goto do_binop;
8601 case LSHIFTRT:
8602 op = DW_OP_shr;
8603 goto do_binop;
8605 do_binop:
8607 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
8608 can_use_fbreg);
8609 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
8610 can_use_fbreg);
8612 if (op0 == 0 || op1 == 0)
8613 break;
8615 mem_loc_result = op0;
8616 add_loc_descr (&mem_loc_result, op1);
8617 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
8618 break;
8621 case CONST_INT:
8622 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
8623 break;
8625 case ADDRESSOF:
8626 /* If this is a MEM, return its address. Otherwise, we can't
8627 represent this. */
8628 if (GET_CODE (XEXP (rtl, 0)) == MEM)
8629 return mem_loc_descriptor (XEXP (XEXP (rtl, 0), 0), mode,
8630 can_use_fbreg);
8631 else
8632 return 0;
8634 default:
8635 abort ();
8638 return mem_loc_result;
8641 /* Return a descriptor that describes the concatenation of two locations.
8642 This is typically a complex variable. */
8644 static dw_loc_descr_ref
8645 concat_loc_descriptor (rtx x0, rtx x1)
8647 dw_loc_descr_ref cc_loc_result = NULL;
8648 dw_loc_descr_ref x0_ref = loc_descriptor (x0, true);
8649 dw_loc_descr_ref x1_ref = loc_descriptor (x1, true);
8651 if (x0_ref == 0 || x1_ref == 0)
8652 return 0;
8654 cc_loc_result = x0_ref;
8655 add_loc_descr (&cc_loc_result,
8656 new_loc_descr (DW_OP_piece,
8657 GET_MODE_SIZE (GET_MODE (x0)), 0));
8659 add_loc_descr (&cc_loc_result, x1_ref);
8660 add_loc_descr (&cc_loc_result,
8661 new_loc_descr (DW_OP_piece,
8662 GET_MODE_SIZE (GET_MODE (x1)), 0));
8664 return cc_loc_result;
8667 /* Output a proper Dwarf location descriptor for a variable or parameter
8668 which is either allocated in a register or in a memory location. For a
8669 register, we just generate an OP_REG and the register number. For a
8670 memory location we provide a Dwarf postfix expression describing how to
8671 generate the (dynamic) address of the object onto the address stack.
8673 If we don't know how to describe it, return 0. */
8675 static dw_loc_descr_ref
8676 loc_descriptor (rtx rtl, bool can_use_fbreg)
8678 dw_loc_descr_ref loc_result = NULL;
8680 switch (GET_CODE (rtl))
8682 case SUBREG:
8683 /* The case of a subreg may arise when we have a local (register)
8684 variable or a formal (register) parameter which doesn't quite fill
8685 up an entire register. For now, just assume that it is
8686 legitimate to make the Dwarf info refer to the whole register which
8687 contains the given subreg. */
8688 rtl = SUBREG_REG (rtl);
8690 /* ... fall through ... */
8692 case REG:
8693 loc_result = reg_loc_descriptor (rtl);
8694 break;
8696 case MEM:
8697 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
8698 can_use_fbreg);
8699 break;
8701 case CONCAT:
8702 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
8703 break;
8705 case VAR_LOCATION:
8706 /* Single part. */
8707 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
8709 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), can_use_fbreg);
8711 /* Multiple parts. */
8712 else
8714 rtvec par_elems = XVEC (XEXP (rtl, 1), 0);
8715 int num_elem = GET_NUM_ELEM (par_elems);
8716 enum machine_mode mode;
8717 int i;
8719 /* Create the first one, so we have something to add to. */
8720 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
8721 can_use_fbreg);
8722 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
8723 add_loc_descr (&loc_result,
8724 new_loc_descr (DW_OP_piece, GET_MODE_SIZE (mode), 0));
8725 for (i = 1; i < num_elem; i++)
8727 dw_loc_descr_ref temp;
8729 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
8730 can_use_fbreg);
8731 add_loc_descr (&loc_result, temp);
8732 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
8733 add_loc_descr (&loc_result,
8734 new_loc_descr (DW_OP_piece,
8735 GET_MODE_SIZE (mode), 0));
8738 break;
8740 default:
8741 abort ();
8744 return loc_result;
8747 /* Similar, but generate the descriptor from trees instead of rtl. This comes
8748 up particularly with variable length arrays. If ADDRESSP is nonzero, we are
8749 looking for an address. Otherwise, we return a value. If we can't make a
8750 descriptor, return 0. */
8752 static dw_loc_descr_ref
8753 loc_descriptor_from_tree (tree loc, int addressp)
8755 dw_loc_descr_ref ret, ret1;
8756 int indirect_p = 0;
8757 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
8758 enum dwarf_location_atom op;
8760 /* ??? Most of the time we do not take proper care for sign/zero
8761 extending the values properly. Hopefully this won't be a real
8762 problem... */
8764 switch (TREE_CODE (loc))
8766 case ERROR_MARK:
8767 return 0;
8769 case PLACEHOLDER_EXPR:
8770 /* This case involves extracting fields from an object to determine the
8771 position of other fields. We don't try to encode this here. The
8772 only user of this is Ada, which encodes the needed information using
8773 the names of types. */
8774 return 0;
8776 case CALL_EXPR:
8777 return 0;
8779 case PREINCREMENT_EXPR:
8780 case PREDECREMENT_EXPR:
8781 case POSTINCREMENT_EXPR:
8782 case POSTDECREMENT_EXPR:
8783 /* There are no opcodes for these operations. */
8784 return 0;
8786 case ADDR_EXPR:
8787 /* We can support this only if we can look through conversions and
8788 find an INDIRECT_EXPR. */
8789 for (loc = TREE_OPERAND (loc, 0);
8790 TREE_CODE (loc) == CONVERT_EXPR || TREE_CODE (loc) == NOP_EXPR
8791 || TREE_CODE (loc) == NON_LVALUE_EXPR
8792 || TREE_CODE (loc) == VIEW_CONVERT_EXPR
8793 || TREE_CODE (loc) == SAVE_EXPR;
8794 loc = TREE_OPERAND (loc, 0))
8797 return (TREE_CODE (loc) == INDIRECT_REF
8798 ? loc_descriptor_from_tree (TREE_OPERAND (loc, 0), addressp)
8799 : 0);
8801 case VAR_DECL:
8802 if (DECL_THREAD_LOCAL (loc))
8804 rtx rtl;
8806 #ifndef ASM_OUTPUT_DWARF_DTPREL
8807 /* If this is not defined, we have no way to emit the data. */
8808 return 0;
8809 #endif
8811 /* The way DW_OP_GNU_push_tls_address is specified, we can only
8812 look up addresses of objects in the current module. */
8813 if (DECL_EXTERNAL (loc))
8814 return 0;
8816 rtl = rtl_for_decl_location (loc);
8817 if (rtl == NULL_RTX)
8818 return 0;
8820 if (GET_CODE (rtl) != MEM)
8821 return 0;
8822 rtl = XEXP (rtl, 0);
8823 if (! CONSTANT_P (rtl))
8824 return 0;
8826 ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
8827 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8828 ret->dw_loc_oprnd1.v.val_addr = rtl;
8830 ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
8831 add_loc_descr (&ret, ret1);
8833 indirect_p = 1;
8834 break;
8836 /* Fall through. */
8838 case PARM_DECL:
8839 case RESULT_DECL:
8841 rtx rtl = rtl_for_decl_location (loc);
8843 if (rtl == NULL_RTX)
8844 return 0;
8845 else if (CONSTANT_P (rtl))
8847 ret = new_loc_descr (DW_OP_addr, 0, 0);
8848 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8849 ret->dw_loc_oprnd1.v.val_addr = rtl;
8850 indirect_p = 1;
8852 else
8854 enum machine_mode mode = GET_MODE (rtl);
8856 if (GET_CODE (rtl) == MEM)
8858 indirect_p = 1;
8859 rtl = XEXP (rtl, 0);
8862 ret = mem_loc_descriptor (rtl, mode, true);
8865 break;
8867 case INDIRECT_REF:
8868 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8869 indirect_p = 1;
8870 break;
8872 case COMPOUND_EXPR:
8873 return loc_descriptor_from_tree (TREE_OPERAND (loc, 1), addressp);
8875 case NOP_EXPR:
8876 case CONVERT_EXPR:
8877 case NON_LVALUE_EXPR:
8878 case VIEW_CONVERT_EXPR:
8879 case SAVE_EXPR:
8880 case MODIFY_EXPR:
8881 return loc_descriptor_from_tree (TREE_OPERAND (loc, 0), addressp);
8883 case COMPONENT_REF:
8884 case BIT_FIELD_REF:
8885 case ARRAY_REF:
8886 case ARRAY_RANGE_REF:
8888 tree obj, offset;
8889 HOST_WIDE_INT bitsize, bitpos, bytepos;
8890 enum machine_mode mode;
8891 int volatilep;
8893 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
8894 &unsignedp, &volatilep);
8896 if (obj == loc)
8897 return 0;
8899 ret = loc_descriptor_from_tree (obj, 1);
8900 if (ret == 0
8901 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
8902 return 0;
8904 if (offset != NULL_TREE)
8906 /* Variable offset. */
8907 add_loc_descr (&ret, loc_descriptor_from_tree (offset, 0));
8908 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
8911 if (!addressp)
8912 indirect_p = 1;
8914 bytepos = bitpos / BITS_PER_UNIT;
8915 if (bytepos > 0)
8916 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
8917 else if (bytepos < 0)
8919 add_loc_descr (&ret, int_loc_descriptor (bytepos));
8920 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
8922 break;
8925 case INTEGER_CST:
8926 if (host_integerp (loc, 0))
8927 ret = int_loc_descriptor (tree_low_cst (loc, 0));
8928 else
8929 return 0;
8930 break;
8932 case CONSTRUCTOR:
8934 /* Get an RTL for this, if something has been emitted. */
8935 rtx rtl = lookup_constant_def (loc);
8936 enum machine_mode mode;
8938 if (GET_CODE (rtl) != MEM)
8939 return 0;
8940 mode = GET_MODE (rtl);
8941 rtl = XEXP (rtl, 0);
8943 rtl = targetm.delegitimize_address (rtl);
8945 indirect_p = 1;
8946 ret = mem_loc_descriptor (rtl, mode, true);
8947 break;
8950 case TRUTH_AND_EXPR:
8951 case TRUTH_ANDIF_EXPR:
8952 case BIT_AND_EXPR:
8953 op = DW_OP_and;
8954 goto do_binop;
8956 case TRUTH_XOR_EXPR:
8957 case BIT_XOR_EXPR:
8958 op = DW_OP_xor;
8959 goto do_binop;
8961 case TRUTH_OR_EXPR:
8962 case TRUTH_ORIF_EXPR:
8963 case BIT_IOR_EXPR:
8964 op = DW_OP_or;
8965 goto do_binop;
8967 case FLOOR_DIV_EXPR:
8968 case CEIL_DIV_EXPR:
8969 case ROUND_DIV_EXPR:
8970 case TRUNC_DIV_EXPR:
8971 op = DW_OP_div;
8972 goto do_binop;
8974 case MINUS_EXPR:
8975 op = DW_OP_minus;
8976 goto do_binop;
8978 case FLOOR_MOD_EXPR:
8979 case CEIL_MOD_EXPR:
8980 case ROUND_MOD_EXPR:
8981 case TRUNC_MOD_EXPR:
8982 op = DW_OP_mod;
8983 goto do_binop;
8985 case MULT_EXPR:
8986 op = DW_OP_mul;
8987 goto do_binop;
8989 case LSHIFT_EXPR:
8990 op = DW_OP_shl;
8991 goto do_binop;
8993 case RSHIFT_EXPR:
8994 op = (unsignedp ? DW_OP_shr : DW_OP_shra);
8995 goto do_binop;
8997 case PLUS_EXPR:
8998 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
8999 && host_integerp (TREE_OPERAND (loc, 1), 0))
9001 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
9002 if (ret == 0)
9003 return 0;
9005 add_loc_descr (&ret,
9006 new_loc_descr (DW_OP_plus_uconst,
9007 tree_low_cst (TREE_OPERAND (loc, 1),
9009 0));
9010 break;
9013 op = DW_OP_plus;
9014 goto do_binop;
9016 case LE_EXPR:
9017 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9018 return 0;
9020 op = DW_OP_le;
9021 goto do_binop;
9023 case GE_EXPR:
9024 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9025 return 0;
9027 op = DW_OP_ge;
9028 goto do_binop;
9030 case LT_EXPR:
9031 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9032 return 0;
9034 op = DW_OP_lt;
9035 goto do_binop;
9037 case GT_EXPR:
9038 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9039 return 0;
9041 op = DW_OP_gt;
9042 goto do_binop;
9044 case EQ_EXPR:
9045 op = DW_OP_eq;
9046 goto do_binop;
9048 case NE_EXPR:
9049 op = DW_OP_ne;
9050 goto do_binop;
9052 do_binop:
9053 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
9054 ret1 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
9055 if (ret == 0 || ret1 == 0)
9056 return 0;
9058 add_loc_descr (&ret, ret1);
9059 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9060 break;
9062 case TRUTH_NOT_EXPR:
9063 case BIT_NOT_EXPR:
9064 op = DW_OP_not;
9065 goto do_unop;
9067 case ABS_EXPR:
9068 op = DW_OP_abs;
9069 goto do_unop;
9071 case NEGATE_EXPR:
9072 op = DW_OP_neg;
9073 goto do_unop;
9075 do_unop:
9076 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
9077 if (ret == 0)
9078 return 0;
9080 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9081 break;
9083 case MIN_EXPR:
9084 case MAX_EXPR:
9086 const enum tree_code code =
9087 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9089 loc = build (COND_EXPR, TREE_TYPE (loc),
9090 build (code, integer_type_node,
9091 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9092 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9095 /* ... fall through ... */
9097 case COND_EXPR:
9099 dw_loc_descr_ref lhs
9100 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
9101 dw_loc_descr_ref rhs
9102 = loc_descriptor_from_tree (TREE_OPERAND (loc, 2), 0);
9103 dw_loc_descr_ref bra_node, jump_node, tmp;
9105 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
9106 if (ret == 0 || lhs == 0 || rhs == 0)
9107 return 0;
9109 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9110 add_loc_descr (&ret, bra_node);
9112 add_loc_descr (&ret, rhs);
9113 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9114 add_loc_descr (&ret, jump_node);
9116 add_loc_descr (&ret, lhs);
9117 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9118 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9120 /* ??? Need a node to point the skip at. Use a nop. */
9121 tmp = new_loc_descr (DW_OP_nop, 0, 0);
9122 add_loc_descr (&ret, tmp);
9123 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9124 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9126 break;
9128 default:
9129 /* Leave front-end specific codes as simply unknown. This comes
9130 up, for instance, with the C STMT_EXPR. */
9131 if ((unsigned int) TREE_CODE (loc)
9132 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9133 return 0;
9135 /* Otherwise this is a generic code; we should just lists all of
9136 these explicitly. Aborting means we forgot one. */
9137 abort ();
9140 /* Show if we can't fill the request for an address. */
9141 if (addressp && indirect_p == 0)
9142 return 0;
9144 /* If we've got an address and don't want one, dereference. */
9145 if (!addressp && indirect_p > 0)
9147 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9149 if (size > DWARF2_ADDR_SIZE || size == -1)
9150 return 0;
9151 else if (size == DWARF2_ADDR_SIZE)
9152 op = DW_OP_deref;
9153 else
9154 op = DW_OP_deref_size;
9156 add_loc_descr (&ret, new_loc_descr (op, size, 0));
9159 return ret;
9162 /* Given a value, round it up to the lowest multiple of `boundary'
9163 which is not less than the value itself. */
9165 static inline HOST_WIDE_INT
9166 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9168 return (((value + boundary - 1) / boundary) * boundary);
9171 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9172 pointer to the declared type for the relevant field variable, or return
9173 `integer_type_node' if the given node turns out to be an
9174 ERROR_MARK node. */
9176 static inline tree
9177 field_type (tree decl)
9179 tree type;
9181 if (TREE_CODE (decl) == ERROR_MARK)
9182 return integer_type_node;
9184 type = DECL_BIT_FIELD_TYPE (decl);
9185 if (type == NULL_TREE)
9186 type = TREE_TYPE (decl);
9188 return type;
9191 /* Given a pointer to a tree node, return the alignment in bits for
9192 it, or else return BITS_PER_WORD if the node actually turns out to
9193 be an ERROR_MARK node. */
9195 static inline unsigned
9196 simple_type_align_in_bits (tree type)
9198 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9201 static inline unsigned
9202 simple_decl_align_in_bits (tree decl)
9204 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9207 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9208 lowest addressed byte of the "containing object" for the given FIELD_DECL,
9209 or return 0 if we are unable to determine what that offset is, either
9210 because the argument turns out to be a pointer to an ERROR_MARK node, or
9211 because the offset is actually variable. (We can't handle the latter case
9212 just yet). */
9214 static HOST_WIDE_INT
9215 field_byte_offset (tree decl)
9217 unsigned int type_align_in_bits;
9218 unsigned int decl_align_in_bits;
9219 unsigned HOST_WIDE_INT type_size_in_bits;
9220 HOST_WIDE_INT object_offset_in_bits;
9221 tree type;
9222 tree field_size_tree;
9223 HOST_WIDE_INT bitpos_int;
9224 HOST_WIDE_INT deepest_bitpos;
9225 unsigned HOST_WIDE_INT field_size_in_bits;
9227 if (TREE_CODE (decl) == ERROR_MARK)
9228 return 0;
9229 else if (TREE_CODE (decl) != FIELD_DECL)
9230 abort ();
9232 type = field_type (decl);
9233 field_size_tree = DECL_SIZE (decl);
9235 /* The size could be unspecified if there was an error, or for
9236 a flexible array member. */
9237 if (! field_size_tree)
9238 field_size_tree = bitsize_zero_node;
9240 /* We cannot yet cope with fields whose positions are variable, so
9241 for now, when we see such things, we simply return 0. Someday, we may
9242 be able to handle such cases, but it will be damn difficult. */
9243 if (! host_integerp (bit_position (decl), 0))
9244 return 0;
9246 bitpos_int = int_bit_position (decl);
9248 /* If we don't know the size of the field, pretend it's a full word. */
9249 if (host_integerp (field_size_tree, 1))
9250 field_size_in_bits = tree_low_cst (field_size_tree, 1);
9251 else
9252 field_size_in_bits = BITS_PER_WORD;
9254 type_size_in_bits = simple_type_size_in_bits (type);
9255 type_align_in_bits = simple_type_align_in_bits (type);
9256 decl_align_in_bits = simple_decl_align_in_bits (decl);
9258 /* The GCC front-end doesn't make any attempt to keep track of the starting
9259 bit offset (relative to the start of the containing structure type) of the
9260 hypothetical "containing object" for a bit-field. Thus, when computing
9261 the byte offset value for the start of the "containing object" of a
9262 bit-field, we must deduce this information on our own. This can be rather
9263 tricky to do in some cases. For example, handling the following structure
9264 type definition when compiling for an i386/i486 target (which only aligns
9265 long long's to 32-bit boundaries) can be very tricky:
9267 struct S { int field1; long long field2:31; };
9269 Fortunately, there is a simple rule-of-thumb which can be used in such
9270 cases. When compiling for an i386/i486, GCC will allocate 8 bytes for the
9271 structure shown above. It decides to do this based upon one simple rule
9272 for bit-field allocation. GCC allocates each "containing object" for each
9273 bit-field at the first (i.e. lowest addressed) legitimate alignment
9274 boundary (based upon the required minimum alignment for the declared type
9275 of the field) which it can possibly use, subject to the condition that
9276 there is still enough available space remaining in the containing object
9277 (when allocated at the selected point) to fully accommodate all of the
9278 bits of the bit-field itself.
9280 This simple rule makes it obvious why GCC allocates 8 bytes for each
9281 object of the structure type shown above. When looking for a place to
9282 allocate the "containing object" for `field2', the compiler simply tries
9283 to allocate a 64-bit "containing object" at each successive 32-bit
9284 boundary (starting at zero) until it finds a place to allocate that 64-
9285 bit field such that at least 31 contiguous (and previously unallocated)
9286 bits remain within that selected 64 bit field. (As it turns out, for the
9287 example above, the compiler finds it is OK to allocate the "containing
9288 object" 64-bit field at bit-offset zero within the structure type.)
9290 Here we attempt to work backwards from the limited set of facts we're
9291 given, and we try to deduce from those facts, where GCC must have believed
9292 that the containing object started (within the structure type). The value
9293 we deduce is then used (by the callers of this routine) to generate
9294 DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
9295 and, in the case of DW_AT_location, regular fields as well). */
9297 /* Figure out the bit-distance from the start of the structure to the
9298 "deepest" bit of the bit-field. */
9299 deepest_bitpos = bitpos_int + field_size_in_bits;
9301 /* This is the tricky part. Use some fancy footwork to deduce where the
9302 lowest addressed bit of the containing object must be. */
9303 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9305 /* Round up to type_align by default. This works best for bitfields. */
9306 object_offset_in_bits += type_align_in_bits - 1;
9307 object_offset_in_bits /= type_align_in_bits;
9308 object_offset_in_bits *= type_align_in_bits;
9310 if (object_offset_in_bits > bitpos_int)
9312 /* Sigh, the decl must be packed. */
9313 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9315 /* Round up to decl_align instead. */
9316 object_offset_in_bits += decl_align_in_bits - 1;
9317 object_offset_in_bits /= decl_align_in_bits;
9318 object_offset_in_bits *= decl_align_in_bits;
9321 return object_offset_in_bits / BITS_PER_UNIT;
9324 /* The following routines define various Dwarf attributes and any data
9325 associated with them. */
9327 /* Add a location description attribute value to a DIE.
9329 This emits location attributes suitable for whole variables and
9330 whole parameters. Note that the location attributes for struct fields are
9331 generated by the routine `data_member_location_attribute' below. */
9333 static inline void
9334 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9335 dw_loc_descr_ref descr)
9337 if (descr != 0)
9338 add_AT_loc (die, attr_kind, descr);
9341 /* Attach the specialized form of location attribute used for data members of
9342 struct and union types. In the special case of a FIELD_DECL node which
9343 represents a bit-field, the "offset" part of this special location
9344 descriptor must indicate the distance in bytes from the lowest-addressed
9345 byte of the containing struct or union type to the lowest-addressed byte of
9346 the "containing object" for the bit-field. (See the `field_byte_offset'
9347 function above).
9349 For any given bit-field, the "containing object" is a hypothetical object
9350 (of some integral or enum type) within which the given bit-field lives. The
9351 type of this hypothetical "containing object" is always the same as the
9352 declared type of the individual bit-field itself (for GCC anyway... the
9353 DWARF spec doesn't actually mandate this). Note that it is the size (in
9354 bytes) of the hypothetical "containing object" which will be given in the
9355 DW_AT_byte_size attribute for this bit-field. (See the
9356 `byte_size_attribute' function below.) It is also used when calculating the
9357 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
9358 function below.) */
9360 static void
9361 add_data_member_location_attribute (dw_die_ref die, tree decl)
9363 HOST_WIDE_INT offset;
9364 dw_loc_descr_ref loc_descr = 0;
9366 if (TREE_CODE (decl) == TREE_VEC)
9368 /* We're working on the TAG_inheritance for a base class. */
9369 if (TREE_VIA_VIRTUAL (decl) && is_cxx ())
9371 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9372 aren't at a fixed offset from all (sub)objects of the same
9373 type. We need to extract the appropriate offset from our
9374 vtable. The following dwarf expression means
9376 BaseAddr = ObAddr + *((*ObAddr) - Offset)
9378 This is specific to the V3 ABI, of course. */
9380 dw_loc_descr_ref tmp;
9382 /* Make a copy of the object address. */
9383 tmp = new_loc_descr (DW_OP_dup, 0, 0);
9384 add_loc_descr (&loc_descr, tmp);
9386 /* Extract the vtable address. */
9387 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9388 add_loc_descr (&loc_descr, tmp);
9390 /* Calculate the address of the offset. */
9391 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9392 if (offset >= 0)
9393 abort ();
9395 tmp = int_loc_descriptor (-offset);
9396 add_loc_descr (&loc_descr, tmp);
9397 tmp = new_loc_descr (DW_OP_minus, 0, 0);
9398 add_loc_descr (&loc_descr, tmp);
9400 /* Extract the offset. */
9401 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9402 add_loc_descr (&loc_descr, tmp);
9404 /* Add it to the object address. */
9405 tmp = new_loc_descr (DW_OP_plus, 0, 0);
9406 add_loc_descr (&loc_descr, tmp);
9408 else
9409 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9411 else
9412 offset = field_byte_offset (decl);
9414 if (! loc_descr)
9416 enum dwarf_location_atom op;
9418 /* The DWARF2 standard says that we should assume that the structure
9419 address is already on the stack, so we can specify a structure field
9420 address by using DW_OP_plus_uconst. */
9422 #ifdef MIPS_DEBUGGING_INFO
9423 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9424 operator correctly. It works only if we leave the offset on the
9425 stack. */
9426 op = DW_OP_constu;
9427 #else
9428 op = DW_OP_plus_uconst;
9429 #endif
9431 loc_descr = new_loc_descr (op, offset, 0);
9434 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9437 /* Writes integer values to dw_vec_const array. */
9439 static void
9440 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9442 while (size != 0)
9444 *dest++ = val & 0xff;
9445 val >>= 8;
9446 --size;
9450 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
9452 static HOST_WIDE_INT
9453 extract_int (const unsigned char *src, unsigned int size)
9455 HOST_WIDE_INT val = 0;
9457 src += size;
9458 while (size != 0)
9460 val <<= 8;
9461 val |= *--src & 0xff;
9462 --size;
9464 return val;
9467 /* Writes floating point values to dw_vec_const array. */
9469 static void
9470 insert_float (rtx rtl, unsigned char *array)
9472 REAL_VALUE_TYPE rv;
9473 long val[4];
9474 int i;
9476 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9477 real_to_target (val, &rv, GET_MODE (rtl));
9479 /* real_to_target puts 32-bit pieces in each long. Pack them. */
9480 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9482 insert_int (val[i], 4, array);
9483 array += 4;
9487 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
9488 does not have a "location" either in memory or in a register. These
9489 things can arise in GNU C when a constant is passed as an actual parameter
9490 to an inlined function. They can also arise in C++ where declared
9491 constants do not necessarily get memory "homes". */
9493 static void
9494 add_const_value_attribute (dw_die_ref die, rtx rtl)
9496 switch (GET_CODE (rtl))
9498 case CONST_INT:
9500 HOST_WIDE_INT val = INTVAL (rtl);
9502 if (val < 0)
9503 add_AT_int (die, DW_AT_const_value, val);
9504 else
9505 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
9507 break;
9509 case CONST_DOUBLE:
9510 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9511 floating-point constant. A CONST_DOUBLE is used whenever the
9512 constant requires more than one word in order to be adequately
9513 represented. We output CONST_DOUBLEs as blocks. */
9515 enum machine_mode mode = GET_MODE (rtl);
9517 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
9519 unsigned int length = GET_MODE_SIZE (mode);
9520 unsigned char *array = ggc_alloc (length);
9522 insert_float (rtl, array);
9523 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
9525 else
9527 /* ??? We really should be using HOST_WIDE_INT throughout. */
9528 if (HOST_BITS_PER_LONG != HOST_BITS_PER_WIDE_INT)
9529 abort ();
9531 add_AT_long_long (die, DW_AT_const_value,
9532 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
9535 break;
9537 case CONST_VECTOR:
9539 enum machine_mode mode = GET_MODE (rtl);
9540 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9541 unsigned int length = CONST_VECTOR_NUNITS (rtl);
9542 unsigned char *array = ggc_alloc (length * elt_size);
9543 unsigned int i;
9544 unsigned char *p;
9546 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
9548 for (i = 0, p = array; i < length; i++, p += elt_size)
9550 rtx elt = CONST_VECTOR_ELT (rtl, i);
9551 HOST_WIDE_INT lo, hi;
9552 if (GET_CODE (elt) == CONST_INT)
9554 lo = INTVAL (elt);
9555 hi = -(lo < 0);
9557 else if (GET_CODE (elt) == CONST_DOUBLE)
9559 lo = CONST_DOUBLE_LOW (elt);
9560 hi = CONST_DOUBLE_HIGH (elt);
9562 else
9563 abort ();
9565 if (elt_size <= sizeof (HOST_WIDE_INT))
9566 insert_int (lo, elt_size, p);
9567 else if (elt_size == 2 * sizeof (HOST_WIDE_INT))
9569 unsigned char *p0 = p;
9570 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9572 if (WORDS_BIG_ENDIAN)
9574 p0 = p1;
9575 p1 = p;
9577 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9578 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9580 else
9581 abort ();
9584 else if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
9586 for (i = 0, p = array; i < length; i++, p += elt_size)
9588 rtx elt = CONST_VECTOR_ELT (rtl, i);
9589 insert_float (elt, p);
9592 else
9593 abort ();
9595 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
9597 break;
9599 case CONST_STRING:
9600 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
9601 break;
9603 case SYMBOL_REF:
9604 case LABEL_REF:
9605 case CONST:
9606 add_AT_addr (die, DW_AT_const_value, rtl);
9607 VARRAY_PUSH_RTX (used_rtx_varray, rtl);
9608 break;
9610 case PLUS:
9611 /* In cases where an inlined instance of an inline function is passed
9612 the address of an `auto' variable (which is local to the caller) we
9613 can get a situation where the DECL_RTL of the artificial local
9614 variable (for the inlining) which acts as a stand-in for the
9615 corresponding formal parameter (of the inline function) will look
9616 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
9617 exactly a compile-time constant expression, but it isn't the address
9618 of the (artificial) local variable either. Rather, it represents the
9619 *value* which the artificial local variable always has during its
9620 lifetime. We currently have no way to represent such quasi-constant
9621 values in Dwarf, so for now we just punt and generate nothing. */
9622 break;
9624 default:
9625 /* No other kinds of rtx should be possible here. */
9626 abort ();
9631 static rtx
9632 rtl_for_decl_location (tree decl)
9634 rtx rtl;
9636 /* Here we have to decide where we are going to say the parameter "lives"
9637 (as far as the debugger is concerned). We only have a couple of
9638 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
9640 DECL_RTL normally indicates where the parameter lives during most of the
9641 activation of the function. If optimization is enabled however, this
9642 could be either NULL or else a pseudo-reg. Both of those cases indicate
9643 that the parameter doesn't really live anywhere (as far as the code
9644 generation parts of GCC are concerned) during most of the function's
9645 activation. That will happen (for example) if the parameter is never
9646 referenced within the function.
9648 We could just generate a location descriptor here for all non-NULL
9649 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
9650 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
9651 where DECL_RTL is NULL or is a pseudo-reg.
9653 Note however that we can only get away with using DECL_INCOMING_RTL as
9654 a backup substitute for DECL_RTL in certain limited cases. In cases
9655 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
9656 we can be sure that the parameter was passed using the same type as it is
9657 declared to have within the function, and that its DECL_INCOMING_RTL
9658 points us to a place where a value of that type is passed.
9660 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
9661 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
9662 because in these cases DECL_INCOMING_RTL points us to a value of some
9663 type which is *different* from the type of the parameter itself. Thus,
9664 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
9665 such cases, the debugger would end up (for example) trying to fetch a
9666 `float' from a place which actually contains the first part of a
9667 `double'. That would lead to really incorrect and confusing
9668 output at debug-time.
9670 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
9671 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
9672 are a couple of exceptions however. On little-endian machines we can
9673 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
9674 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
9675 an integral type that is smaller than TREE_TYPE (decl). These cases arise
9676 when (on a little-endian machine) a non-prototyped function has a
9677 parameter declared to be of type `short' or `char'. In such cases,
9678 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
9679 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
9680 passed `int' value. If the debugger then uses that address to fetch
9681 a `short' or a `char' (on a little-endian machine) the result will be
9682 the correct data, so we allow for such exceptional cases below.
9684 Note that our goal here is to describe the place where the given formal
9685 parameter lives during most of the function's activation (i.e. between the
9686 end of the prologue and the start of the epilogue). We'll do that as best
9687 as we can. Note however that if the given formal parameter is modified
9688 sometime during the execution of the function, then a stack backtrace (at
9689 debug-time) will show the function as having been called with the *new*
9690 value rather than the value which was originally passed in. This happens
9691 rarely enough that it is not a major problem, but it *is* a problem, and
9692 I'd like to fix it.
9694 A future version of dwarf2out.c may generate two additional attributes for
9695 any given DW_TAG_formal_parameter DIE which will describe the "passed
9696 type" and the "passed location" for the given formal parameter in addition
9697 to the attributes we now generate to indicate the "declared type" and the
9698 "active location" for each parameter. This additional set of attributes
9699 could be used by debuggers for stack backtraces. Separately, note that
9700 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
9701 This happens (for example) for inlined-instances of inline function formal
9702 parameters which are never referenced. This really shouldn't be
9703 happening. All PARM_DECL nodes should get valid non-NULL
9704 DECL_INCOMING_RTL values. FIXME. */
9706 /* Use DECL_RTL as the "location" unless we find something better. */
9707 rtl = DECL_RTL_IF_SET (decl);
9709 /* When generating abstract instances, ignore everything except
9710 constants, symbols living in memory, and symbols living in
9711 fixed registers. */
9712 if (! reload_completed)
9714 if (rtl
9715 && (CONSTANT_P (rtl)
9716 || (GET_CODE (rtl) == MEM
9717 && CONSTANT_P (XEXP (rtl, 0)))
9718 || (REG_P (rtl)
9719 && TREE_CODE (decl) == VAR_DECL
9720 && TREE_STATIC (decl))))
9722 rtl = targetm.delegitimize_address (rtl);
9723 return rtl;
9725 rtl = NULL_RTX;
9727 else if (TREE_CODE (decl) == PARM_DECL)
9729 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
9731 tree declared_type = type_main_variant (TREE_TYPE (decl));
9732 tree passed_type = type_main_variant (DECL_ARG_TYPE (decl));
9734 /* This decl represents a formal parameter which was optimized out.
9735 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
9736 all cases where (rtl == NULL_RTX) just below. */
9737 if (declared_type == passed_type)
9738 rtl = DECL_INCOMING_RTL (decl);
9739 else if (! BYTES_BIG_ENDIAN
9740 && TREE_CODE (declared_type) == INTEGER_TYPE
9741 && (GET_MODE_SIZE (TYPE_MODE (declared_type))
9742 <= GET_MODE_SIZE (TYPE_MODE (passed_type))))
9743 rtl = DECL_INCOMING_RTL (decl);
9746 /* If the parm was passed in registers, but lives on the stack, then
9747 make a big endian correction if the mode of the type of the
9748 parameter is not the same as the mode of the rtl. */
9749 /* ??? This is the same series of checks that are made in dbxout.c before
9750 we reach the big endian correction code there. It isn't clear if all
9751 of these checks are necessary here, but keeping them all is the safe
9752 thing to do. */
9753 else if (GET_CODE (rtl) == MEM
9754 && XEXP (rtl, 0) != const0_rtx
9755 && ! CONSTANT_P (XEXP (rtl, 0))
9756 /* Not passed in memory. */
9757 && GET_CODE (DECL_INCOMING_RTL (decl)) != MEM
9758 /* Not passed by invisible reference. */
9759 && (!REG_P (XEXP (rtl, 0))
9760 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
9761 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
9762 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
9763 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
9764 #endif
9766 /* Big endian correction check. */
9767 && BYTES_BIG_ENDIAN
9768 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
9769 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
9770 < UNITS_PER_WORD))
9772 int offset = (UNITS_PER_WORD
9773 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
9775 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
9776 plus_constant (XEXP (rtl, 0), offset));
9779 else if (TREE_CODE (decl) == VAR_DECL
9780 && rtl
9781 && GET_CODE (rtl) == MEM
9782 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
9783 && BYTES_BIG_ENDIAN)
9785 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
9786 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
9788 /* If a variable is declared "register" yet is smaller than
9789 a register, then if we store the variable to memory, it
9790 looks like we're storing a register-sized value, when in
9791 fact we are not. We need to adjust the offset of the
9792 storage location to reflect the actual value's bytes,
9793 else gdb will not be able to display it. */
9794 if (rsize > dsize)
9795 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
9796 plus_constant (XEXP (rtl, 0), rsize-dsize));
9799 if (rtl != NULL_RTX)
9801 rtl = eliminate_regs (rtl, 0, NULL_RTX);
9802 #ifdef LEAF_REG_REMAP
9803 if (current_function_uses_only_leaf_regs)
9804 leaf_renumber_regs_insn (rtl);
9805 #endif
9808 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
9809 and will have been substituted directly into all expressions that use it.
9810 C does not have such a concept, but C++ and other languages do. */
9811 else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
9813 /* If a variable is initialized with a string constant without embedded
9814 zeros, build CONST_STRING. */
9815 if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
9816 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9818 tree arrtype = TREE_TYPE (decl);
9819 tree enttype = TREE_TYPE (arrtype);
9820 tree domain = TYPE_DOMAIN (arrtype);
9821 tree init = DECL_INITIAL (decl);
9822 enum machine_mode mode = TYPE_MODE (enttype);
9824 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
9825 && domain
9826 && integer_zerop (TYPE_MIN_VALUE (domain))
9827 && compare_tree_int (TYPE_MAX_VALUE (domain),
9828 TREE_STRING_LENGTH (init) - 1) == 0
9829 && ((size_t) TREE_STRING_LENGTH (init)
9830 == strlen (TREE_STRING_POINTER (init)) + 1))
9831 rtl = gen_rtx_CONST_STRING (VOIDmode, TREE_STRING_POINTER (init));
9833 /* If the initializer is something that we know will expand into an
9834 immediate RTL constant, expand it now. Expanding anything else
9835 tends to produce unresolved symbols; see debug/5770 and c++/6381. */
9836 else if (TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST
9837 || TREE_CODE (DECL_INITIAL (decl)) == REAL_CST)
9839 rtl = expand_expr (DECL_INITIAL (decl), NULL_RTX, VOIDmode,
9840 EXPAND_INITIALIZER);
9841 /* If expand_expr returns a MEM, it wasn't immediate. */
9842 if (rtl && GET_CODE (rtl) == MEM)
9843 abort ();
9847 if (rtl)
9848 rtl = targetm.delegitimize_address (rtl);
9850 /* If we don't look past the constant pool, we risk emitting a
9851 reference to a constant pool entry that isn't referenced from
9852 code, and thus is not emitted. */
9853 if (rtl)
9854 rtl = avoid_constant_pool_reference (rtl);
9856 return rtl;
9859 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
9860 data attribute for a variable or a parameter. We generate the
9861 DW_AT_const_value attribute only in those cases where the given variable
9862 or parameter does not have a true "location" either in memory or in a
9863 register. This can happen (for example) when a constant is passed as an
9864 actual argument in a call to an inline function. (It's possible that
9865 these things can crop up in other ways also.) Note that one type of
9866 constant value which can be passed into an inlined function is a constant
9867 pointer. This can happen for example if an actual argument in an inlined
9868 function call evaluates to a compile-time constant address. */
9870 static void
9871 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
9872 enum dwarf_attribute attr)
9874 rtx rtl;
9875 dw_loc_descr_ref descr;
9876 var_loc_list *loc_list;
9878 if (TREE_CODE (decl) == ERROR_MARK)
9879 return;
9880 else if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != PARM_DECL
9881 && TREE_CODE (decl) != RESULT_DECL)
9882 abort ();
9884 /* See if we possibly have multiple locations for this variable. */
9885 loc_list = lookup_decl_loc (decl);
9887 /* If it truly has multiple locations, the first and last node will
9888 differ. */
9889 if (loc_list && loc_list->first != loc_list->last)
9891 const char *secname;
9892 const char *endname;
9893 dw_loc_list_ref list;
9894 rtx varloc;
9895 struct var_loc_node *node;
9897 /* We need to figure out what section we should use as the base
9898 for the address ranges where a given location is valid.
9899 1. If this particular DECL has a section associated with it,
9900 use that.
9901 2. If this function has a section associated with it, use
9902 that.
9903 3. Otherwise, use the text section.
9904 XXX: If you split a variable across multiple sections, this
9905 won't notice. */
9907 if (DECL_SECTION_NAME (decl))
9909 tree sectree = DECL_SECTION_NAME (decl);
9910 secname = TREE_STRING_POINTER (sectree);
9912 else if (current_function_decl
9913 && DECL_SECTION_NAME (current_function_decl))
9915 tree sectree = DECL_SECTION_NAME (current_function_decl);
9916 secname = TREE_STRING_POINTER (sectree);
9918 else
9919 secname = text_section_label;
9921 /* Now that we know what section we are using for a base,
9922 actually construct the list of locations.
9923 The first location information is what is passed to the
9924 function that creates the location list, and the remaining
9925 locations just get added on to that list.
9926 Note that we only know the start address for a location
9927 (IE location changes), so to build the range, we use
9928 the range [current location start, next location start].
9929 This means we have to special case the last node, and generate
9930 a range of [last location start, end of function label]. */
9932 node = loc_list->first;
9933 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
9934 list = new_loc_list (loc_descriptor (varloc, attr != DW_AT_frame_base),
9935 node->label, node->next->label, secname, 1);
9936 node = node->next;
9938 for (; node->next; node = node->next)
9939 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
9941 /* The variable has a location between NODE->LABEL and
9942 NODE->NEXT->LABEL. */
9943 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
9944 add_loc_descr_to_loc_list (&list,
9945 loc_descriptor (varloc,
9946 attr != DW_AT_frame_base),
9947 node->label, node->next->label, secname);
9950 /* If the variable has a location at the last label
9951 it keeps its location until the end of function. */
9952 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
9954 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
9956 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
9957 if (!current_function_decl)
9958 endname = text_end_label;
9959 else
9961 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
9962 current_function_funcdef_no);
9963 endname = ggc_strdup (label_id);
9965 add_loc_descr_to_loc_list (&list,
9966 loc_descriptor (varloc,
9967 attr != DW_AT_frame_base),
9968 node->label, endname, secname);
9971 /* Finally, add the location list to the DIE, and we are done. */
9972 add_AT_loc_list (die, attr, list);
9973 return;
9976 rtl = rtl_for_decl_location (decl);
9977 if (rtl == NULL_RTX)
9978 return;
9980 switch (GET_CODE (rtl))
9982 case ADDRESSOF:
9983 /* The address of a variable that was optimized away;
9984 don't emit anything. */
9985 break;
9987 case CONST_INT:
9988 case CONST_DOUBLE:
9989 case CONST_VECTOR:
9990 case CONST_STRING:
9991 case SYMBOL_REF:
9992 case LABEL_REF:
9993 case CONST:
9994 case PLUS:
9995 /* DECL_RTL could be (plus (reg ...) (const_int ...)) */
9996 add_const_value_attribute (die, rtl);
9997 break;
9999 case MEM:
10000 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL (decl))
10002 /* Need loc_descriptor_from_tree since that's where we know
10003 how to handle TLS variables. Want the object's address
10004 since the top-level DW_AT_location assumes such. See
10005 the confusion in loc_descriptor for reference. */
10006 descr = loc_descriptor_from_tree (decl, 1);
10008 else
10010 case REG:
10011 case SUBREG:
10012 case CONCAT:
10013 descr = loc_descriptor (rtl, true);
10015 add_AT_location_description (die, attr, descr);
10016 break;
10018 case PARALLEL:
10020 rtvec par_elems = XVEC (rtl, 0);
10021 int num_elem = GET_NUM_ELEM (par_elems);
10022 enum machine_mode mode;
10023 int i;
10025 /* Create the first one, so we have something to add to. */
10026 descr = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0), true);
10027 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10028 add_loc_descr (&descr,
10029 new_loc_descr (DW_OP_piece, GET_MODE_SIZE (mode), 0));
10030 for (i = 1; i < num_elem; i++)
10032 dw_loc_descr_ref temp;
10034 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0), true);
10035 add_loc_descr (&descr, temp);
10036 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10037 add_loc_descr (&descr,
10038 new_loc_descr (DW_OP_piece,
10039 GET_MODE_SIZE (mode), 0));
10042 add_AT_location_description (die, DW_AT_location, descr);
10043 break;
10045 default:
10046 abort ();
10050 /* If we don't have a copy of this variable in memory for some reason (such
10051 as a C++ member constant that doesn't have an out-of-line definition),
10052 we should tell the debugger about the constant value. */
10054 static void
10055 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10057 tree init = DECL_INITIAL (decl);
10058 tree type = TREE_TYPE (decl);
10060 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init
10061 && initializer_constant_valid_p (init, type) == null_pointer_node)
10062 /* OK */;
10063 else
10064 return;
10066 switch (TREE_CODE (type))
10068 case INTEGER_TYPE:
10069 if (host_integerp (init, 0))
10070 add_AT_unsigned (var_die, DW_AT_const_value,
10071 tree_low_cst (init, 0));
10072 else
10073 add_AT_long_long (var_die, DW_AT_const_value,
10074 TREE_INT_CST_HIGH (init),
10075 TREE_INT_CST_LOW (init));
10076 break;
10078 default:;
10082 /* Generate a DW_AT_name attribute given some string value to be included as
10083 the value of the attribute. */
10085 static void
10086 add_name_attribute (dw_die_ref die, const char *name_string)
10088 if (name_string != NULL && *name_string != 0)
10090 if (demangle_name_func)
10091 name_string = (*demangle_name_func) (name_string);
10093 add_AT_string (die, DW_AT_name, name_string);
10097 /* Generate a DW_AT_comp_dir attribute for DIE. */
10099 static void
10100 add_comp_dir_attribute (dw_die_ref die)
10102 const char *wd = get_src_pwd ();
10103 if (wd != NULL)
10104 add_AT_string (die, DW_AT_comp_dir, wd);
10107 /* Given a tree node describing an array bound (either lower or upper) output
10108 a representation for that bound. */
10110 static void
10111 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10113 switch (TREE_CODE (bound))
10115 case ERROR_MARK:
10116 return;
10118 /* All fixed-bounds are represented by INTEGER_CST nodes. */
10119 case INTEGER_CST:
10120 if (! host_integerp (bound, 0)
10121 || (bound_attr == DW_AT_lower_bound
10122 && (((is_c_family () || is_java ()) && integer_zerop (bound))
10123 || (is_fortran () && integer_onep (bound)))))
10124 /* Use the default. */
10126 else
10127 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
10128 break;
10130 case CONVERT_EXPR:
10131 case NOP_EXPR:
10132 case NON_LVALUE_EXPR:
10133 case VIEW_CONVERT_EXPR:
10134 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10135 break;
10137 case SAVE_EXPR:
10138 /* If optimization is turned on, the SAVE_EXPRs that describe how to
10139 access the upper bound values may be bogus. If they refer to a
10140 register, they may only describe how to get at these values at the
10141 points in the generated code right after they have just been
10142 computed. Worse yet, in the typical case, the upper bound values
10143 will not even *be* computed in the optimized code (though the
10144 number of elements will), so these SAVE_EXPRs are entirely
10145 bogus. In order to compensate for this fact, we check here to see
10146 if optimization is enabled, and if so, we don't add an attribute
10147 for the (unknown and unknowable) upper bound. This should not
10148 cause too much trouble for existing (stupid?) debuggers because
10149 they have to deal with empty upper bounds location descriptions
10150 anyway in order to be able to deal with incomplete array types.
10151 Of course an intelligent debugger (GDB?) should be able to
10152 comprehend that a missing upper bound specification in an array
10153 type used for a storage class `auto' local array variable
10154 indicates that the upper bound is both unknown (at compile- time)
10155 and unknowable (at run-time) due to optimization.
10157 We assume that a MEM rtx is safe because gcc wouldn't put the
10158 value there unless it was going to be used repeatedly in the
10159 function, i.e. for cleanups. */
10160 if (SAVE_EXPR_RTL (bound)
10161 && (! optimize || GET_CODE (SAVE_EXPR_RTL (bound)) == MEM))
10163 dw_die_ref ctx = lookup_decl_die (current_function_decl);
10164 dw_die_ref decl_die = new_die (DW_TAG_variable, ctx, bound);
10165 rtx loc = SAVE_EXPR_RTL (bound);
10167 /* If the RTL for the SAVE_EXPR is memory, handle the case where
10168 it references an outer function's frame. */
10169 if (GET_CODE (loc) == MEM)
10171 rtx new_addr = fix_lexical_addr (XEXP (loc, 0), bound);
10173 if (XEXP (loc, 0) != new_addr)
10174 loc = gen_rtx_MEM (GET_MODE (loc), new_addr);
10177 add_AT_flag (decl_die, DW_AT_artificial, 1);
10178 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10179 add_AT_location_description (decl_die, DW_AT_location,
10180 loc_descriptor (loc, true));
10181 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10184 /* Else leave out the attribute. */
10185 break;
10187 case VAR_DECL:
10188 case PARM_DECL:
10189 case RESULT_DECL:
10191 dw_die_ref decl_die = lookup_decl_die (bound);
10193 /* ??? Can this happen, or should the variable have been bound
10194 first? Probably it can, since I imagine that we try to create
10195 the types of parameters in the order in which they exist in
10196 the list, and won't have created a forward reference to a
10197 later parameter. */
10198 if (decl_die != NULL)
10199 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10200 break;
10203 default:
10205 /* Otherwise try to create a stack operation procedure to
10206 evaluate the value of the array bound. */
10208 dw_die_ref ctx, decl_die;
10209 dw_loc_descr_ref loc;
10211 loc = loc_descriptor_from_tree (bound, 0);
10212 if (loc == NULL)
10213 break;
10215 if (current_function_decl == 0)
10216 ctx = comp_unit_die;
10217 else
10218 ctx = lookup_decl_die (current_function_decl);
10220 /* If we weren't able to find a context, it's most likely the case
10221 that we are processing the return type of the function. So
10222 make a SAVE_EXPR to point to it and have the limbo DIE code
10223 find the proper die. The save_expr function doesn't always
10224 make a SAVE_EXPR, so do it ourselves. */
10225 if (ctx == 0)
10226 bound = build (SAVE_EXPR, TREE_TYPE (bound), bound,
10227 current_function_decl, NULL_TREE);
10229 decl_die = new_die (DW_TAG_variable, ctx, bound);
10230 add_AT_flag (decl_die, DW_AT_artificial, 1);
10231 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10232 add_AT_loc (decl_die, DW_AT_location, loc);
10234 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10235 break;
10240 /* Note that the block of subscript information for an array type also
10241 includes information about the element type of type given array type. */
10243 static void
10244 add_subscript_info (dw_die_ref type_die, tree type)
10246 #ifndef MIPS_DEBUGGING_INFO
10247 unsigned dimension_number;
10248 #endif
10249 tree lower, upper;
10250 dw_die_ref subrange_die;
10252 /* The GNU compilers represent multidimensional array types as sequences of
10253 one dimensional array types whose element types are themselves array
10254 types. Here we squish that down, so that each multidimensional array
10255 type gets only one array_type DIE in the Dwarf debugging info. The draft
10256 Dwarf specification say that we are allowed to do this kind of
10257 compression in C (because there is no difference between an array or
10258 arrays and a multidimensional array in C) but for other source languages
10259 (e.g. Ada) we probably shouldn't do this. */
10261 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10262 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10263 We work around this by disabling this feature. See also
10264 gen_array_type_die. */
10265 #ifndef MIPS_DEBUGGING_INFO
10266 for (dimension_number = 0;
10267 TREE_CODE (type) == ARRAY_TYPE;
10268 type = TREE_TYPE (type), dimension_number++)
10269 #endif
10271 tree domain = TYPE_DOMAIN (type);
10273 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
10274 and (in GNU C only) variable bounds. Handle all three forms
10275 here. */
10276 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
10277 if (domain)
10279 /* We have an array type with specified bounds. */
10280 lower = TYPE_MIN_VALUE (domain);
10281 upper = TYPE_MAX_VALUE (domain);
10283 /* Define the index type. */
10284 if (TREE_TYPE (domain))
10286 /* ??? This is probably an Ada unnamed subrange type. Ignore the
10287 TREE_TYPE field. We can't emit debug info for this
10288 because it is an unnamed integral type. */
10289 if (TREE_CODE (domain) == INTEGER_TYPE
10290 && TYPE_NAME (domain) == NULL_TREE
10291 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10292 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
10294 else
10295 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10296 type_die);
10299 /* ??? If upper is NULL, the array has unspecified length,
10300 but it does have a lower bound. This happens with Fortran
10301 dimension arr(N:*)
10302 Since the debugger is definitely going to need to know N
10303 to produce useful results, go ahead and output the lower
10304 bound solo, and hope the debugger can cope. */
10306 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
10307 if (upper)
10308 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
10311 /* Otherwise we have an array type with an unspecified length. The
10312 DWARF-2 spec does not say how to handle this; let's just leave out the
10313 bounds. */
10317 static void
10318 add_byte_size_attribute (dw_die_ref die, tree tree_node)
10320 unsigned size;
10322 switch (TREE_CODE (tree_node))
10324 case ERROR_MARK:
10325 size = 0;
10326 break;
10327 case ENUMERAL_TYPE:
10328 case RECORD_TYPE:
10329 case UNION_TYPE:
10330 case QUAL_UNION_TYPE:
10331 size = int_size_in_bytes (tree_node);
10332 break;
10333 case FIELD_DECL:
10334 /* For a data member of a struct or union, the DW_AT_byte_size is
10335 generally given as the number of bytes normally allocated for an
10336 object of the *declared* type of the member itself. This is true
10337 even for bit-fields. */
10338 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10339 break;
10340 default:
10341 abort ();
10344 /* Note that `size' might be -1 when we get to this point. If it is, that
10345 indicates that the byte size of the entity in question is variable. We
10346 have no good way of expressing this fact in Dwarf at the present time,
10347 so just let the -1 pass on through. */
10348 add_AT_unsigned (die, DW_AT_byte_size, size);
10351 /* For a FIELD_DECL node which represents a bit-field, output an attribute
10352 which specifies the distance in bits from the highest order bit of the
10353 "containing object" for the bit-field to the highest order bit of the
10354 bit-field itself.
10356 For any given bit-field, the "containing object" is a hypothetical object
10357 (of some integral or enum type) within which the given bit-field lives. The
10358 type of this hypothetical "containing object" is always the same as the
10359 declared type of the individual bit-field itself. The determination of the
10360 exact location of the "containing object" for a bit-field is rather
10361 complicated. It's handled by the `field_byte_offset' function (above).
10363 Note that it is the size (in bytes) of the hypothetical "containing object"
10364 which will be given in the DW_AT_byte_size attribute for this bit-field.
10365 (See `byte_size_attribute' above). */
10367 static inline void
10368 add_bit_offset_attribute (dw_die_ref die, tree decl)
10370 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10371 tree type = DECL_BIT_FIELD_TYPE (decl);
10372 HOST_WIDE_INT bitpos_int;
10373 HOST_WIDE_INT highest_order_object_bit_offset;
10374 HOST_WIDE_INT highest_order_field_bit_offset;
10375 HOST_WIDE_INT unsigned bit_offset;
10377 /* Must be a field and a bit field. */
10378 if (!type
10379 || TREE_CODE (decl) != FIELD_DECL)
10380 abort ();
10382 /* We can't yet handle bit-fields whose offsets are variable, so if we
10383 encounter such things, just return without generating any attribute
10384 whatsoever. Likewise for variable or too large size. */
10385 if (! host_integerp (bit_position (decl), 0)
10386 || ! host_integerp (DECL_SIZE (decl), 1))
10387 return;
10389 bitpos_int = int_bit_position (decl);
10391 /* Note that the bit offset is always the distance (in bits) from the
10392 highest-order bit of the "containing object" to the highest-order bit of
10393 the bit-field itself. Since the "high-order end" of any object or field
10394 is different on big-endian and little-endian machines, the computation
10395 below must take account of these differences. */
10396 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10397 highest_order_field_bit_offset = bitpos_int;
10399 if (! BYTES_BIG_ENDIAN)
10401 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
10402 highest_order_object_bit_offset += simple_type_size_in_bits (type);
10405 bit_offset
10406 = (! BYTES_BIG_ENDIAN
10407 ? highest_order_object_bit_offset - highest_order_field_bit_offset
10408 : highest_order_field_bit_offset - highest_order_object_bit_offset);
10410 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10413 /* For a FIELD_DECL node which represents a bit field, output an attribute
10414 which specifies the length in bits of the given field. */
10416 static inline void
10417 add_bit_size_attribute (dw_die_ref die, tree decl)
10419 /* Must be a field and a bit field. */
10420 if (TREE_CODE (decl) != FIELD_DECL
10421 || ! DECL_BIT_FIELD_TYPE (decl))
10422 abort ();
10424 if (host_integerp (DECL_SIZE (decl), 1))
10425 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
10428 /* If the compiled language is ANSI C, then add a 'prototyped'
10429 attribute, if arg types are given for the parameters of a function. */
10431 static inline void
10432 add_prototyped_attribute (dw_die_ref die, tree func_type)
10434 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10435 && TYPE_ARG_TYPES (func_type) != NULL)
10436 add_AT_flag (die, DW_AT_prototyped, 1);
10439 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
10440 by looking in either the type declaration or object declaration
10441 equate table. */
10443 static inline void
10444 add_abstract_origin_attribute (dw_die_ref die, tree origin)
10446 dw_die_ref origin_die = NULL;
10448 if (TREE_CODE (origin) != FUNCTION_DECL)
10450 /* We may have gotten separated from the block for the inlined
10451 function, if we're in an exception handler or some such; make
10452 sure that the abstract function has been written out.
10454 Doing this for nested functions is wrong, however; functions are
10455 distinct units, and our context might not even be inline. */
10456 tree fn = origin;
10458 if (TYPE_P (fn))
10459 fn = TYPE_STUB_DECL (fn);
10461 fn = decl_function_context (fn);
10462 if (fn)
10463 dwarf2out_abstract_function (fn);
10466 if (DECL_P (origin))
10467 origin_die = lookup_decl_die (origin);
10468 else if (TYPE_P (origin))
10469 origin_die = lookup_type_die (origin);
10471 if (origin_die == NULL)
10472 abort ();
10474 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
10477 /* We do not currently support the pure_virtual attribute. */
10479 static inline void
10480 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
10482 if (DECL_VINDEX (func_decl))
10484 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10486 if (host_integerp (DECL_VINDEX (func_decl), 0))
10487 add_AT_loc (die, DW_AT_vtable_elem_location,
10488 new_loc_descr (DW_OP_constu,
10489 tree_low_cst (DECL_VINDEX (func_decl), 0),
10490 0));
10492 /* GNU extension: Record what type this method came from originally. */
10493 if (debug_info_level > DINFO_LEVEL_TERSE)
10494 add_AT_die_ref (die, DW_AT_containing_type,
10495 lookup_type_die (DECL_CONTEXT (func_decl)));
10499 /* Add source coordinate attributes for the given decl. */
10501 static void
10502 add_src_coords_attributes (dw_die_ref die, tree decl)
10504 unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
10506 add_AT_unsigned (die, DW_AT_decl_file, file_index);
10507 add_AT_unsigned (die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
10510 /* Add a DW_AT_name attribute and source coordinate attribute for the
10511 given decl, but only if it actually has a name. */
10513 static void
10514 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
10516 tree decl_name;
10518 decl_name = DECL_NAME (decl);
10519 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
10521 add_name_attribute (die, dwarf2_name (decl, 0));
10522 if (! DECL_ARTIFICIAL (decl))
10523 add_src_coords_attributes (die, decl);
10525 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
10526 && TREE_PUBLIC (decl)
10527 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
10528 && !DECL_ABSTRACT (decl))
10529 add_AT_string (die, DW_AT_MIPS_linkage_name,
10530 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
10533 #ifdef VMS_DEBUGGING_INFO
10534 /* Get the function's name, as described by its RTL. This may be different
10535 from the DECL_NAME name used in the source file. */
10536 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
10538 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
10539 XEXP (DECL_RTL (decl), 0));
10540 VARRAY_PUSH_RTX (used_rtx_varray, XEXP (DECL_RTL (decl), 0));
10542 #endif
10545 /* Push a new declaration scope. */
10547 static void
10548 push_decl_scope (tree scope)
10550 VARRAY_PUSH_TREE (decl_scope_table, scope);
10553 /* Pop a declaration scope. */
10555 static inline void
10556 pop_decl_scope (void)
10558 if (VARRAY_ACTIVE_SIZE (decl_scope_table) <= 0)
10559 abort ();
10561 VARRAY_POP (decl_scope_table);
10564 /* Return the DIE for the scope that immediately contains this type.
10565 Non-named types get global scope. Named types nested in other
10566 types get their containing scope if it's open, or global scope
10567 otherwise. All other types (i.e. function-local named types) get
10568 the current active scope. */
10570 static dw_die_ref
10571 scope_die_for (tree t, dw_die_ref context_die)
10573 dw_die_ref scope_die = NULL;
10574 tree containing_scope;
10575 int i;
10577 /* Non-types always go in the current scope. */
10578 if (! TYPE_P (t))
10579 abort ();
10581 containing_scope = TYPE_CONTEXT (t);
10583 /* Use the containing namespace if it was passed in (for a declaration). */
10584 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
10586 if (context_die == lookup_decl_die (containing_scope))
10587 /* OK */;
10588 else
10589 containing_scope = NULL_TREE;
10592 /* Ignore function type "scopes" from the C frontend. They mean that
10593 a tagged type is local to a parmlist of a function declarator, but
10594 that isn't useful to DWARF. */
10595 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
10596 containing_scope = NULL_TREE;
10598 if (containing_scope == NULL_TREE)
10599 scope_die = comp_unit_die;
10600 else if (TYPE_P (containing_scope))
10602 /* For types, we can just look up the appropriate DIE. But
10603 first we check to see if we're in the middle of emitting it
10604 so we know where the new DIE should go. */
10605 for (i = VARRAY_ACTIVE_SIZE (decl_scope_table) - 1; i >= 0; --i)
10606 if (VARRAY_TREE (decl_scope_table, i) == containing_scope)
10607 break;
10609 if (i < 0)
10611 if (debug_info_level > DINFO_LEVEL_TERSE
10612 && !TREE_ASM_WRITTEN (containing_scope))
10613 abort ();
10615 /* If none of the current dies are suitable, we get file scope. */
10616 scope_die = comp_unit_die;
10618 else
10619 scope_die = lookup_type_die (containing_scope);
10621 else
10622 scope_die = context_die;
10624 return scope_die;
10627 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
10629 static inline int
10630 local_scope_p (dw_die_ref context_die)
10632 for (; context_die; context_die = context_die->die_parent)
10633 if (context_die->die_tag == DW_TAG_inlined_subroutine
10634 || context_die->die_tag == DW_TAG_subprogram)
10635 return 1;
10637 return 0;
10640 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
10641 whether or not to treat a DIE in this context as a declaration. */
10643 static inline int
10644 class_or_namespace_scope_p (dw_die_ref context_die)
10646 return (context_die
10647 && (context_die->die_tag == DW_TAG_structure_type
10648 || context_die->die_tag == DW_TAG_union_type
10649 || context_die->die_tag == DW_TAG_namespace));
10652 /* Many forms of DIEs require a "type description" attribute. This
10653 routine locates the proper "type descriptor" die for the type given
10654 by 'type', and adds a DW_AT_type attribute below the given die. */
10656 static void
10657 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
10658 int decl_volatile, dw_die_ref context_die)
10660 enum tree_code code = TREE_CODE (type);
10661 dw_die_ref type_die = NULL;
10663 /* ??? If this type is an unnamed subrange type of an integral or
10664 floating-point type, use the inner type. This is because we have no
10665 support for unnamed types in base_type_die. This can happen if this is
10666 an Ada subrange type. Correct solution is emit a subrange type die. */
10667 if ((code == INTEGER_TYPE || code == REAL_TYPE)
10668 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
10669 type = TREE_TYPE (type), code = TREE_CODE (type);
10671 if (code == ERROR_MARK
10672 /* Handle a special case. For functions whose return type is void, we
10673 generate *no* type attribute. (Note that no object may have type
10674 `void', so this only applies to function return types). */
10675 || code == VOID_TYPE)
10676 return;
10678 type_die = modified_type_die (type,
10679 decl_const || TYPE_READONLY (type),
10680 decl_volatile || TYPE_VOLATILE (type),
10681 context_die);
10683 if (type_die != NULL)
10684 add_AT_die_ref (object_die, DW_AT_type, type_die);
10687 /* Given a tree pointer to a struct, class, union, or enum type node, return
10688 a pointer to the (string) tag name for the given type, or zero if the type
10689 was declared without a tag. */
10691 static const char *
10692 type_tag (tree type)
10694 const char *name = 0;
10696 if (TYPE_NAME (type) != 0)
10698 tree t = 0;
10700 /* Find the IDENTIFIER_NODE for the type name. */
10701 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
10702 t = TYPE_NAME (type);
10704 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
10705 a TYPE_DECL node, regardless of whether or not a `typedef' was
10706 involved. */
10707 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10708 && ! DECL_IGNORED_P (TYPE_NAME (type)))
10709 t = DECL_NAME (TYPE_NAME (type));
10711 /* Now get the name as a string, or invent one. */
10712 if (t != 0)
10713 name = IDENTIFIER_POINTER (t);
10716 return (name == 0 || *name == '\0') ? 0 : name;
10719 /* Return the type associated with a data member, make a special check
10720 for bit field types. */
10722 static inline tree
10723 member_declared_type (tree member)
10725 return (DECL_BIT_FIELD_TYPE (member)
10726 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
10729 /* Get the decl's label, as described by its RTL. This may be different
10730 from the DECL_NAME name used in the source file. */
10732 #if 0
10733 static const char *
10734 decl_start_label (tree decl)
10736 rtx x;
10737 const char *fnname;
10739 x = DECL_RTL (decl);
10740 if (GET_CODE (x) != MEM)
10741 abort ();
10743 x = XEXP (x, 0);
10744 if (GET_CODE (x) != SYMBOL_REF)
10745 abort ();
10747 fnname = XSTR (x, 0);
10748 return fnname;
10750 #endif
10752 /* These routines generate the internal representation of the DIE's for
10753 the compilation unit. Debugging information is collected by walking
10754 the declaration trees passed in from dwarf2out_decl(). */
10756 static void
10757 gen_array_type_die (tree type, dw_die_ref context_die)
10759 dw_die_ref scope_die = scope_die_for (type, context_die);
10760 dw_die_ref array_die;
10761 tree element_type;
10763 /* ??? The SGI dwarf reader fails for array of array of enum types unless
10764 the inner array type comes before the outer array type. Thus we must
10765 call gen_type_die before we call new_die. See below also. */
10766 #ifdef MIPS_DEBUGGING_INFO
10767 gen_type_die (TREE_TYPE (type), context_die);
10768 #endif
10770 array_die = new_die (DW_TAG_array_type, scope_die, type);
10771 add_name_attribute (array_die, type_tag (type));
10772 equate_type_number_to_die (type, array_die);
10774 if (TREE_CODE (type) == VECTOR_TYPE)
10776 /* The frontend feeds us a representation for the vector as a struct
10777 containing an array. Pull out the array type. */
10778 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
10779 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
10782 #if 0
10783 /* We default the array ordering. SDB will probably do
10784 the right things even if DW_AT_ordering is not present. It's not even
10785 an issue until we start to get into multidimensional arrays anyway. If
10786 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
10787 then we'll have to put the DW_AT_ordering attribute back in. (But if
10788 and when we find out that we need to put these in, we will only do so
10789 for multidimensional arrays. */
10790 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
10791 #endif
10793 #ifdef MIPS_DEBUGGING_INFO
10794 /* The SGI compilers handle arrays of unknown bound by setting
10795 AT_declaration and not emitting any subrange DIEs. */
10796 if (! TYPE_DOMAIN (type))
10797 add_AT_flag (array_die, DW_AT_declaration, 1);
10798 else
10799 #endif
10800 add_subscript_info (array_die, type);
10802 /* Add representation of the type of the elements of this array type. */
10803 element_type = TREE_TYPE (type);
10805 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10806 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10807 We work around this by disabling this feature. See also
10808 add_subscript_info. */
10809 #ifndef MIPS_DEBUGGING_INFO
10810 while (TREE_CODE (element_type) == ARRAY_TYPE)
10811 element_type = TREE_TYPE (element_type);
10813 gen_type_die (element_type, context_die);
10814 #endif
10816 add_type_attribute (array_die, element_type, 0, 0, context_die);
10819 static void
10820 gen_set_type_die (tree type, dw_die_ref context_die)
10822 dw_die_ref type_die
10823 = new_die (DW_TAG_set_type, scope_die_for (type, context_die), type);
10825 equate_type_number_to_die (type, type_die);
10826 add_type_attribute (type_die, TREE_TYPE (type), 0, 0, context_die);
10829 #if 0
10830 static void
10831 gen_entry_point_die (tree decl, dw_die_ref context_die)
10833 tree origin = decl_ultimate_origin (decl);
10834 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
10836 if (origin != NULL)
10837 add_abstract_origin_attribute (decl_die, origin);
10838 else
10840 add_name_and_src_coords_attributes (decl_die, decl);
10841 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
10842 0, 0, context_die);
10845 if (DECL_ABSTRACT (decl))
10846 equate_decl_number_to_die (decl, decl_die);
10847 else
10848 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
10850 #endif
10852 /* Walk through the list of incomplete types again, trying once more to
10853 emit full debugging info for them. */
10855 static void
10856 retry_incomplete_types (void)
10858 int i;
10860 for (i = VARRAY_ACTIVE_SIZE (incomplete_types) - 1; i >= 0; i--)
10861 gen_type_die (VARRAY_TREE (incomplete_types, i), comp_unit_die);
10864 /* Generate a DIE to represent an inlined instance of an enumeration type. */
10866 static void
10867 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
10869 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
10871 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10872 be incomplete and such types are not marked. */
10873 add_abstract_origin_attribute (type_die, type);
10876 /* Generate a DIE to represent an inlined instance of a structure type. */
10878 static void
10879 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
10881 dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
10883 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10884 be incomplete and such types are not marked. */
10885 add_abstract_origin_attribute (type_die, type);
10888 /* Generate a DIE to represent an inlined instance of a union type. */
10890 static void
10891 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
10893 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
10895 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10896 be incomplete and such types are not marked. */
10897 add_abstract_origin_attribute (type_die, type);
10900 /* Generate a DIE to represent an enumeration type. Note that these DIEs
10901 include all of the information about the enumeration values also. Each
10902 enumerated type name/value is listed as a child of the enumerated type
10903 DIE. */
10905 static dw_die_ref
10906 gen_enumeration_type_die (tree type, dw_die_ref context_die)
10908 dw_die_ref type_die = lookup_type_die (type);
10910 if (type_die == NULL)
10912 type_die = new_die (DW_TAG_enumeration_type,
10913 scope_die_for (type, context_die), type);
10914 equate_type_number_to_die (type, type_die);
10915 add_name_attribute (type_die, type_tag (type));
10917 else if (! TYPE_SIZE (type))
10918 return type_die;
10919 else
10920 remove_AT (type_die, DW_AT_declaration);
10922 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
10923 given enum type is incomplete, do not generate the DW_AT_byte_size
10924 attribute or the DW_AT_element_list attribute. */
10925 if (TYPE_SIZE (type))
10927 tree link;
10929 TREE_ASM_WRITTEN (type) = 1;
10930 add_byte_size_attribute (type_die, type);
10931 if (TYPE_STUB_DECL (type) != NULL_TREE)
10932 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
10934 /* If the first reference to this type was as the return type of an
10935 inline function, then it may not have a parent. Fix this now. */
10936 if (type_die->die_parent == NULL)
10937 add_child_die (scope_die_for (type, context_die), type_die);
10939 for (link = TYPE_VALUES (type);
10940 link != NULL; link = TREE_CHAIN (link))
10942 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
10943 tree value = TREE_VALUE (link);
10945 add_name_attribute (enum_die,
10946 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
10948 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
10949 /* DWARF2 does not provide a way of indicating whether or
10950 not enumeration constants are signed or unsigned. GDB
10951 always assumes the values are signed, so we output all
10952 values as if they were signed. That means that
10953 enumeration constants with very large unsigned values
10954 will appear to have negative values in the debugger. */
10955 add_AT_int (enum_die, DW_AT_const_value,
10956 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
10959 else
10960 add_AT_flag (type_die, DW_AT_declaration, 1);
10962 return type_die;
10965 /* Generate a DIE to represent either a real live formal parameter decl or to
10966 represent just the type of some formal parameter position in some function
10967 type.
10969 Note that this routine is a bit unusual because its argument may be a
10970 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
10971 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
10972 node. If it's the former then this function is being called to output a
10973 DIE to represent a formal parameter object (or some inlining thereof). If
10974 it's the latter, then this function is only being called to output a
10975 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
10976 argument type of some subprogram type. */
10978 static dw_die_ref
10979 gen_formal_parameter_die (tree node, dw_die_ref context_die)
10981 dw_die_ref parm_die
10982 = new_die (DW_TAG_formal_parameter, context_die, node);
10983 tree origin;
10985 switch (TREE_CODE_CLASS (TREE_CODE (node)))
10987 case 'd':
10988 origin = decl_ultimate_origin (node);
10989 if (origin != NULL)
10990 add_abstract_origin_attribute (parm_die, origin);
10991 else
10993 add_name_and_src_coords_attributes (parm_die, node);
10994 add_type_attribute (parm_die, TREE_TYPE (node),
10995 TREE_READONLY (node),
10996 TREE_THIS_VOLATILE (node),
10997 context_die);
10998 if (DECL_ARTIFICIAL (node))
10999 add_AT_flag (parm_die, DW_AT_artificial, 1);
11002 equate_decl_number_to_die (node, parm_die);
11003 if (! DECL_ABSTRACT (node))
11004 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
11006 break;
11008 case 't':
11009 /* We were called with some kind of a ..._TYPE node. */
11010 add_type_attribute (parm_die, node, 0, 0, context_die);
11011 break;
11013 default:
11014 abort ();
11017 return parm_die;
11020 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11021 at the end of an (ANSI prototyped) formal parameters list. */
11023 static void
11024 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11026 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11029 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11030 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11031 parameters as specified in some function type specification (except for
11032 those which appear as part of a function *definition*). */
11034 static void
11035 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11037 tree link;
11038 tree formal_type = NULL;
11039 tree first_parm_type;
11040 tree arg;
11042 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11044 arg = DECL_ARGUMENTS (function_or_method_type);
11045 function_or_method_type = TREE_TYPE (function_or_method_type);
11047 else
11048 arg = NULL_TREE;
11050 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11052 /* Make our first pass over the list of formal parameter types and output a
11053 DW_TAG_formal_parameter DIE for each one. */
11054 for (link = first_parm_type; link; )
11056 dw_die_ref parm_die;
11058 formal_type = TREE_VALUE (link);
11059 if (formal_type == void_type_node)
11060 break;
11062 /* Output a (nameless) DIE to represent the formal parameter itself. */
11063 parm_die = gen_formal_parameter_die (formal_type, context_die);
11064 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11065 && link == first_parm_type)
11066 || (arg && DECL_ARTIFICIAL (arg)))
11067 add_AT_flag (parm_die, DW_AT_artificial, 1);
11069 link = TREE_CHAIN (link);
11070 if (arg)
11071 arg = TREE_CHAIN (arg);
11074 /* If this function type has an ellipsis, add a
11075 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
11076 if (formal_type != void_type_node)
11077 gen_unspecified_parameters_die (function_or_method_type, context_die);
11079 /* Make our second (and final) pass over the list of formal parameter types
11080 and output DIEs to represent those types (as necessary). */
11081 for (link = TYPE_ARG_TYPES (function_or_method_type);
11082 link && TREE_VALUE (link);
11083 link = TREE_CHAIN (link))
11084 gen_type_die (TREE_VALUE (link), context_die);
11087 /* We want to generate the DIE for TYPE so that we can generate the
11088 die for MEMBER, which has been defined; we will need to refer back
11089 to the member declaration nested within TYPE. If we're trying to
11090 generate minimal debug info for TYPE, processing TYPE won't do the
11091 trick; we need to attach the member declaration by hand. */
11093 static void
11094 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11096 gen_type_die (type, context_die);
11098 /* If we're trying to avoid duplicate debug info, we may not have
11099 emitted the member decl for this function. Emit it now. */
11100 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11101 && ! lookup_decl_die (member))
11103 if (decl_ultimate_origin (member))
11104 abort ();
11106 push_decl_scope (type);
11107 if (TREE_CODE (member) == FUNCTION_DECL)
11108 gen_subprogram_die (member, lookup_type_die (type));
11109 else
11110 gen_variable_die (member, lookup_type_die (type));
11112 pop_decl_scope ();
11116 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11117 may later generate inlined and/or out-of-line instances of. */
11119 static void
11120 dwarf2out_abstract_function (tree decl)
11122 dw_die_ref old_die;
11123 tree save_fn;
11124 tree context;
11125 int was_abstract = DECL_ABSTRACT (decl);
11127 /* Make sure we have the actual abstract inline, not a clone. */
11128 decl = DECL_ORIGIN (decl);
11130 old_die = lookup_decl_die (decl);
11131 if (old_die && get_AT (old_die, DW_AT_inline))
11132 /* We've already generated the abstract instance. */
11133 return;
11135 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11136 we don't get confused by DECL_ABSTRACT. */
11137 if (debug_info_level > DINFO_LEVEL_TERSE)
11139 context = decl_class_context (decl);
11140 if (context)
11141 gen_type_die_for_member
11142 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11145 /* Pretend we've just finished compiling this function. */
11146 save_fn = current_function_decl;
11147 current_function_decl = decl;
11149 set_decl_abstract_flags (decl, 1);
11150 dwarf2out_decl (decl);
11151 if (! was_abstract)
11152 set_decl_abstract_flags (decl, 0);
11154 current_function_decl = save_fn;
11157 /* Generate a DIE to represent a declared function (either file-scope or
11158 block-local). */
11160 static void
11161 gen_subprogram_die (tree decl, dw_die_ref context_die)
11163 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11164 tree origin = decl_ultimate_origin (decl);
11165 dw_die_ref subr_die;
11166 rtx fp_reg;
11167 tree fn_arg_types;
11168 tree outer_scope;
11169 dw_die_ref old_die = lookup_decl_die (decl);
11170 int declaration = (current_function_decl != decl
11171 || class_or_namespace_scope_p (context_die));
11173 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11174 started to generate the abstract instance of an inline, decided to output
11175 its containing class, and proceeded to emit the declaration of the inline
11176 from the member list for the class. If so, DECLARATION takes priority;
11177 we'll get back to the abstract instance when done with the class. */
11179 /* The class-scope declaration DIE must be the primary DIE. */
11180 if (origin && declaration && class_or_namespace_scope_p (context_die))
11182 origin = NULL;
11183 if (old_die)
11184 abort ();
11187 if (origin != NULL)
11189 if (declaration && ! local_scope_p (context_die))
11190 abort ();
11192 /* Fixup die_parent for the abstract instance of a nested
11193 inline function. */
11194 if (old_die && old_die->die_parent == NULL)
11195 add_child_die (context_die, old_die);
11197 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11198 add_abstract_origin_attribute (subr_die, origin);
11200 else if (old_die)
11202 unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
11204 if (!get_AT_flag (old_die, DW_AT_declaration)
11205 /* We can have a normal definition following an inline one in the
11206 case of redefinition of GNU C extern inlines.
11207 It seems reasonable to use AT_specification in this case. */
11208 && !get_AT (old_die, DW_AT_inline))
11210 /* ??? This can happen if there is a bug in the program, for
11211 instance, if it has duplicate function definitions. Ideally,
11212 we should detect this case and ignore it. For now, if we have
11213 already reported an error, any error at all, then assume that
11214 we got here because of an input error, not a dwarf2 bug. */
11215 if (errorcount)
11216 return;
11217 abort ();
11220 /* If the definition comes from the same place as the declaration,
11221 maybe use the old DIE. We always want the DIE for this function
11222 that has the *_pc attributes to be under comp_unit_die so the
11223 debugger can find it. We also need to do this for abstract
11224 instances of inlines, since the spec requires the out-of-line copy
11225 to have the same parent. For local class methods, this doesn't
11226 apply; we just use the old DIE. */
11227 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
11228 && (DECL_ARTIFICIAL (decl)
11229 || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
11230 && (get_AT_unsigned (old_die, DW_AT_decl_line)
11231 == (unsigned) DECL_SOURCE_LINE (decl)))))
11233 subr_die = old_die;
11235 /* Clear out the declaration attribute and the formal parameters.
11236 Do not remove all children, because it is possible that this
11237 declaration die was forced using force_decl_die(). In such
11238 cases die that forced declaration die (e.g. TAG_imported_module)
11239 is one of the children that we do not want to remove. */
11240 remove_AT (subr_die, DW_AT_declaration);
11241 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
11243 else
11245 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11246 add_AT_specification (subr_die, old_die);
11247 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11248 add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
11249 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11250 != (unsigned) DECL_SOURCE_LINE (decl))
11251 add_AT_unsigned
11252 (subr_die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
11255 else
11257 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11259 if (TREE_PUBLIC (decl))
11260 add_AT_flag (subr_die, DW_AT_external, 1);
11262 add_name_and_src_coords_attributes (subr_die, decl);
11263 if (debug_info_level > DINFO_LEVEL_TERSE)
11265 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11266 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11267 0, 0, context_die);
11270 add_pure_or_virtual_attribute (subr_die, decl);
11271 if (DECL_ARTIFICIAL (decl))
11272 add_AT_flag (subr_die, DW_AT_artificial, 1);
11274 if (TREE_PROTECTED (decl))
11275 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11276 else if (TREE_PRIVATE (decl))
11277 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
11280 if (declaration)
11282 if (!old_die || !get_AT (old_die, DW_AT_inline))
11284 add_AT_flag (subr_die, DW_AT_declaration, 1);
11286 /* The first time we see a member function, it is in the context of
11287 the class to which it belongs. We make sure of this by emitting
11288 the class first. The next time is the definition, which is
11289 handled above. The two may come from the same source text.
11291 Note that force_decl_die() forces function declaration die. It is
11292 later reused to represent definition. */
11293 equate_decl_number_to_die (decl, subr_die);
11296 else if (DECL_ABSTRACT (decl))
11298 if (DECL_DECLARED_INLINE_P (decl))
11300 if (cgraph_function_possibly_inlined_p (decl))
11301 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11302 else
11303 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
11305 else
11307 if (cgraph_function_possibly_inlined_p (decl))
11308 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11309 else
11310 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11313 equate_decl_number_to_die (decl, subr_die);
11315 else if (!DECL_EXTERNAL (decl))
11317 if (!old_die || !get_AT (old_die, DW_AT_inline))
11318 equate_decl_number_to_die (decl, subr_die);
11320 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11321 current_function_funcdef_no);
11322 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11323 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11324 current_function_funcdef_no);
11325 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11327 add_pubname (decl, subr_die);
11328 add_arange (decl, subr_die);
11330 #ifdef MIPS_DEBUGGING_INFO
11331 /* Add a reference to the FDE for this routine. */
11332 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11333 #endif
11335 /* Define the "frame base" location for this routine. We use the
11336 frame pointer or stack pointer registers, since the RTL for local
11337 variables is relative to one of them. */
11338 if (frame_base_decl && lookup_decl_loc (frame_base_decl) != NULL)
11340 add_location_or_const_value_attribute (subr_die, frame_base_decl,
11341 DW_AT_frame_base);
11343 else
11345 fp_reg
11346 = frame_pointer_needed ? hard_frame_pointer_rtx : stack_pointer_rtx;
11347 add_AT_loc (subr_die, DW_AT_frame_base, reg_loc_descriptor (fp_reg));
11350 if (cfun->static_chain_decl)
11351 add_AT_location_description (subr_die, DW_AT_static_link,
11352 loc_descriptor_from_tree (cfun->static_chain_decl, 0));
11355 /* Now output descriptions of the arguments for this function. This gets
11356 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
11357 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
11358 `...' at the end of the formal parameter list. In order to find out if
11359 there was a trailing ellipsis or not, we must instead look at the type
11360 associated with the FUNCTION_DECL. This will be a node of type
11361 FUNCTION_TYPE. If the chain of type nodes hanging off of this
11362 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
11363 an ellipsis at the end. */
11365 /* In the case where we are describing a mere function declaration, all we
11366 need to do here (and all we *can* do here) is to describe the *types* of
11367 its formal parameters. */
11368 if (debug_info_level <= DINFO_LEVEL_TERSE)
11370 else if (declaration)
11371 gen_formal_types_die (decl, subr_die);
11372 else
11374 /* Generate DIEs to represent all known formal parameters. */
11375 tree arg_decls = DECL_ARGUMENTS (decl);
11376 tree parm;
11378 /* When generating DIEs, generate the unspecified_parameters DIE
11379 instead if we come across the arg "__builtin_va_alist" */
11380 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
11381 if (TREE_CODE (parm) == PARM_DECL)
11383 if (DECL_NAME (parm)
11384 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
11385 "__builtin_va_alist"))
11386 gen_unspecified_parameters_die (parm, subr_die);
11387 else
11388 gen_decl_die (parm, subr_die);
11391 /* Decide whether we need an unspecified_parameters DIE at the end.
11392 There are 2 more cases to do this for: 1) the ansi ... declaration -
11393 this is detectable when the end of the arg list is not a
11394 void_type_node 2) an unprototyped function declaration (not a
11395 definition). This just means that we have no info about the
11396 parameters at all. */
11397 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
11398 if (fn_arg_types != NULL)
11400 /* This is the prototyped case, check for.... */
11401 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
11402 gen_unspecified_parameters_die (decl, subr_die);
11404 else if (DECL_INITIAL (decl) == NULL_TREE)
11405 gen_unspecified_parameters_die (decl, subr_die);
11408 /* Output Dwarf info for all of the stuff within the body of the function
11409 (if it has one - it may be just a declaration). */
11410 outer_scope = DECL_INITIAL (decl);
11412 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
11413 a function. This BLOCK actually represents the outermost binding contour
11414 for the function, i.e. the contour in which the function's formal
11415 parameters and labels get declared. Curiously, it appears that the front
11416 end doesn't actually put the PARM_DECL nodes for the current function onto
11417 the BLOCK_VARS list for this outer scope, but are strung off of the
11418 DECL_ARGUMENTS list for the function instead.
11420 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
11421 the LABEL_DECL nodes for the function however, and we output DWARF info
11422 for those in decls_for_scope. Just within the `outer_scope' there will be
11423 a BLOCK node representing the function's outermost pair of curly braces,
11424 and any blocks used for the base and member initializers of a C++
11425 constructor function. */
11426 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
11428 /* Emit a DW_TAG_variable DIE for a named return value. */
11429 if (DECL_NAME (DECL_RESULT (decl)))
11430 gen_decl_die (DECL_RESULT (decl), subr_die);
11432 current_function_has_inlines = 0;
11433 decls_for_scope (outer_scope, subr_die, 0);
11435 #if 0 && defined (MIPS_DEBUGGING_INFO)
11436 if (current_function_has_inlines)
11438 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
11439 if (! comp_unit_has_inlines)
11441 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
11442 comp_unit_has_inlines = 1;
11445 #endif
11449 /* Generate a DIE to represent a declared data object. */
11451 static void
11452 gen_variable_die (tree decl, dw_die_ref context_die)
11454 tree origin = decl_ultimate_origin (decl);
11455 dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
11457 dw_die_ref old_die = lookup_decl_die (decl);
11458 int declaration = (DECL_EXTERNAL (decl)
11459 || class_or_namespace_scope_p (context_die));
11461 if (origin != NULL)
11462 add_abstract_origin_attribute (var_die, origin);
11464 /* Loop unrolling can create multiple blocks that refer to the same
11465 static variable, so we must test for the DW_AT_declaration flag.
11467 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
11468 copy decls and set the DECL_ABSTRACT flag on them instead of
11469 sharing them.
11471 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
11472 else if (old_die && TREE_STATIC (decl)
11473 && get_AT_flag (old_die, DW_AT_declaration) == 1)
11475 /* This is a definition of a C++ class level static. */
11476 add_AT_specification (var_die, old_die);
11477 if (DECL_NAME (decl))
11479 unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
11481 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11482 add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
11484 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11485 != (unsigned) DECL_SOURCE_LINE (decl))
11487 add_AT_unsigned (var_die, DW_AT_decl_line,
11488 DECL_SOURCE_LINE (decl));
11491 else
11493 add_name_and_src_coords_attributes (var_die, decl);
11494 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
11495 TREE_THIS_VOLATILE (decl), context_die);
11497 if (TREE_PUBLIC (decl))
11498 add_AT_flag (var_die, DW_AT_external, 1);
11500 if (DECL_ARTIFICIAL (decl))
11501 add_AT_flag (var_die, DW_AT_artificial, 1);
11503 if (TREE_PROTECTED (decl))
11504 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
11505 else if (TREE_PRIVATE (decl))
11506 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
11509 if (declaration)
11510 add_AT_flag (var_die, DW_AT_declaration, 1);
11512 if (DECL_ABSTRACT (decl) || declaration)
11513 equate_decl_number_to_die (decl, var_die);
11515 if (! declaration && ! DECL_ABSTRACT (decl))
11517 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
11518 add_pubname (decl, var_die);
11520 else
11521 tree_add_const_value_attribute (var_die, decl);
11524 /* Generate a DIE to represent a label identifier. */
11526 static void
11527 gen_label_die (tree decl, dw_die_ref context_die)
11529 tree origin = decl_ultimate_origin (decl);
11530 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
11531 rtx insn;
11532 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11534 if (origin != NULL)
11535 add_abstract_origin_attribute (lbl_die, origin);
11536 else
11537 add_name_and_src_coords_attributes (lbl_die, decl);
11539 if (DECL_ABSTRACT (decl))
11540 equate_decl_number_to_die (decl, lbl_die);
11541 else
11543 insn = DECL_RTL_IF_SET (decl);
11545 /* Deleted labels are programmer specified labels which have been
11546 eliminated because of various optimizations. We still emit them
11547 here so that it is possible to put breakpoints on them. */
11548 if (insn
11549 && (GET_CODE (insn) == CODE_LABEL
11550 || ((GET_CODE (insn) == NOTE
11551 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
11553 /* When optimization is enabled (via -O) some parts of the compiler
11554 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
11555 represent source-level labels which were explicitly declared by
11556 the user. This really shouldn't be happening though, so catch
11557 it if it ever does happen. */
11558 if (INSN_DELETED_P (insn))
11559 abort ();
11561 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
11562 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
11567 /* Generate a DIE for a lexical block. */
11569 static void
11570 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
11572 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
11573 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11575 if (! BLOCK_ABSTRACT (stmt))
11577 if (BLOCK_FRAGMENT_CHAIN (stmt))
11579 tree chain;
11581 add_AT_range_list (stmt_die, DW_AT_ranges, add_ranges (stmt));
11583 chain = BLOCK_FRAGMENT_CHAIN (stmt);
11586 add_ranges (chain);
11587 chain = BLOCK_FRAGMENT_CHAIN (chain);
11589 while (chain);
11590 add_ranges (NULL);
11592 else
11594 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
11595 BLOCK_NUMBER (stmt));
11596 add_AT_lbl_id (stmt_die, DW_AT_low_pc, label);
11597 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
11598 BLOCK_NUMBER (stmt));
11599 add_AT_lbl_id (stmt_die, DW_AT_high_pc, label);
11603 decls_for_scope (stmt, stmt_die, depth);
11606 /* Generate a DIE for an inlined subprogram. */
11608 static void
11609 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
11611 tree decl = block_ultimate_origin (stmt);
11613 /* Emit info for the abstract instance first, if we haven't yet. We
11614 must emit this even if the block is abstract, otherwise when we
11615 emit the block below (or elsewhere), we may end up trying to emit
11616 a die whose origin die hasn't been emitted, and crashing. */
11617 dwarf2out_abstract_function (decl);
11619 if (! BLOCK_ABSTRACT (stmt))
11621 dw_die_ref subr_die
11622 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
11623 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11625 add_abstract_origin_attribute (subr_die, decl);
11626 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
11627 BLOCK_NUMBER (stmt));
11628 add_AT_lbl_id (subr_die, DW_AT_low_pc, label);
11629 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
11630 BLOCK_NUMBER (stmt));
11631 add_AT_lbl_id (subr_die, DW_AT_high_pc, label);
11632 decls_for_scope (stmt, subr_die, depth);
11633 current_function_has_inlines = 1;
11635 else
11636 /* We may get here if we're the outer block of function A that was
11637 inlined into function B that was inlined into function C. When
11638 generating debugging info for C, dwarf2out_abstract_function(B)
11639 would mark all inlined blocks as abstract, including this one.
11640 So, we wouldn't (and shouldn't) expect labels to be generated
11641 for this one. Instead, just emit debugging info for
11642 declarations within the block. This is particularly important
11643 in the case of initializers of arguments passed from B to us:
11644 if they're statement expressions containing declarations, we
11645 wouldn't generate dies for their abstract variables, and then,
11646 when generating dies for the real variables, we'd die (pun
11647 intended :-) */
11648 gen_lexical_block_die (stmt, context_die, depth);
11651 /* Generate a DIE for a field in a record, or structure. */
11653 static void
11654 gen_field_die (tree decl, dw_die_ref context_die)
11656 dw_die_ref decl_die;
11658 if (TREE_TYPE (decl) == error_mark_node)
11659 return;
11661 decl_die = new_die (DW_TAG_member, context_die, decl);
11662 add_name_and_src_coords_attributes (decl_die, decl);
11663 add_type_attribute (decl_die, member_declared_type (decl),
11664 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
11665 context_die);
11667 if (DECL_BIT_FIELD_TYPE (decl))
11669 add_byte_size_attribute (decl_die, decl);
11670 add_bit_size_attribute (decl_die, decl);
11671 add_bit_offset_attribute (decl_die, decl);
11674 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
11675 add_data_member_location_attribute (decl_die, decl);
11677 if (DECL_ARTIFICIAL (decl))
11678 add_AT_flag (decl_die, DW_AT_artificial, 1);
11680 if (TREE_PROTECTED (decl))
11681 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
11682 else if (TREE_PRIVATE (decl))
11683 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
11686 #if 0
11687 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
11688 Use modified_type_die instead.
11689 We keep this code here just in case these types of DIEs may be needed to
11690 represent certain things in other languages (e.g. Pascal) someday. */
11692 static void
11693 gen_pointer_type_die (tree type, dw_die_ref context_die)
11695 dw_die_ref ptr_die
11696 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
11698 equate_type_number_to_die (type, ptr_die);
11699 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
11700 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
11703 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
11704 Use modified_type_die instead.
11705 We keep this code here just in case these types of DIEs may be needed to
11706 represent certain things in other languages (e.g. Pascal) someday. */
11708 static void
11709 gen_reference_type_die (tree type, dw_die_ref context_die)
11711 dw_die_ref ref_die
11712 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
11714 equate_type_number_to_die (type, ref_die);
11715 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
11716 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
11718 #endif
11720 /* Generate a DIE for a pointer to a member type. */
11722 static void
11723 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
11725 dw_die_ref ptr_die
11726 = new_die (DW_TAG_ptr_to_member_type,
11727 scope_die_for (type, context_die), type);
11729 equate_type_number_to_die (type, ptr_die);
11730 add_AT_die_ref (ptr_die, DW_AT_containing_type,
11731 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
11732 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
11735 /* Generate the DIE for the compilation unit. */
11737 static dw_die_ref
11738 gen_compile_unit_die (const char *filename)
11740 dw_die_ref die;
11741 char producer[250];
11742 const char *language_string = lang_hooks.name;
11743 int language;
11745 die = new_die (DW_TAG_compile_unit, NULL, NULL);
11747 if (filename)
11749 add_name_attribute (die, filename);
11750 /* Don't add cwd for <built-in>. */
11751 if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
11752 add_comp_dir_attribute (die);
11755 sprintf (producer, "%s %s", language_string, version_string);
11757 #ifdef MIPS_DEBUGGING_INFO
11758 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
11759 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
11760 not appear in the producer string, the debugger reaches the conclusion
11761 that the object file is stripped and has no debugging information.
11762 To get the MIPS/SGI debugger to believe that there is debugging
11763 information in the object file, we add a -g to the producer string. */
11764 if (debug_info_level > DINFO_LEVEL_TERSE)
11765 strcat (producer, " -g");
11766 #endif
11768 add_AT_string (die, DW_AT_producer, producer);
11770 if (strcmp (language_string, "GNU C++") == 0)
11771 language = DW_LANG_C_plus_plus;
11772 else if (strcmp (language_string, "GNU Ada") == 0)
11773 language = DW_LANG_Ada95;
11774 else if (strcmp (language_string, "GNU F77") == 0)
11775 language = DW_LANG_Fortran77;
11776 else if (strcmp (language_string, "GNU F95") == 0)
11777 language = DW_LANG_Fortran95;
11778 else if (strcmp (language_string, "GNU Pascal") == 0)
11779 language = DW_LANG_Pascal83;
11780 else if (strcmp (language_string, "GNU Java") == 0)
11781 language = DW_LANG_Java;
11782 else
11783 language = DW_LANG_C89;
11785 add_AT_unsigned (die, DW_AT_language, language);
11786 return die;
11789 /* Generate a DIE for a string type. */
11791 static void
11792 gen_string_type_die (tree type, dw_die_ref context_die)
11794 dw_die_ref type_die
11795 = new_die (DW_TAG_string_type, scope_die_for (type, context_die), type);
11797 equate_type_number_to_die (type, type_die);
11799 /* ??? Fudge the string length attribute for now.
11800 TODO: add string length info. */
11801 #if 0
11802 string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
11803 bound_representation (upper_bound, 0, 'u');
11804 #endif
11807 /* Generate the DIE for a base class. */
11809 static void
11810 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
11812 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
11814 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
11815 add_data_member_location_attribute (die, binfo);
11817 if (TREE_VIA_VIRTUAL (binfo))
11818 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11820 if (access == access_public_node)
11821 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
11822 else if (access == access_protected_node)
11823 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
11826 /* Generate a DIE for a class member. */
11828 static void
11829 gen_member_die (tree type, dw_die_ref context_die)
11831 tree member;
11832 tree binfo = TYPE_BINFO (type);
11833 dw_die_ref child;
11835 /* If this is not an incomplete type, output descriptions of each of its
11836 members. Note that as we output the DIEs necessary to represent the
11837 members of this record or union type, we will also be trying to output
11838 DIEs to represent the *types* of those members. However the `type'
11839 function (above) will specifically avoid generating type DIEs for member
11840 types *within* the list of member DIEs for this (containing) type except
11841 for those types (of members) which are explicitly marked as also being
11842 members of this (containing) type themselves. The g++ front- end can
11843 force any given type to be treated as a member of some other (containing)
11844 type by setting the TYPE_CONTEXT of the given (member) type to point to
11845 the TREE node representing the appropriate (containing) type. */
11847 /* First output info about the base classes. */
11848 if (binfo && BINFO_BASETYPES (binfo))
11850 tree bases = BINFO_BASETYPES (binfo);
11851 tree accesses = BINFO_BASEACCESSES (binfo);
11852 int n_bases = TREE_VEC_LENGTH (bases);
11853 int i;
11855 for (i = 0; i < n_bases; i++)
11856 gen_inheritance_die (TREE_VEC_ELT (bases, i),
11857 (accesses ? TREE_VEC_ELT (accesses, i)
11858 : access_public_node), context_die);
11861 /* Now output info about the data members and type members. */
11862 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
11864 /* If we thought we were generating minimal debug info for TYPE
11865 and then changed our minds, some of the member declarations
11866 may have already been defined. Don't define them again, but
11867 do put them in the right order. */
11869 child = lookup_decl_die (member);
11870 if (child)
11871 splice_child_die (context_die, child);
11872 else
11873 gen_decl_die (member, context_die);
11876 /* Now output info about the function members (if any). */
11877 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
11879 /* Don't include clones in the member list. */
11880 if (DECL_ABSTRACT_ORIGIN (member))
11881 continue;
11883 child = lookup_decl_die (member);
11884 if (child)
11885 splice_child_die (context_die, child);
11886 else
11887 gen_decl_die (member, context_die);
11891 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
11892 is set, we pretend that the type was never defined, so we only get the
11893 member DIEs needed by later specification DIEs. */
11895 static void
11896 gen_struct_or_union_type_die (tree type, dw_die_ref context_die)
11898 dw_die_ref type_die = lookup_type_die (type);
11899 dw_die_ref scope_die = 0;
11900 int nested = 0;
11901 int complete = (TYPE_SIZE (type)
11902 && (! TYPE_STUB_DECL (type)
11903 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
11904 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
11906 if (type_die && ! complete)
11907 return;
11909 if (TYPE_CONTEXT (type) != NULL_TREE
11910 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
11911 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
11912 nested = 1;
11914 scope_die = scope_die_for (type, context_die);
11916 if (! type_die || (nested && scope_die == comp_unit_die))
11917 /* First occurrence of type or toplevel definition of nested class. */
11919 dw_die_ref old_die = type_die;
11921 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
11922 ? DW_TAG_structure_type : DW_TAG_union_type,
11923 scope_die, type);
11924 equate_type_number_to_die (type, type_die);
11925 if (old_die)
11926 add_AT_specification (type_die, old_die);
11927 else
11928 add_name_attribute (type_die, type_tag (type));
11930 else
11931 remove_AT (type_die, DW_AT_declaration);
11933 /* If this type has been completed, then give it a byte_size attribute and
11934 then give a list of members. */
11935 if (complete && !ns_decl)
11937 /* Prevent infinite recursion in cases where the type of some member of
11938 this type is expressed in terms of this type itself. */
11939 TREE_ASM_WRITTEN (type) = 1;
11940 add_byte_size_attribute (type_die, type);
11941 if (TYPE_STUB_DECL (type) != NULL_TREE)
11942 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11944 /* If the first reference to this type was as the return type of an
11945 inline function, then it may not have a parent. Fix this now. */
11946 if (type_die->die_parent == NULL)
11947 add_child_die (scope_die, type_die);
11949 push_decl_scope (type);
11950 gen_member_die (type, type_die);
11951 pop_decl_scope ();
11953 /* GNU extension: Record what type our vtable lives in. */
11954 if (TYPE_VFIELD (type))
11956 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
11958 gen_type_die (vtype, context_die);
11959 add_AT_die_ref (type_die, DW_AT_containing_type,
11960 lookup_type_die (vtype));
11963 else
11965 add_AT_flag (type_die, DW_AT_declaration, 1);
11967 /* We don't need to do this for function-local types. */
11968 if (TYPE_STUB_DECL (type)
11969 && ! decl_function_context (TYPE_STUB_DECL (type)))
11970 VARRAY_PUSH_TREE (incomplete_types, type);
11974 /* Generate a DIE for a subroutine _type_. */
11976 static void
11977 gen_subroutine_type_die (tree type, dw_die_ref context_die)
11979 tree return_type = TREE_TYPE (type);
11980 dw_die_ref subr_die
11981 = new_die (DW_TAG_subroutine_type,
11982 scope_die_for (type, context_die), type);
11984 equate_type_number_to_die (type, subr_die);
11985 add_prototyped_attribute (subr_die, type);
11986 add_type_attribute (subr_die, return_type, 0, 0, context_die);
11987 gen_formal_types_die (type, subr_die);
11990 /* Generate a DIE for a type definition. */
11992 static void
11993 gen_typedef_die (tree decl, dw_die_ref context_die)
11995 dw_die_ref type_die;
11996 tree origin;
11998 if (TREE_ASM_WRITTEN (decl))
11999 return;
12001 TREE_ASM_WRITTEN (decl) = 1;
12002 type_die = new_die (DW_TAG_typedef, context_die, decl);
12003 origin = decl_ultimate_origin (decl);
12004 if (origin != NULL)
12005 add_abstract_origin_attribute (type_die, origin);
12006 else
12008 tree type;
12010 add_name_and_src_coords_attributes (type_die, decl);
12011 if (DECL_ORIGINAL_TYPE (decl))
12013 type = DECL_ORIGINAL_TYPE (decl);
12015 if (type == TREE_TYPE (decl))
12016 abort ();
12017 else
12018 equate_type_number_to_die (TREE_TYPE (decl), type_die);
12020 else
12021 type = TREE_TYPE (decl);
12023 add_type_attribute (type_die, type, TREE_READONLY (decl),
12024 TREE_THIS_VOLATILE (decl), context_die);
12027 if (DECL_ABSTRACT (decl))
12028 equate_decl_number_to_die (decl, type_die);
12031 /* Generate a type description DIE. */
12033 static void
12034 gen_type_die (tree type, dw_die_ref context_die)
12036 int need_pop;
12038 if (type == NULL_TREE || type == error_mark_node)
12039 return;
12041 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12042 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
12044 if (TREE_ASM_WRITTEN (type))
12045 return;
12047 /* Prevent broken recursion; we can't hand off to the same type. */
12048 if (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) == type)
12049 abort ();
12051 TREE_ASM_WRITTEN (type) = 1;
12052 gen_decl_die (TYPE_NAME (type), context_die);
12053 return;
12056 /* We are going to output a DIE to represent the unqualified version
12057 of this type (i.e. without any const or volatile qualifiers) so
12058 get the main variant (i.e. the unqualified version) of this type
12059 now. (Vectors are special because the debugging info is in the
12060 cloned type itself). */
12061 if (TREE_CODE (type) != VECTOR_TYPE)
12062 type = type_main_variant (type);
12064 if (TREE_ASM_WRITTEN (type))
12065 return;
12067 switch (TREE_CODE (type))
12069 case ERROR_MARK:
12070 break;
12072 case POINTER_TYPE:
12073 case REFERENCE_TYPE:
12074 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
12075 ensures that the gen_type_die recursion will terminate even if the
12076 type is recursive. Recursive types are possible in Ada. */
12077 /* ??? We could perhaps do this for all types before the switch
12078 statement. */
12079 TREE_ASM_WRITTEN (type) = 1;
12081 /* For these types, all that is required is that we output a DIE (or a
12082 set of DIEs) to represent the "basis" type. */
12083 gen_type_die (TREE_TYPE (type), context_die);
12084 break;
12086 case OFFSET_TYPE:
12087 /* This code is used for C++ pointer-to-data-member types.
12088 Output a description of the relevant class type. */
12089 gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
12091 /* Output a description of the type of the object pointed to. */
12092 gen_type_die (TREE_TYPE (type), context_die);
12094 /* Now output a DIE to represent this pointer-to-data-member type
12095 itself. */
12096 gen_ptr_to_mbr_type_die (type, context_die);
12097 break;
12099 case SET_TYPE:
12100 gen_type_die (TYPE_DOMAIN (type), context_die);
12101 gen_set_type_die (type, context_die);
12102 break;
12104 case FILE_TYPE:
12105 gen_type_die (TREE_TYPE (type), context_die);
12106 abort (); /* No way to represent these in Dwarf yet! */
12107 break;
12109 case FUNCTION_TYPE:
12110 /* Force out return type (in case it wasn't forced out already). */
12111 gen_type_die (TREE_TYPE (type), context_die);
12112 gen_subroutine_type_die (type, context_die);
12113 break;
12115 case METHOD_TYPE:
12116 /* Force out return type (in case it wasn't forced out already). */
12117 gen_type_die (TREE_TYPE (type), context_die);
12118 gen_subroutine_type_die (type, context_die);
12119 break;
12121 case ARRAY_TYPE:
12122 if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
12124 gen_type_die (TREE_TYPE (type), context_die);
12125 gen_string_type_die (type, context_die);
12127 else
12128 gen_array_type_die (type, context_die);
12129 break;
12131 case VECTOR_TYPE:
12132 gen_array_type_die (type, context_die);
12133 break;
12135 case ENUMERAL_TYPE:
12136 case RECORD_TYPE:
12137 case UNION_TYPE:
12138 case QUAL_UNION_TYPE:
12139 /* If this is a nested type whose containing class hasn't been written
12140 out yet, writing it out will cover this one, too. This does not apply
12141 to instantiations of member class templates; they need to be added to
12142 the containing class as they are generated. FIXME: This hurts the
12143 idea of combining type decls from multiple TUs, since we can't predict
12144 what set of template instantiations we'll get. */
12145 if (TYPE_CONTEXT (type)
12146 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12147 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
12149 gen_type_die (TYPE_CONTEXT (type), context_die);
12151 if (TREE_ASM_WRITTEN (type))
12152 return;
12154 /* If that failed, attach ourselves to the stub. */
12155 push_decl_scope (TYPE_CONTEXT (type));
12156 context_die = lookup_type_die (TYPE_CONTEXT (type));
12157 need_pop = 1;
12159 else
12161 declare_in_namespace (type, context_die);
12162 need_pop = 0;
12165 if (TREE_CODE (type) == ENUMERAL_TYPE)
12166 gen_enumeration_type_die (type, context_die);
12167 else
12168 gen_struct_or_union_type_die (type, context_die);
12170 if (need_pop)
12171 pop_decl_scope ();
12173 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
12174 it up if it is ever completed. gen_*_type_die will set it for us
12175 when appropriate. */
12176 return;
12178 case VOID_TYPE:
12179 case INTEGER_TYPE:
12180 case REAL_TYPE:
12181 case COMPLEX_TYPE:
12182 case BOOLEAN_TYPE:
12183 case CHAR_TYPE:
12184 /* No DIEs needed for fundamental types. */
12185 break;
12187 case LANG_TYPE:
12188 /* No Dwarf representation currently defined. */
12189 break;
12191 default:
12192 abort ();
12195 TREE_ASM_WRITTEN (type) = 1;
12198 /* Generate a DIE for a tagged type instantiation. */
12200 static void
12201 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
12203 if (type == NULL_TREE || type == error_mark_node)
12204 return;
12206 /* We are going to output a DIE to represent the unqualified version of
12207 this type (i.e. without any const or volatile qualifiers) so make sure
12208 that we have the main variant (i.e. the unqualified version) of this
12209 type now. */
12210 if (type != type_main_variant (type))
12211 abort ();
12213 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
12214 an instance of an unresolved type. */
12216 switch (TREE_CODE (type))
12218 case ERROR_MARK:
12219 break;
12221 case ENUMERAL_TYPE:
12222 gen_inlined_enumeration_type_die (type, context_die);
12223 break;
12225 case RECORD_TYPE:
12226 gen_inlined_structure_type_die (type, context_die);
12227 break;
12229 case UNION_TYPE:
12230 case QUAL_UNION_TYPE:
12231 gen_inlined_union_type_die (type, context_die);
12232 break;
12234 default:
12235 abort ();
12239 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12240 things which are local to the given block. */
12242 static void
12243 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
12245 int must_output_die = 0;
12246 tree origin;
12247 tree decl;
12248 enum tree_code origin_code;
12250 /* Ignore blocks never really used to make RTL. */
12251 if (stmt == NULL_TREE || !TREE_USED (stmt)
12252 || (!TREE_ASM_WRITTEN (stmt) && !BLOCK_ABSTRACT (stmt)))
12253 return;
12255 /* If the block is one fragment of a non-contiguous block, do not
12256 process the variables, since they will have been done by the
12257 origin block. Do process subblocks. */
12258 if (BLOCK_FRAGMENT_ORIGIN (stmt))
12260 tree sub;
12262 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
12263 gen_block_die (sub, context_die, depth + 1);
12265 return;
12268 /* Determine the "ultimate origin" of this block. This block may be an
12269 inlined instance of an inlined instance of inline function, so we have
12270 to trace all of the way back through the origin chain to find out what
12271 sort of node actually served as the original seed for the creation of
12272 the current block. */
12273 origin = block_ultimate_origin (stmt);
12274 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
12276 /* Determine if we need to output any Dwarf DIEs at all to represent this
12277 block. */
12278 if (origin_code == FUNCTION_DECL)
12279 /* The outer scopes for inlinings *must* always be represented. We
12280 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
12281 must_output_die = 1;
12282 else
12284 /* In the case where the current block represents an inlining of the
12285 "body block" of an inline function, we must *NOT* output any DIE for
12286 this block because we have already output a DIE to represent the whole
12287 inlined function scope and the "body block" of any function doesn't
12288 really represent a different scope according to ANSI C rules. So we
12289 check here to make sure that this block does not represent a "body
12290 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
12291 if (! is_body_block (origin ? origin : stmt))
12293 /* Determine if this block directly contains any "significant"
12294 local declarations which we will need to output DIEs for. */
12295 if (debug_info_level > DINFO_LEVEL_TERSE)
12296 /* We are not in terse mode so *any* local declaration counts
12297 as being a "significant" one. */
12298 must_output_die = (BLOCK_VARS (stmt) != NULL);
12299 else
12300 /* We are in terse mode, so only local (nested) function
12301 definitions count as "significant" local declarations. */
12302 for (decl = BLOCK_VARS (stmt);
12303 decl != NULL; decl = TREE_CHAIN (decl))
12304 if (TREE_CODE (decl) == FUNCTION_DECL
12305 && DECL_INITIAL (decl))
12307 must_output_die = 1;
12308 break;
12313 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
12314 DIE for any block which contains no significant local declarations at
12315 all. Rather, in such cases we just call `decls_for_scope' so that any
12316 needed Dwarf info for any sub-blocks will get properly generated. Note
12317 that in terse mode, our definition of what constitutes a "significant"
12318 local declaration gets restricted to include only inlined function
12319 instances and local (nested) function definitions. */
12320 if (must_output_die)
12322 if (origin_code == FUNCTION_DECL)
12323 gen_inlined_subroutine_die (stmt, context_die, depth);
12324 else
12325 gen_lexical_block_die (stmt, context_die, depth);
12327 else
12328 decls_for_scope (stmt, context_die, depth);
12331 /* Generate all of the decls declared within a given scope and (recursively)
12332 all of its sub-blocks. */
12334 static void
12335 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
12337 tree decl;
12338 tree subblocks;
12340 /* Ignore blocks never really used to make RTL. */
12341 if (stmt == NULL_TREE || ! TREE_USED (stmt))
12342 return;
12344 /* Output the DIEs to represent all of the data objects and typedefs
12345 declared directly within this block but not within any nested
12346 sub-blocks. Also, nested function and tag DIEs have been
12347 generated with a parent of NULL; fix that up now. */
12348 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
12350 dw_die_ref die;
12352 if (TREE_CODE (decl) == FUNCTION_DECL)
12353 die = lookup_decl_die (decl);
12354 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
12355 die = lookup_type_die (TREE_TYPE (decl));
12356 else
12357 die = NULL;
12359 if (die != NULL && die->die_parent == NULL)
12360 add_child_die (context_die, die);
12361 else
12362 gen_decl_die (decl, context_die);
12365 /* If we're at -g1, we're not interested in subblocks. */
12366 if (debug_info_level <= DINFO_LEVEL_TERSE)
12367 return;
12369 /* Output the DIEs to represent all sub-blocks (and the items declared
12370 therein) of this block. */
12371 for (subblocks = BLOCK_SUBBLOCKS (stmt);
12372 subblocks != NULL;
12373 subblocks = BLOCK_CHAIN (subblocks))
12374 gen_block_die (subblocks, context_die, depth + 1);
12377 /* Is this a typedef we can avoid emitting? */
12379 static inline int
12380 is_redundant_typedef (tree decl)
12382 if (TYPE_DECL_IS_STUB (decl))
12383 return 1;
12385 if (DECL_ARTIFICIAL (decl)
12386 && DECL_CONTEXT (decl)
12387 && is_tagged_type (DECL_CONTEXT (decl))
12388 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
12389 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
12390 /* Also ignore the artificial member typedef for the class name. */
12391 return 1;
12393 return 0;
12396 /* Returns the DIE for decl or aborts. */
12398 static dw_die_ref
12399 force_decl_die (tree decl)
12401 dw_die_ref decl_die;
12402 unsigned saved_external_flag;
12403 tree save_fn = NULL_TREE;
12404 decl_die = lookup_decl_die (decl);
12405 if (!decl_die)
12407 dw_die_ref context_die;
12408 tree decl_context = DECL_CONTEXT (decl);
12409 if (decl_context)
12411 /* Find die that represents this context. */
12412 if (TYPE_P (decl_context))
12413 context_die = force_type_die (decl_context);
12414 else
12415 context_die = force_decl_die (decl_context);
12417 else
12418 context_die = comp_unit_die;
12420 switch (TREE_CODE (decl))
12422 case FUNCTION_DECL:
12423 /* Clear current_function_decl, so that gen_subprogram_die thinks
12424 that this is a declaration. At this point, we just want to force
12425 declaration die. */
12426 save_fn = current_function_decl;
12427 current_function_decl = NULL_TREE;
12428 gen_subprogram_die (decl, context_die);
12429 current_function_decl = save_fn;
12430 break;
12432 case VAR_DECL:
12433 /* Set external flag to force declaration die. Restore it after
12434 gen_decl_die() call. */
12435 saved_external_flag = DECL_EXTERNAL (decl);
12436 DECL_EXTERNAL (decl) = 1;
12437 gen_decl_die (decl, context_die);
12438 DECL_EXTERNAL (decl) = saved_external_flag;
12439 break;
12441 case NAMESPACE_DECL:
12442 dwarf2out_decl (decl);
12443 break;
12445 default:
12446 abort ();
12449 /* See if we can find the die for this deci now.
12450 If not then abort. */
12451 if (!decl_die)
12452 decl_die = lookup_decl_die (decl);
12453 if (!decl_die)
12454 abort ();
12457 return decl_die;
12460 /* Returns the DIE for decl or aborts. */
12462 static dw_die_ref
12463 force_type_die (tree type)
12465 dw_die_ref type_die;
12467 type_die = lookup_type_die (type);
12468 if (!type_die)
12470 dw_die_ref context_die;
12471 if (TYPE_CONTEXT (type))
12472 if (TYPE_P (TYPE_CONTEXT (type)))
12473 context_die = force_type_die (TYPE_CONTEXT (type));
12474 else
12475 context_die = force_decl_die (TYPE_CONTEXT (type));
12476 else
12477 context_die = comp_unit_die;
12479 gen_type_die (type, context_die);
12480 type_die = lookup_type_die (type);
12481 if (!type_die)
12482 abort();
12484 return type_die;
12487 /* Force out any required namespaces to be able to output DECL,
12488 and return the new context_die for it, if it's changed. */
12490 static dw_die_ref
12491 setup_namespace_context (tree thing, dw_die_ref context_die)
12493 tree context = DECL_P (thing) ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing);
12494 if (context && TREE_CODE (context) == NAMESPACE_DECL)
12495 /* Force out the namespace. */
12496 context_die = force_decl_die (context);
12498 return context_die;
12501 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
12502 type) within its namespace, if appropriate.
12504 For compatibility with older debuggers, namespace DIEs only contain
12505 declarations; all definitions are emitted at CU scope. */
12507 static void
12508 declare_in_namespace (tree thing, dw_die_ref context_die)
12510 dw_die_ref ns_context;
12512 if (debug_info_level <= DINFO_LEVEL_TERSE)
12513 return;
12515 ns_context = setup_namespace_context (thing, context_die);
12517 if (ns_context != context_die)
12519 if (DECL_P (thing))
12520 gen_decl_die (thing, ns_context);
12521 else
12522 gen_type_die (thing, ns_context);
12526 /* Generate a DIE for a namespace or namespace alias. */
12528 static void
12529 gen_namespace_die (tree decl)
12531 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
12533 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
12534 they are an alias of. */
12535 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
12537 /* Output a real namespace. */
12538 dw_die_ref namespace_die
12539 = new_die (DW_TAG_namespace, context_die, decl);
12540 add_name_and_src_coords_attributes (namespace_die, decl);
12541 equate_decl_number_to_die (decl, namespace_die);
12543 else
12545 /* Output a namespace alias. */
12547 /* Force out the namespace we are an alias of, if necessary. */
12548 dw_die_ref origin_die
12549 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
12551 /* Now create the namespace alias DIE. */
12552 dw_die_ref namespace_die
12553 = new_die (DW_TAG_imported_declaration, context_die, decl);
12554 add_name_and_src_coords_attributes (namespace_die, decl);
12555 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
12556 equate_decl_number_to_die (decl, namespace_die);
12560 /* Generate Dwarf debug information for a decl described by DECL. */
12562 static void
12563 gen_decl_die (tree decl, dw_die_ref context_die)
12565 tree origin;
12567 if (DECL_P (decl) && DECL_IGNORED_P (decl))
12568 return;
12570 switch (TREE_CODE (decl))
12572 case ERROR_MARK:
12573 break;
12575 case CONST_DECL:
12576 /* The individual enumerators of an enum type get output when we output
12577 the Dwarf representation of the relevant enum type itself. */
12578 break;
12580 case FUNCTION_DECL:
12581 /* Don't output any DIEs to represent mere function declarations,
12582 unless they are class members or explicit block externs. */
12583 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
12584 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
12585 break;
12587 #if 0
12588 /* FIXME */
12589 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
12590 on local redeclarations of global functions. That seems broken. */
12591 if (current_function_decl != decl)
12592 /* This is only a declaration. */;
12593 #endif
12595 /* If we're emitting a clone, emit info for the abstract instance. */
12596 if (DECL_ORIGIN (decl) != decl)
12597 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
12599 /* If we're emitting an out-of-line copy of an inline function,
12600 emit info for the abstract instance and set up to refer to it. */
12601 else if (cgraph_function_possibly_inlined_p (decl)
12602 && ! DECL_ABSTRACT (decl)
12603 && ! class_or_namespace_scope_p (context_die)
12604 /* dwarf2out_abstract_function won't emit a die if this is just
12605 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
12606 that case, because that works only if we have a die. */
12607 && DECL_INITIAL (decl) != NULL_TREE)
12609 dwarf2out_abstract_function (decl);
12610 set_decl_origin_self (decl);
12613 /* Otherwise we're emitting the primary DIE for this decl. */
12614 else if (debug_info_level > DINFO_LEVEL_TERSE)
12616 /* Before we describe the FUNCTION_DECL itself, make sure that we
12617 have described its return type. */
12618 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
12620 /* And its virtual context. */
12621 if (DECL_VINDEX (decl) != NULL_TREE)
12622 gen_type_die (DECL_CONTEXT (decl), context_die);
12624 /* And its containing type. */
12625 origin = decl_class_context (decl);
12626 if (origin != NULL_TREE)
12627 gen_type_die_for_member (origin, decl, context_die);
12629 /* And its containing namespace. */
12630 declare_in_namespace (decl, context_die);
12633 /* Now output a DIE to represent the function itself. */
12634 gen_subprogram_die (decl, context_die);
12635 break;
12637 case TYPE_DECL:
12638 /* If we are in terse mode, don't generate any DIEs to represent any
12639 actual typedefs. */
12640 if (debug_info_level <= DINFO_LEVEL_TERSE)
12641 break;
12643 /* In the special case of a TYPE_DECL node representing the declaration
12644 of some type tag, if the given TYPE_DECL is marked as having been
12645 instantiated from some other (original) TYPE_DECL node (e.g. one which
12646 was generated within the original definition of an inline function) we
12647 have to generate a special (abbreviated) DW_TAG_structure_type,
12648 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
12649 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
12651 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
12652 break;
12655 if (is_redundant_typedef (decl))
12656 gen_type_die (TREE_TYPE (decl), context_die);
12657 else
12658 /* Output a DIE to represent the typedef itself. */
12659 gen_typedef_die (decl, context_die);
12660 break;
12662 case LABEL_DECL:
12663 if (debug_info_level >= DINFO_LEVEL_NORMAL)
12664 gen_label_die (decl, context_die);
12665 break;
12667 case VAR_DECL:
12668 case RESULT_DECL:
12669 /* If we are in terse mode, don't generate any DIEs to represent any
12670 variable declarations or definitions. */
12671 if (debug_info_level <= DINFO_LEVEL_TERSE)
12672 break;
12674 /* Output any DIEs that are needed to specify the type of this data
12675 object. */
12676 gen_type_die (TREE_TYPE (decl), context_die);
12678 /* And its containing type. */
12679 origin = decl_class_context (decl);
12680 if (origin != NULL_TREE)
12681 gen_type_die_for_member (origin, decl, context_die);
12683 /* And its containing namespace. */
12684 declare_in_namespace (decl, context_die);
12686 /* Now output the DIE to represent the data object itself. This gets
12687 complicated because of the possibility that the VAR_DECL really
12688 represents an inlined instance of a formal parameter for an inline
12689 function. */
12690 origin = decl_ultimate_origin (decl);
12691 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
12692 gen_formal_parameter_die (decl, context_die);
12693 else
12694 gen_variable_die (decl, context_die);
12695 break;
12697 case FIELD_DECL:
12698 /* Ignore the nameless fields that are used to skip bits but handle C++
12699 anonymous unions and structs. */
12700 if (DECL_NAME (decl) != NULL_TREE
12701 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
12702 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
12704 gen_type_die (member_declared_type (decl), context_die);
12705 gen_field_die (decl, context_die);
12707 break;
12709 case PARM_DECL:
12710 gen_type_die (TREE_TYPE (decl), context_die);
12711 gen_formal_parameter_die (decl, context_die);
12712 break;
12714 case NAMESPACE_DECL:
12715 gen_namespace_die (decl);
12716 break;
12718 default:
12719 if ((int)TREE_CODE (decl) > NUM_TREE_CODES)
12720 /* Probably some frontend-internal decl. Assume we don't care. */
12721 break;
12722 abort ();
12726 /* Add Ada "use" clause information for SGI Workshop debugger. */
12728 void
12729 dwarf2out_add_library_unit_info (const char *filename, const char *context_list)
12731 unsigned int file_index;
12733 if (filename != NULL)
12735 dw_die_ref unit_die = new_die (DW_TAG_module, comp_unit_die, NULL);
12736 tree context_list_decl
12737 = build_decl (LABEL_DECL, get_identifier (context_list),
12738 void_type_node);
12740 TREE_PUBLIC (context_list_decl) = TRUE;
12741 add_name_attribute (unit_die, context_list);
12742 file_index = lookup_filename (filename);
12743 add_AT_unsigned (unit_die, DW_AT_decl_file, file_index);
12744 add_pubname (context_list_decl, unit_die);
12748 /* Output debug information for global decl DECL. Called from toplev.c after
12749 compilation proper has finished. */
12751 static void
12752 dwarf2out_global_decl (tree decl)
12754 /* Output DWARF2 information for file-scope tentative data object
12755 declarations, file-scope (extern) function declarations (which had no
12756 corresponding body) and file-scope tagged type declarations and
12757 definitions which have not yet been forced out. */
12758 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
12759 dwarf2out_decl (decl);
12762 /* Output debug information for type decl DECL. Called from toplev.c
12763 and from language front ends (to record built-in types). */
12764 static void
12765 dwarf2out_type_decl (tree decl, int local)
12767 if (!local)
12768 dwarf2out_decl (decl);
12771 /* Output debug information for imported module or decl. */
12773 static void
12774 dwarf2out_imported_module_or_decl (tree decl, tree context)
12776 dw_die_ref imported_die, at_import_die;
12777 dw_die_ref scope_die;
12778 unsigned file_index;
12780 if (debug_info_level <= DINFO_LEVEL_TERSE)
12781 return;
12783 if (!decl)
12784 abort ();
12786 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
12787 We need decl DIE for reference and scope die. First, get DIE for the decl
12788 itself. */
12790 /* Get the scope die for decl context. Use comp_unit_die for global module
12791 or decl. If die is not found for non globals, force new die. */
12792 if (!context)
12793 scope_die = comp_unit_die;
12794 else if (TYPE_P (context))
12795 scope_die = force_type_die (context);
12796 else
12797 scope_die = force_decl_die (context);
12799 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
12800 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
12801 at_import_die = force_type_die (TREE_TYPE (decl));
12802 else
12803 at_import_die = force_decl_die (decl);
12805 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
12806 if (TREE_CODE (decl) == NAMESPACE_DECL)
12807 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
12808 else
12809 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
12811 file_index = lookup_filename (input_filename);
12812 add_AT_unsigned (imported_die, DW_AT_decl_file, file_index);
12813 add_AT_unsigned (imported_die, DW_AT_decl_line, input_line);
12814 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
12817 /* Write the debugging output for DECL. */
12819 void
12820 dwarf2out_decl (tree decl)
12822 dw_die_ref context_die = comp_unit_die;
12824 switch (TREE_CODE (decl))
12826 case ERROR_MARK:
12827 return;
12829 case FUNCTION_DECL:
12830 /* What we would really like to do here is to filter out all mere
12831 file-scope declarations of file-scope functions which are never
12832 referenced later within this translation unit (and keep all of ones
12833 that *are* referenced later on) but we aren't clairvoyant, so we have
12834 no idea which functions will be referenced in the future (i.e. later
12835 on within the current translation unit). So here we just ignore all
12836 file-scope function declarations which are not also definitions. If
12837 and when the debugger needs to know something about these functions,
12838 it will have to hunt around and find the DWARF information associated
12839 with the definition of the function.
12841 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
12842 nodes represent definitions and which ones represent mere
12843 declarations. We have to check DECL_INITIAL instead. That's because
12844 the C front-end supports some weird semantics for "extern inline"
12845 function definitions. These can get inlined within the current
12846 translation unit (an thus, we need to generate Dwarf info for their
12847 abstract instances so that the Dwarf info for the concrete inlined
12848 instances can have something to refer to) but the compiler never
12849 generates any out-of-lines instances of such things (despite the fact
12850 that they *are* definitions).
12852 The important point is that the C front-end marks these "extern
12853 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
12854 them anyway. Note that the C++ front-end also plays some similar games
12855 for inline function definitions appearing within include files which
12856 also contain `#pragma interface' pragmas. */
12857 if (DECL_INITIAL (decl) == NULL_TREE)
12858 return;
12860 /* If we're a nested function, initially use a parent of NULL; if we're
12861 a plain function, this will be fixed up in decls_for_scope. If
12862 we're a method, it will be ignored, since we already have a DIE. */
12863 if (decl_function_context (decl)
12864 /* But if we're in terse mode, we don't care about scope. */
12865 && debug_info_level > DINFO_LEVEL_TERSE)
12866 context_die = NULL;
12867 break;
12869 case VAR_DECL:
12870 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
12871 declaration and if the declaration was never even referenced from
12872 within this entire compilation unit. We suppress these DIEs in
12873 order to save space in the .debug section (by eliminating entries
12874 which are probably useless). Note that we must not suppress
12875 block-local extern declarations (whether used or not) because that
12876 would screw-up the debugger's name lookup mechanism and cause it to
12877 miss things which really ought to be in scope at a given point. */
12878 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
12879 return;
12881 /* If we are in terse mode, don't generate any DIEs to represent any
12882 variable declarations or definitions. */
12883 if (debug_info_level <= DINFO_LEVEL_TERSE)
12884 return;
12885 break;
12887 case NAMESPACE_DECL:
12888 if (debug_info_level <= DINFO_LEVEL_TERSE)
12889 return;
12890 if (lookup_decl_die (decl) != NULL)
12891 return;
12892 break;
12894 case TYPE_DECL:
12895 /* Don't emit stubs for types unless they are needed by other DIEs. */
12896 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
12897 return;
12899 /* Don't bother trying to generate any DIEs to represent any of the
12900 normal built-in types for the language we are compiling. */
12901 if (DECL_SOURCE_LINE (decl) == 0)
12903 /* OK, we need to generate one for `bool' so GDB knows what type
12904 comparisons have. */
12905 if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
12906 == DW_LANG_C_plus_plus)
12907 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
12908 && ! DECL_IGNORED_P (decl))
12909 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
12911 return;
12914 /* If we are in terse mode, don't generate any DIEs for types. */
12915 if (debug_info_level <= DINFO_LEVEL_TERSE)
12916 return;
12918 /* If we're a function-scope tag, initially use a parent of NULL;
12919 this will be fixed up in decls_for_scope. */
12920 if (decl_function_context (decl))
12921 context_die = NULL;
12923 break;
12925 default:
12926 return;
12929 gen_decl_die (decl, context_die);
12932 /* Output a marker (i.e. a label) for the beginning of the generated code for
12933 a lexical block. */
12935 static void
12936 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
12937 unsigned int blocknum)
12939 function_section (current_function_decl);
12940 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
12943 /* Output a marker (i.e. a label) for the end of the generated code for a
12944 lexical block. */
12946 static void
12947 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
12949 function_section (current_function_decl);
12950 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
12953 /* Returns nonzero if it is appropriate not to emit any debugging
12954 information for BLOCK, because it doesn't contain any instructions.
12956 Don't allow this for blocks with nested functions or local classes
12957 as we would end up with orphans, and in the presence of scheduling
12958 we may end up calling them anyway. */
12960 static bool
12961 dwarf2out_ignore_block (tree block)
12963 tree decl;
12965 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
12966 if (TREE_CODE (decl) == FUNCTION_DECL
12967 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
12968 return 0;
12970 return 1;
12973 /* Lookup FILE_NAME (in the list of filenames that we know about here in
12974 dwarf2out.c) and return its "index". The index of each (known) filename is
12975 just a unique number which is associated with only that one filename. We
12976 need such numbers for the sake of generating labels (in the .debug_sfnames
12977 section) and references to those files numbers (in the .debug_srcinfo
12978 and.debug_macinfo sections). If the filename given as an argument is not
12979 found in our current list, add it to the list and assign it the next
12980 available unique index number. In order to speed up searches, we remember
12981 the index of the filename was looked up last. This handles the majority of
12982 all searches. */
12984 static unsigned
12985 lookup_filename (const char *file_name)
12987 size_t i, n;
12988 char *save_file_name;
12990 /* Check to see if the file name that was searched on the previous
12991 call matches this file name. If so, return the index. */
12992 if (file_table_last_lookup_index != 0)
12994 const char *last
12995 = VARRAY_CHAR_PTR (file_table, file_table_last_lookup_index);
12996 if (strcmp (file_name, last) == 0)
12997 return file_table_last_lookup_index;
13000 /* Didn't match the previous lookup, search the table */
13001 n = VARRAY_ACTIVE_SIZE (file_table);
13002 for (i = 1; i < n; i++)
13003 if (strcmp (file_name, VARRAY_CHAR_PTR (file_table, i)) == 0)
13005 file_table_last_lookup_index = i;
13006 return i;
13009 /* Add the new entry to the end of the filename table. */
13010 file_table_last_lookup_index = n;
13011 save_file_name = (char *) ggc_strdup (file_name);
13012 VARRAY_PUSH_CHAR_PTR (file_table, save_file_name);
13013 VARRAY_PUSH_UINT (file_table_emitted, 0);
13015 return i;
13018 static int
13019 maybe_emit_file (int fileno)
13021 if (DWARF2_ASM_LINE_DEBUG_INFO && fileno > 0)
13023 if (!VARRAY_UINT (file_table_emitted, fileno))
13025 VARRAY_UINT (file_table_emitted, fileno) = ++emitcount;
13026 fprintf (asm_out_file, "\t.file %u ",
13027 VARRAY_UINT (file_table_emitted, fileno));
13028 output_quoted_string (asm_out_file,
13029 VARRAY_CHAR_PTR (file_table, fileno));
13030 fputc ('\n', asm_out_file);
13032 return VARRAY_UINT (file_table_emitted, fileno);
13034 else
13035 return fileno;
13038 static void
13039 init_file_table (void)
13041 /* Allocate the initial hunk of the file_table. */
13042 VARRAY_CHAR_PTR_INIT (file_table, 64, "file_table");
13043 VARRAY_UINT_INIT (file_table_emitted, 64, "file_table_emitted");
13045 /* Skip the first entry - file numbers begin at 1. */
13046 VARRAY_PUSH_CHAR_PTR (file_table, NULL);
13047 VARRAY_PUSH_UINT (file_table_emitted, 0);
13048 file_table_last_lookup_index = 0;
13051 /* Called by the final INSN scan whenever we see a var location. We
13052 use it to drop labels in the right places, and throw the location in
13053 our lookup table. */
13055 static void
13056 dwarf2out_var_location (rtx loc_note)
13058 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13059 struct var_loc_node *newloc;
13060 rtx prev_insn;
13061 static rtx last_insn;
13062 static const char *last_label;
13064 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13065 return;
13066 prev_insn = PREV_INSN (loc_note);
13068 newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13069 /* If the insn we processed last time is the previous insn
13070 and it is also a var location note, use the label we emitted
13071 last time. */
13072 if (last_insn != NULL_RTX
13073 && last_insn == prev_insn
13074 && GET_CODE (prev_insn) == NOTE
13075 && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13077 newloc->label = last_label;
13079 else
13081 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13082 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13083 loclabel_num++;
13084 newloc->label = ggc_strdup (loclabel);
13086 newloc->var_loc_note = loc_note;
13087 newloc->next = NULL;
13089 last_insn = loc_note;
13090 last_label = newloc->label;
13092 add_var_loc_to_decl (NOTE_VAR_LOCATION_DECL (loc_note), newloc);
13095 /* We need to reset the locations at the beginning of each
13096 function. We can't do this in the end_function hook, because the
13097 declarations that use the locations won't have been outputted when
13098 that hook is called. */
13100 static void
13101 dwarf2out_begin_function (tree unused ATTRIBUTE_UNUSED)
13103 htab_empty (decl_loc_table);
13106 /* Output a label to mark the beginning of a source code line entry
13107 and record information relating to this source line, in
13108 'line_info_table' for later output of the .debug_line section. */
13110 static void
13111 dwarf2out_source_line (unsigned int line, const char *filename)
13113 if (debug_info_level >= DINFO_LEVEL_NORMAL
13114 && line != 0)
13116 function_section (current_function_decl);
13118 /* If requested, emit something human-readable. */
13119 if (flag_debug_asm)
13120 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13121 filename, line);
13123 if (DWARF2_ASM_LINE_DEBUG_INFO)
13125 unsigned file_num = lookup_filename (filename);
13127 file_num = maybe_emit_file (file_num);
13129 /* Emit the .loc directive understood by GNU as. */
13130 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
13132 /* Indicate that line number info exists. */
13133 line_info_table_in_use++;
13135 /* Indicate that multiple line number tables exist. */
13136 if (DECL_SECTION_NAME (current_function_decl))
13137 separate_line_info_table_in_use++;
13139 else if (DECL_SECTION_NAME (current_function_decl))
13141 dw_separate_line_info_ref line_info;
13142 targetm.asm_out.internal_label (asm_out_file, SEPARATE_LINE_CODE_LABEL,
13143 separate_line_info_table_in_use);
13145 /* Expand the line info table if necessary. */
13146 if (separate_line_info_table_in_use
13147 == separate_line_info_table_allocated)
13149 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13150 separate_line_info_table
13151 = ggc_realloc (separate_line_info_table,
13152 separate_line_info_table_allocated
13153 * sizeof (dw_separate_line_info_entry));
13154 memset (separate_line_info_table
13155 + separate_line_info_table_in_use,
13157 (LINE_INFO_TABLE_INCREMENT
13158 * sizeof (dw_separate_line_info_entry)));
13161 /* Add the new entry at the end of the line_info_table. */
13162 line_info
13163 = &separate_line_info_table[separate_line_info_table_in_use++];
13164 line_info->dw_file_num = lookup_filename (filename);
13165 line_info->dw_line_num = line;
13166 line_info->function = current_function_funcdef_no;
13168 else
13170 dw_line_info_ref line_info;
13172 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
13173 line_info_table_in_use);
13175 /* Expand the line info table if necessary. */
13176 if (line_info_table_in_use == line_info_table_allocated)
13178 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13179 line_info_table
13180 = ggc_realloc (line_info_table,
13181 (line_info_table_allocated
13182 * sizeof (dw_line_info_entry)));
13183 memset (line_info_table + line_info_table_in_use, 0,
13184 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
13187 /* Add the new entry at the end of the line_info_table. */
13188 line_info = &line_info_table[line_info_table_in_use++];
13189 line_info->dw_file_num = lookup_filename (filename);
13190 line_info->dw_line_num = line;
13195 /* Record the beginning of a new source file. */
13197 static void
13198 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
13200 if (flag_eliminate_dwarf2_dups)
13202 /* Record the beginning of the file for break_out_includes. */
13203 dw_die_ref bincl_die;
13205 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
13206 add_AT_string (bincl_die, DW_AT_name, filename);
13209 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13211 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13212 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
13213 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13214 lineno);
13215 maybe_emit_file (lookup_filename (filename));
13216 dw2_asm_output_data_uleb128 (lookup_filename (filename),
13217 "Filename we just started");
13221 /* Record the end of a source file. */
13223 static void
13224 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
13226 if (flag_eliminate_dwarf2_dups)
13227 /* Record the end of the file for break_out_includes. */
13228 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
13230 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13232 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13233 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13237 /* Called from debug_define in toplev.c. The `buffer' parameter contains
13238 the tail part of the directive line, i.e. the part which is past the
13239 initial whitespace, #, whitespace, directive-name, whitespace part. */
13241 static void
13242 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13243 const char *buffer ATTRIBUTE_UNUSED)
13245 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13247 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13248 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
13249 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13250 dw2_asm_output_nstring (buffer, -1, "The macro");
13254 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
13255 the tail part of the directive line, i.e. the part which is past the
13256 initial whitespace, #, whitespace, directive-name, whitespace part. */
13258 static void
13259 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13260 const char *buffer ATTRIBUTE_UNUSED)
13262 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13264 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13265 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
13266 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13267 dw2_asm_output_nstring (buffer, -1, "The macro");
13271 /* Set up for Dwarf output at the start of compilation. */
13273 static void
13274 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
13276 init_file_table ();
13278 /* Allocate the decl_die_table. */
13279 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13280 decl_die_table_eq, NULL);
13282 /* Allocate the decl_loc_table. */
13283 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13284 decl_loc_table_eq, NULL);
13286 /* Allocate the initial hunk of the decl_scope_table. */
13287 VARRAY_TREE_INIT (decl_scope_table, 256, "decl_scope_table");
13289 /* Allocate the initial hunk of the abbrev_die_table. */
13290 abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
13291 * sizeof (dw_die_ref));
13292 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
13293 /* Zero-th entry is allocated, but unused */
13294 abbrev_die_table_in_use = 1;
13296 /* Allocate the initial hunk of the line_info_table. */
13297 line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
13298 * sizeof (dw_line_info_entry));
13299 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
13301 /* Zero-th entry is allocated, but unused */
13302 line_info_table_in_use = 1;
13304 /* Generate the initial DIE for the .debug section. Note that the (string)
13305 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
13306 will (typically) be a relative pathname and that this pathname should be
13307 taken as being relative to the directory from which the compiler was
13308 invoked when the given (base) source file was compiled. We will fill
13309 in this value in dwarf2out_finish. */
13310 comp_unit_die = gen_compile_unit_die (NULL);
13312 VARRAY_TREE_INIT (incomplete_types, 64, "incomplete_types");
13314 VARRAY_RTX_INIT (used_rtx_varray, 32, "used_rtx_varray");
13316 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
13317 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
13318 DEBUG_ABBREV_SECTION_LABEL, 0);
13319 if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
13320 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
13321 else
13322 strcpy (text_section_label, stripattributes (TEXT_SECTION_NAME));
13324 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
13325 DEBUG_INFO_SECTION_LABEL, 0);
13326 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
13327 DEBUG_LINE_SECTION_LABEL, 0);
13328 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
13329 DEBUG_RANGES_SECTION_LABEL, 0);
13330 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13331 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
13332 named_section_flags (DEBUG_INFO_SECTION, SECTION_DEBUG);
13333 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
13334 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13335 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
13337 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13339 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13340 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
13341 DEBUG_MACINFO_SECTION_LABEL, 0);
13342 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
13345 if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
13347 text_section ();
13348 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
13352 /* A helper function for dwarf2out_finish called through
13353 ht_forall. Emit one queued .debug_str string. */
13355 static int
13356 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
13358 struct indirect_string_node *node = (struct indirect_string_node *) *h;
13360 if (node->form == DW_FORM_strp)
13362 named_section_flags (DEBUG_STR_SECTION, DEBUG_STR_SECTION_FLAGS);
13363 ASM_OUTPUT_LABEL (asm_out_file, node->label);
13364 assemble_string (node->str, strlen (node->str) + 1);
13367 return 1;
13372 /* Clear the marks for a die and its children.
13373 Be cool if the mark isn't set. */
13375 static void
13376 prune_unmark_dies (dw_die_ref die)
13378 dw_die_ref c;
13379 die->die_mark = 0;
13380 for (c = die->die_child; c; c = c->die_sib)
13381 prune_unmark_dies (c);
13385 /* Given DIE that we're marking as used, find any other dies
13386 it references as attributes and mark them as used. */
13388 static void
13389 prune_unused_types_walk_attribs (dw_die_ref die)
13391 dw_attr_ref a;
13393 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
13395 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
13397 /* A reference to another DIE.
13398 Make sure that it will get emitted. */
13399 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
13401 else if (a->dw_attr == DW_AT_decl_file)
13403 /* A reference to a file. Make sure the file name is emitted. */
13404 a->dw_attr_val.v.val_unsigned =
13405 maybe_emit_file (a->dw_attr_val.v.val_unsigned);
13411 /* Mark DIE as being used. If DOKIDS is true, then walk down
13412 to DIE's children. */
13414 static void
13415 prune_unused_types_mark (dw_die_ref die, int dokids)
13417 dw_die_ref c;
13419 if (die->die_mark == 0)
13421 /* We haven't done this node yet. Mark it as used. */
13422 die->die_mark = 1;
13424 /* We also have to mark its parents as used.
13425 (But we don't want to mark our parents' kids due to this.) */
13426 if (die->die_parent)
13427 prune_unused_types_mark (die->die_parent, 0);
13429 /* Mark any referenced nodes. */
13430 prune_unused_types_walk_attribs (die);
13432 /* If this node is a specification,
13433 also mark the definition, if it exists. */
13434 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
13435 prune_unused_types_mark (die->die_definition, 1);
13438 if (dokids && die->die_mark != 2)
13440 /* We need to walk the children, but haven't done so yet.
13441 Remember that we've walked the kids. */
13442 die->die_mark = 2;
13444 /* Walk them. */
13445 for (c = die->die_child; c; c = c->die_sib)
13447 /* If this is an array type, we need to make sure our
13448 kids get marked, even if they're types. */
13449 if (die->die_tag == DW_TAG_array_type)
13450 prune_unused_types_mark (c, 1);
13451 else
13452 prune_unused_types_walk (c);
13458 /* Walk the tree DIE and mark types that we actually use. */
13460 static void
13461 prune_unused_types_walk (dw_die_ref die)
13463 dw_die_ref c;
13465 /* Don't do anything if this node is already marked. */
13466 if (die->die_mark)
13467 return;
13469 switch (die->die_tag) {
13470 case DW_TAG_const_type:
13471 case DW_TAG_packed_type:
13472 case DW_TAG_pointer_type:
13473 case DW_TAG_reference_type:
13474 case DW_TAG_volatile_type:
13475 case DW_TAG_typedef:
13476 case DW_TAG_array_type:
13477 case DW_TAG_structure_type:
13478 case DW_TAG_union_type:
13479 case DW_TAG_class_type:
13480 case DW_TAG_friend:
13481 case DW_TAG_variant_part:
13482 case DW_TAG_enumeration_type:
13483 case DW_TAG_subroutine_type:
13484 case DW_TAG_string_type:
13485 case DW_TAG_set_type:
13486 case DW_TAG_subrange_type:
13487 case DW_TAG_ptr_to_member_type:
13488 case DW_TAG_file_type:
13489 /* It's a type node --- don't mark it. */
13490 return;
13492 default:
13493 /* Mark everything else. */
13494 break;
13497 die->die_mark = 1;
13499 /* Now, mark any dies referenced from here. */
13500 prune_unused_types_walk_attribs (die);
13502 /* Mark children. */
13503 for (c = die->die_child; c; c = c->die_sib)
13504 prune_unused_types_walk (c);
13508 /* Remove from the tree DIE any dies that aren't marked. */
13510 static void
13511 prune_unused_types_prune (dw_die_ref die)
13513 dw_die_ref c, p, n;
13514 if (!die->die_mark)
13515 abort();
13517 p = NULL;
13518 for (c = die->die_child; c; c = n)
13520 n = c->die_sib;
13521 if (c->die_mark)
13523 prune_unused_types_prune (c);
13524 p = c;
13526 else
13528 if (p)
13529 p->die_sib = n;
13530 else
13531 die->die_child = n;
13532 free_die (c);
13538 /* Remove dies representing declarations that we never use. */
13540 static void
13541 prune_unused_types (void)
13543 unsigned int i;
13544 limbo_die_node *node;
13546 /* Clear all the marks. */
13547 prune_unmark_dies (comp_unit_die);
13548 for (node = limbo_die_list; node; node = node->next)
13549 prune_unmark_dies (node->die);
13551 /* Set the mark on nodes that are actually used. */
13552 prune_unused_types_walk (comp_unit_die);
13553 for (node = limbo_die_list; node; node = node->next)
13554 prune_unused_types_walk (node->die);
13556 /* Also set the mark on nodes referenced from the
13557 pubname_table or arange_table. */
13558 for (i = 0; i < pubname_table_in_use; i++)
13559 prune_unused_types_mark (pubname_table[i].die, 1);
13560 for (i = 0; i < arange_table_in_use; i++)
13561 prune_unused_types_mark (arange_table[i], 1);
13563 /* Get rid of nodes that aren't marked. */
13564 prune_unused_types_prune (comp_unit_die);
13565 for (node = limbo_die_list; node; node = node->next)
13566 prune_unused_types_prune (node->die);
13568 /* Leave the marks clear. */
13569 prune_unmark_dies (comp_unit_die);
13570 for (node = limbo_die_list; node; node = node->next)
13571 prune_unmark_dies (node->die);
13574 /* Output stuff that dwarf requires at the end of every file,
13575 and generate the DWARF-2 debugging info. */
13577 static void
13578 dwarf2out_finish (const char *filename)
13580 limbo_die_node *node, *next_node;
13581 dw_die_ref die = 0;
13583 /* Add the name for the main input file now. We delayed this from
13584 dwarf2out_init to avoid complications with PCH. */
13585 add_name_attribute (comp_unit_die, filename);
13586 if (filename[0] != DIR_SEPARATOR)
13587 add_comp_dir_attribute (comp_unit_die);
13588 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
13590 size_t i;
13591 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
13592 if (VARRAY_CHAR_PTR (file_table, i)[0] != DIR_SEPARATOR
13593 /* Don't add cwd for <built-in>. */
13594 && VARRAY_CHAR_PTR (file_table, i)[0] != '<')
13596 add_comp_dir_attribute (comp_unit_die);
13597 break;
13601 /* Traverse the limbo die list, and add parent/child links. The only
13602 dies without parents that should be here are concrete instances of
13603 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
13604 For concrete instances, we can get the parent die from the abstract
13605 instance. */
13606 for (node = limbo_die_list; node; node = next_node)
13608 next_node = node->next;
13609 die = node->die;
13611 if (die->die_parent == NULL)
13613 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
13614 tree context;
13616 if (origin)
13617 add_child_die (origin->die_parent, die);
13618 else if (die == comp_unit_die)
13620 /* If this was an expression for a bound involved in a function
13621 return type, it may be a SAVE_EXPR for which we weren't able
13622 to find a DIE previously. So try now. */
13623 else if (node->created_for
13624 && TREE_CODE (node->created_for) == SAVE_EXPR
13625 && 0 != (origin = (lookup_decl_die
13626 (SAVE_EXPR_CONTEXT
13627 (node->created_for)))))
13628 add_child_die (origin, die);
13629 else if (errorcount > 0 || sorrycount > 0)
13630 /* It's OK to be confused by errors in the input. */
13631 add_child_die (comp_unit_die, die);
13632 else if (node->created_for
13633 && ((DECL_P (node->created_for)
13634 && (context = DECL_CONTEXT (node->created_for)))
13635 || (TYPE_P (node->created_for)
13636 && (context = TYPE_CONTEXT (node->created_for))))
13637 && TREE_CODE (context) == FUNCTION_DECL)
13639 /* In certain situations, the lexical block containing a
13640 nested function can be optimized away, which results
13641 in the nested function die being orphaned. Likewise
13642 with the return type of that nested function. Force
13643 this to be a child of the containing function. */
13644 origin = lookup_decl_die (context);
13645 if (! origin)
13646 abort ();
13647 add_child_die (origin, die);
13649 else
13650 abort ();
13654 limbo_die_list = NULL;
13656 /* Walk through the list of incomplete types again, trying once more to
13657 emit full debugging info for them. */
13658 retry_incomplete_types ();
13660 /* We need to reverse all the dies before break_out_includes, or
13661 we'll see the end of an include file before the beginning. */
13662 reverse_all_dies (comp_unit_die);
13664 if (flag_eliminate_unused_debug_types)
13665 prune_unused_types ();
13667 /* Generate separate CUs for each of the include files we've seen.
13668 They will go into limbo_die_list. */
13669 if (flag_eliminate_dwarf2_dups)
13670 break_out_includes (comp_unit_die);
13672 /* Traverse the DIE's and add add sibling attributes to those DIE's
13673 that have children. */
13674 add_sibling_attributes (comp_unit_die);
13675 for (node = limbo_die_list; node; node = node->next)
13676 add_sibling_attributes (node->die);
13678 /* Output a terminator label for the .text section. */
13679 text_section ();
13680 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
13682 /* Output the source line correspondence table. We must do this
13683 even if there is no line information. Otherwise, on an empty
13684 translation unit, we will generate a present, but empty,
13685 .debug_info section. IRIX 6.5 `nm' will then complain when
13686 examining the file. */
13687 if (! DWARF2_ASM_LINE_DEBUG_INFO)
13689 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13690 output_line_info ();
13693 /* Output location list section if necessary. */
13694 if (have_location_lists)
13696 /* Output the location lists info. */
13697 named_section_flags (DEBUG_LOC_SECTION, SECTION_DEBUG);
13698 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
13699 DEBUG_LOC_SECTION_LABEL, 0);
13700 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
13701 output_location_lists (die);
13702 have_location_lists = 0;
13705 /* We can only use the low/high_pc attributes if all of the code was
13706 in .text. */
13707 if (separate_line_info_table_in_use == 0)
13709 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
13710 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
13713 /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
13714 "base address". Use zero so that these addresses become absolute. */
13715 else if (have_location_lists || ranges_table_in_use)
13716 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
13718 if (debug_info_level >= DINFO_LEVEL_NORMAL)
13719 add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
13720 debug_line_section_label);
13722 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13723 add_AT_lbl_offset (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
13725 /* Output all of the compilation units. We put the main one last so that
13726 the offsets are available to output_pubnames. */
13727 for (node = limbo_die_list; node; node = node->next)
13728 output_comp_unit (node->die, 0);
13730 output_comp_unit (comp_unit_die, 0);
13732 /* Output the abbreviation table. */
13733 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13734 output_abbrev_section ();
13736 /* Output public names table if necessary. */
13737 if (pubname_table_in_use)
13739 named_section_flags (DEBUG_PUBNAMES_SECTION, SECTION_DEBUG);
13740 output_pubnames ();
13743 /* Output the address range information. We only put functions in the arange
13744 table, so don't write it out if we don't have any. */
13745 if (fde_table_in_use)
13747 named_section_flags (DEBUG_ARANGES_SECTION, SECTION_DEBUG);
13748 output_aranges ();
13751 /* Output ranges section if necessary. */
13752 if (ranges_table_in_use)
13754 named_section_flags (DEBUG_RANGES_SECTION, SECTION_DEBUG);
13755 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
13756 output_ranges ();
13759 /* Have to end the primary source file. */
13760 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13762 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13763 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13764 dw2_asm_output_data (1, 0, "End compilation unit");
13767 /* If we emitted any DW_FORM_strp form attribute, output the string
13768 table too. */
13769 if (debug_str_hash)
13770 htab_traverse (debug_str_hash, output_indirect_string, NULL);
13772 #else
13774 /* This should never be used, but its address is needed for comparisons. */
13775 const struct gcc_debug_hooks dwarf2_debug_hooks;
13777 #endif /* DWARF2_DEBUGGING_INFO */
13779 #include "gt-dwarf2out.h"