Ayee, missed a file.
[official-gcc.git] / gcc / dwarf2out.c
blob46b3b512660460320e747858d5e74fefc4c4891e
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 flush_queued_reg_saves (void);
363 static bool clobbers_queued_reg_save (rtx);
364 static void dwarf2out_frame_debug_expr (rtx, const char *);
366 /* Support for complex CFA locations. */
367 static void output_cfa_loc (dw_cfi_ref);
368 static void get_cfa_from_loc_descr (dw_cfa_location *,
369 struct dw_loc_descr_struct *);
370 static struct dw_loc_descr_struct *build_cfa_loc
371 (dw_cfa_location *);
372 static void def_cfa_1 (const char *, dw_cfa_location *);
374 /* How to start an assembler comment. */
375 #ifndef ASM_COMMENT_START
376 #define ASM_COMMENT_START ";#"
377 #endif
379 /* Data and reference forms for relocatable data. */
380 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
381 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
383 #ifndef DEBUG_FRAME_SECTION
384 #define DEBUG_FRAME_SECTION ".debug_frame"
385 #endif
387 #ifndef FUNC_BEGIN_LABEL
388 #define FUNC_BEGIN_LABEL "LFB"
389 #endif
391 #ifndef FUNC_END_LABEL
392 #define FUNC_END_LABEL "LFE"
393 #endif
395 #ifndef FRAME_BEGIN_LABEL
396 #define FRAME_BEGIN_LABEL "Lframe"
397 #endif
398 #define CIE_AFTER_SIZE_LABEL "LSCIE"
399 #define CIE_END_LABEL "LECIE"
400 #define FDE_LABEL "LSFDE"
401 #define FDE_AFTER_SIZE_LABEL "LASFDE"
402 #define FDE_END_LABEL "LEFDE"
403 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
404 #define LINE_NUMBER_END_LABEL "LELT"
405 #define LN_PROLOG_AS_LABEL "LASLTP"
406 #define LN_PROLOG_END_LABEL "LELTP"
407 #define DIE_LABEL_PREFIX "DW"
409 /* The DWARF 2 CFA column which tracks the return address. Normally this
410 is the column for PC, or the first column after all of the hard
411 registers. */
412 #ifndef DWARF_FRAME_RETURN_COLUMN
413 #ifdef PC_REGNUM
414 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
415 #else
416 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
417 #endif
418 #endif
420 /* The mapping from gcc register number to DWARF 2 CFA column number. By
421 default, we just provide columns for all registers. */
422 #ifndef DWARF_FRAME_REGNUM
423 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
424 #endif
426 /* The offset from the incoming value of %sp to the top of the stack frame
427 for the current function. */
428 #ifndef INCOMING_FRAME_SP_OFFSET
429 #define INCOMING_FRAME_SP_OFFSET 0
430 #endif
432 /* Hook used by __throw. */
435 expand_builtin_dwarf_sp_column (void)
437 return GEN_INT (DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
440 /* Return a pointer to a copy of the section string name S with all
441 attributes stripped off, and an asterisk prepended (for assemble_name). */
443 static inline char *
444 stripattributes (const char *s)
446 char *stripped = xmalloc (strlen (s) + 2);
447 char *p = stripped;
449 *p++ = '*';
451 while (*s && *s != ',')
452 *p++ = *s++;
454 *p = '\0';
455 return stripped;
458 /* Generate code to initialize the register size table. */
460 void
461 expand_builtin_init_dwarf_reg_sizes (tree address)
463 int i;
464 enum machine_mode mode = TYPE_MODE (char_type_node);
465 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, 0);
466 rtx mem = gen_rtx_MEM (BLKmode, addr);
467 bool wrote_return_column = false;
469 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
470 if (DWARF_FRAME_REGNUM (i) < DWARF_FRAME_REGISTERS)
472 HOST_WIDE_INT offset = DWARF_FRAME_REGNUM (i) * GET_MODE_SIZE (mode);
473 enum machine_mode save_mode = reg_raw_mode[i];
474 HOST_WIDE_INT size;
476 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
477 save_mode = choose_hard_reg_mode (i, 1, true);
478 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
480 if (save_mode == VOIDmode)
481 continue;
482 wrote_return_column = true;
484 size = GET_MODE_SIZE (save_mode);
485 if (offset < 0)
486 continue;
488 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
491 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
492 if (! wrote_return_column)
493 abort ();
494 i = DWARF_ALT_FRAME_RETURN_COLUMN;
495 wrote_return_column = false;
496 #else
497 i = DWARF_FRAME_RETURN_COLUMN;
498 #endif
500 if (! wrote_return_column)
502 enum machine_mode save_mode = Pmode;
503 HOST_WIDE_INT offset = i * GET_MODE_SIZE (mode);
504 HOST_WIDE_INT size = GET_MODE_SIZE (save_mode);
505 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
509 /* Convert a DWARF call frame info. operation to its string name */
511 static const char *
512 dwarf_cfi_name (unsigned int cfi_opc)
514 switch (cfi_opc)
516 case DW_CFA_advance_loc:
517 return "DW_CFA_advance_loc";
518 case DW_CFA_offset:
519 return "DW_CFA_offset";
520 case DW_CFA_restore:
521 return "DW_CFA_restore";
522 case DW_CFA_nop:
523 return "DW_CFA_nop";
524 case DW_CFA_set_loc:
525 return "DW_CFA_set_loc";
526 case DW_CFA_advance_loc1:
527 return "DW_CFA_advance_loc1";
528 case DW_CFA_advance_loc2:
529 return "DW_CFA_advance_loc2";
530 case DW_CFA_advance_loc4:
531 return "DW_CFA_advance_loc4";
532 case DW_CFA_offset_extended:
533 return "DW_CFA_offset_extended";
534 case DW_CFA_restore_extended:
535 return "DW_CFA_restore_extended";
536 case DW_CFA_undefined:
537 return "DW_CFA_undefined";
538 case DW_CFA_same_value:
539 return "DW_CFA_same_value";
540 case DW_CFA_register:
541 return "DW_CFA_register";
542 case DW_CFA_remember_state:
543 return "DW_CFA_remember_state";
544 case DW_CFA_restore_state:
545 return "DW_CFA_restore_state";
546 case DW_CFA_def_cfa:
547 return "DW_CFA_def_cfa";
548 case DW_CFA_def_cfa_register:
549 return "DW_CFA_def_cfa_register";
550 case DW_CFA_def_cfa_offset:
551 return "DW_CFA_def_cfa_offset";
553 /* DWARF 3 */
554 case DW_CFA_def_cfa_expression:
555 return "DW_CFA_def_cfa_expression";
556 case DW_CFA_expression:
557 return "DW_CFA_expression";
558 case DW_CFA_offset_extended_sf:
559 return "DW_CFA_offset_extended_sf";
560 case DW_CFA_def_cfa_sf:
561 return "DW_CFA_def_cfa_sf";
562 case DW_CFA_def_cfa_offset_sf:
563 return "DW_CFA_def_cfa_offset_sf";
565 /* SGI/MIPS specific */
566 case DW_CFA_MIPS_advance_loc8:
567 return "DW_CFA_MIPS_advance_loc8";
569 /* GNU extensions */
570 case DW_CFA_GNU_window_save:
571 return "DW_CFA_GNU_window_save";
572 case DW_CFA_GNU_args_size:
573 return "DW_CFA_GNU_args_size";
574 case DW_CFA_GNU_negative_offset_extended:
575 return "DW_CFA_GNU_negative_offset_extended";
577 default:
578 return "DW_CFA_<unknown>";
582 /* Return a pointer to a newly allocated Call Frame Instruction. */
584 static inline dw_cfi_ref
585 new_cfi (void)
587 dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
589 cfi->dw_cfi_next = NULL;
590 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
591 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
593 return cfi;
596 /* Add a Call Frame Instruction to list of instructions. */
598 static inline void
599 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
601 dw_cfi_ref *p;
603 /* Find the end of the chain. */
604 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
607 *p = cfi;
610 /* Generate a new label for the CFI info to refer to. */
612 char *
613 dwarf2out_cfi_label (void)
615 static char label[20];
617 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
618 ASM_OUTPUT_LABEL (asm_out_file, label);
619 return label;
622 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
623 or to the CIE if LABEL is NULL. */
625 static void
626 add_fde_cfi (const char *label, dw_cfi_ref cfi)
628 if (label)
630 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
632 if (*label == 0)
633 label = dwarf2out_cfi_label ();
635 if (fde->dw_fde_current_label == NULL
636 || strcmp (label, fde->dw_fde_current_label) != 0)
638 dw_cfi_ref xcfi;
640 fde->dw_fde_current_label = label = xstrdup (label);
642 /* Set the location counter to the new label. */
643 xcfi = new_cfi ();
644 xcfi->dw_cfi_opc = DW_CFA_advance_loc4;
645 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
646 add_cfi (&fde->dw_fde_cfi, xcfi);
649 add_cfi (&fde->dw_fde_cfi, cfi);
652 else
653 add_cfi (&cie_cfi_head, cfi);
656 /* Subroutine of lookup_cfa. */
658 static inline void
659 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
661 switch (cfi->dw_cfi_opc)
663 case DW_CFA_def_cfa_offset:
664 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
665 break;
666 case DW_CFA_def_cfa_register:
667 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
668 break;
669 case DW_CFA_def_cfa:
670 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
671 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
672 break;
673 case DW_CFA_def_cfa_expression:
674 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
675 break;
676 default:
677 break;
681 /* Find the previous value for the CFA. */
683 static void
684 lookup_cfa (dw_cfa_location *loc)
686 dw_cfi_ref cfi;
688 loc->reg = (unsigned long) -1;
689 loc->offset = 0;
690 loc->indirect = 0;
691 loc->base_offset = 0;
693 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
694 lookup_cfa_1 (cfi, loc);
696 if (fde_table_in_use)
698 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
699 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
700 lookup_cfa_1 (cfi, loc);
704 /* The current rule for calculating the DWARF2 canonical frame address. */
705 static dw_cfa_location cfa;
707 /* The register used for saving registers to the stack, and its offset
708 from the CFA. */
709 static dw_cfa_location cfa_store;
711 /* The running total of the size of arguments pushed onto the stack. */
712 static HOST_WIDE_INT args_size;
714 /* The last args_size we actually output. */
715 static HOST_WIDE_INT old_args_size;
717 /* Entry point to update the canonical frame address (CFA).
718 LABEL is passed to add_fde_cfi. The value of CFA is now to be
719 calculated from REG+OFFSET. */
721 void
722 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
724 dw_cfa_location loc;
725 loc.indirect = 0;
726 loc.base_offset = 0;
727 loc.reg = reg;
728 loc.offset = offset;
729 def_cfa_1 (label, &loc);
732 /* This routine does the actual work. The CFA is now calculated from
733 the dw_cfa_location structure. */
735 static void
736 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
738 dw_cfi_ref cfi;
739 dw_cfa_location old_cfa, loc;
741 cfa = *loc_p;
742 loc = *loc_p;
744 if (cfa_store.reg == loc.reg && loc.indirect == 0)
745 cfa_store.offset = loc.offset;
747 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
748 lookup_cfa (&old_cfa);
750 /* If nothing changed, no need to issue any call frame instructions. */
751 if (loc.reg == old_cfa.reg && loc.offset == old_cfa.offset
752 && loc.indirect == old_cfa.indirect
753 && (loc.indirect == 0 || loc.base_offset == old_cfa.base_offset))
754 return;
756 cfi = new_cfi ();
758 if (loc.reg == old_cfa.reg && !loc.indirect)
760 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction,
761 indicating the CFA register did not change but the offset
762 did. */
763 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
764 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
767 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
768 else if (loc.offset == old_cfa.offset && old_cfa.reg != (unsigned long) -1
769 && !loc.indirect)
771 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
772 indicating the CFA register has changed to <register> but the
773 offset has not changed. */
774 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
775 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
777 #endif
779 else if (loc.indirect == 0)
781 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
782 indicating the CFA register has changed to <register> with
783 the specified offset. */
784 cfi->dw_cfi_opc = DW_CFA_def_cfa;
785 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
786 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
788 else
790 /* Construct a DW_CFA_def_cfa_expression instruction to
791 calculate the CFA using a full location expression since no
792 register-offset pair is available. */
793 struct dw_loc_descr_struct *loc_list;
795 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
796 loc_list = build_cfa_loc (&loc);
797 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
800 add_fde_cfi (label, cfi);
803 /* Add the CFI for saving a register. REG is the CFA column number.
804 LABEL is passed to add_fde_cfi.
805 If SREG is -1, the register is saved at OFFSET from the CFA;
806 otherwise it is saved in SREG. */
808 static void
809 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
811 dw_cfi_ref cfi = new_cfi ();
813 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
815 if (sreg == INVALID_REGNUM)
817 if (reg & ~0x3f)
818 /* The register number won't fit in 6 bits, so we have to use
819 the long form. */
820 cfi->dw_cfi_opc = DW_CFA_offset_extended;
821 else
822 cfi->dw_cfi_opc = DW_CFA_offset;
824 #ifdef ENABLE_CHECKING
826 /* If we get an offset that is not a multiple of
827 DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
828 definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
829 description. */
830 HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
832 if (check_offset * DWARF_CIE_DATA_ALIGNMENT != offset)
833 abort ();
835 #endif
836 offset /= DWARF_CIE_DATA_ALIGNMENT;
837 if (offset < 0)
838 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
840 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
842 else if (sreg == reg)
843 cfi->dw_cfi_opc = DW_CFA_same_value;
844 else
846 cfi->dw_cfi_opc = DW_CFA_register;
847 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
850 add_fde_cfi (label, cfi);
853 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
854 This CFI tells the unwinder that it needs to restore the window registers
855 from the previous frame's window save area.
857 ??? Perhaps we should note in the CIE where windows are saved (instead of
858 assuming 0(cfa)) and what registers are in the window. */
860 void
861 dwarf2out_window_save (const char *label)
863 dw_cfi_ref cfi = new_cfi ();
865 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
866 add_fde_cfi (label, cfi);
869 /* Add a CFI to update the running total of the size of arguments
870 pushed onto the stack. */
872 void
873 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
875 dw_cfi_ref cfi;
877 if (size == old_args_size)
878 return;
880 old_args_size = size;
882 cfi = new_cfi ();
883 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
884 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
885 add_fde_cfi (label, cfi);
888 /* Entry point for saving a register to the stack. REG is the GCC register
889 number. LABEL and OFFSET are passed to reg_save. */
891 void
892 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
894 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
897 /* Entry point for saving the return address in the stack.
898 LABEL and OFFSET are passed to reg_save. */
900 void
901 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
903 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
906 /* Entry point for saving the return address in a register.
907 LABEL and SREG are passed to reg_save. */
909 void
910 dwarf2out_return_reg (const char *label, unsigned int sreg)
912 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
915 /* Record the initial position of the return address. RTL is
916 INCOMING_RETURN_ADDR_RTX. */
918 static void
919 initial_return_save (rtx rtl)
921 unsigned int reg = INVALID_REGNUM;
922 HOST_WIDE_INT offset = 0;
924 switch (GET_CODE (rtl))
926 case REG:
927 /* RA is in a register. */
928 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
929 break;
931 case MEM:
932 /* RA is on the stack. */
933 rtl = XEXP (rtl, 0);
934 switch (GET_CODE (rtl))
936 case REG:
937 if (REGNO (rtl) != STACK_POINTER_REGNUM)
938 abort ();
939 offset = 0;
940 break;
942 case PLUS:
943 if (REGNO (XEXP (rtl, 0)) != STACK_POINTER_REGNUM)
944 abort ();
945 offset = INTVAL (XEXP (rtl, 1));
946 break;
948 case MINUS:
949 if (REGNO (XEXP (rtl, 0)) != STACK_POINTER_REGNUM)
950 abort ();
951 offset = -INTVAL (XEXP (rtl, 1));
952 break;
954 default:
955 abort ();
958 break;
960 case PLUS:
961 /* The return address is at some offset from any value we can
962 actually load. For instance, on the SPARC it is in %i7+8. Just
963 ignore the offset for now; it doesn't matter for unwinding frames. */
964 if (GET_CODE (XEXP (rtl, 1)) != CONST_INT)
965 abort ();
966 initial_return_save (XEXP (rtl, 0));
967 return;
969 default:
970 abort ();
973 if (reg != DWARF_FRAME_RETURN_COLUMN)
974 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
977 /* Given a SET, calculate the amount of stack adjustment it
978 contains. */
980 static HOST_WIDE_INT
981 stack_adjust_offset (rtx pattern)
983 rtx src = SET_SRC (pattern);
984 rtx dest = SET_DEST (pattern);
985 HOST_WIDE_INT offset = 0;
986 enum rtx_code code;
988 if (dest == stack_pointer_rtx)
990 /* (set (reg sp) (plus (reg sp) (const_int))) */
991 code = GET_CODE (src);
992 if (! (code == PLUS || code == MINUS)
993 || XEXP (src, 0) != stack_pointer_rtx
994 || GET_CODE (XEXP (src, 1)) != CONST_INT)
995 return 0;
997 offset = INTVAL (XEXP (src, 1));
998 if (code == PLUS)
999 offset = -offset;
1001 else if (MEM_P (dest))
1003 /* (set (mem (pre_dec (reg sp))) (foo)) */
1004 src = XEXP (dest, 0);
1005 code = GET_CODE (src);
1007 switch (code)
1009 case PRE_MODIFY:
1010 case POST_MODIFY:
1011 if (XEXP (src, 0) == stack_pointer_rtx)
1013 rtx val = XEXP (XEXP (src, 1), 1);
1014 /* We handle only adjustments by constant amount. */
1015 if (GET_CODE (XEXP (src, 1)) != PLUS ||
1016 GET_CODE (val) != CONST_INT)
1017 abort ();
1018 offset = -INTVAL (val);
1019 break;
1021 return 0;
1023 case PRE_DEC:
1024 case POST_DEC:
1025 if (XEXP (src, 0) == stack_pointer_rtx)
1027 offset = GET_MODE_SIZE (GET_MODE (dest));
1028 break;
1030 return 0;
1032 case PRE_INC:
1033 case POST_INC:
1034 if (XEXP (src, 0) == stack_pointer_rtx)
1036 offset = -GET_MODE_SIZE (GET_MODE (dest));
1037 break;
1039 return 0;
1041 default:
1042 return 0;
1045 else
1046 return 0;
1048 return offset;
1051 /* Check INSN to see if it looks like a push or a stack adjustment, and
1052 make a note of it if it does. EH uses this information to find out how
1053 much extra space it needs to pop off the stack. */
1055 static void
1056 dwarf2out_stack_adjust (rtx insn)
1058 HOST_WIDE_INT offset;
1059 const char *label;
1060 int i;
1062 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1063 with this function. Proper support would require all frame-related
1064 insns to be marked, and to be able to handle saving state around
1065 epilogues textually in the middle of the function. */
1066 if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1067 return;
1069 if (!flag_asynchronous_unwind_tables && CALL_P (insn))
1071 /* Extract the size of the args from the CALL rtx itself. */
1072 insn = PATTERN (insn);
1073 if (GET_CODE (insn) == PARALLEL)
1074 insn = XVECEXP (insn, 0, 0);
1075 if (GET_CODE (insn) == SET)
1076 insn = SET_SRC (insn);
1077 if (GET_CODE (insn) != CALL)
1078 abort ();
1080 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1081 return;
1084 /* If only calls can throw, and we have a frame pointer,
1085 save up adjustments until we see the CALL_INSN. */
1086 else if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1087 return;
1089 if (BARRIER_P (insn))
1091 /* When we see a BARRIER, we know to reset args_size to 0. Usually
1092 the compiler will have already emitted a stack adjustment, but
1093 doesn't bother for calls to noreturn functions. */
1094 #ifdef STACK_GROWS_DOWNWARD
1095 offset = -args_size;
1096 #else
1097 offset = args_size;
1098 #endif
1100 else if (GET_CODE (PATTERN (insn)) == SET)
1101 offset = stack_adjust_offset (PATTERN (insn));
1102 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1103 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1105 /* There may be stack adjustments inside compound insns. Search
1106 for them. */
1107 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1108 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1109 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1111 else
1112 return;
1114 if (offset == 0)
1115 return;
1117 if (cfa.reg == STACK_POINTER_REGNUM)
1118 cfa.offset += offset;
1120 #ifndef STACK_GROWS_DOWNWARD
1121 offset = -offset;
1122 #endif
1124 args_size += offset;
1125 if (args_size < 0)
1126 args_size = 0;
1128 label = dwarf2out_cfi_label ();
1129 def_cfa_1 (label, &cfa);
1130 dwarf2out_args_size (label, args_size);
1133 #endif
1135 /* We delay emitting a register save until either (a) we reach the end
1136 of the prologue or (b) the register is clobbered. This clusters
1137 register saves so that there are fewer pc advances. */
1139 struct queued_reg_save GTY(())
1141 struct queued_reg_save *next;
1142 rtx reg;
1143 HOST_WIDE_INT cfa_offset;
1144 rtx saved_reg;
1147 static GTY(()) struct queued_reg_save *queued_reg_saves;
1149 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1150 struct reg_saved_in_data GTY(()) {
1151 rtx orig_reg;
1152 rtx saved_in_reg;
1155 /* A list of registers saved in other registers.
1156 The list intentionally has a small maximum capacity of 4; if your
1157 port needs more than that, you might consider implementing a
1158 more efficient data structure. */
1159 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1160 static GTY(()) size_t num_regs_saved_in_regs;
1162 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1163 static const char *last_reg_save_label;
1165 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1166 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1168 static void
1169 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1171 struct queued_reg_save *q;
1173 /* Duplicates waste space, but it's also necessary to remove them
1174 for correctness, since the queue gets output in reverse
1175 order. */
1176 for (q = queued_reg_saves; q != NULL; q = q->next)
1177 if (REGNO (q->reg) == REGNO (reg))
1178 break;
1180 if (q == NULL)
1182 q = ggc_alloc (sizeof (*q));
1183 q->next = queued_reg_saves;
1184 queued_reg_saves = q;
1187 q->reg = reg;
1188 q->cfa_offset = offset;
1189 q->saved_reg = sreg;
1191 last_reg_save_label = label;
1194 /* Output all the entries in QUEUED_REG_SAVES. */
1196 static void
1197 flush_queued_reg_saves (void)
1199 struct queued_reg_save *q;
1201 for (q = queued_reg_saves; q; q = q->next)
1203 size_t i;
1204 unsigned int reg, sreg;
1206 for (i = 0; i < num_regs_saved_in_regs; i++)
1207 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1208 break;
1209 if (q->saved_reg && i == num_regs_saved_in_regs)
1211 if (i == ARRAY_SIZE (regs_saved_in_regs))
1212 abort ();
1213 num_regs_saved_in_regs++;
1215 if (i != num_regs_saved_in_regs)
1217 regs_saved_in_regs[i].orig_reg = q->reg;
1218 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1221 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1222 if (q->saved_reg)
1223 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1224 else
1225 sreg = INVALID_REGNUM;
1226 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1229 queued_reg_saves = NULL;
1230 last_reg_save_label = NULL;
1233 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1234 location for? Or, does it clobber a register which we've previously
1235 said that some other register is saved in, and for which we now
1236 have a new location for? */
1238 static bool
1239 clobbers_queued_reg_save (rtx insn)
1241 struct queued_reg_save *q;
1243 for (q = queued_reg_saves; q; q = q->next)
1245 size_t i;
1246 if (modified_in_p (q->reg, insn))
1247 return true;
1248 for (i = 0; i < num_regs_saved_in_regs; i++)
1249 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1250 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1251 return true;
1254 return false;
1257 /* What register, if any, is currently saved in REG? */
1259 static rtx
1260 reg_saved_in (rtx reg)
1262 unsigned int regn = REGNO (reg);
1263 size_t i;
1264 struct queued_reg_save *q;
1266 for (q = queued_reg_saves; q; q = q->next)
1267 if (q->saved_reg && regn == REGNO (q->saved_reg))
1268 return q->reg;
1270 for (i = 0; i < num_regs_saved_in_regs; i++)
1271 if (regs_saved_in_regs[i].saved_in_reg
1272 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1273 return regs_saved_in_regs[i].orig_reg;
1275 return NULL_RTX;
1279 /* A temporary register holding an integral value used in adjusting SP
1280 or setting up the store_reg. The "offset" field holds the integer
1281 value, not an offset. */
1282 static dw_cfa_location cfa_temp;
1284 /* Record call frame debugging information for an expression EXPR,
1285 which either sets SP or FP (adjusting how we calculate the frame
1286 address) or saves a register to the stack or another register.
1287 LABEL indicates the address of EXPR.
1289 This function encodes a state machine mapping rtxes to actions on
1290 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1291 users need not read the source code.
1293 The High-Level Picture
1295 Changes in the register we use to calculate the CFA: Currently we
1296 assume that if you copy the CFA register into another register, we
1297 should take the other one as the new CFA register; this seems to
1298 work pretty well. If it's wrong for some target, it's simple
1299 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1301 Changes in the register we use for saving registers to the stack:
1302 This is usually SP, but not always. Again, we deduce that if you
1303 copy SP into another register (and SP is not the CFA register),
1304 then the new register is the one we will be using for register
1305 saves. This also seems to work.
1307 Register saves: There's not much guesswork about this one; if
1308 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1309 register save, and the register used to calculate the destination
1310 had better be the one we think we're using for this purpose.
1311 It's also assumed that a copy from a call-saved register to another
1312 register is saving that register if RTX_FRAME_RELATED_P is set on
1313 that instruction. If the copy is from a call-saved register to
1314 the *same* register, that means that the register is now the same
1315 value as in the caller.
1317 Except: If the register being saved is the CFA register, and the
1318 offset is nonzero, we are saving the CFA, so we assume we have to
1319 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1320 the intent is to save the value of SP from the previous frame.
1322 In addition, if a register has previously been saved to a different
1323 register,
1325 Invariants / Summaries of Rules
1327 cfa current rule for calculating the CFA. It usually
1328 consists of a register and an offset.
1329 cfa_store register used by prologue code to save things to the stack
1330 cfa_store.offset is the offset from the value of
1331 cfa_store.reg to the actual CFA
1332 cfa_temp register holding an integral value. cfa_temp.offset
1333 stores the value, which will be used to adjust the
1334 stack pointer. cfa_temp is also used like cfa_store,
1335 to track stores to the stack via fp or a temp reg.
1337 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1338 with cfa.reg as the first operand changes the cfa.reg and its
1339 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1340 cfa_temp.offset.
1342 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1343 expression yielding a constant. This sets cfa_temp.reg
1344 and cfa_temp.offset.
1346 Rule 5: Create a new register cfa_store used to save items to the
1347 stack.
1349 Rules 10-14: Save a register to the stack. Define offset as the
1350 difference of the original location and cfa_store's
1351 location (or cfa_temp's location if cfa_temp is used).
1353 The Rules
1355 "{a,b}" indicates a choice of a xor b.
1356 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1358 Rule 1:
1359 (set <reg1> <reg2>:cfa.reg)
1360 effects: cfa.reg = <reg1>
1361 cfa.offset unchanged
1362 cfa_temp.reg = <reg1>
1363 cfa_temp.offset = cfa.offset
1365 Rule 2:
1366 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1367 {<const_int>,<reg>:cfa_temp.reg}))
1368 effects: cfa.reg = sp if fp used
1369 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1370 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1371 if cfa_store.reg==sp
1373 Rule 3:
1374 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1375 effects: cfa.reg = fp
1376 cfa_offset += +/- <const_int>
1378 Rule 4:
1379 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1380 constraints: <reg1> != fp
1381 <reg1> != sp
1382 effects: cfa.reg = <reg1>
1383 cfa_temp.reg = <reg1>
1384 cfa_temp.offset = cfa.offset
1386 Rule 5:
1387 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1388 constraints: <reg1> != fp
1389 <reg1> != sp
1390 effects: cfa_store.reg = <reg1>
1391 cfa_store.offset = cfa.offset - cfa_temp.offset
1393 Rule 6:
1394 (set <reg> <const_int>)
1395 effects: cfa_temp.reg = <reg>
1396 cfa_temp.offset = <const_int>
1398 Rule 7:
1399 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1400 effects: cfa_temp.reg = <reg1>
1401 cfa_temp.offset |= <const_int>
1403 Rule 8:
1404 (set <reg> (high <exp>))
1405 effects: none
1407 Rule 9:
1408 (set <reg> (lo_sum <exp> <const_int>))
1409 effects: cfa_temp.reg = <reg>
1410 cfa_temp.offset = <const_int>
1412 Rule 10:
1413 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1414 effects: cfa_store.offset -= <const_int>
1415 cfa.offset = cfa_store.offset if cfa.reg == sp
1416 cfa.reg = sp
1417 cfa.base_offset = -cfa_store.offset
1419 Rule 11:
1420 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1421 effects: cfa_store.offset += -/+ mode_size(mem)
1422 cfa.offset = cfa_store.offset if cfa.reg == sp
1423 cfa.reg = sp
1424 cfa.base_offset = -cfa_store.offset
1426 Rule 12:
1427 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1429 <reg2>)
1430 effects: cfa.reg = <reg1>
1431 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1433 Rule 13:
1434 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1435 effects: cfa.reg = <reg1>
1436 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1438 Rule 14:
1439 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1440 effects: cfa.reg = <reg1>
1441 cfa.base_offset = -cfa_temp.offset
1442 cfa_temp.offset -= mode_size(mem) */
1444 static void
1445 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1447 rtx src, dest;
1448 HOST_WIDE_INT offset;
1450 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1451 the PARALLEL independently. The first element is always processed if
1452 it is a SET. This is for backward compatibility. Other elements
1453 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1454 flag is set in them. */
1455 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1457 int par_index;
1458 int limit = XVECLEN (expr, 0);
1460 for (par_index = 0; par_index < limit; par_index++)
1461 if (GET_CODE (XVECEXP (expr, 0, par_index)) == SET
1462 && (RTX_FRAME_RELATED_P (XVECEXP (expr, 0, par_index))
1463 || par_index == 0))
1464 dwarf2out_frame_debug_expr (XVECEXP (expr, 0, par_index), label);
1466 return;
1469 if (GET_CODE (expr) != SET)
1470 abort ();
1472 src = SET_SRC (expr);
1473 dest = SET_DEST (expr);
1475 if (GET_CODE (src) == REG)
1477 rtx rsi = reg_saved_in (src);
1478 if (rsi)
1479 src = rsi;
1482 switch (GET_CODE (dest))
1484 case REG:
1485 switch (GET_CODE (src))
1487 /* Setting FP from SP. */
1488 case REG:
1489 if (cfa.reg == (unsigned) REGNO (src))
1491 /* Rule 1 */
1492 /* Update the CFA rule wrt SP or FP. Make sure src is
1493 relative to the current CFA register.
1495 We used to require that dest be either SP or FP, but the
1496 ARM copies SP to a temporary register, and from there to
1497 FP. So we just rely on the backends to only set
1498 RTX_FRAME_RELATED_P on appropriate insns. */
1499 cfa.reg = REGNO (dest);
1500 cfa_temp.reg = cfa.reg;
1501 cfa_temp.offset = cfa.offset;
1503 else if (call_used_regs [REGNO (dest)]
1504 && ! fixed_regs [REGNO (dest)])
1506 /* Saving a register in a register. */
1507 queue_reg_save (label, src, dest, 0);
1509 else
1510 abort ();
1511 break;
1513 case PLUS:
1514 case MINUS:
1515 case LO_SUM:
1516 if (dest == stack_pointer_rtx)
1518 /* Rule 2 */
1519 /* Adjusting SP. */
1520 switch (GET_CODE (XEXP (src, 1)))
1522 case CONST_INT:
1523 offset = INTVAL (XEXP (src, 1));
1524 break;
1525 case REG:
1526 if ((unsigned) REGNO (XEXP (src, 1)) != cfa_temp.reg)
1527 abort ();
1528 offset = cfa_temp.offset;
1529 break;
1530 default:
1531 abort ();
1534 if (XEXP (src, 0) == hard_frame_pointer_rtx)
1536 /* Restoring SP from FP in the epilogue. */
1537 if (cfa.reg != (unsigned) HARD_FRAME_POINTER_REGNUM)
1538 abort ();
1539 cfa.reg = STACK_POINTER_REGNUM;
1541 else if (GET_CODE (src) == LO_SUM)
1542 /* Assume we've set the source reg of the LO_SUM from sp. */
1544 else if (XEXP (src, 0) != stack_pointer_rtx)
1545 abort ();
1547 if (GET_CODE (src) != MINUS)
1548 offset = -offset;
1549 if (cfa.reg == STACK_POINTER_REGNUM)
1550 cfa.offset += offset;
1551 if (cfa_store.reg == STACK_POINTER_REGNUM)
1552 cfa_store.offset += offset;
1554 else if (dest == hard_frame_pointer_rtx)
1556 /* Rule 3 */
1557 /* Either setting the FP from an offset of the SP,
1558 or adjusting the FP */
1559 if (! frame_pointer_needed)
1560 abort ();
1562 if (REG_P (XEXP (src, 0))
1563 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1564 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1566 offset = INTVAL (XEXP (src, 1));
1567 if (GET_CODE (src) != MINUS)
1568 offset = -offset;
1569 cfa.offset += offset;
1570 cfa.reg = HARD_FRAME_POINTER_REGNUM;
1572 else
1573 abort ();
1575 else
1577 if (GET_CODE (src) == MINUS)
1578 abort ();
1580 /* Rule 4 */
1581 if (REG_P (XEXP (src, 0))
1582 && REGNO (XEXP (src, 0)) == cfa.reg
1583 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1585 /* Setting a temporary CFA register that will be copied
1586 into the FP later on. */
1587 offset = - INTVAL (XEXP (src, 1));
1588 cfa.offset += offset;
1589 cfa.reg = REGNO (dest);
1590 /* Or used to save regs to the stack. */
1591 cfa_temp.reg = cfa.reg;
1592 cfa_temp.offset = cfa.offset;
1595 /* Rule 5 */
1596 else if (REG_P (XEXP (src, 0))
1597 && REGNO (XEXP (src, 0)) == cfa_temp.reg
1598 && XEXP (src, 1) == stack_pointer_rtx)
1600 /* Setting a scratch register that we will use instead
1601 of SP for saving registers to the stack. */
1602 if (cfa.reg != STACK_POINTER_REGNUM)
1603 abort ();
1604 cfa_store.reg = REGNO (dest);
1605 cfa_store.offset = cfa.offset - cfa_temp.offset;
1608 /* Rule 9 */
1609 else if (GET_CODE (src) == LO_SUM
1610 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1612 cfa_temp.reg = REGNO (dest);
1613 cfa_temp.offset = INTVAL (XEXP (src, 1));
1615 else
1616 abort ();
1618 break;
1620 /* Rule 6 */
1621 case CONST_INT:
1622 cfa_temp.reg = REGNO (dest);
1623 cfa_temp.offset = INTVAL (src);
1624 break;
1626 /* Rule 7 */
1627 case IOR:
1628 if (!REG_P (XEXP (src, 0))
1629 || (unsigned) REGNO (XEXP (src, 0)) != cfa_temp.reg
1630 || GET_CODE (XEXP (src, 1)) != CONST_INT)
1631 abort ();
1633 if ((unsigned) REGNO (dest) != cfa_temp.reg)
1634 cfa_temp.reg = REGNO (dest);
1635 cfa_temp.offset |= INTVAL (XEXP (src, 1));
1636 break;
1638 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1639 which will fill in all of the bits. */
1640 /* Rule 8 */
1641 case HIGH:
1642 break;
1644 default:
1645 abort ();
1648 def_cfa_1 (label, &cfa);
1649 break;
1651 case MEM:
1652 if (!REG_P (src))
1653 abort ();
1655 /* Saving a register to the stack. Make sure dest is relative to the
1656 CFA register. */
1657 switch (GET_CODE (XEXP (dest, 0)))
1659 /* Rule 10 */
1660 /* With a push. */
1661 case PRE_MODIFY:
1662 /* We can't handle variable size modifications. */
1663 if (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1)) != CONST_INT)
1664 abort ();
1665 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1667 if (REGNO (XEXP (XEXP (dest, 0), 0)) != STACK_POINTER_REGNUM
1668 || cfa_store.reg != STACK_POINTER_REGNUM)
1669 abort ();
1671 cfa_store.offset += offset;
1672 if (cfa.reg == STACK_POINTER_REGNUM)
1673 cfa.offset = cfa_store.offset;
1675 offset = -cfa_store.offset;
1676 break;
1678 /* Rule 11 */
1679 case PRE_INC:
1680 case PRE_DEC:
1681 offset = GET_MODE_SIZE (GET_MODE (dest));
1682 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1683 offset = -offset;
1685 if (REGNO (XEXP (XEXP (dest, 0), 0)) != STACK_POINTER_REGNUM
1686 || cfa_store.reg != STACK_POINTER_REGNUM)
1687 abort ();
1689 cfa_store.offset += offset;
1690 if (cfa.reg == STACK_POINTER_REGNUM)
1691 cfa.offset = cfa_store.offset;
1693 offset = -cfa_store.offset;
1694 break;
1696 /* Rule 12 */
1697 /* With an offset. */
1698 case PLUS:
1699 case MINUS:
1700 case LO_SUM:
1701 if (GET_CODE (XEXP (XEXP (dest, 0), 1)) != CONST_INT)
1702 abort ();
1703 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1704 if (GET_CODE (XEXP (dest, 0)) == MINUS)
1705 offset = -offset;
1707 if (cfa_store.reg == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)))
1708 offset -= cfa_store.offset;
1709 else if (cfa_temp.reg == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)))
1710 offset -= cfa_temp.offset;
1711 else
1712 abort ();
1713 break;
1715 /* Rule 13 */
1716 /* Without an offset. */
1717 case REG:
1718 if (cfa_store.reg == (unsigned) REGNO (XEXP (dest, 0)))
1719 offset = -cfa_store.offset;
1720 else if (cfa_temp.reg == (unsigned) REGNO (XEXP (dest, 0)))
1721 offset = -cfa_temp.offset;
1722 else
1723 abort ();
1724 break;
1726 /* Rule 14 */
1727 case POST_INC:
1728 if (cfa_temp.reg != (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)))
1729 abort ();
1730 offset = -cfa_temp.offset;
1731 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1732 break;
1734 default:
1735 abort ();
1738 if (REGNO (src) != STACK_POINTER_REGNUM
1739 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1740 && (unsigned) REGNO (src) == cfa.reg)
1742 /* We're storing the current CFA reg into the stack. */
1744 if (cfa.offset == 0)
1746 /* If the source register is exactly the CFA, assume
1747 we're saving SP like any other register; this happens
1748 on the ARM. */
1749 def_cfa_1 (label, &cfa);
1750 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1751 break;
1753 else
1755 /* Otherwise, we'll need to look in the stack to
1756 calculate the CFA. */
1757 rtx x = XEXP (dest, 0);
1759 if (!REG_P (x))
1760 x = XEXP (x, 0);
1761 if (!REG_P (x))
1762 abort ();
1764 cfa.reg = REGNO (x);
1765 cfa.base_offset = offset;
1766 cfa.indirect = 1;
1767 def_cfa_1 (label, &cfa);
1768 break;
1772 def_cfa_1 (label, &cfa);
1773 queue_reg_save (label, src, NULL_RTX, offset);
1774 break;
1776 default:
1777 abort ();
1781 /* Record call frame debugging information for INSN, which either
1782 sets SP or FP (adjusting how we calculate the frame address) or saves a
1783 register to the stack. If INSN is NULL_RTX, initialize our state. */
1785 void
1786 dwarf2out_frame_debug (rtx insn)
1788 const char *label;
1789 rtx src;
1791 if (insn == NULL_RTX)
1793 size_t i;
1795 /* Flush any queued register saves. */
1796 flush_queued_reg_saves ();
1798 /* Set up state for generating call frame debug info. */
1799 lookup_cfa (&cfa);
1800 if (cfa.reg != (unsigned long) DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM))
1801 abort ();
1803 cfa.reg = STACK_POINTER_REGNUM;
1804 cfa_store = cfa;
1805 cfa_temp.reg = -1;
1806 cfa_temp.offset = 0;
1808 for (i = 0; i < num_regs_saved_in_regs; i++)
1810 regs_saved_in_regs[i].orig_reg = NULL_RTX;
1811 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1813 num_regs_saved_in_regs = 0;
1814 return;
1817 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1818 flush_queued_reg_saves ();
1820 if (! RTX_FRAME_RELATED_P (insn))
1822 if (!ACCUMULATE_OUTGOING_ARGS)
1823 dwarf2out_stack_adjust (insn);
1825 return;
1828 label = dwarf2out_cfi_label ();
1829 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1830 if (src)
1831 insn = XEXP (src, 0);
1832 else
1833 insn = PATTERN (insn);
1835 dwarf2out_frame_debug_expr (insn, label);
1838 #endif
1840 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
1841 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1842 (enum dwarf_call_frame_info cfi);
1844 static enum dw_cfi_oprnd_type
1845 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1847 switch (cfi)
1849 case DW_CFA_nop:
1850 case DW_CFA_GNU_window_save:
1851 return dw_cfi_oprnd_unused;
1853 case DW_CFA_set_loc:
1854 case DW_CFA_advance_loc1:
1855 case DW_CFA_advance_loc2:
1856 case DW_CFA_advance_loc4:
1857 case DW_CFA_MIPS_advance_loc8:
1858 return dw_cfi_oprnd_addr;
1860 case DW_CFA_offset:
1861 case DW_CFA_offset_extended:
1862 case DW_CFA_def_cfa:
1863 case DW_CFA_offset_extended_sf:
1864 case DW_CFA_def_cfa_sf:
1865 case DW_CFA_restore_extended:
1866 case DW_CFA_undefined:
1867 case DW_CFA_same_value:
1868 case DW_CFA_def_cfa_register:
1869 case DW_CFA_register:
1870 return dw_cfi_oprnd_reg_num;
1872 case DW_CFA_def_cfa_offset:
1873 case DW_CFA_GNU_args_size:
1874 case DW_CFA_def_cfa_offset_sf:
1875 return dw_cfi_oprnd_offset;
1877 case DW_CFA_def_cfa_expression:
1878 case DW_CFA_expression:
1879 return dw_cfi_oprnd_loc;
1881 default:
1882 abort ();
1886 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
1887 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1888 (enum dwarf_call_frame_info cfi);
1890 static enum dw_cfi_oprnd_type
1891 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1893 switch (cfi)
1895 case DW_CFA_def_cfa:
1896 case DW_CFA_def_cfa_sf:
1897 case DW_CFA_offset:
1898 case DW_CFA_offset_extended_sf:
1899 case DW_CFA_offset_extended:
1900 return dw_cfi_oprnd_offset;
1902 case DW_CFA_register:
1903 return dw_cfi_oprnd_reg_num;
1905 default:
1906 return dw_cfi_oprnd_unused;
1910 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1912 /* Map register numbers held in the call frame info that gcc has
1913 collected using DWARF_FRAME_REGNUM to those that should be output in
1914 .debug_frame and .eh_frame. */
1915 #ifndef DWARF2_FRAME_REG_OUT
1916 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
1917 #endif
1919 /* Output a Call Frame Information opcode and its operand(s). */
1921 static void
1922 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
1924 unsigned long r;
1925 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
1926 dw2_asm_output_data (1, (cfi->dw_cfi_opc
1927 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
1928 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
1929 cfi->dw_cfi_oprnd1.dw_cfi_offset);
1930 else if (cfi->dw_cfi_opc == DW_CFA_offset)
1932 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1933 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
1934 "DW_CFA_offset, column 0x%lx", r);
1935 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1937 else if (cfi->dw_cfi_opc == DW_CFA_restore)
1939 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1940 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
1941 "DW_CFA_restore, column 0x%lx", r);
1943 else
1945 dw2_asm_output_data (1, cfi->dw_cfi_opc,
1946 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
1948 switch (cfi->dw_cfi_opc)
1950 case DW_CFA_set_loc:
1951 if (for_eh)
1952 dw2_asm_output_encoded_addr_rtx (
1953 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
1954 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
1955 NULL);
1956 else
1957 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
1958 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
1959 break;
1961 case DW_CFA_advance_loc1:
1962 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1963 fde->dw_fde_current_label, NULL);
1964 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1965 break;
1967 case DW_CFA_advance_loc2:
1968 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1969 fde->dw_fde_current_label, NULL);
1970 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1971 break;
1973 case DW_CFA_advance_loc4:
1974 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1975 fde->dw_fde_current_label, NULL);
1976 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1977 break;
1979 case DW_CFA_MIPS_advance_loc8:
1980 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1981 fde->dw_fde_current_label, NULL);
1982 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1983 break;
1985 case DW_CFA_offset_extended:
1986 case DW_CFA_def_cfa:
1987 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1988 dw2_asm_output_data_uleb128 (r, NULL);
1989 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1990 break;
1992 case DW_CFA_offset_extended_sf:
1993 case DW_CFA_def_cfa_sf:
1994 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1995 dw2_asm_output_data_uleb128 (r, NULL);
1996 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1997 break;
1999 case DW_CFA_restore_extended:
2000 case DW_CFA_undefined:
2001 case DW_CFA_same_value:
2002 case DW_CFA_def_cfa_register:
2003 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2004 dw2_asm_output_data_uleb128 (r, NULL);
2005 break;
2007 case DW_CFA_register:
2008 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2009 dw2_asm_output_data_uleb128 (r, NULL);
2010 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2011 dw2_asm_output_data_uleb128 (r, NULL);
2012 break;
2014 case DW_CFA_def_cfa_offset:
2015 case DW_CFA_GNU_args_size:
2016 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2017 break;
2019 case DW_CFA_def_cfa_offset_sf:
2020 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2021 break;
2023 case DW_CFA_GNU_window_save:
2024 break;
2026 case DW_CFA_def_cfa_expression:
2027 case DW_CFA_expression:
2028 output_cfa_loc (cfi);
2029 break;
2031 case DW_CFA_GNU_negative_offset_extended:
2032 /* Obsoleted by DW_CFA_offset_extended_sf. */
2033 abort ();
2035 default:
2036 break;
2041 /* Output the call frame information used to record information
2042 that relates to calculating the frame pointer, and records the
2043 location of saved registers. */
2045 static void
2046 output_call_frame_info (int for_eh)
2048 unsigned int i;
2049 dw_fde_ref fde;
2050 dw_cfi_ref cfi;
2051 char l1[20], l2[20], section_start_label[20];
2052 bool any_lsda_needed = false;
2053 char augmentation[6];
2054 int augmentation_size;
2055 int fde_encoding = DW_EH_PE_absptr;
2056 int per_encoding = DW_EH_PE_absptr;
2057 int lsda_encoding = DW_EH_PE_absptr;
2059 /* Don't emit a CIE if there won't be any FDEs. */
2060 if (fde_table_in_use == 0)
2061 return;
2063 /* If we make FDEs linkonce, we may have to emit an empty label for
2064 an FDE that wouldn't otherwise be emitted. We want to avoid
2065 having an FDE kept around when the function it refers to is
2066 discarded. (Example where this matters: a primary function
2067 template in C++ requires EH information, but an explicit
2068 specialization doesn't. */
2069 if (TARGET_USES_WEAK_UNWIND_INFO
2070 && ! flag_asynchronous_unwind_tables
2071 && for_eh)
2072 for (i = 0; i < fde_table_in_use; i++)
2073 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2074 && !fde_table[i].uses_eh_lsda
2075 && ! DECL_ONE_ONLY (fde_table[i].decl))
2076 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2077 for_eh, /* empty */ 1);
2079 /* If we don't have any functions we'll want to unwind out of, don't
2080 emit any EH unwind information. Note that if exceptions aren't
2081 enabled, we won't have collected nothrow information, and if we
2082 asked for asynchronous tables, we always want this info. */
2083 if (for_eh)
2085 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2087 for (i = 0; i < fde_table_in_use; i++)
2088 if (fde_table[i].uses_eh_lsda)
2089 any_eh_needed = any_lsda_needed = true;
2090 else if (TARGET_USES_WEAK_UNWIND_INFO
2091 && DECL_ONE_ONLY (fde_table[i].decl))
2092 any_eh_needed = true;
2093 else if (! fde_table[i].nothrow
2094 && ! fde_table[i].all_throwers_are_sibcalls)
2095 any_eh_needed = true;
2097 if (! any_eh_needed)
2098 return;
2101 /* We're going to be generating comments, so turn on app. */
2102 if (flag_debug_asm)
2103 app_enable ();
2105 if (for_eh)
2106 targetm.asm_out.eh_frame_section ();
2107 else
2108 named_section_flags (DEBUG_FRAME_SECTION, SECTION_DEBUG);
2110 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2111 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2113 /* Output the CIE. */
2114 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2115 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2116 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2117 "Length of Common Information Entry");
2118 ASM_OUTPUT_LABEL (asm_out_file, l1);
2120 /* Now that the CIE pointer is PC-relative for EH,
2121 use 0 to identify the CIE. */
2122 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2123 (for_eh ? 0 : DW_CIE_ID),
2124 "CIE Identifier Tag");
2126 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2128 augmentation[0] = 0;
2129 augmentation_size = 0;
2130 if (for_eh)
2132 char *p;
2134 /* Augmentation:
2135 z Indicates that a uleb128 is present to size the
2136 augmentation section.
2137 L Indicates the encoding (and thus presence) of
2138 an LSDA pointer in the FDE augmentation.
2139 R Indicates a non-default pointer encoding for
2140 FDE code pointers.
2141 P Indicates the presence of an encoding + language
2142 personality routine in the CIE augmentation. */
2144 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2145 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2146 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2148 p = augmentation + 1;
2149 if (eh_personality_libfunc)
2151 *p++ = 'P';
2152 augmentation_size += 1 + size_of_encoded_value (per_encoding);
2154 if (any_lsda_needed)
2156 *p++ = 'L';
2157 augmentation_size += 1;
2159 if (fde_encoding != DW_EH_PE_absptr)
2161 *p++ = 'R';
2162 augmentation_size += 1;
2164 if (p > augmentation + 1)
2166 augmentation[0] = 'z';
2167 *p = '\0';
2170 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
2171 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2173 int offset = ( 4 /* Length */
2174 + 4 /* CIE Id */
2175 + 1 /* CIE version */
2176 + strlen (augmentation) + 1 /* Augmentation */
2177 + size_of_uleb128 (1) /* Code alignment */
2178 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2179 + 1 /* RA column */
2180 + 1 /* Augmentation size */
2181 + 1 /* Personality encoding */ );
2182 int pad = -offset & (PTR_SIZE - 1);
2184 augmentation_size += pad;
2186 /* Augmentations should be small, so there's scarce need to
2187 iterate for a solution. Die if we exceed one uleb128 byte. */
2188 if (size_of_uleb128 (augmentation_size) != 1)
2189 abort ();
2193 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2194 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2195 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2196 "CIE Data Alignment Factor");
2198 if (DW_CIE_VERSION == 1)
2199 dw2_asm_output_data (1, DWARF_FRAME_RETURN_COLUMN, "CIE RA Column");
2200 else
2201 dw2_asm_output_data_uleb128 (DWARF_FRAME_RETURN_COLUMN, "CIE RA Column");
2203 if (augmentation[0])
2205 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2206 if (eh_personality_libfunc)
2208 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2209 eh_data_format_name (per_encoding));
2210 dw2_asm_output_encoded_addr_rtx (per_encoding,
2211 eh_personality_libfunc, NULL);
2214 if (any_lsda_needed)
2215 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2216 eh_data_format_name (lsda_encoding));
2218 if (fde_encoding != DW_EH_PE_absptr)
2219 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2220 eh_data_format_name (fde_encoding));
2223 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2224 output_cfi (cfi, NULL, for_eh);
2226 /* Pad the CIE out to an address sized boundary. */
2227 ASM_OUTPUT_ALIGN (asm_out_file,
2228 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2229 ASM_OUTPUT_LABEL (asm_out_file, l2);
2231 /* Loop through all of the FDE's. */
2232 for (i = 0; i < fde_table_in_use; i++)
2234 fde = &fde_table[i];
2236 /* Don't emit EH unwind info for leaf functions that don't need it. */
2237 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2238 && (fde->nothrow || fde->all_throwers_are_sibcalls)
2239 && (! TARGET_USES_WEAK_UNWIND_INFO || ! DECL_ONE_ONLY (fde->decl))
2240 && !fde->uses_eh_lsda)
2241 continue;
2243 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2244 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2245 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2246 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2247 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2248 "FDE Length");
2249 ASM_OUTPUT_LABEL (asm_out_file, l1);
2251 if (for_eh)
2252 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2253 else
2254 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2255 "FDE CIE offset");
2257 if (for_eh)
2259 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2260 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2261 dw2_asm_output_encoded_addr_rtx (fde_encoding,
2262 sym_ref,
2263 "FDE initial location");
2264 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2265 fde->dw_fde_end, fde->dw_fde_begin,
2266 "FDE address range");
2268 else
2270 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2271 "FDE initial location");
2272 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2273 fde->dw_fde_end, fde->dw_fde_begin,
2274 "FDE address range");
2277 if (augmentation[0])
2279 if (any_lsda_needed)
2281 int size = size_of_encoded_value (lsda_encoding);
2283 if (lsda_encoding == DW_EH_PE_aligned)
2285 int offset = ( 4 /* Length */
2286 + 4 /* CIE offset */
2287 + 2 * size_of_encoded_value (fde_encoding)
2288 + 1 /* Augmentation size */ );
2289 int pad = -offset & (PTR_SIZE - 1);
2291 size += pad;
2292 if (size_of_uleb128 (size) != 1)
2293 abort ();
2296 dw2_asm_output_data_uleb128 (size, "Augmentation size");
2298 if (fde->uses_eh_lsda)
2300 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2301 fde->funcdef_number);
2302 dw2_asm_output_encoded_addr_rtx (
2303 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2304 "Language Specific Data Area");
2306 else
2308 if (lsda_encoding == DW_EH_PE_aligned)
2309 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2310 dw2_asm_output_data
2311 (size_of_encoded_value (lsda_encoding), 0,
2312 "Language Specific Data Area (none)");
2315 else
2316 dw2_asm_output_data_uleb128 (0, "Augmentation size");
2319 /* Loop through the Call Frame Instructions associated with
2320 this FDE. */
2321 fde->dw_fde_current_label = fde->dw_fde_begin;
2322 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2323 output_cfi (cfi, fde, for_eh);
2325 /* Pad the FDE out to an address sized boundary. */
2326 ASM_OUTPUT_ALIGN (asm_out_file,
2327 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2328 ASM_OUTPUT_LABEL (asm_out_file, l2);
2331 if (for_eh && targetm.terminate_dw2_eh_frame_info)
2332 dw2_asm_output_data (4, 0, "End of Table");
2333 #ifdef MIPS_DEBUGGING_INFO
2334 /* Work around Irix 6 assembler bug whereby labels at the end of a section
2335 get a value of 0. Putting .align 0 after the label fixes it. */
2336 ASM_OUTPUT_ALIGN (asm_out_file, 0);
2337 #endif
2339 /* Turn off app to make assembly quicker. */
2340 if (flag_debug_asm)
2341 app_disable ();
2344 /* Output a marker (i.e. a label) for the beginning of a function, before
2345 the prologue. */
2347 void
2348 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2349 const char *file ATTRIBUTE_UNUSED)
2351 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2352 dw_fde_ref fde;
2354 current_function_func_begin_label = 0;
2356 #ifdef TARGET_UNWIND_INFO
2357 /* ??? current_function_func_begin_label is also used by except.c
2358 for call-site information. We must emit this label if it might
2359 be used. */
2360 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2361 && ! dwarf2out_do_frame ())
2362 return;
2363 #else
2364 if (! dwarf2out_do_frame ())
2365 return;
2366 #endif
2368 function_section (current_function_decl);
2369 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2370 current_function_funcdef_no);
2371 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2372 current_function_funcdef_no);
2373 current_function_func_begin_label = get_identifier (label);
2375 #ifdef TARGET_UNWIND_INFO
2376 /* We can elide the fde allocation if we're not emitting debug info. */
2377 if (! dwarf2out_do_frame ())
2378 return;
2379 #endif
2381 /* Expand the fde table if necessary. */
2382 if (fde_table_in_use == fde_table_allocated)
2384 fde_table_allocated += FDE_TABLE_INCREMENT;
2385 fde_table = ggc_realloc (fde_table,
2386 fde_table_allocated * sizeof (dw_fde_node));
2387 memset (fde_table + fde_table_in_use, 0,
2388 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2391 /* Record the FDE associated with this function. */
2392 current_funcdef_fde = fde_table_in_use;
2394 /* Add the new FDE at the end of the fde_table. */
2395 fde = &fde_table[fde_table_in_use++];
2396 fde->decl = current_function_decl;
2397 fde->dw_fde_begin = xstrdup (label);
2398 fde->dw_fde_current_label = NULL;
2399 fde->dw_fde_end = NULL;
2400 fde->dw_fde_cfi = NULL;
2401 fde->funcdef_number = current_function_funcdef_no;
2402 fde->nothrow = TREE_NOTHROW (current_function_decl);
2403 fde->uses_eh_lsda = cfun->uses_eh_lsda;
2404 fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2406 args_size = old_args_size = 0;
2408 /* We only want to output line number information for the genuine dwarf2
2409 prologue case, not the eh frame case. */
2410 #ifdef DWARF2_DEBUGGING_INFO
2411 if (file)
2412 dwarf2out_source_line (line, file);
2413 #endif
2416 /* Output a marker (i.e. a label) for the absolute end of the generated code
2417 for a function definition. This gets called *after* the epilogue code has
2418 been generated. */
2420 void
2421 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2422 const char *file ATTRIBUTE_UNUSED)
2424 dw_fde_ref fde;
2425 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2427 /* Output a label to mark the endpoint of the code generated for this
2428 function. */
2429 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2430 current_function_funcdef_no);
2431 ASM_OUTPUT_LABEL (asm_out_file, label);
2432 fde = &fde_table[fde_table_in_use - 1];
2433 fde->dw_fde_end = xstrdup (label);
2436 void
2437 dwarf2out_frame_init (void)
2439 /* Allocate the initial hunk of the fde_table. */
2440 fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2441 fde_table_allocated = FDE_TABLE_INCREMENT;
2442 fde_table_in_use = 0;
2444 /* Generate the CFA instructions common to all FDE's. Do it now for the
2445 sake of lookup_cfa. */
2447 #ifdef DWARF2_UNWIND_INFO
2448 /* On entry, the Canonical Frame Address is at SP. */
2449 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2450 initial_return_save (INCOMING_RETURN_ADDR_RTX);
2451 #endif
2454 void
2455 dwarf2out_frame_finish (void)
2457 /* Output call frame information. */
2458 if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
2459 output_call_frame_info (0);
2461 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2462 output_call_frame_info (1);
2464 #endif
2466 /* And now, the subset of the debugging information support code necessary
2467 for emitting location expressions. */
2469 /* We need some way to distinguish DW_OP_addr with a direct symbol
2470 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
2471 #define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
2474 typedef struct dw_val_struct *dw_val_ref;
2475 typedef struct die_struct *dw_die_ref;
2476 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2477 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2479 /* Each DIE may have a series of attribute/value pairs. Values
2480 can take on several forms. The forms that are used in this
2481 implementation are listed below. */
2483 enum dw_val_class
2485 dw_val_class_addr,
2486 dw_val_class_offset,
2487 dw_val_class_loc,
2488 dw_val_class_loc_list,
2489 dw_val_class_range_list,
2490 dw_val_class_const,
2491 dw_val_class_unsigned_const,
2492 dw_val_class_long_long,
2493 dw_val_class_vec,
2494 dw_val_class_flag,
2495 dw_val_class_die_ref,
2496 dw_val_class_fde_ref,
2497 dw_val_class_lbl_id,
2498 dw_val_class_lbl_offset,
2499 dw_val_class_str
2502 /* Describe a double word constant value. */
2503 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
2505 typedef struct dw_long_long_struct GTY(())
2507 unsigned long hi;
2508 unsigned long low;
2510 dw_long_long_const;
2512 /* Describe a floating point constant value, or a vector constant value. */
2514 typedef struct dw_vec_struct GTY(())
2516 unsigned char * GTY((length ("%h.length"))) array;
2517 unsigned length;
2518 unsigned elt_size;
2520 dw_vec_const;
2522 /* The dw_val_node describes an attribute's value, as it is
2523 represented internally. */
2525 typedef struct dw_val_struct GTY(())
2527 enum dw_val_class val_class;
2528 union dw_val_struct_union
2530 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2531 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2532 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2533 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2534 HOST_WIDE_INT GTY ((default)) val_int;
2535 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2536 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2537 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2538 struct dw_val_die_union
2540 dw_die_ref die;
2541 int external;
2542 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2543 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2544 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2545 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2546 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2548 GTY ((desc ("%1.val_class"))) v;
2550 dw_val_node;
2552 /* Locations in memory are described using a sequence of stack machine
2553 operations. */
2555 typedef struct dw_loc_descr_struct GTY(())
2557 dw_loc_descr_ref dw_loc_next;
2558 enum dwarf_location_atom dw_loc_opc;
2559 dw_val_node dw_loc_oprnd1;
2560 dw_val_node dw_loc_oprnd2;
2561 int dw_loc_addr;
2563 dw_loc_descr_node;
2565 /* Location lists are ranges + location descriptions for that range,
2566 so you can track variables that are in different places over
2567 their entire life. */
2568 typedef struct dw_loc_list_struct GTY(())
2570 dw_loc_list_ref dw_loc_next;
2571 const char *begin; /* Label for begin address of range */
2572 const char *end; /* Label for end address of range */
2573 char *ll_symbol; /* Label for beginning of location list.
2574 Only on head of list */
2575 const char *section; /* Section this loclist is relative to */
2576 dw_loc_descr_ref expr;
2577 } dw_loc_list_node;
2579 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2581 static const char *dwarf_stack_op_name (unsigned);
2582 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2583 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2584 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2585 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2586 static unsigned long size_of_locs (dw_loc_descr_ref);
2587 static void output_loc_operands (dw_loc_descr_ref);
2588 static void output_loc_sequence (dw_loc_descr_ref);
2590 /* Convert a DWARF stack opcode into its string name. */
2592 static const char *
2593 dwarf_stack_op_name (unsigned int op)
2595 switch (op)
2597 case DW_OP_addr:
2598 case INTERNAL_DW_OP_tls_addr:
2599 return "DW_OP_addr";
2600 case DW_OP_deref:
2601 return "DW_OP_deref";
2602 case DW_OP_const1u:
2603 return "DW_OP_const1u";
2604 case DW_OP_const1s:
2605 return "DW_OP_const1s";
2606 case DW_OP_const2u:
2607 return "DW_OP_const2u";
2608 case DW_OP_const2s:
2609 return "DW_OP_const2s";
2610 case DW_OP_const4u:
2611 return "DW_OP_const4u";
2612 case DW_OP_const4s:
2613 return "DW_OP_const4s";
2614 case DW_OP_const8u:
2615 return "DW_OP_const8u";
2616 case DW_OP_const8s:
2617 return "DW_OP_const8s";
2618 case DW_OP_constu:
2619 return "DW_OP_constu";
2620 case DW_OP_consts:
2621 return "DW_OP_consts";
2622 case DW_OP_dup:
2623 return "DW_OP_dup";
2624 case DW_OP_drop:
2625 return "DW_OP_drop";
2626 case DW_OP_over:
2627 return "DW_OP_over";
2628 case DW_OP_pick:
2629 return "DW_OP_pick";
2630 case DW_OP_swap:
2631 return "DW_OP_swap";
2632 case DW_OP_rot:
2633 return "DW_OP_rot";
2634 case DW_OP_xderef:
2635 return "DW_OP_xderef";
2636 case DW_OP_abs:
2637 return "DW_OP_abs";
2638 case DW_OP_and:
2639 return "DW_OP_and";
2640 case DW_OP_div:
2641 return "DW_OP_div";
2642 case DW_OP_minus:
2643 return "DW_OP_minus";
2644 case DW_OP_mod:
2645 return "DW_OP_mod";
2646 case DW_OP_mul:
2647 return "DW_OP_mul";
2648 case DW_OP_neg:
2649 return "DW_OP_neg";
2650 case DW_OP_not:
2651 return "DW_OP_not";
2652 case DW_OP_or:
2653 return "DW_OP_or";
2654 case DW_OP_plus:
2655 return "DW_OP_plus";
2656 case DW_OP_plus_uconst:
2657 return "DW_OP_plus_uconst";
2658 case DW_OP_shl:
2659 return "DW_OP_shl";
2660 case DW_OP_shr:
2661 return "DW_OP_shr";
2662 case DW_OP_shra:
2663 return "DW_OP_shra";
2664 case DW_OP_xor:
2665 return "DW_OP_xor";
2666 case DW_OP_bra:
2667 return "DW_OP_bra";
2668 case DW_OP_eq:
2669 return "DW_OP_eq";
2670 case DW_OP_ge:
2671 return "DW_OP_ge";
2672 case DW_OP_gt:
2673 return "DW_OP_gt";
2674 case DW_OP_le:
2675 return "DW_OP_le";
2676 case DW_OP_lt:
2677 return "DW_OP_lt";
2678 case DW_OP_ne:
2679 return "DW_OP_ne";
2680 case DW_OP_skip:
2681 return "DW_OP_skip";
2682 case DW_OP_lit0:
2683 return "DW_OP_lit0";
2684 case DW_OP_lit1:
2685 return "DW_OP_lit1";
2686 case DW_OP_lit2:
2687 return "DW_OP_lit2";
2688 case DW_OP_lit3:
2689 return "DW_OP_lit3";
2690 case DW_OP_lit4:
2691 return "DW_OP_lit4";
2692 case DW_OP_lit5:
2693 return "DW_OP_lit5";
2694 case DW_OP_lit6:
2695 return "DW_OP_lit6";
2696 case DW_OP_lit7:
2697 return "DW_OP_lit7";
2698 case DW_OP_lit8:
2699 return "DW_OP_lit8";
2700 case DW_OP_lit9:
2701 return "DW_OP_lit9";
2702 case DW_OP_lit10:
2703 return "DW_OP_lit10";
2704 case DW_OP_lit11:
2705 return "DW_OP_lit11";
2706 case DW_OP_lit12:
2707 return "DW_OP_lit12";
2708 case DW_OP_lit13:
2709 return "DW_OP_lit13";
2710 case DW_OP_lit14:
2711 return "DW_OP_lit14";
2712 case DW_OP_lit15:
2713 return "DW_OP_lit15";
2714 case DW_OP_lit16:
2715 return "DW_OP_lit16";
2716 case DW_OP_lit17:
2717 return "DW_OP_lit17";
2718 case DW_OP_lit18:
2719 return "DW_OP_lit18";
2720 case DW_OP_lit19:
2721 return "DW_OP_lit19";
2722 case DW_OP_lit20:
2723 return "DW_OP_lit20";
2724 case DW_OP_lit21:
2725 return "DW_OP_lit21";
2726 case DW_OP_lit22:
2727 return "DW_OP_lit22";
2728 case DW_OP_lit23:
2729 return "DW_OP_lit23";
2730 case DW_OP_lit24:
2731 return "DW_OP_lit24";
2732 case DW_OP_lit25:
2733 return "DW_OP_lit25";
2734 case DW_OP_lit26:
2735 return "DW_OP_lit26";
2736 case DW_OP_lit27:
2737 return "DW_OP_lit27";
2738 case DW_OP_lit28:
2739 return "DW_OP_lit28";
2740 case DW_OP_lit29:
2741 return "DW_OP_lit29";
2742 case DW_OP_lit30:
2743 return "DW_OP_lit30";
2744 case DW_OP_lit31:
2745 return "DW_OP_lit31";
2746 case DW_OP_reg0:
2747 return "DW_OP_reg0";
2748 case DW_OP_reg1:
2749 return "DW_OP_reg1";
2750 case DW_OP_reg2:
2751 return "DW_OP_reg2";
2752 case DW_OP_reg3:
2753 return "DW_OP_reg3";
2754 case DW_OP_reg4:
2755 return "DW_OP_reg4";
2756 case DW_OP_reg5:
2757 return "DW_OP_reg5";
2758 case DW_OP_reg6:
2759 return "DW_OP_reg6";
2760 case DW_OP_reg7:
2761 return "DW_OP_reg7";
2762 case DW_OP_reg8:
2763 return "DW_OP_reg8";
2764 case DW_OP_reg9:
2765 return "DW_OP_reg9";
2766 case DW_OP_reg10:
2767 return "DW_OP_reg10";
2768 case DW_OP_reg11:
2769 return "DW_OP_reg11";
2770 case DW_OP_reg12:
2771 return "DW_OP_reg12";
2772 case DW_OP_reg13:
2773 return "DW_OP_reg13";
2774 case DW_OP_reg14:
2775 return "DW_OP_reg14";
2776 case DW_OP_reg15:
2777 return "DW_OP_reg15";
2778 case DW_OP_reg16:
2779 return "DW_OP_reg16";
2780 case DW_OP_reg17:
2781 return "DW_OP_reg17";
2782 case DW_OP_reg18:
2783 return "DW_OP_reg18";
2784 case DW_OP_reg19:
2785 return "DW_OP_reg19";
2786 case DW_OP_reg20:
2787 return "DW_OP_reg20";
2788 case DW_OP_reg21:
2789 return "DW_OP_reg21";
2790 case DW_OP_reg22:
2791 return "DW_OP_reg22";
2792 case DW_OP_reg23:
2793 return "DW_OP_reg23";
2794 case DW_OP_reg24:
2795 return "DW_OP_reg24";
2796 case DW_OP_reg25:
2797 return "DW_OP_reg25";
2798 case DW_OP_reg26:
2799 return "DW_OP_reg26";
2800 case DW_OP_reg27:
2801 return "DW_OP_reg27";
2802 case DW_OP_reg28:
2803 return "DW_OP_reg28";
2804 case DW_OP_reg29:
2805 return "DW_OP_reg29";
2806 case DW_OP_reg30:
2807 return "DW_OP_reg30";
2808 case DW_OP_reg31:
2809 return "DW_OP_reg31";
2810 case DW_OP_breg0:
2811 return "DW_OP_breg0";
2812 case DW_OP_breg1:
2813 return "DW_OP_breg1";
2814 case DW_OP_breg2:
2815 return "DW_OP_breg2";
2816 case DW_OP_breg3:
2817 return "DW_OP_breg3";
2818 case DW_OP_breg4:
2819 return "DW_OP_breg4";
2820 case DW_OP_breg5:
2821 return "DW_OP_breg5";
2822 case DW_OP_breg6:
2823 return "DW_OP_breg6";
2824 case DW_OP_breg7:
2825 return "DW_OP_breg7";
2826 case DW_OP_breg8:
2827 return "DW_OP_breg8";
2828 case DW_OP_breg9:
2829 return "DW_OP_breg9";
2830 case DW_OP_breg10:
2831 return "DW_OP_breg10";
2832 case DW_OP_breg11:
2833 return "DW_OP_breg11";
2834 case DW_OP_breg12:
2835 return "DW_OP_breg12";
2836 case DW_OP_breg13:
2837 return "DW_OP_breg13";
2838 case DW_OP_breg14:
2839 return "DW_OP_breg14";
2840 case DW_OP_breg15:
2841 return "DW_OP_breg15";
2842 case DW_OP_breg16:
2843 return "DW_OP_breg16";
2844 case DW_OP_breg17:
2845 return "DW_OP_breg17";
2846 case DW_OP_breg18:
2847 return "DW_OP_breg18";
2848 case DW_OP_breg19:
2849 return "DW_OP_breg19";
2850 case DW_OP_breg20:
2851 return "DW_OP_breg20";
2852 case DW_OP_breg21:
2853 return "DW_OP_breg21";
2854 case DW_OP_breg22:
2855 return "DW_OP_breg22";
2856 case DW_OP_breg23:
2857 return "DW_OP_breg23";
2858 case DW_OP_breg24:
2859 return "DW_OP_breg24";
2860 case DW_OP_breg25:
2861 return "DW_OP_breg25";
2862 case DW_OP_breg26:
2863 return "DW_OP_breg26";
2864 case DW_OP_breg27:
2865 return "DW_OP_breg27";
2866 case DW_OP_breg28:
2867 return "DW_OP_breg28";
2868 case DW_OP_breg29:
2869 return "DW_OP_breg29";
2870 case DW_OP_breg30:
2871 return "DW_OP_breg30";
2872 case DW_OP_breg31:
2873 return "DW_OP_breg31";
2874 case DW_OP_regx:
2875 return "DW_OP_regx";
2876 case DW_OP_fbreg:
2877 return "DW_OP_fbreg";
2878 case DW_OP_bregx:
2879 return "DW_OP_bregx";
2880 case DW_OP_piece:
2881 return "DW_OP_piece";
2882 case DW_OP_deref_size:
2883 return "DW_OP_deref_size";
2884 case DW_OP_xderef_size:
2885 return "DW_OP_xderef_size";
2886 case DW_OP_nop:
2887 return "DW_OP_nop";
2888 case DW_OP_push_object_address:
2889 return "DW_OP_push_object_address";
2890 case DW_OP_call2:
2891 return "DW_OP_call2";
2892 case DW_OP_call4:
2893 return "DW_OP_call4";
2894 case DW_OP_call_ref:
2895 return "DW_OP_call_ref";
2896 case DW_OP_GNU_push_tls_address:
2897 return "DW_OP_GNU_push_tls_address";
2898 default:
2899 return "OP_<unknown>";
2903 /* Return a pointer to a newly allocated location description. Location
2904 descriptions are simple expression terms that can be strung
2905 together to form more complicated location (address) descriptions. */
2907 static inline dw_loc_descr_ref
2908 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
2909 unsigned HOST_WIDE_INT oprnd2)
2911 dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
2913 descr->dw_loc_opc = op;
2914 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
2915 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
2916 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
2917 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
2919 return descr;
2923 /* Add a location description term to a location description expression. */
2925 static inline void
2926 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
2928 dw_loc_descr_ref *d;
2930 /* Find the end of the chain. */
2931 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
2934 *d = descr;
2937 /* Return the size of a location descriptor. */
2939 static unsigned long
2940 size_of_loc_descr (dw_loc_descr_ref loc)
2942 unsigned long size = 1;
2944 switch (loc->dw_loc_opc)
2946 case DW_OP_addr:
2947 case INTERNAL_DW_OP_tls_addr:
2948 size += DWARF2_ADDR_SIZE;
2949 break;
2950 case DW_OP_const1u:
2951 case DW_OP_const1s:
2952 size += 1;
2953 break;
2954 case DW_OP_const2u:
2955 case DW_OP_const2s:
2956 size += 2;
2957 break;
2958 case DW_OP_const4u:
2959 case DW_OP_const4s:
2960 size += 4;
2961 break;
2962 case DW_OP_const8u:
2963 case DW_OP_const8s:
2964 size += 8;
2965 break;
2966 case DW_OP_constu:
2967 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2968 break;
2969 case DW_OP_consts:
2970 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2971 break;
2972 case DW_OP_pick:
2973 size += 1;
2974 break;
2975 case DW_OP_plus_uconst:
2976 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2977 break;
2978 case DW_OP_skip:
2979 case DW_OP_bra:
2980 size += 2;
2981 break;
2982 case DW_OP_breg0:
2983 case DW_OP_breg1:
2984 case DW_OP_breg2:
2985 case DW_OP_breg3:
2986 case DW_OP_breg4:
2987 case DW_OP_breg5:
2988 case DW_OP_breg6:
2989 case DW_OP_breg7:
2990 case DW_OP_breg8:
2991 case DW_OP_breg9:
2992 case DW_OP_breg10:
2993 case DW_OP_breg11:
2994 case DW_OP_breg12:
2995 case DW_OP_breg13:
2996 case DW_OP_breg14:
2997 case DW_OP_breg15:
2998 case DW_OP_breg16:
2999 case DW_OP_breg17:
3000 case DW_OP_breg18:
3001 case DW_OP_breg19:
3002 case DW_OP_breg20:
3003 case DW_OP_breg21:
3004 case DW_OP_breg22:
3005 case DW_OP_breg23:
3006 case DW_OP_breg24:
3007 case DW_OP_breg25:
3008 case DW_OP_breg26:
3009 case DW_OP_breg27:
3010 case DW_OP_breg28:
3011 case DW_OP_breg29:
3012 case DW_OP_breg30:
3013 case DW_OP_breg31:
3014 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3015 break;
3016 case DW_OP_regx:
3017 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3018 break;
3019 case DW_OP_fbreg:
3020 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3021 break;
3022 case DW_OP_bregx:
3023 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3024 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3025 break;
3026 case DW_OP_piece:
3027 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3028 break;
3029 case DW_OP_deref_size:
3030 case DW_OP_xderef_size:
3031 size += 1;
3032 break;
3033 case DW_OP_call2:
3034 size += 2;
3035 break;
3036 case DW_OP_call4:
3037 size += 4;
3038 break;
3039 case DW_OP_call_ref:
3040 size += DWARF2_ADDR_SIZE;
3041 break;
3042 default:
3043 break;
3046 return size;
3049 /* Return the size of a series of location descriptors. */
3051 static unsigned long
3052 size_of_locs (dw_loc_descr_ref loc)
3054 unsigned long size;
3056 for (size = 0; loc != NULL; loc = loc->dw_loc_next)
3058 loc->dw_loc_addr = size;
3059 size += size_of_loc_descr (loc);
3062 return size;
3065 /* Output location description stack opcode's operands (if any). */
3067 static void
3068 output_loc_operands (dw_loc_descr_ref loc)
3070 dw_val_ref val1 = &loc->dw_loc_oprnd1;
3071 dw_val_ref val2 = &loc->dw_loc_oprnd2;
3073 switch (loc->dw_loc_opc)
3075 #ifdef DWARF2_DEBUGGING_INFO
3076 case DW_OP_addr:
3077 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3078 break;
3079 case DW_OP_const2u:
3080 case DW_OP_const2s:
3081 dw2_asm_output_data (2, val1->v.val_int, NULL);
3082 break;
3083 case DW_OP_const4u:
3084 case DW_OP_const4s:
3085 dw2_asm_output_data (4, val1->v.val_int, NULL);
3086 break;
3087 case DW_OP_const8u:
3088 case DW_OP_const8s:
3089 if (HOST_BITS_PER_LONG < 64)
3090 abort ();
3091 dw2_asm_output_data (8, val1->v.val_int, NULL);
3092 break;
3093 case DW_OP_skip:
3094 case DW_OP_bra:
3096 int offset;
3098 if (val1->val_class == dw_val_class_loc)
3099 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3100 else
3101 abort ();
3103 dw2_asm_output_data (2, offset, NULL);
3105 break;
3106 #else
3107 case DW_OP_addr:
3108 case DW_OP_const2u:
3109 case DW_OP_const2s:
3110 case DW_OP_const4u:
3111 case DW_OP_const4s:
3112 case DW_OP_const8u:
3113 case DW_OP_const8s:
3114 case DW_OP_skip:
3115 case DW_OP_bra:
3116 /* We currently don't make any attempt to make sure these are
3117 aligned properly like we do for the main unwind info, so
3118 don't support emitting things larger than a byte if we're
3119 only doing unwinding. */
3120 abort ();
3121 #endif
3122 case DW_OP_const1u:
3123 case DW_OP_const1s:
3124 dw2_asm_output_data (1, val1->v.val_int, NULL);
3125 break;
3126 case DW_OP_constu:
3127 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3128 break;
3129 case DW_OP_consts:
3130 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3131 break;
3132 case DW_OP_pick:
3133 dw2_asm_output_data (1, val1->v.val_int, NULL);
3134 break;
3135 case DW_OP_plus_uconst:
3136 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3137 break;
3138 case DW_OP_breg0:
3139 case DW_OP_breg1:
3140 case DW_OP_breg2:
3141 case DW_OP_breg3:
3142 case DW_OP_breg4:
3143 case DW_OP_breg5:
3144 case DW_OP_breg6:
3145 case DW_OP_breg7:
3146 case DW_OP_breg8:
3147 case DW_OP_breg9:
3148 case DW_OP_breg10:
3149 case DW_OP_breg11:
3150 case DW_OP_breg12:
3151 case DW_OP_breg13:
3152 case DW_OP_breg14:
3153 case DW_OP_breg15:
3154 case DW_OP_breg16:
3155 case DW_OP_breg17:
3156 case DW_OP_breg18:
3157 case DW_OP_breg19:
3158 case DW_OP_breg20:
3159 case DW_OP_breg21:
3160 case DW_OP_breg22:
3161 case DW_OP_breg23:
3162 case DW_OP_breg24:
3163 case DW_OP_breg25:
3164 case DW_OP_breg26:
3165 case DW_OP_breg27:
3166 case DW_OP_breg28:
3167 case DW_OP_breg29:
3168 case DW_OP_breg30:
3169 case DW_OP_breg31:
3170 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3171 break;
3172 case DW_OP_regx:
3173 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3174 break;
3175 case DW_OP_fbreg:
3176 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3177 break;
3178 case DW_OP_bregx:
3179 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3180 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3181 break;
3182 case DW_OP_piece:
3183 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3184 break;
3185 case DW_OP_deref_size:
3186 case DW_OP_xderef_size:
3187 dw2_asm_output_data (1, val1->v.val_int, NULL);
3188 break;
3190 case INTERNAL_DW_OP_tls_addr:
3191 #ifdef ASM_OUTPUT_DWARF_DTPREL
3192 ASM_OUTPUT_DWARF_DTPREL (asm_out_file, DWARF2_ADDR_SIZE,
3193 val1->v.val_addr);
3194 fputc ('\n', asm_out_file);
3195 #else
3196 abort ();
3197 #endif
3198 break;
3200 default:
3201 /* Other codes have no operands. */
3202 break;
3206 /* Output a sequence of location operations. */
3208 static void
3209 output_loc_sequence (dw_loc_descr_ref loc)
3211 for (; loc != NULL; loc = loc->dw_loc_next)
3213 /* Output the opcode. */
3214 dw2_asm_output_data (1, loc->dw_loc_opc,
3215 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3217 /* Output the operand(s) (if any). */
3218 output_loc_operands (loc);
3222 /* This routine will generate the correct assembly data for a location
3223 description based on a cfi entry with a complex address. */
3225 static void
3226 output_cfa_loc (dw_cfi_ref cfi)
3228 dw_loc_descr_ref loc;
3229 unsigned long size;
3231 /* Output the size of the block. */
3232 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3233 size = size_of_locs (loc);
3234 dw2_asm_output_data_uleb128 (size, NULL);
3236 /* Now output the operations themselves. */
3237 output_loc_sequence (loc);
3240 /* This function builds a dwarf location descriptor sequence from
3241 a dw_cfa_location. */
3243 static struct dw_loc_descr_struct *
3244 build_cfa_loc (dw_cfa_location *cfa)
3246 struct dw_loc_descr_struct *head, *tmp;
3248 if (cfa->indirect == 0)
3249 abort ();
3251 if (cfa->base_offset)
3253 if (cfa->reg <= 31)
3254 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3255 else
3256 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3258 else if (cfa->reg <= 31)
3259 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3260 else
3261 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3263 head->dw_loc_oprnd1.val_class = dw_val_class_const;
3264 tmp = new_loc_descr (DW_OP_deref, 0, 0);
3265 add_loc_descr (&head, tmp);
3266 if (cfa->offset != 0)
3268 tmp = new_loc_descr (DW_OP_plus_uconst, cfa->offset, 0);
3269 add_loc_descr (&head, tmp);
3272 return head;
3275 /* This function fills in aa dw_cfa_location structure from a dwarf location
3276 descriptor sequence. */
3278 static void
3279 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3281 struct dw_loc_descr_struct *ptr;
3282 cfa->offset = 0;
3283 cfa->base_offset = 0;
3284 cfa->indirect = 0;
3285 cfa->reg = -1;
3287 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3289 enum dwarf_location_atom op = ptr->dw_loc_opc;
3291 switch (op)
3293 case DW_OP_reg0:
3294 case DW_OP_reg1:
3295 case DW_OP_reg2:
3296 case DW_OP_reg3:
3297 case DW_OP_reg4:
3298 case DW_OP_reg5:
3299 case DW_OP_reg6:
3300 case DW_OP_reg7:
3301 case DW_OP_reg8:
3302 case DW_OP_reg9:
3303 case DW_OP_reg10:
3304 case DW_OP_reg11:
3305 case DW_OP_reg12:
3306 case DW_OP_reg13:
3307 case DW_OP_reg14:
3308 case DW_OP_reg15:
3309 case DW_OP_reg16:
3310 case DW_OP_reg17:
3311 case DW_OP_reg18:
3312 case DW_OP_reg19:
3313 case DW_OP_reg20:
3314 case DW_OP_reg21:
3315 case DW_OP_reg22:
3316 case DW_OP_reg23:
3317 case DW_OP_reg24:
3318 case DW_OP_reg25:
3319 case DW_OP_reg26:
3320 case DW_OP_reg27:
3321 case DW_OP_reg28:
3322 case DW_OP_reg29:
3323 case DW_OP_reg30:
3324 case DW_OP_reg31:
3325 cfa->reg = op - DW_OP_reg0;
3326 break;
3327 case DW_OP_regx:
3328 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3329 break;
3330 case DW_OP_breg0:
3331 case DW_OP_breg1:
3332 case DW_OP_breg2:
3333 case DW_OP_breg3:
3334 case DW_OP_breg4:
3335 case DW_OP_breg5:
3336 case DW_OP_breg6:
3337 case DW_OP_breg7:
3338 case DW_OP_breg8:
3339 case DW_OP_breg9:
3340 case DW_OP_breg10:
3341 case DW_OP_breg11:
3342 case DW_OP_breg12:
3343 case DW_OP_breg13:
3344 case DW_OP_breg14:
3345 case DW_OP_breg15:
3346 case DW_OP_breg16:
3347 case DW_OP_breg17:
3348 case DW_OP_breg18:
3349 case DW_OP_breg19:
3350 case DW_OP_breg20:
3351 case DW_OP_breg21:
3352 case DW_OP_breg22:
3353 case DW_OP_breg23:
3354 case DW_OP_breg24:
3355 case DW_OP_breg25:
3356 case DW_OP_breg26:
3357 case DW_OP_breg27:
3358 case DW_OP_breg28:
3359 case DW_OP_breg29:
3360 case DW_OP_breg30:
3361 case DW_OP_breg31:
3362 cfa->reg = op - DW_OP_breg0;
3363 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3364 break;
3365 case DW_OP_bregx:
3366 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3367 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3368 break;
3369 case DW_OP_deref:
3370 cfa->indirect = 1;
3371 break;
3372 case DW_OP_plus_uconst:
3373 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3374 break;
3375 default:
3376 internal_error ("DW_LOC_OP %s not implemented\n",
3377 dwarf_stack_op_name (ptr->dw_loc_opc));
3381 #endif /* .debug_frame support */
3383 /* And now, the support for symbolic debugging information. */
3384 #ifdef DWARF2_DEBUGGING_INFO
3386 /* .debug_str support. */
3387 static int output_indirect_string (void **, void *);
3389 static void dwarf2out_init (const char *);
3390 static void dwarf2out_finish (const char *);
3391 static void dwarf2out_define (unsigned int, const char *);
3392 static void dwarf2out_undef (unsigned int, const char *);
3393 static void dwarf2out_start_source_file (unsigned, const char *);
3394 static void dwarf2out_end_source_file (unsigned);
3395 static void dwarf2out_begin_block (unsigned, unsigned);
3396 static void dwarf2out_end_block (unsigned, unsigned);
3397 static bool dwarf2out_ignore_block (tree);
3398 static void dwarf2out_global_decl (tree);
3399 static void dwarf2out_type_decl (tree, int);
3400 static void dwarf2out_imported_module_or_decl (tree, tree);
3401 static void dwarf2out_abstract_function (tree);
3402 static void dwarf2out_var_location (rtx);
3403 static void dwarf2out_begin_function (tree);
3405 /* The debug hooks structure. */
3407 const struct gcc_debug_hooks dwarf2_debug_hooks =
3409 dwarf2out_init,
3410 dwarf2out_finish,
3411 dwarf2out_define,
3412 dwarf2out_undef,
3413 dwarf2out_start_source_file,
3414 dwarf2out_end_source_file,
3415 dwarf2out_begin_block,
3416 dwarf2out_end_block,
3417 dwarf2out_ignore_block,
3418 dwarf2out_source_line,
3419 dwarf2out_begin_prologue,
3420 debug_nothing_int_charstar, /* end_prologue */
3421 dwarf2out_end_epilogue,
3422 dwarf2out_begin_function,
3423 debug_nothing_int, /* end_function */
3424 dwarf2out_decl, /* function_decl */
3425 dwarf2out_global_decl,
3426 dwarf2out_type_decl, /* type_decl */
3427 dwarf2out_imported_module_or_decl,
3428 debug_nothing_tree, /* deferred_inline_function */
3429 /* The DWARF 2 backend tries to reduce debugging bloat by not
3430 emitting the abstract description of inline functions until
3431 something tries to reference them. */
3432 dwarf2out_abstract_function, /* outlining_inline_function */
3433 debug_nothing_rtx, /* label */
3434 debug_nothing_int, /* handle_pch */
3435 dwarf2out_var_location
3437 #endif
3439 /* NOTE: In the comments in this file, many references are made to
3440 "Debugging Information Entries". This term is abbreviated as `DIE'
3441 throughout the remainder of this file. */
3443 /* An internal representation of the DWARF output is built, and then
3444 walked to generate the DWARF debugging info. The walk of the internal
3445 representation is done after the entire program has been compiled.
3446 The types below are used to describe the internal representation. */
3448 /* Various DIE's use offsets relative to the beginning of the
3449 .debug_info section to refer to each other. */
3451 typedef long int dw_offset;
3453 /* Define typedefs here to avoid circular dependencies. */
3455 typedef struct dw_attr_struct *dw_attr_ref;
3456 typedef struct dw_line_info_struct *dw_line_info_ref;
3457 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3458 typedef struct pubname_struct *pubname_ref;
3459 typedef struct dw_ranges_struct *dw_ranges_ref;
3461 /* Each entry in the line_info_table maintains the file and
3462 line number associated with the label generated for that
3463 entry. The label gives the PC value associated with
3464 the line number entry. */
3466 typedef struct dw_line_info_struct GTY(())
3468 unsigned long dw_file_num;
3469 unsigned long dw_line_num;
3471 dw_line_info_entry;
3473 /* Line information for functions in separate sections; each one gets its
3474 own sequence. */
3475 typedef struct dw_separate_line_info_struct GTY(())
3477 unsigned long dw_file_num;
3478 unsigned long dw_line_num;
3479 unsigned long function;
3481 dw_separate_line_info_entry;
3483 /* Each DIE attribute has a field specifying the attribute kind,
3484 a link to the next attribute in the chain, and an attribute value.
3485 Attributes are typically linked below the DIE they modify. */
3487 typedef struct dw_attr_struct GTY(())
3489 enum dwarf_attribute dw_attr;
3490 dw_attr_ref dw_attr_next;
3491 dw_val_node dw_attr_val;
3493 dw_attr_node;
3495 /* The Debugging Information Entry (DIE) structure */
3497 typedef struct die_struct GTY(())
3499 enum dwarf_tag die_tag;
3500 char *die_symbol;
3501 dw_attr_ref die_attr;
3502 dw_die_ref die_parent;
3503 dw_die_ref die_child;
3504 dw_die_ref die_sib;
3505 dw_die_ref die_definition; /* ref from a specification to its definition */
3506 dw_offset die_offset;
3507 unsigned long die_abbrev;
3508 int die_mark;
3509 unsigned int decl_id;
3511 die_node;
3513 /* The pubname structure */
3515 typedef struct pubname_struct GTY(())
3517 dw_die_ref die;
3518 char *name;
3520 pubname_entry;
3522 struct dw_ranges_struct GTY(())
3524 int block_num;
3527 /* The limbo die list structure. */
3528 typedef struct limbo_die_struct GTY(())
3530 dw_die_ref die;
3531 tree created_for;
3532 struct limbo_die_struct *next;
3534 limbo_die_node;
3536 /* How to start an assembler comment. */
3537 #ifndef ASM_COMMENT_START
3538 #define ASM_COMMENT_START ";#"
3539 #endif
3541 /* Define a macro which returns nonzero for a TYPE_DECL which was
3542 implicitly generated for a tagged type.
3544 Note that unlike the gcc front end (which generates a NULL named
3545 TYPE_DECL node for each complete tagged type, each array type, and
3546 each function type node created) the g++ front end generates a
3547 _named_ TYPE_DECL node for each tagged type node created.
3548 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3549 generate a DW_TAG_typedef DIE for them. */
3551 #define TYPE_DECL_IS_STUB(decl) \
3552 (DECL_NAME (decl) == NULL_TREE \
3553 || (DECL_ARTIFICIAL (decl) \
3554 && is_tagged_type (TREE_TYPE (decl)) \
3555 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3556 /* This is necessary for stub decls that \
3557 appear in nested inline functions. */ \
3558 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3559 && (decl_ultimate_origin (decl) \
3560 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3562 /* Information concerning the compilation unit's programming
3563 language, and compiler version. */
3565 /* Fixed size portion of the DWARF compilation unit header. */
3566 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3567 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3569 /* Fixed size portion of public names info. */
3570 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3572 /* Fixed size portion of the address range info. */
3573 #define DWARF_ARANGES_HEADER_SIZE \
3574 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3575 DWARF2_ADDR_SIZE * 2) \
3576 - DWARF_INITIAL_LENGTH_SIZE)
3578 /* Size of padding portion in the address range info. It must be
3579 aligned to twice the pointer size. */
3580 #define DWARF_ARANGES_PAD_SIZE \
3581 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3582 DWARF2_ADDR_SIZE * 2) \
3583 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3585 /* Use assembler line directives if available. */
3586 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3587 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3588 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3589 #else
3590 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3591 #endif
3592 #endif
3594 /* Minimum line offset in a special line info. opcode.
3595 This value was chosen to give a reasonable range of values. */
3596 #define DWARF_LINE_BASE -10
3598 /* First special line opcode - leave room for the standard opcodes. */
3599 #define DWARF_LINE_OPCODE_BASE 10
3601 /* Range of line offsets in a special line info. opcode. */
3602 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3604 /* Flag that indicates the initial value of the is_stmt_start flag.
3605 In the present implementation, we do not mark any lines as
3606 the beginning of a source statement, because that information
3607 is not made available by the GCC front-end. */
3608 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3610 #ifdef DWARF2_DEBUGGING_INFO
3611 /* This location is used by calc_die_sizes() to keep track
3612 the offset of each DIE within the .debug_info section. */
3613 static unsigned long next_die_offset;
3614 #endif
3616 /* Record the root of the DIE's built for the current compilation unit. */
3617 static GTY(()) dw_die_ref comp_unit_die;
3619 /* A list of DIEs with a NULL parent waiting to be relocated. */
3620 static GTY(()) limbo_die_node *limbo_die_list;
3622 /* Filenames referenced by this compilation unit. */
3623 static GTY(()) varray_type file_table;
3624 static GTY(()) varray_type file_table_emitted;
3625 static GTY(()) size_t file_table_last_lookup_index;
3627 /* A hash table of references to DIE's that describe declarations.
3628 The key is a DECL_UID() which is a unique number identifying each decl. */
3629 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3631 /* Node of the variable location list. */
3632 struct var_loc_node GTY ((chain_next ("%h.next")))
3634 rtx GTY (()) var_loc_note;
3635 const char * GTY (()) label;
3636 struct var_loc_node * GTY (()) next;
3639 /* Variable location list. */
3640 struct var_loc_list_def GTY (())
3642 struct var_loc_node * GTY (()) first;
3644 /* Do not mark the last element of the chained list because
3645 it is marked through the chain. */
3646 struct var_loc_node * GTY ((skip ("%h"))) last;
3648 /* DECL_UID of the variable decl. */
3649 unsigned int decl_id;
3651 typedef struct var_loc_list_def var_loc_list;
3654 /* Table of decl location linked lists. */
3655 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3657 /* A pointer to the base of a list of references to DIE's that
3658 are uniquely identified by their tag, presence/absence of
3659 children DIE's, and list of attribute/value pairs. */
3660 static GTY((length ("abbrev_die_table_allocated")))
3661 dw_die_ref *abbrev_die_table;
3663 /* Number of elements currently allocated for abbrev_die_table. */
3664 static GTY(()) unsigned abbrev_die_table_allocated;
3666 /* Number of elements in type_die_table currently in use. */
3667 static GTY(()) unsigned abbrev_die_table_in_use;
3669 /* Size (in elements) of increments by which we may expand the
3670 abbrev_die_table. */
3671 #define ABBREV_DIE_TABLE_INCREMENT 256
3673 /* A pointer to the base of a table that contains line information
3674 for each source code line in .text in the compilation unit. */
3675 static GTY((length ("line_info_table_allocated")))
3676 dw_line_info_ref line_info_table;
3678 /* Number of elements currently allocated for line_info_table. */
3679 static GTY(()) unsigned line_info_table_allocated;
3681 /* Number of elements in line_info_table currently in use. */
3682 static GTY(()) unsigned line_info_table_in_use;
3684 /* A pointer to the base of a table that contains line information
3685 for each source code line outside of .text in the compilation unit. */
3686 static GTY ((length ("separate_line_info_table_allocated")))
3687 dw_separate_line_info_ref separate_line_info_table;
3689 /* Number of elements currently allocated for separate_line_info_table. */
3690 static GTY(()) unsigned separate_line_info_table_allocated;
3692 /* Number of elements in separate_line_info_table currently in use. */
3693 static GTY(()) unsigned separate_line_info_table_in_use;
3695 /* Size (in elements) of increments by which we may expand the
3696 line_info_table. */
3697 #define LINE_INFO_TABLE_INCREMENT 1024
3699 /* A pointer to the base of a table that contains a list of publicly
3700 accessible names. */
3701 static GTY ((length ("pubname_table_allocated"))) pubname_ref pubname_table;
3703 /* Number of elements currently allocated for pubname_table. */
3704 static GTY(()) unsigned pubname_table_allocated;
3706 /* Number of elements in pubname_table currently in use. */
3707 static GTY(()) unsigned pubname_table_in_use;
3709 /* Size (in elements) of increments by which we may expand the
3710 pubname_table. */
3711 #define PUBNAME_TABLE_INCREMENT 64
3713 /* Array of dies for which we should generate .debug_arange info. */
3714 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3716 /* Number of elements currently allocated for arange_table. */
3717 static GTY(()) unsigned arange_table_allocated;
3719 /* Number of elements in arange_table currently in use. */
3720 static GTY(()) unsigned arange_table_in_use;
3722 /* Size (in elements) of increments by which we may expand the
3723 arange_table. */
3724 #define ARANGE_TABLE_INCREMENT 64
3726 /* Array of dies for which we should generate .debug_ranges info. */
3727 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3729 /* Number of elements currently allocated for ranges_table. */
3730 static GTY(()) unsigned ranges_table_allocated;
3732 /* Number of elements in ranges_table currently in use. */
3733 static GTY(()) unsigned ranges_table_in_use;
3735 /* Size (in elements) of increments by which we may expand the
3736 ranges_table. */
3737 #define RANGES_TABLE_INCREMENT 64
3739 /* Whether we have location lists that need outputting */
3740 static GTY(()) unsigned have_location_lists;
3742 /* Unique label counter. */
3743 static GTY(()) unsigned int loclabel_num;
3745 #ifdef DWARF2_DEBUGGING_INFO
3746 /* Record whether the function being analyzed contains inlined functions. */
3747 static int current_function_has_inlines;
3748 #endif
3749 #if 0 && defined (MIPS_DEBUGGING_INFO)
3750 static int comp_unit_has_inlines;
3751 #endif
3753 /* Number of file tables emitted in maybe_emit_file(). */
3754 static GTY(()) int emitcount = 0;
3756 /* Number of internal labels generated by gen_internal_sym(). */
3757 static GTY(()) int label_num;
3759 #ifdef DWARF2_DEBUGGING_INFO
3761 /* Forward declarations for functions defined in this file. */
3763 static int is_pseudo_reg (rtx);
3764 static tree type_main_variant (tree);
3765 static int is_tagged_type (tree);
3766 static const char *dwarf_tag_name (unsigned);
3767 static const char *dwarf_attr_name (unsigned);
3768 static const char *dwarf_form_name (unsigned);
3769 #if 0
3770 static const char *dwarf_type_encoding_name (unsigned);
3771 #endif
3772 static tree decl_ultimate_origin (tree);
3773 static tree block_ultimate_origin (tree);
3774 static tree decl_class_context (tree);
3775 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3776 static inline enum dw_val_class AT_class (dw_attr_ref);
3777 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3778 static inline unsigned AT_flag (dw_attr_ref);
3779 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3780 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3781 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3782 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3783 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
3784 unsigned long);
3785 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3786 unsigned int, unsigned char *);
3787 static hashval_t debug_str_do_hash (const void *);
3788 static int debug_str_eq (const void *, const void *);
3789 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3790 static inline const char *AT_string (dw_attr_ref);
3791 static int AT_string_form (dw_attr_ref);
3792 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3793 static void add_AT_specification (dw_die_ref, dw_die_ref);
3794 static inline dw_die_ref AT_ref (dw_attr_ref);
3795 static inline int AT_ref_external (dw_attr_ref);
3796 static inline void set_AT_ref_external (dw_attr_ref, int);
3797 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3798 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3799 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3800 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3801 dw_loc_list_ref);
3802 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3803 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3804 static inline rtx AT_addr (dw_attr_ref);
3805 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3806 static void add_AT_lbl_offset (dw_die_ref, enum dwarf_attribute, const char *);
3807 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3808 unsigned HOST_WIDE_INT);
3809 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3810 unsigned long);
3811 static inline const char *AT_lbl (dw_attr_ref);
3812 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3813 static const char *get_AT_low_pc (dw_die_ref);
3814 static const char *get_AT_hi_pc (dw_die_ref);
3815 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3816 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3817 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3818 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3819 static bool is_c_family (void);
3820 static bool is_cxx (void);
3821 static bool is_java (void);
3822 static bool is_fortran (void);
3823 static bool is_ada (void);
3824 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3825 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3826 static inline void free_die (dw_die_ref);
3827 static void remove_children (dw_die_ref);
3828 static void add_child_die (dw_die_ref, dw_die_ref);
3829 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3830 static dw_die_ref lookup_type_die (tree);
3831 static void equate_type_number_to_die (tree, dw_die_ref);
3832 static hashval_t decl_die_table_hash (const void *);
3833 static int decl_die_table_eq (const void *, const void *);
3834 static dw_die_ref lookup_decl_die (tree);
3835 static hashval_t decl_loc_table_hash (const void *);
3836 static int decl_loc_table_eq (const void *, const void *);
3837 static var_loc_list *lookup_decl_loc (tree);
3838 static void equate_decl_number_to_die (tree, dw_die_ref);
3839 static void add_var_loc_to_decl (tree, struct var_loc_node *);
3840 static void print_spaces (FILE *);
3841 static void print_die (dw_die_ref, FILE *);
3842 static void print_dwarf_line_table (FILE *);
3843 static void reverse_die_lists (dw_die_ref);
3844 static void reverse_all_dies (dw_die_ref);
3845 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3846 static dw_die_ref pop_compile_unit (dw_die_ref);
3847 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3848 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3849 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3850 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3851 static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
3852 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3853 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3854 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3855 static void compute_section_prefix (dw_die_ref);
3856 static int is_type_die (dw_die_ref);
3857 static int is_comdat_die (dw_die_ref);
3858 static int is_symbol_die (dw_die_ref);
3859 static void assign_symbol_names (dw_die_ref);
3860 static void break_out_includes (dw_die_ref);
3861 static hashval_t htab_cu_hash (const void *);
3862 static int htab_cu_eq (const void *, const void *);
3863 static void htab_cu_del (void *);
3864 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3865 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3866 static void add_sibling_attributes (dw_die_ref);
3867 static void build_abbrev_table (dw_die_ref);
3868 static void output_location_lists (dw_die_ref);
3869 static int constant_size (long unsigned);
3870 static unsigned long size_of_die (dw_die_ref);
3871 static void calc_die_sizes (dw_die_ref);
3872 static void mark_dies (dw_die_ref);
3873 static void unmark_dies (dw_die_ref);
3874 static void unmark_all_dies (dw_die_ref);
3875 static unsigned long size_of_pubnames (void);
3876 static unsigned long size_of_aranges (void);
3877 static enum dwarf_form value_format (dw_attr_ref);
3878 static void output_value_format (dw_attr_ref);
3879 static void output_abbrev_section (void);
3880 static void output_die_symbol (dw_die_ref);
3881 static void output_die (dw_die_ref);
3882 static void output_compilation_unit_header (void);
3883 static void output_comp_unit (dw_die_ref, int);
3884 static const char *dwarf2_name (tree, int);
3885 static void add_pubname (tree, dw_die_ref);
3886 static void output_pubnames (void);
3887 static void add_arange (tree, dw_die_ref);
3888 static void output_aranges (void);
3889 static unsigned int add_ranges (tree);
3890 static void output_ranges (void);
3891 static void output_line_info (void);
3892 static void output_file_names (void);
3893 static dw_die_ref base_type_die (tree);
3894 static tree root_type (tree);
3895 static int is_base_type (tree);
3896 static bool is_subrange_type (tree);
3897 static dw_die_ref subrange_type_die (tree, dw_die_ref);
3898 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3899 static int type_is_enum (tree);
3900 static unsigned int dbx_reg_number (rtx);
3901 static dw_loc_descr_ref reg_loc_descriptor (rtx);
3902 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
3903 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
3904 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3905 static dw_loc_descr_ref based_loc_descr (unsigned, HOST_WIDE_INT, bool);
3906 static int is_based_loc (rtx);
3907 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode, bool);
3908 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
3909 static dw_loc_descr_ref loc_descriptor (rtx, bool);
3910 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3911 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3912 static tree field_type (tree);
3913 static unsigned int simple_type_align_in_bits (tree);
3914 static unsigned int simple_decl_align_in_bits (tree);
3915 static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
3916 static HOST_WIDE_INT field_byte_offset (tree);
3917 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3918 dw_loc_descr_ref);
3919 static void add_data_member_location_attribute (dw_die_ref, tree);
3920 static void add_const_value_attribute (dw_die_ref, rtx);
3921 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3922 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
3923 static void insert_float (rtx, unsigned char *);
3924 static rtx rtl_for_decl_location (tree);
3925 static void add_location_or_const_value_attribute (dw_die_ref, tree,
3926 enum dwarf_attribute);
3927 static void tree_add_const_value_attribute (dw_die_ref, tree);
3928 static void add_name_attribute (dw_die_ref, const char *);
3929 static void add_comp_dir_attribute (dw_die_ref);
3930 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3931 static void add_subscript_info (dw_die_ref, tree);
3932 static void add_byte_size_attribute (dw_die_ref, tree);
3933 static void add_bit_offset_attribute (dw_die_ref, tree);
3934 static void add_bit_size_attribute (dw_die_ref, tree);
3935 static void add_prototyped_attribute (dw_die_ref, tree);
3936 static void add_abstract_origin_attribute (dw_die_ref, tree);
3937 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3938 static void add_src_coords_attributes (dw_die_ref, tree);
3939 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3940 static void push_decl_scope (tree);
3941 static void pop_decl_scope (void);
3942 static dw_die_ref scope_die_for (tree, dw_die_ref);
3943 static inline int local_scope_p (dw_die_ref);
3944 static inline int class_or_namespace_scope_p (dw_die_ref);
3945 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3946 static const char *type_tag (tree);
3947 static tree member_declared_type (tree);
3948 #if 0
3949 static const char *decl_start_label (tree);
3950 #endif
3951 static void gen_array_type_die (tree, dw_die_ref);
3952 static void gen_set_type_die (tree, dw_die_ref);
3953 #if 0
3954 static void gen_entry_point_die (tree, dw_die_ref);
3955 #endif
3956 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
3957 static void gen_inlined_structure_type_die (tree, dw_die_ref);
3958 static void gen_inlined_union_type_die (tree, dw_die_ref);
3959 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3960 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
3961 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3962 static void gen_formal_types_die (tree, dw_die_ref);
3963 static void gen_subprogram_die (tree, dw_die_ref);
3964 static void gen_variable_die (tree, dw_die_ref);
3965 static void gen_label_die (tree, dw_die_ref);
3966 static void gen_lexical_block_die (tree, dw_die_ref, int);
3967 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3968 static void gen_field_die (tree, dw_die_ref);
3969 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3970 static dw_die_ref gen_compile_unit_die (const char *);
3971 static void gen_string_type_die (tree, dw_die_ref);
3972 static void gen_inheritance_die (tree, tree, dw_die_ref);
3973 static void gen_member_die (tree, dw_die_ref);
3974 static void gen_struct_or_union_type_die (tree, dw_die_ref);
3975 static void gen_subroutine_type_die (tree, dw_die_ref);
3976 static void gen_typedef_die (tree, dw_die_ref);
3977 static void gen_type_die (tree, dw_die_ref);
3978 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
3979 static void gen_block_die (tree, dw_die_ref, int);
3980 static void decls_for_scope (tree, dw_die_ref, int);
3981 static int is_redundant_typedef (tree);
3982 static void gen_namespace_die (tree);
3983 static void gen_decl_die (tree, dw_die_ref);
3984 static dw_die_ref force_decl_die (tree);
3985 static dw_die_ref force_type_die (tree);
3986 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3987 static void declare_in_namespace (tree, dw_die_ref);
3988 static unsigned lookup_filename (const char *);
3989 static void init_file_table (void);
3990 static void retry_incomplete_types (void);
3991 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3992 static void splice_child_die (dw_die_ref, dw_die_ref);
3993 static int file_info_cmp (const void *, const void *);
3994 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3995 const char *, const char *, unsigned);
3996 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
3997 const char *, const char *,
3998 const char *);
3999 static void output_loc_list (dw_loc_list_ref);
4000 static char *gen_internal_sym (const char *);
4002 static void prune_unmark_dies (dw_die_ref);
4003 static void prune_unused_types_mark (dw_die_ref, int);
4004 static void prune_unused_types_walk (dw_die_ref);
4005 static void prune_unused_types_walk_attribs (dw_die_ref);
4006 static void prune_unused_types_prune (dw_die_ref);
4007 static void prune_unused_types (void);
4008 static int maybe_emit_file (int);
4010 /* Section names used to hold DWARF debugging information. */
4011 #ifndef DEBUG_INFO_SECTION
4012 #define DEBUG_INFO_SECTION ".debug_info"
4013 #endif
4014 #ifndef DEBUG_ABBREV_SECTION
4015 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4016 #endif
4017 #ifndef DEBUG_ARANGES_SECTION
4018 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4019 #endif
4020 #ifndef DEBUG_MACINFO_SECTION
4021 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4022 #endif
4023 #ifndef DEBUG_LINE_SECTION
4024 #define DEBUG_LINE_SECTION ".debug_line"
4025 #endif
4026 #ifndef DEBUG_LOC_SECTION
4027 #define DEBUG_LOC_SECTION ".debug_loc"
4028 #endif
4029 #ifndef DEBUG_PUBNAMES_SECTION
4030 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
4031 #endif
4032 #ifndef DEBUG_STR_SECTION
4033 #define DEBUG_STR_SECTION ".debug_str"
4034 #endif
4035 #ifndef DEBUG_RANGES_SECTION
4036 #define DEBUG_RANGES_SECTION ".debug_ranges"
4037 #endif
4039 /* Standard ELF section names for compiled code and data. */
4040 #ifndef TEXT_SECTION_NAME
4041 #define TEXT_SECTION_NAME ".text"
4042 #endif
4044 /* Section flags for .debug_str section. */
4045 #define DEBUG_STR_SECTION_FLAGS \
4046 (HAVE_GAS_SHF_MERGE && flag_merge_constants \
4047 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4048 : SECTION_DEBUG)
4050 /* Labels we insert at beginning sections we can reference instead of
4051 the section names themselves. */
4053 #ifndef TEXT_SECTION_LABEL
4054 #define TEXT_SECTION_LABEL "Ltext"
4055 #endif
4056 #ifndef DEBUG_LINE_SECTION_LABEL
4057 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4058 #endif
4059 #ifndef DEBUG_INFO_SECTION_LABEL
4060 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4061 #endif
4062 #ifndef DEBUG_ABBREV_SECTION_LABEL
4063 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4064 #endif
4065 #ifndef DEBUG_LOC_SECTION_LABEL
4066 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4067 #endif
4068 #ifndef DEBUG_RANGES_SECTION_LABEL
4069 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4070 #endif
4071 #ifndef DEBUG_MACINFO_SECTION_LABEL
4072 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4073 #endif
4075 /* Definitions of defaults for formats and names of various special
4076 (artificial) labels which may be generated within this file (when the -g
4077 options is used and DWARF2_DEBUGGING_INFO is in effect.
4078 If necessary, these may be overridden from within the tm.h file, but
4079 typically, overriding these defaults is unnecessary. */
4081 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4082 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4083 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4084 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4085 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4086 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4087 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4088 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4090 #ifndef TEXT_END_LABEL
4091 #define TEXT_END_LABEL "Letext"
4092 #endif
4093 #ifndef BLOCK_BEGIN_LABEL
4094 #define BLOCK_BEGIN_LABEL "LBB"
4095 #endif
4096 #ifndef BLOCK_END_LABEL
4097 #define BLOCK_END_LABEL "LBE"
4098 #endif
4099 #ifndef LINE_CODE_LABEL
4100 #define LINE_CODE_LABEL "LM"
4101 #endif
4102 #ifndef SEPARATE_LINE_CODE_LABEL
4103 #define SEPARATE_LINE_CODE_LABEL "LSM"
4104 #endif
4106 /* We allow a language front-end to designate a function that is to be
4107 called to "demangle" any name before it it put into a DIE. */
4109 static const char *(*demangle_name_func) (const char *);
4111 void
4112 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4114 demangle_name_func = func;
4117 /* Test if rtl node points to a pseudo register. */
4119 static inline int
4120 is_pseudo_reg (rtx rtl)
4122 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4123 || (GET_CODE (rtl) == SUBREG
4124 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4127 /* Return a reference to a type, with its const and volatile qualifiers
4128 removed. */
4130 static inline tree
4131 type_main_variant (tree type)
4133 type = TYPE_MAIN_VARIANT (type);
4135 /* ??? There really should be only one main variant among any group of
4136 variants of a given type (and all of the MAIN_VARIANT values for all
4137 members of the group should point to that one type) but sometimes the C
4138 front-end messes this up for array types, so we work around that bug
4139 here. */
4140 if (TREE_CODE (type) == ARRAY_TYPE)
4141 while (type != TYPE_MAIN_VARIANT (type))
4142 type = TYPE_MAIN_VARIANT (type);
4144 return type;
4147 /* Return nonzero if the given type node represents a tagged type. */
4149 static inline int
4150 is_tagged_type (tree type)
4152 enum tree_code code = TREE_CODE (type);
4154 return (code == RECORD_TYPE || code == UNION_TYPE
4155 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4158 /* Convert a DIE tag into its string name. */
4160 static const char *
4161 dwarf_tag_name (unsigned int tag)
4163 switch (tag)
4165 case DW_TAG_padding:
4166 return "DW_TAG_padding";
4167 case DW_TAG_array_type:
4168 return "DW_TAG_array_type";
4169 case DW_TAG_class_type:
4170 return "DW_TAG_class_type";
4171 case DW_TAG_entry_point:
4172 return "DW_TAG_entry_point";
4173 case DW_TAG_enumeration_type:
4174 return "DW_TAG_enumeration_type";
4175 case DW_TAG_formal_parameter:
4176 return "DW_TAG_formal_parameter";
4177 case DW_TAG_imported_declaration:
4178 return "DW_TAG_imported_declaration";
4179 case DW_TAG_label:
4180 return "DW_TAG_label";
4181 case DW_TAG_lexical_block:
4182 return "DW_TAG_lexical_block";
4183 case DW_TAG_member:
4184 return "DW_TAG_member";
4185 case DW_TAG_pointer_type:
4186 return "DW_TAG_pointer_type";
4187 case DW_TAG_reference_type:
4188 return "DW_TAG_reference_type";
4189 case DW_TAG_compile_unit:
4190 return "DW_TAG_compile_unit";
4191 case DW_TAG_string_type:
4192 return "DW_TAG_string_type";
4193 case DW_TAG_structure_type:
4194 return "DW_TAG_structure_type";
4195 case DW_TAG_subroutine_type:
4196 return "DW_TAG_subroutine_type";
4197 case DW_TAG_typedef:
4198 return "DW_TAG_typedef";
4199 case DW_TAG_union_type:
4200 return "DW_TAG_union_type";
4201 case DW_TAG_unspecified_parameters:
4202 return "DW_TAG_unspecified_parameters";
4203 case DW_TAG_variant:
4204 return "DW_TAG_variant";
4205 case DW_TAG_common_block:
4206 return "DW_TAG_common_block";
4207 case DW_TAG_common_inclusion:
4208 return "DW_TAG_common_inclusion";
4209 case DW_TAG_inheritance:
4210 return "DW_TAG_inheritance";
4211 case DW_TAG_inlined_subroutine:
4212 return "DW_TAG_inlined_subroutine";
4213 case DW_TAG_module:
4214 return "DW_TAG_module";
4215 case DW_TAG_ptr_to_member_type:
4216 return "DW_TAG_ptr_to_member_type";
4217 case DW_TAG_set_type:
4218 return "DW_TAG_set_type";
4219 case DW_TAG_subrange_type:
4220 return "DW_TAG_subrange_type";
4221 case DW_TAG_with_stmt:
4222 return "DW_TAG_with_stmt";
4223 case DW_TAG_access_declaration:
4224 return "DW_TAG_access_declaration";
4225 case DW_TAG_base_type:
4226 return "DW_TAG_base_type";
4227 case DW_TAG_catch_block:
4228 return "DW_TAG_catch_block";
4229 case DW_TAG_const_type:
4230 return "DW_TAG_const_type";
4231 case DW_TAG_constant:
4232 return "DW_TAG_constant";
4233 case DW_TAG_enumerator:
4234 return "DW_TAG_enumerator";
4235 case DW_TAG_file_type:
4236 return "DW_TAG_file_type";
4237 case DW_TAG_friend:
4238 return "DW_TAG_friend";
4239 case DW_TAG_namelist:
4240 return "DW_TAG_namelist";
4241 case DW_TAG_namelist_item:
4242 return "DW_TAG_namelist_item";
4243 case DW_TAG_namespace:
4244 return "DW_TAG_namespace";
4245 case DW_TAG_packed_type:
4246 return "DW_TAG_packed_type";
4247 case DW_TAG_subprogram:
4248 return "DW_TAG_subprogram";
4249 case DW_TAG_template_type_param:
4250 return "DW_TAG_template_type_param";
4251 case DW_TAG_template_value_param:
4252 return "DW_TAG_template_value_param";
4253 case DW_TAG_thrown_type:
4254 return "DW_TAG_thrown_type";
4255 case DW_TAG_try_block:
4256 return "DW_TAG_try_block";
4257 case DW_TAG_variant_part:
4258 return "DW_TAG_variant_part";
4259 case DW_TAG_variable:
4260 return "DW_TAG_variable";
4261 case DW_TAG_volatile_type:
4262 return "DW_TAG_volatile_type";
4263 case DW_TAG_imported_module:
4264 return "DW_TAG_imported_module";
4265 case DW_TAG_MIPS_loop:
4266 return "DW_TAG_MIPS_loop";
4267 case DW_TAG_format_label:
4268 return "DW_TAG_format_label";
4269 case DW_TAG_function_template:
4270 return "DW_TAG_function_template";
4271 case DW_TAG_class_template:
4272 return "DW_TAG_class_template";
4273 case DW_TAG_GNU_BINCL:
4274 return "DW_TAG_GNU_BINCL";
4275 case DW_TAG_GNU_EINCL:
4276 return "DW_TAG_GNU_EINCL";
4277 default:
4278 return "DW_TAG_<unknown>";
4282 /* Convert a DWARF attribute code into its string name. */
4284 static const char *
4285 dwarf_attr_name (unsigned int attr)
4287 switch (attr)
4289 case DW_AT_sibling:
4290 return "DW_AT_sibling";
4291 case DW_AT_location:
4292 return "DW_AT_location";
4293 case DW_AT_name:
4294 return "DW_AT_name";
4295 case DW_AT_ordering:
4296 return "DW_AT_ordering";
4297 case DW_AT_subscr_data:
4298 return "DW_AT_subscr_data";
4299 case DW_AT_byte_size:
4300 return "DW_AT_byte_size";
4301 case DW_AT_bit_offset:
4302 return "DW_AT_bit_offset";
4303 case DW_AT_bit_size:
4304 return "DW_AT_bit_size";
4305 case DW_AT_element_list:
4306 return "DW_AT_element_list";
4307 case DW_AT_stmt_list:
4308 return "DW_AT_stmt_list";
4309 case DW_AT_low_pc:
4310 return "DW_AT_low_pc";
4311 case DW_AT_high_pc:
4312 return "DW_AT_high_pc";
4313 case DW_AT_language:
4314 return "DW_AT_language";
4315 case DW_AT_member:
4316 return "DW_AT_member";
4317 case DW_AT_discr:
4318 return "DW_AT_discr";
4319 case DW_AT_discr_value:
4320 return "DW_AT_discr_value";
4321 case DW_AT_visibility:
4322 return "DW_AT_visibility";
4323 case DW_AT_import:
4324 return "DW_AT_import";
4325 case DW_AT_string_length:
4326 return "DW_AT_string_length";
4327 case DW_AT_common_reference:
4328 return "DW_AT_common_reference";
4329 case DW_AT_comp_dir:
4330 return "DW_AT_comp_dir";
4331 case DW_AT_const_value:
4332 return "DW_AT_const_value";
4333 case DW_AT_containing_type:
4334 return "DW_AT_containing_type";
4335 case DW_AT_default_value:
4336 return "DW_AT_default_value";
4337 case DW_AT_inline:
4338 return "DW_AT_inline";
4339 case DW_AT_is_optional:
4340 return "DW_AT_is_optional";
4341 case DW_AT_lower_bound:
4342 return "DW_AT_lower_bound";
4343 case DW_AT_producer:
4344 return "DW_AT_producer";
4345 case DW_AT_prototyped:
4346 return "DW_AT_prototyped";
4347 case DW_AT_return_addr:
4348 return "DW_AT_return_addr";
4349 case DW_AT_start_scope:
4350 return "DW_AT_start_scope";
4351 case DW_AT_stride_size:
4352 return "DW_AT_stride_size";
4353 case DW_AT_upper_bound:
4354 return "DW_AT_upper_bound";
4355 case DW_AT_abstract_origin:
4356 return "DW_AT_abstract_origin";
4357 case DW_AT_accessibility:
4358 return "DW_AT_accessibility";
4359 case DW_AT_address_class:
4360 return "DW_AT_address_class";
4361 case DW_AT_artificial:
4362 return "DW_AT_artificial";
4363 case DW_AT_base_types:
4364 return "DW_AT_base_types";
4365 case DW_AT_calling_convention:
4366 return "DW_AT_calling_convention";
4367 case DW_AT_count:
4368 return "DW_AT_count";
4369 case DW_AT_data_member_location:
4370 return "DW_AT_data_member_location";
4371 case DW_AT_decl_column:
4372 return "DW_AT_decl_column";
4373 case DW_AT_decl_file:
4374 return "DW_AT_decl_file";
4375 case DW_AT_decl_line:
4376 return "DW_AT_decl_line";
4377 case DW_AT_declaration:
4378 return "DW_AT_declaration";
4379 case DW_AT_discr_list:
4380 return "DW_AT_discr_list";
4381 case DW_AT_encoding:
4382 return "DW_AT_encoding";
4383 case DW_AT_external:
4384 return "DW_AT_external";
4385 case DW_AT_frame_base:
4386 return "DW_AT_frame_base";
4387 case DW_AT_friend:
4388 return "DW_AT_friend";
4389 case DW_AT_identifier_case:
4390 return "DW_AT_identifier_case";
4391 case DW_AT_macro_info:
4392 return "DW_AT_macro_info";
4393 case DW_AT_namelist_items:
4394 return "DW_AT_namelist_items";
4395 case DW_AT_priority:
4396 return "DW_AT_priority";
4397 case DW_AT_segment:
4398 return "DW_AT_segment";
4399 case DW_AT_specification:
4400 return "DW_AT_specification";
4401 case DW_AT_static_link:
4402 return "DW_AT_static_link";
4403 case DW_AT_type:
4404 return "DW_AT_type";
4405 case DW_AT_use_location:
4406 return "DW_AT_use_location";
4407 case DW_AT_variable_parameter:
4408 return "DW_AT_variable_parameter";
4409 case DW_AT_virtuality:
4410 return "DW_AT_virtuality";
4411 case DW_AT_vtable_elem_location:
4412 return "DW_AT_vtable_elem_location";
4414 case DW_AT_allocated:
4415 return "DW_AT_allocated";
4416 case DW_AT_associated:
4417 return "DW_AT_associated";
4418 case DW_AT_data_location:
4419 return "DW_AT_data_location";
4420 case DW_AT_stride:
4421 return "DW_AT_stride";
4422 case DW_AT_entry_pc:
4423 return "DW_AT_entry_pc";
4424 case DW_AT_use_UTF8:
4425 return "DW_AT_use_UTF8";
4426 case DW_AT_extension:
4427 return "DW_AT_extension";
4428 case DW_AT_ranges:
4429 return "DW_AT_ranges";
4430 case DW_AT_trampoline:
4431 return "DW_AT_trampoline";
4432 case DW_AT_call_column:
4433 return "DW_AT_call_column";
4434 case DW_AT_call_file:
4435 return "DW_AT_call_file";
4436 case DW_AT_call_line:
4437 return "DW_AT_call_line";
4439 case DW_AT_MIPS_fde:
4440 return "DW_AT_MIPS_fde";
4441 case DW_AT_MIPS_loop_begin:
4442 return "DW_AT_MIPS_loop_begin";
4443 case DW_AT_MIPS_tail_loop_begin:
4444 return "DW_AT_MIPS_tail_loop_begin";
4445 case DW_AT_MIPS_epilog_begin:
4446 return "DW_AT_MIPS_epilog_begin";
4447 case DW_AT_MIPS_loop_unroll_factor:
4448 return "DW_AT_MIPS_loop_unroll_factor";
4449 case DW_AT_MIPS_software_pipeline_depth:
4450 return "DW_AT_MIPS_software_pipeline_depth";
4451 case DW_AT_MIPS_linkage_name:
4452 return "DW_AT_MIPS_linkage_name";
4453 case DW_AT_MIPS_stride:
4454 return "DW_AT_MIPS_stride";
4455 case DW_AT_MIPS_abstract_name:
4456 return "DW_AT_MIPS_abstract_name";
4457 case DW_AT_MIPS_clone_origin:
4458 return "DW_AT_MIPS_clone_origin";
4459 case DW_AT_MIPS_has_inlines:
4460 return "DW_AT_MIPS_has_inlines";
4462 case DW_AT_sf_names:
4463 return "DW_AT_sf_names";
4464 case DW_AT_src_info:
4465 return "DW_AT_src_info";
4466 case DW_AT_mac_info:
4467 return "DW_AT_mac_info";
4468 case DW_AT_src_coords:
4469 return "DW_AT_src_coords";
4470 case DW_AT_body_begin:
4471 return "DW_AT_body_begin";
4472 case DW_AT_body_end:
4473 return "DW_AT_body_end";
4474 case DW_AT_GNU_vector:
4475 return "DW_AT_GNU_vector";
4477 case DW_AT_VMS_rtnbeg_pd_address:
4478 return "DW_AT_VMS_rtnbeg_pd_address";
4480 default:
4481 return "DW_AT_<unknown>";
4485 /* Convert a DWARF value form code into its string name. */
4487 static const char *
4488 dwarf_form_name (unsigned int form)
4490 switch (form)
4492 case DW_FORM_addr:
4493 return "DW_FORM_addr";
4494 case DW_FORM_block2:
4495 return "DW_FORM_block2";
4496 case DW_FORM_block4:
4497 return "DW_FORM_block4";
4498 case DW_FORM_data2:
4499 return "DW_FORM_data2";
4500 case DW_FORM_data4:
4501 return "DW_FORM_data4";
4502 case DW_FORM_data8:
4503 return "DW_FORM_data8";
4504 case DW_FORM_string:
4505 return "DW_FORM_string";
4506 case DW_FORM_block:
4507 return "DW_FORM_block";
4508 case DW_FORM_block1:
4509 return "DW_FORM_block1";
4510 case DW_FORM_data1:
4511 return "DW_FORM_data1";
4512 case DW_FORM_flag:
4513 return "DW_FORM_flag";
4514 case DW_FORM_sdata:
4515 return "DW_FORM_sdata";
4516 case DW_FORM_strp:
4517 return "DW_FORM_strp";
4518 case DW_FORM_udata:
4519 return "DW_FORM_udata";
4520 case DW_FORM_ref_addr:
4521 return "DW_FORM_ref_addr";
4522 case DW_FORM_ref1:
4523 return "DW_FORM_ref1";
4524 case DW_FORM_ref2:
4525 return "DW_FORM_ref2";
4526 case DW_FORM_ref4:
4527 return "DW_FORM_ref4";
4528 case DW_FORM_ref8:
4529 return "DW_FORM_ref8";
4530 case DW_FORM_ref_udata:
4531 return "DW_FORM_ref_udata";
4532 case DW_FORM_indirect:
4533 return "DW_FORM_indirect";
4534 default:
4535 return "DW_FORM_<unknown>";
4539 /* Convert a DWARF type code into its string name. */
4541 #if 0
4542 static const char *
4543 dwarf_type_encoding_name (unsigned enc)
4545 switch (enc)
4547 case DW_ATE_address:
4548 return "DW_ATE_address";
4549 case DW_ATE_boolean:
4550 return "DW_ATE_boolean";
4551 case DW_ATE_complex_float:
4552 return "DW_ATE_complex_float";
4553 case DW_ATE_float:
4554 return "DW_ATE_float";
4555 case DW_ATE_signed:
4556 return "DW_ATE_signed";
4557 case DW_ATE_signed_char:
4558 return "DW_ATE_signed_char";
4559 case DW_ATE_unsigned:
4560 return "DW_ATE_unsigned";
4561 case DW_ATE_unsigned_char:
4562 return "DW_ATE_unsigned_char";
4563 default:
4564 return "DW_ATE_<unknown>";
4567 #endif
4569 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4570 instance of an inlined instance of a decl which is local to an inline
4571 function, so we have to trace all of the way back through the origin chain
4572 to find out what sort of node actually served as the original seed for the
4573 given block. */
4575 static tree
4576 decl_ultimate_origin (tree decl)
4578 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4579 nodes in the function to point to themselves; ignore that if
4580 we're trying to output the abstract instance of this function. */
4581 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4582 return NULL_TREE;
4584 #ifdef ENABLE_CHECKING
4585 if (DECL_FROM_INLINE (DECL_ORIGIN (decl)))
4586 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4587 most distant ancestor, this should never happen. */
4588 abort ();
4589 #endif
4591 return DECL_ABSTRACT_ORIGIN (decl);
4594 /* Determine the "ultimate origin" of a block. The block may be an inlined
4595 instance of an inlined instance of a block which is local to an inline
4596 function, so we have to trace all of the way back through the origin chain
4597 to find out what sort of node actually served as the original seed for the
4598 given block. */
4600 static tree
4601 block_ultimate_origin (tree block)
4603 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4605 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4606 nodes in the function to point to themselves; ignore that if
4607 we're trying to output the abstract instance of this function. */
4608 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4609 return NULL_TREE;
4611 if (immediate_origin == NULL_TREE)
4612 return NULL_TREE;
4613 else
4615 tree ret_val;
4616 tree lookahead = immediate_origin;
4620 ret_val = lookahead;
4621 lookahead = (TREE_CODE (ret_val) == BLOCK
4622 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4624 while (lookahead != NULL && lookahead != ret_val);
4626 return ret_val;
4630 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4631 of a virtual function may refer to a base class, so we check the 'this'
4632 parameter. */
4634 static tree
4635 decl_class_context (tree decl)
4637 tree context = NULL_TREE;
4639 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4640 context = DECL_CONTEXT (decl);
4641 else
4642 context = TYPE_MAIN_VARIANT
4643 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4645 if (context && !TYPE_P (context))
4646 context = NULL_TREE;
4648 return context;
4651 /* Add an attribute/value pair to a DIE. We build the lists up in reverse
4652 addition order, and correct that in reverse_all_dies. */
4654 static inline void
4655 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4657 if (die != NULL && attr != NULL)
4659 attr->dw_attr_next = die->die_attr;
4660 die->die_attr = attr;
4664 static inline enum dw_val_class
4665 AT_class (dw_attr_ref a)
4667 return a->dw_attr_val.val_class;
4670 /* Add a flag value attribute to a DIE. */
4672 static inline void
4673 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4675 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4677 attr->dw_attr_next = NULL;
4678 attr->dw_attr = attr_kind;
4679 attr->dw_attr_val.val_class = dw_val_class_flag;
4680 attr->dw_attr_val.v.val_flag = flag;
4681 add_dwarf_attr (die, attr);
4684 static inline unsigned
4685 AT_flag (dw_attr_ref a)
4687 if (a && AT_class (a) == dw_val_class_flag)
4688 return a->dw_attr_val.v.val_flag;
4690 abort ();
4693 /* Add a signed integer attribute value to a DIE. */
4695 static inline void
4696 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4698 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4700 attr->dw_attr_next = NULL;
4701 attr->dw_attr = attr_kind;
4702 attr->dw_attr_val.val_class = dw_val_class_const;
4703 attr->dw_attr_val.v.val_int = int_val;
4704 add_dwarf_attr (die, attr);
4707 static inline HOST_WIDE_INT
4708 AT_int (dw_attr_ref a)
4710 if (a && AT_class (a) == dw_val_class_const)
4711 return a->dw_attr_val.v.val_int;
4713 abort ();
4716 /* Add an unsigned integer attribute value to a DIE. */
4718 static inline void
4719 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4720 unsigned HOST_WIDE_INT unsigned_val)
4722 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4724 attr->dw_attr_next = NULL;
4725 attr->dw_attr = attr_kind;
4726 attr->dw_attr_val.val_class = dw_val_class_unsigned_const;
4727 attr->dw_attr_val.v.val_unsigned = unsigned_val;
4728 add_dwarf_attr (die, attr);
4731 static inline unsigned HOST_WIDE_INT
4732 AT_unsigned (dw_attr_ref a)
4734 if (a && AT_class (a) == dw_val_class_unsigned_const)
4735 return a->dw_attr_val.v.val_unsigned;
4737 abort ();
4740 /* Add an unsigned double integer attribute value to a DIE. */
4742 static inline void
4743 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4744 long unsigned int val_hi, long unsigned int val_low)
4746 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4748 attr->dw_attr_next = NULL;
4749 attr->dw_attr = attr_kind;
4750 attr->dw_attr_val.val_class = dw_val_class_long_long;
4751 attr->dw_attr_val.v.val_long_long.hi = val_hi;
4752 attr->dw_attr_val.v.val_long_long.low = val_low;
4753 add_dwarf_attr (die, attr);
4756 /* Add a floating point attribute value to a DIE and return it. */
4758 static inline void
4759 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4760 unsigned int length, unsigned int elt_size, unsigned char *array)
4762 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4764 attr->dw_attr_next = NULL;
4765 attr->dw_attr = attr_kind;
4766 attr->dw_attr_val.val_class = dw_val_class_vec;
4767 attr->dw_attr_val.v.val_vec.length = length;
4768 attr->dw_attr_val.v.val_vec.elt_size = elt_size;
4769 attr->dw_attr_val.v.val_vec.array = array;
4770 add_dwarf_attr (die, attr);
4773 /* Hash and equality functions for debug_str_hash. */
4775 static hashval_t
4776 debug_str_do_hash (const void *x)
4778 return htab_hash_string (((const struct indirect_string_node *)x)->str);
4781 static int
4782 debug_str_eq (const void *x1, const void *x2)
4784 return strcmp ((((const struct indirect_string_node *)x1)->str),
4785 (const char *)x2) == 0;
4788 /* Add a string attribute value to a DIE. */
4790 static inline void
4791 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4793 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4794 struct indirect_string_node *node;
4795 void **slot;
4797 if (! debug_str_hash)
4798 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4799 debug_str_eq, NULL);
4801 slot = htab_find_slot_with_hash (debug_str_hash, str,
4802 htab_hash_string (str), INSERT);
4803 if (*slot == NULL)
4804 *slot = ggc_alloc_cleared (sizeof (struct indirect_string_node));
4805 node = (struct indirect_string_node *) *slot;
4806 node->str = ggc_strdup (str);
4807 node->refcount++;
4809 attr->dw_attr_next = NULL;
4810 attr->dw_attr = attr_kind;
4811 attr->dw_attr_val.val_class = dw_val_class_str;
4812 attr->dw_attr_val.v.val_str = node;
4813 add_dwarf_attr (die, attr);
4816 static inline const char *
4817 AT_string (dw_attr_ref a)
4819 if (a && AT_class (a) == dw_val_class_str)
4820 return a->dw_attr_val.v.val_str->str;
4822 abort ();
4825 /* Find out whether a string should be output inline in DIE
4826 or out-of-line in .debug_str section. */
4828 static int
4829 AT_string_form (dw_attr_ref a)
4831 if (a && AT_class (a) == dw_val_class_str)
4833 struct indirect_string_node *node;
4834 unsigned int len;
4835 char label[32];
4837 node = a->dw_attr_val.v.val_str;
4838 if (node->form)
4839 return node->form;
4841 len = strlen (node->str) + 1;
4843 /* If the string is shorter or equal to the size of the reference, it is
4844 always better to put it inline. */
4845 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4846 return node->form = DW_FORM_string;
4848 /* If we cannot expect the linker to merge strings in .debug_str
4849 section, only put it into .debug_str if it is worth even in this
4850 single module. */
4851 if ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) == 0
4852 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
4853 return node->form = DW_FORM_string;
4855 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4856 ++dw2_string_counter;
4857 node->label = xstrdup (label);
4859 return node->form = DW_FORM_strp;
4862 abort ();
4865 /* Add a DIE reference attribute value to a DIE. */
4867 static inline void
4868 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4870 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4872 attr->dw_attr_next = NULL;
4873 attr->dw_attr = attr_kind;
4874 attr->dw_attr_val.val_class = dw_val_class_die_ref;
4875 attr->dw_attr_val.v.val_die_ref.die = targ_die;
4876 attr->dw_attr_val.v.val_die_ref.external = 0;
4877 add_dwarf_attr (die, attr);
4880 /* Add an AT_specification attribute to a DIE, and also make the back
4881 pointer from the specification to the definition. */
4883 static inline void
4884 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4886 add_AT_die_ref (die, DW_AT_specification, targ_die);
4887 if (targ_die->die_definition)
4888 abort ();
4889 targ_die->die_definition = die;
4892 static inline dw_die_ref
4893 AT_ref (dw_attr_ref a)
4895 if (a && AT_class (a) == dw_val_class_die_ref)
4896 return a->dw_attr_val.v.val_die_ref.die;
4898 abort ();
4901 static inline int
4902 AT_ref_external (dw_attr_ref a)
4904 if (a && AT_class (a) == dw_val_class_die_ref)
4905 return a->dw_attr_val.v.val_die_ref.external;
4907 return 0;
4910 static inline void
4911 set_AT_ref_external (dw_attr_ref a, int i)
4913 if (a && AT_class (a) == dw_val_class_die_ref)
4914 a->dw_attr_val.v.val_die_ref.external = i;
4915 else
4916 abort ();
4919 /* Add an FDE reference attribute value to a DIE. */
4921 static inline void
4922 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4924 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4926 attr->dw_attr_next = NULL;
4927 attr->dw_attr = attr_kind;
4928 attr->dw_attr_val.val_class = dw_val_class_fde_ref;
4929 attr->dw_attr_val.v.val_fde_index = targ_fde;
4930 add_dwarf_attr (die, attr);
4933 /* Add a location description attribute value to a DIE. */
4935 static inline void
4936 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4938 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4940 attr->dw_attr_next = NULL;
4941 attr->dw_attr = attr_kind;
4942 attr->dw_attr_val.val_class = dw_val_class_loc;
4943 attr->dw_attr_val.v.val_loc = loc;
4944 add_dwarf_attr (die, attr);
4947 static inline dw_loc_descr_ref
4948 AT_loc (dw_attr_ref a)
4950 if (a && AT_class (a) == dw_val_class_loc)
4951 return a->dw_attr_val.v.val_loc;
4953 abort ();
4956 static inline void
4957 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4959 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4961 attr->dw_attr_next = NULL;
4962 attr->dw_attr = attr_kind;
4963 attr->dw_attr_val.val_class = dw_val_class_loc_list;
4964 attr->dw_attr_val.v.val_loc_list = loc_list;
4965 add_dwarf_attr (die, attr);
4966 have_location_lists = 1;
4969 static inline dw_loc_list_ref
4970 AT_loc_list (dw_attr_ref a)
4972 if (a && AT_class (a) == dw_val_class_loc_list)
4973 return a->dw_attr_val.v.val_loc_list;
4975 abort ();
4978 /* Add an address constant attribute value to a DIE. */
4980 static inline void
4981 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
4983 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4985 attr->dw_attr_next = NULL;
4986 attr->dw_attr = attr_kind;
4987 attr->dw_attr_val.val_class = dw_val_class_addr;
4988 attr->dw_attr_val.v.val_addr = addr;
4989 add_dwarf_attr (die, attr);
4992 static inline rtx
4993 AT_addr (dw_attr_ref a)
4995 if (a && AT_class (a) == dw_val_class_addr)
4996 return a->dw_attr_val.v.val_addr;
4998 abort ();
5001 /* Add a label identifier attribute value to a DIE. */
5003 static inline void
5004 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5006 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5008 attr->dw_attr_next = NULL;
5009 attr->dw_attr = attr_kind;
5010 attr->dw_attr_val.val_class = dw_val_class_lbl_id;
5011 attr->dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5012 add_dwarf_attr (die, attr);
5015 /* Add a section offset attribute value to a DIE. */
5017 static inline void
5018 add_AT_lbl_offset (dw_die_ref die, enum dwarf_attribute attr_kind, const char *label)
5020 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5022 attr->dw_attr_next = NULL;
5023 attr->dw_attr = attr_kind;
5024 attr->dw_attr_val.val_class = dw_val_class_lbl_offset;
5025 attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
5026 add_dwarf_attr (die, attr);
5029 /* Add an offset attribute value to a DIE. */
5031 static inline void
5032 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5033 unsigned HOST_WIDE_INT offset)
5035 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5037 attr->dw_attr_next = NULL;
5038 attr->dw_attr = attr_kind;
5039 attr->dw_attr_val.val_class = dw_val_class_offset;
5040 attr->dw_attr_val.v.val_offset = offset;
5041 add_dwarf_attr (die, attr);
5044 /* Add an range_list attribute value to a DIE. */
5046 static void
5047 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5048 long unsigned int offset)
5050 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5052 attr->dw_attr_next = NULL;
5053 attr->dw_attr = attr_kind;
5054 attr->dw_attr_val.val_class = dw_val_class_range_list;
5055 attr->dw_attr_val.v.val_offset = offset;
5056 add_dwarf_attr (die, attr);
5059 static inline const char *
5060 AT_lbl (dw_attr_ref a)
5062 if (a && (AT_class (a) == dw_val_class_lbl_id
5063 || AT_class (a) == dw_val_class_lbl_offset))
5064 return a->dw_attr_val.v.val_lbl_id;
5066 abort ();
5069 /* Get the attribute of type attr_kind. */
5071 static dw_attr_ref
5072 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5074 dw_attr_ref a;
5075 dw_die_ref spec = NULL;
5077 if (die != NULL)
5079 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5080 if (a->dw_attr == attr_kind)
5081 return a;
5082 else if (a->dw_attr == DW_AT_specification
5083 || a->dw_attr == DW_AT_abstract_origin)
5084 spec = AT_ref (a);
5086 if (spec)
5087 return get_AT (spec, attr_kind);
5090 return NULL;
5093 /* Return the "low pc" attribute value, typically associated with a subprogram
5094 DIE. Return null if the "low pc" attribute is either not present, or if it
5095 cannot be represented as an assembler label identifier. */
5097 static inline const char *
5098 get_AT_low_pc (dw_die_ref die)
5100 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5102 return a ? AT_lbl (a) : NULL;
5105 /* Return the "high pc" attribute value, typically associated with a subprogram
5106 DIE. Return null if the "high pc" attribute is either not present, or if it
5107 cannot be represented as an assembler label identifier. */
5109 static inline const char *
5110 get_AT_hi_pc (dw_die_ref die)
5112 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5114 return a ? AT_lbl (a) : NULL;
5117 /* Return the value of the string attribute designated by ATTR_KIND, or
5118 NULL if it is not present. */
5120 static inline const char *
5121 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5123 dw_attr_ref a = get_AT (die, attr_kind);
5125 return a ? AT_string (a) : NULL;
5128 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5129 if it is not present. */
5131 static inline int
5132 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5134 dw_attr_ref a = get_AT (die, attr_kind);
5136 return a ? AT_flag (a) : 0;
5139 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5140 if it is not present. */
5142 static inline unsigned
5143 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5145 dw_attr_ref a = get_AT (die, attr_kind);
5147 return a ? AT_unsigned (a) : 0;
5150 static inline dw_die_ref
5151 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5153 dw_attr_ref a = get_AT (die, attr_kind);
5155 return a ? AT_ref (a) : NULL;
5158 /* Return TRUE if the language is C or C++. */
5160 static inline bool
5161 is_c_family (void)
5163 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5165 return (lang == DW_LANG_C || lang == DW_LANG_C89
5166 || lang == DW_LANG_C_plus_plus);
5169 /* Return TRUE if the language is C++. */
5171 static inline bool
5172 is_cxx (void)
5174 return (get_AT_unsigned (comp_unit_die, DW_AT_language)
5175 == DW_LANG_C_plus_plus);
5178 /* Return TRUE if the language is Fortran. */
5180 static inline bool
5181 is_fortran (void)
5183 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5185 return (lang == DW_LANG_Fortran77
5186 || lang == DW_LANG_Fortran90
5187 || lang == DW_LANG_Fortran95);
5190 /* Return TRUE if the language is Java. */
5192 static inline bool
5193 is_java (void)
5195 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5197 return lang == DW_LANG_Java;
5200 /* Return TRUE if the language is Ada. */
5202 static inline bool
5203 is_ada (void)
5205 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5207 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5210 /* Free up the memory used by A. */
5212 static inline void free_AT (dw_attr_ref);
5213 static inline void
5214 free_AT (dw_attr_ref a)
5216 if (AT_class (a) == dw_val_class_str)
5217 if (a->dw_attr_val.v.val_str->refcount)
5218 a->dw_attr_val.v.val_str->refcount--;
5221 /* Remove the specified attribute if present. */
5223 static void
5224 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5226 dw_attr_ref *p;
5227 dw_attr_ref removed = NULL;
5229 if (die != NULL)
5231 for (p = &(die->die_attr); *p; p = &((*p)->dw_attr_next))
5232 if ((*p)->dw_attr == attr_kind)
5234 removed = *p;
5235 *p = (*p)->dw_attr_next;
5236 break;
5239 if (removed != 0)
5240 free_AT (removed);
5244 /* Remove child die whose die_tag is specified tag. */
5246 static void
5247 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5249 dw_die_ref current, prev, next;
5250 current = die->die_child;
5251 prev = NULL;
5252 while (current != NULL)
5254 if (current->die_tag == tag)
5256 next = current->die_sib;
5257 if (prev == NULL)
5258 die->die_child = next;
5259 else
5260 prev->die_sib = next;
5261 free_die (current);
5262 current = next;
5264 else
5266 prev = current;
5267 current = current->die_sib;
5272 /* Free up the memory used by DIE. */
5274 static inline void
5275 free_die (dw_die_ref die)
5277 remove_children (die);
5280 /* Discard the children of this DIE. */
5282 static void
5283 remove_children (dw_die_ref die)
5285 dw_die_ref child_die = die->die_child;
5287 die->die_child = NULL;
5289 while (child_die != NULL)
5291 dw_die_ref tmp_die = child_die;
5292 dw_attr_ref a;
5294 child_die = child_die->die_sib;
5296 for (a = tmp_die->die_attr; a != NULL;)
5298 dw_attr_ref tmp_a = a;
5300 a = a->dw_attr_next;
5301 free_AT (tmp_a);
5304 free_die (tmp_die);
5308 /* Add a child DIE below its parent. We build the lists up in reverse
5309 addition order, and correct that in reverse_all_dies. */
5311 static inline void
5312 add_child_die (dw_die_ref die, dw_die_ref child_die)
5314 if (die != NULL && child_die != NULL)
5316 if (die == child_die)
5317 abort ();
5319 child_die->die_parent = die;
5320 child_die->die_sib = die->die_child;
5321 die->die_child = child_die;
5325 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5326 is the specification, to the front of PARENT's list of children. */
5328 static void
5329 splice_child_die (dw_die_ref parent, dw_die_ref child)
5331 dw_die_ref *p;
5333 /* We want the declaration DIE from inside the class, not the
5334 specification DIE at toplevel. */
5335 if (child->die_parent != parent)
5337 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5339 if (tmp)
5340 child = tmp;
5343 if (child->die_parent != parent
5344 && child->die_parent != get_AT_ref (parent, DW_AT_specification))
5345 abort ();
5347 for (p = &(child->die_parent->die_child); *p; p = &((*p)->die_sib))
5348 if (*p == child)
5350 *p = child->die_sib;
5351 break;
5354 child->die_parent = parent;
5355 child->die_sib = parent->die_child;
5356 parent->die_child = child;
5359 /* Return a pointer to a newly created DIE node. */
5361 static inline dw_die_ref
5362 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5364 dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5366 die->die_tag = tag_value;
5368 if (parent_die != NULL)
5369 add_child_die (parent_die, die);
5370 else
5372 limbo_die_node *limbo_node;
5374 limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5375 limbo_node->die = die;
5376 limbo_node->created_for = t;
5377 limbo_node->next = limbo_die_list;
5378 limbo_die_list = limbo_node;
5381 return die;
5384 /* Return the DIE associated with the given type specifier. */
5386 static inline dw_die_ref
5387 lookup_type_die (tree type)
5389 return TYPE_SYMTAB_DIE (type);
5392 /* Equate a DIE to a given type specifier. */
5394 static inline void
5395 equate_type_number_to_die (tree type, dw_die_ref type_die)
5397 TYPE_SYMTAB_DIE (type) = type_die;
5400 /* Returns a hash value for X (which really is a die_struct). */
5402 static hashval_t
5403 decl_die_table_hash (const void *x)
5405 return (hashval_t) ((const dw_die_ref) x)->decl_id;
5408 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5410 static int
5411 decl_die_table_eq (const void *x, const void *y)
5413 return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5416 /* Return the DIE associated with a given declaration. */
5418 static inline dw_die_ref
5419 lookup_decl_die (tree decl)
5421 return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5424 /* Returns a hash value for X (which really is a var_loc_list). */
5426 static hashval_t
5427 decl_loc_table_hash (const void *x)
5429 return (hashval_t) ((const var_loc_list *) x)->decl_id;
5432 /* Return nonzero if decl_id of var_loc_list X is the same as
5433 UID of decl *Y. */
5435 static int
5436 decl_loc_table_eq (const void *x, const void *y)
5438 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5441 /* Return the var_loc list associated with a given declaration. */
5443 static inline var_loc_list *
5444 lookup_decl_loc (tree decl)
5446 return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5449 /* Equate a DIE to a particular declaration. */
5451 static void
5452 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5454 unsigned int decl_id = DECL_UID (decl);
5455 void **slot;
5457 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5458 *slot = decl_die;
5459 decl_die->decl_id = decl_id;
5462 /* Add a variable location node to the linked list for DECL. */
5464 static void
5465 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5467 unsigned int decl_id = DECL_UID (decl);
5468 var_loc_list *temp;
5469 void **slot;
5471 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5472 if (*slot == NULL)
5474 temp = ggc_alloc_cleared (sizeof (var_loc_list));
5475 temp->decl_id = decl_id;
5476 *slot = temp;
5478 else
5479 temp = *slot;
5481 if (temp->last)
5483 /* If the current location is the same as the end of the list,
5484 we have nothing to do. */
5485 if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5486 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5488 /* Add LOC to the end of list and update LAST. */
5489 temp->last->next = loc;
5490 temp->last = loc;
5493 /* Do not add empty location to the beginning of the list. */
5494 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5496 temp->first = loc;
5497 temp->last = loc;
5501 /* Keep track of the number of spaces used to indent the
5502 output of the debugging routines that print the structure of
5503 the DIE internal representation. */
5504 static int print_indent;
5506 /* Indent the line the number of spaces given by print_indent. */
5508 static inline void
5509 print_spaces (FILE *outfile)
5511 fprintf (outfile, "%*s", print_indent, "");
5514 /* Print the information associated with a given DIE, and its children.
5515 This routine is a debugging aid only. */
5517 static void
5518 print_die (dw_die_ref die, FILE *outfile)
5520 dw_attr_ref a;
5521 dw_die_ref c;
5523 print_spaces (outfile);
5524 fprintf (outfile, "DIE %4lu: %s\n",
5525 die->die_offset, dwarf_tag_name (die->die_tag));
5526 print_spaces (outfile);
5527 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5528 fprintf (outfile, " offset: %lu\n", die->die_offset);
5530 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5532 print_spaces (outfile);
5533 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5535 switch (AT_class (a))
5537 case dw_val_class_addr:
5538 fprintf (outfile, "address");
5539 break;
5540 case dw_val_class_offset:
5541 fprintf (outfile, "offset");
5542 break;
5543 case dw_val_class_loc:
5544 fprintf (outfile, "location descriptor");
5545 break;
5546 case dw_val_class_loc_list:
5547 fprintf (outfile, "location list -> label:%s",
5548 AT_loc_list (a)->ll_symbol);
5549 break;
5550 case dw_val_class_range_list:
5551 fprintf (outfile, "range list");
5552 break;
5553 case dw_val_class_const:
5554 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5555 break;
5556 case dw_val_class_unsigned_const:
5557 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5558 break;
5559 case dw_val_class_long_long:
5560 fprintf (outfile, "constant (%lu,%lu)",
5561 a->dw_attr_val.v.val_long_long.hi,
5562 a->dw_attr_val.v.val_long_long.low);
5563 break;
5564 case dw_val_class_vec:
5565 fprintf (outfile, "floating-point or vector constant");
5566 break;
5567 case dw_val_class_flag:
5568 fprintf (outfile, "%u", AT_flag (a));
5569 break;
5570 case dw_val_class_die_ref:
5571 if (AT_ref (a) != NULL)
5573 if (AT_ref (a)->die_symbol)
5574 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5575 else
5576 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5578 else
5579 fprintf (outfile, "die -> <null>");
5580 break;
5581 case dw_val_class_lbl_id:
5582 case dw_val_class_lbl_offset:
5583 fprintf (outfile, "label: %s", AT_lbl (a));
5584 break;
5585 case dw_val_class_str:
5586 if (AT_string (a) != NULL)
5587 fprintf (outfile, "\"%s\"", AT_string (a));
5588 else
5589 fprintf (outfile, "<null>");
5590 break;
5591 default:
5592 break;
5595 fprintf (outfile, "\n");
5598 if (die->die_child != NULL)
5600 print_indent += 4;
5601 for (c = die->die_child; c != NULL; c = c->die_sib)
5602 print_die (c, outfile);
5604 print_indent -= 4;
5606 if (print_indent == 0)
5607 fprintf (outfile, "\n");
5610 /* Print the contents of the source code line number correspondence table.
5611 This routine is a debugging aid only. */
5613 static void
5614 print_dwarf_line_table (FILE *outfile)
5616 unsigned i;
5617 dw_line_info_ref line_info;
5619 fprintf (outfile, "\n\nDWARF source line information\n");
5620 for (i = 1; i < line_info_table_in_use; i++)
5622 line_info = &line_info_table[i];
5623 fprintf (outfile, "%5d: ", i);
5624 fprintf (outfile, "%-20s",
5625 VARRAY_CHAR_PTR (file_table, line_info->dw_file_num));
5626 fprintf (outfile, "%6ld", line_info->dw_line_num);
5627 fprintf (outfile, "\n");
5630 fprintf (outfile, "\n\n");
5633 /* Print the information collected for a given DIE. */
5635 void
5636 debug_dwarf_die (dw_die_ref die)
5638 print_die (die, stderr);
5641 /* Print all DWARF information collected for the compilation unit.
5642 This routine is a debugging aid only. */
5644 void
5645 debug_dwarf (void)
5647 print_indent = 0;
5648 print_die (comp_unit_die, stderr);
5649 if (! DWARF2_ASM_LINE_DEBUG_INFO)
5650 print_dwarf_line_table (stderr);
5653 /* We build up the lists of children and attributes by pushing new ones
5654 onto the beginning of the list. Reverse the lists for DIE so that
5655 they are in order of addition. */
5657 static void
5658 reverse_die_lists (dw_die_ref die)
5660 dw_die_ref c, cp, cn;
5661 dw_attr_ref a, ap, an;
5663 for (a = die->die_attr, ap = 0; a; a = an)
5665 an = a->dw_attr_next;
5666 a->dw_attr_next = ap;
5667 ap = a;
5670 die->die_attr = ap;
5672 for (c = die->die_child, cp = 0; c; c = cn)
5674 cn = c->die_sib;
5675 c->die_sib = cp;
5676 cp = c;
5679 die->die_child = cp;
5682 /* reverse_die_lists only reverses the single die you pass it. Since we used to
5683 reverse all dies in add_sibling_attributes, which runs through all the dies,
5684 it would reverse all the dies. Now, however, since we don't call
5685 reverse_die_lists in add_sibling_attributes, we need a routine to
5686 recursively reverse all the dies. This is that routine. */
5688 static void
5689 reverse_all_dies (dw_die_ref die)
5691 dw_die_ref c;
5693 reverse_die_lists (die);
5695 for (c = die->die_child; c; c = c->die_sib)
5696 reverse_all_dies (c);
5699 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5700 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5701 DIE that marks the start of the DIEs for this include file. */
5703 static dw_die_ref
5704 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5706 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5707 dw_die_ref new_unit = gen_compile_unit_die (filename);
5709 new_unit->die_sib = old_unit;
5710 return new_unit;
5713 /* Close an include-file CU and reopen the enclosing one. */
5715 static dw_die_ref
5716 pop_compile_unit (dw_die_ref old_unit)
5718 dw_die_ref new_unit = old_unit->die_sib;
5720 old_unit->die_sib = NULL;
5721 return new_unit;
5724 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5725 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5727 /* Calculate the checksum of a location expression. */
5729 static inline void
5730 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5732 CHECKSUM (loc->dw_loc_opc);
5733 CHECKSUM (loc->dw_loc_oprnd1);
5734 CHECKSUM (loc->dw_loc_oprnd2);
5737 /* Calculate the checksum of an attribute. */
5739 static void
5740 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5742 dw_loc_descr_ref loc;
5743 rtx r;
5745 CHECKSUM (at->dw_attr);
5747 /* We don't care about differences in file numbering. */
5748 if (at->dw_attr == DW_AT_decl_file
5749 /* Or that this was compiled with a different compiler snapshot; if
5750 the output is the same, that's what matters. */
5751 || at->dw_attr == DW_AT_producer)
5752 return;
5754 switch (AT_class (at))
5756 case dw_val_class_const:
5757 CHECKSUM (at->dw_attr_val.v.val_int);
5758 break;
5759 case dw_val_class_unsigned_const:
5760 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5761 break;
5762 case dw_val_class_long_long:
5763 CHECKSUM (at->dw_attr_val.v.val_long_long);
5764 break;
5765 case dw_val_class_vec:
5766 CHECKSUM (at->dw_attr_val.v.val_vec);
5767 break;
5768 case dw_val_class_flag:
5769 CHECKSUM (at->dw_attr_val.v.val_flag);
5770 break;
5771 case dw_val_class_str:
5772 CHECKSUM_STRING (AT_string (at));
5773 break;
5775 case dw_val_class_addr:
5776 r = AT_addr (at);
5777 switch (GET_CODE (r))
5779 case SYMBOL_REF:
5780 CHECKSUM_STRING (XSTR (r, 0));
5781 break;
5783 default:
5784 abort ();
5786 break;
5788 case dw_val_class_offset:
5789 CHECKSUM (at->dw_attr_val.v.val_offset);
5790 break;
5792 case dw_val_class_loc:
5793 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5794 loc_checksum (loc, ctx);
5795 break;
5797 case dw_val_class_die_ref:
5798 die_checksum (AT_ref (at), ctx, mark);
5799 break;
5801 case dw_val_class_fde_ref:
5802 case dw_val_class_lbl_id:
5803 case dw_val_class_lbl_offset:
5804 break;
5806 default:
5807 break;
5811 /* Calculate the checksum of a DIE. */
5813 static void
5814 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5816 dw_die_ref c;
5817 dw_attr_ref a;
5819 /* To avoid infinite recursion. */
5820 if (die->die_mark)
5822 CHECKSUM (die->die_mark);
5823 return;
5825 die->die_mark = ++(*mark);
5827 CHECKSUM (die->die_tag);
5829 for (a = die->die_attr; a; a = a->dw_attr_next)
5830 attr_checksum (a, ctx, mark);
5832 for (c = die->die_child; c; c = c->die_sib)
5833 die_checksum (c, ctx, mark);
5836 #undef CHECKSUM
5837 #undef CHECKSUM_STRING
5839 /* Do the location expressions look same? */
5840 static inline int
5841 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
5843 return loc1->dw_loc_opc == loc2->dw_loc_opc
5844 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
5845 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
5848 /* Do the values look the same? */
5849 static int
5850 same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
5852 dw_loc_descr_ref loc1, loc2;
5853 rtx r1, r2;
5855 if (v1->val_class != v2->val_class)
5856 return 0;
5858 switch (v1->val_class)
5860 case dw_val_class_const:
5861 return v1->v.val_int == v2->v.val_int;
5862 case dw_val_class_unsigned_const:
5863 return v1->v.val_unsigned == v2->v.val_unsigned;
5864 case dw_val_class_long_long:
5865 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
5866 && v1->v.val_long_long.low == v2->v.val_long_long.low;
5867 case dw_val_class_vec:
5868 if (v1->v.val_vec.length != v2->v.val_vec.length
5869 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
5870 return 0;
5871 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
5872 v1->v.val_vec.length * v1->v.val_vec.elt_size))
5873 return 0;
5874 return 1;
5875 case dw_val_class_flag:
5876 return v1->v.val_flag == v2->v.val_flag;
5877 case dw_val_class_str:
5878 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
5880 case dw_val_class_addr:
5881 r1 = v1->v.val_addr;
5882 r2 = v2->v.val_addr;
5883 if (GET_CODE (r1) != GET_CODE (r2))
5884 return 0;
5885 switch (GET_CODE (r1))
5887 case SYMBOL_REF:
5888 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
5890 default:
5891 abort ();
5894 case dw_val_class_offset:
5895 return v1->v.val_offset == v2->v.val_offset;
5897 case dw_val_class_loc:
5898 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
5899 loc1 && loc2;
5900 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
5901 if (!same_loc_p (loc1, loc2, mark))
5902 return 0;
5903 return !loc1 && !loc2;
5905 case dw_val_class_die_ref:
5906 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
5908 case dw_val_class_fde_ref:
5909 case dw_val_class_lbl_id:
5910 case dw_val_class_lbl_offset:
5911 return 1;
5913 default:
5914 return 1;
5918 /* Do the attributes look the same? */
5920 static int
5921 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
5923 if (at1->dw_attr != at2->dw_attr)
5924 return 0;
5926 /* We don't care about differences in file numbering. */
5927 if (at1->dw_attr == DW_AT_decl_file
5928 /* Or that this was compiled with a different compiler snapshot; if
5929 the output is the same, that's what matters. */
5930 || at1->dw_attr == DW_AT_producer)
5931 return 1;
5933 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
5936 /* Do the dies look the same? */
5938 static int
5939 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
5941 dw_die_ref c1, c2;
5942 dw_attr_ref a1, a2;
5944 /* To avoid infinite recursion. */
5945 if (die1->die_mark)
5946 return die1->die_mark == die2->die_mark;
5947 die1->die_mark = die2->die_mark = ++(*mark);
5949 if (die1->die_tag != die2->die_tag)
5950 return 0;
5952 for (a1 = die1->die_attr, a2 = die2->die_attr;
5953 a1 && a2;
5954 a1 = a1->dw_attr_next, a2 = a2->dw_attr_next)
5955 if (!same_attr_p (a1, a2, mark))
5956 return 0;
5957 if (a1 || a2)
5958 return 0;
5960 for (c1 = die1->die_child, c2 = die2->die_child;
5961 c1 && c2;
5962 c1 = c1->die_sib, c2 = c2->die_sib)
5963 if (!same_die_p (c1, c2, mark))
5964 return 0;
5965 if (c1 || c2)
5966 return 0;
5968 return 1;
5971 /* Do the dies look the same? Wrapper around same_die_p. */
5973 static int
5974 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
5976 int mark = 0;
5977 int ret = same_die_p (die1, die2, &mark);
5979 unmark_all_dies (die1);
5980 unmark_all_dies (die2);
5982 return ret;
5985 /* The prefix to attach to symbols on DIEs in the current comdat debug
5986 info section. */
5987 static char *comdat_symbol_id;
5989 /* The index of the current symbol within the current comdat CU. */
5990 static unsigned int comdat_symbol_number;
5992 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
5993 children, and set comdat_symbol_id accordingly. */
5995 static void
5996 compute_section_prefix (dw_die_ref unit_die)
5998 const char *die_name = get_AT_string (unit_die, DW_AT_name);
5999 const char *base = die_name ? lbasename (die_name) : "anonymous";
6000 char *name = alloca (strlen (base) + 64);
6001 char *p;
6002 int i, mark;
6003 unsigned char checksum[16];
6004 struct md5_ctx ctx;
6006 /* Compute the checksum of the DIE, then append part of it as hex digits to
6007 the name filename of the unit. */
6009 md5_init_ctx (&ctx);
6010 mark = 0;
6011 die_checksum (unit_die, &ctx, &mark);
6012 unmark_all_dies (unit_die);
6013 md5_finish_ctx (&ctx, checksum);
6015 sprintf (name, "%s.", base);
6016 clean_symbol_name (name);
6018 p = name + strlen (name);
6019 for (i = 0; i < 4; i++)
6021 sprintf (p, "%.2x", checksum[i]);
6022 p += 2;
6025 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6026 comdat_symbol_number = 0;
6029 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6031 static int
6032 is_type_die (dw_die_ref die)
6034 switch (die->die_tag)
6036 case DW_TAG_array_type:
6037 case DW_TAG_class_type:
6038 case DW_TAG_enumeration_type:
6039 case DW_TAG_pointer_type:
6040 case DW_TAG_reference_type:
6041 case DW_TAG_string_type:
6042 case DW_TAG_structure_type:
6043 case DW_TAG_subroutine_type:
6044 case DW_TAG_union_type:
6045 case DW_TAG_ptr_to_member_type:
6046 case DW_TAG_set_type:
6047 case DW_TAG_subrange_type:
6048 case DW_TAG_base_type:
6049 case DW_TAG_const_type:
6050 case DW_TAG_file_type:
6051 case DW_TAG_packed_type:
6052 case DW_TAG_volatile_type:
6053 case DW_TAG_typedef:
6054 return 1;
6055 default:
6056 return 0;
6060 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6061 Basically, we want to choose the bits that are likely to be shared between
6062 compilations (types) and leave out the bits that are specific to individual
6063 compilations (functions). */
6065 static int
6066 is_comdat_die (dw_die_ref c)
6068 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6069 we do for stabs. The advantage is a greater likelihood of sharing between
6070 objects that don't include headers in the same order (and therefore would
6071 put the base types in a different comdat). jason 8/28/00 */
6073 if (c->die_tag == DW_TAG_base_type)
6074 return 0;
6076 if (c->die_tag == DW_TAG_pointer_type
6077 || c->die_tag == DW_TAG_reference_type
6078 || c->die_tag == DW_TAG_const_type
6079 || c->die_tag == DW_TAG_volatile_type)
6081 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6083 return t ? is_comdat_die (t) : 0;
6086 return is_type_die (c);
6089 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6090 compilation unit. */
6092 static int
6093 is_symbol_die (dw_die_ref c)
6095 return (is_type_die (c)
6096 || (get_AT (c, DW_AT_declaration)
6097 && !get_AT (c, DW_AT_specification)));
6100 static char *
6101 gen_internal_sym (const char *prefix)
6103 char buf[256];
6105 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6106 return xstrdup (buf);
6109 /* Assign symbols to all worthy DIEs under DIE. */
6111 static void
6112 assign_symbol_names (dw_die_ref die)
6114 dw_die_ref c;
6116 if (is_symbol_die (die))
6118 if (comdat_symbol_id)
6120 char *p = alloca (strlen (comdat_symbol_id) + 64);
6122 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6123 comdat_symbol_id, comdat_symbol_number++);
6124 die->die_symbol = xstrdup (p);
6126 else
6127 die->die_symbol = gen_internal_sym ("LDIE");
6130 for (c = die->die_child; c != NULL; c = c->die_sib)
6131 assign_symbol_names (c);
6134 struct cu_hash_table_entry
6136 dw_die_ref cu;
6137 unsigned min_comdat_num, max_comdat_num;
6138 struct cu_hash_table_entry *next;
6141 /* Routines to manipulate hash table of CUs. */
6142 static hashval_t
6143 htab_cu_hash (const void *of)
6145 const struct cu_hash_table_entry *entry = of;
6147 return htab_hash_string (entry->cu->die_symbol);
6150 static int
6151 htab_cu_eq (const void *of1, const void *of2)
6153 const struct cu_hash_table_entry *entry1 = of1;
6154 const struct die_struct *entry2 = of2;
6156 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6159 static void
6160 htab_cu_del (void *what)
6162 struct cu_hash_table_entry *next, *entry = what;
6164 while (entry)
6166 next = entry->next;
6167 free (entry);
6168 entry = next;
6172 /* Check whether we have already seen this CU and set up SYM_NUM
6173 accordingly. */
6174 static int
6175 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6177 struct cu_hash_table_entry dummy;
6178 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6180 dummy.max_comdat_num = 0;
6182 slot = (struct cu_hash_table_entry **)
6183 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6184 INSERT);
6185 entry = *slot;
6187 for (; entry; last = entry, entry = entry->next)
6189 if (same_die_p_wrap (cu, entry->cu))
6190 break;
6193 if (entry)
6195 *sym_num = entry->min_comdat_num;
6196 return 1;
6199 entry = xcalloc (1, sizeof (struct cu_hash_table_entry));
6200 entry->cu = cu;
6201 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6202 entry->next = *slot;
6203 *slot = entry;
6205 return 0;
6208 /* Record SYM_NUM to record of CU in HTABLE. */
6209 static void
6210 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6212 struct cu_hash_table_entry **slot, *entry;
6214 slot = (struct cu_hash_table_entry **)
6215 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6216 NO_INSERT);
6217 entry = *slot;
6219 entry->max_comdat_num = sym_num;
6222 /* Traverse the DIE (which is always comp_unit_die), and set up
6223 additional compilation units for each of the include files we see
6224 bracketed by BINCL/EINCL. */
6226 static void
6227 break_out_includes (dw_die_ref die)
6229 dw_die_ref *ptr;
6230 dw_die_ref unit = NULL;
6231 limbo_die_node *node, **pnode;
6232 htab_t cu_hash_table;
6234 for (ptr = &(die->die_child); *ptr;)
6236 dw_die_ref c = *ptr;
6238 if (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6239 || (unit && is_comdat_die (c)))
6241 /* This DIE is for a secondary CU; remove it from the main one. */
6242 *ptr = c->die_sib;
6244 if (c->die_tag == DW_TAG_GNU_BINCL)
6246 unit = push_new_compile_unit (unit, c);
6247 free_die (c);
6249 else if (c->die_tag == DW_TAG_GNU_EINCL)
6251 unit = pop_compile_unit (unit);
6252 free_die (c);
6254 else
6255 add_child_die (unit, c);
6257 else
6259 /* Leave this DIE in the main CU. */
6260 ptr = &(c->die_sib);
6261 continue;
6265 #if 0
6266 /* We can only use this in debugging, since the frontend doesn't check
6267 to make sure that we leave every include file we enter. */
6268 if (unit != NULL)
6269 abort ();
6270 #endif
6272 assign_symbol_names (die);
6273 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6274 for (node = limbo_die_list, pnode = &limbo_die_list;
6275 node;
6276 node = node->next)
6278 int is_dupl;
6280 compute_section_prefix (node->die);
6281 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6282 &comdat_symbol_number);
6283 assign_symbol_names (node->die);
6284 if (is_dupl)
6285 *pnode = node->next;
6286 else
6288 pnode = &node->next;
6289 record_comdat_symbol_number (node->die, cu_hash_table,
6290 comdat_symbol_number);
6293 htab_delete (cu_hash_table);
6296 /* Traverse the DIE and add a sibling attribute if it may have the
6297 effect of speeding up access to siblings. To save some space,
6298 avoid generating sibling attributes for DIE's without children. */
6300 static void
6301 add_sibling_attributes (dw_die_ref die)
6303 dw_die_ref c;
6305 if (die->die_tag != DW_TAG_compile_unit
6306 && die->die_sib && die->die_child != NULL)
6307 /* Add the sibling link to the front of the attribute list. */
6308 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6310 for (c = die->die_child; c != NULL; c = c->die_sib)
6311 add_sibling_attributes (c);
6314 /* Output all location lists for the DIE and its children. */
6316 static void
6317 output_location_lists (dw_die_ref die)
6319 dw_die_ref c;
6320 dw_attr_ref d_attr;
6322 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6323 if (AT_class (d_attr) == dw_val_class_loc_list)
6324 output_loc_list (AT_loc_list (d_attr));
6326 for (c = die->die_child; c != NULL; c = c->die_sib)
6327 output_location_lists (c);
6331 /* The format of each DIE (and its attribute value pairs) is encoded in an
6332 abbreviation table. This routine builds the abbreviation table and assigns
6333 a unique abbreviation id for each abbreviation entry. The children of each
6334 die are visited recursively. */
6336 static void
6337 build_abbrev_table (dw_die_ref die)
6339 unsigned long abbrev_id;
6340 unsigned int n_alloc;
6341 dw_die_ref c;
6342 dw_attr_ref d_attr, a_attr;
6344 /* Scan the DIE references, and mark as external any that refer to
6345 DIEs from other CUs (i.e. those which are not marked). */
6346 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6347 if (AT_class (d_attr) == dw_val_class_die_ref
6348 && AT_ref (d_attr)->die_mark == 0)
6350 if (AT_ref (d_attr)->die_symbol == 0)
6351 abort ();
6353 set_AT_ref_external (d_attr, 1);
6356 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6358 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6360 if (abbrev->die_tag == die->die_tag)
6362 if ((abbrev->die_child != NULL) == (die->die_child != NULL))
6364 a_attr = abbrev->die_attr;
6365 d_attr = die->die_attr;
6367 while (a_attr != NULL && d_attr != NULL)
6369 if ((a_attr->dw_attr != d_attr->dw_attr)
6370 || (value_format (a_attr) != value_format (d_attr)))
6371 break;
6373 a_attr = a_attr->dw_attr_next;
6374 d_attr = d_attr->dw_attr_next;
6377 if (a_attr == NULL && d_attr == NULL)
6378 break;
6383 if (abbrev_id >= abbrev_die_table_in_use)
6385 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6387 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6388 abbrev_die_table = ggc_realloc (abbrev_die_table,
6389 sizeof (dw_die_ref) * n_alloc);
6391 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6392 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6393 abbrev_die_table_allocated = n_alloc;
6396 ++abbrev_die_table_in_use;
6397 abbrev_die_table[abbrev_id] = die;
6400 die->die_abbrev = abbrev_id;
6401 for (c = die->die_child; c != NULL; c = c->die_sib)
6402 build_abbrev_table (c);
6405 /* Return the power-of-two number of bytes necessary to represent VALUE. */
6407 static int
6408 constant_size (long unsigned int value)
6410 int log;
6412 if (value == 0)
6413 log = 0;
6414 else
6415 log = floor_log2 (value);
6417 log = log / 8;
6418 log = 1 << (floor_log2 (log) + 1);
6420 return log;
6423 /* Return the size of a DIE as it is represented in the
6424 .debug_info section. */
6426 static unsigned long
6427 size_of_die (dw_die_ref die)
6429 unsigned long size = 0;
6430 dw_attr_ref a;
6432 size += size_of_uleb128 (die->die_abbrev);
6433 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6435 switch (AT_class (a))
6437 case dw_val_class_addr:
6438 size += DWARF2_ADDR_SIZE;
6439 break;
6440 case dw_val_class_offset:
6441 size += DWARF_OFFSET_SIZE;
6442 break;
6443 case dw_val_class_loc:
6445 unsigned long lsize = size_of_locs (AT_loc (a));
6447 /* Block length. */
6448 size += constant_size (lsize);
6449 size += lsize;
6451 break;
6452 case dw_val_class_loc_list:
6453 size += DWARF_OFFSET_SIZE;
6454 break;
6455 case dw_val_class_range_list:
6456 size += DWARF_OFFSET_SIZE;
6457 break;
6458 case dw_val_class_const:
6459 size += size_of_sleb128 (AT_int (a));
6460 break;
6461 case dw_val_class_unsigned_const:
6462 size += constant_size (AT_unsigned (a));
6463 break;
6464 case dw_val_class_long_long:
6465 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6466 break;
6467 case dw_val_class_vec:
6468 size += 1 + (a->dw_attr_val.v.val_vec.length
6469 * a->dw_attr_val.v.val_vec.elt_size); /* block */
6470 break;
6471 case dw_val_class_flag:
6472 size += 1;
6473 break;
6474 case dw_val_class_die_ref:
6475 if (AT_ref_external (a))
6476 size += DWARF2_ADDR_SIZE;
6477 else
6478 size += DWARF_OFFSET_SIZE;
6479 break;
6480 case dw_val_class_fde_ref:
6481 size += DWARF_OFFSET_SIZE;
6482 break;
6483 case dw_val_class_lbl_id:
6484 size += DWARF2_ADDR_SIZE;
6485 break;
6486 case dw_val_class_lbl_offset:
6487 size += DWARF_OFFSET_SIZE;
6488 break;
6489 case dw_val_class_str:
6490 if (AT_string_form (a) == DW_FORM_strp)
6491 size += DWARF_OFFSET_SIZE;
6492 else
6493 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6494 break;
6495 default:
6496 abort ();
6500 return size;
6503 /* Size the debugging information associated with a given DIE. Visits the
6504 DIE's children recursively. Updates the global variable next_die_offset, on
6505 each time through. Uses the current value of next_die_offset to update the
6506 die_offset field in each DIE. */
6508 static void
6509 calc_die_sizes (dw_die_ref die)
6511 dw_die_ref c;
6513 die->die_offset = next_die_offset;
6514 next_die_offset += size_of_die (die);
6516 for (c = die->die_child; c != NULL; c = c->die_sib)
6517 calc_die_sizes (c);
6519 if (die->die_child != NULL)
6520 /* Count the null byte used to terminate sibling lists. */
6521 next_die_offset += 1;
6524 /* Set the marks for a die and its children. We do this so
6525 that we know whether or not a reference needs to use FORM_ref_addr; only
6526 DIEs in the same CU will be marked. We used to clear out the offset
6527 and use that as the flag, but ran into ordering problems. */
6529 static void
6530 mark_dies (dw_die_ref die)
6532 dw_die_ref c;
6534 if (die->die_mark)
6535 abort ();
6537 die->die_mark = 1;
6538 for (c = die->die_child; c; c = c->die_sib)
6539 mark_dies (c);
6542 /* Clear the marks for a die and its children. */
6544 static void
6545 unmark_dies (dw_die_ref die)
6547 dw_die_ref c;
6549 if (!die->die_mark)
6550 abort ();
6552 die->die_mark = 0;
6553 for (c = die->die_child; c; c = c->die_sib)
6554 unmark_dies (c);
6557 /* Clear the marks for a die, its children and referred dies. */
6559 static void
6560 unmark_all_dies (dw_die_ref die)
6562 dw_die_ref c;
6563 dw_attr_ref a;
6565 if (!die->die_mark)
6566 return;
6567 die->die_mark = 0;
6569 for (c = die->die_child; c; c = c->die_sib)
6570 unmark_all_dies (c);
6572 for (a = die->die_attr; a; a = a->dw_attr_next)
6573 if (AT_class (a) == dw_val_class_die_ref)
6574 unmark_all_dies (AT_ref (a));
6577 /* Return the size of the .debug_pubnames table generated for the
6578 compilation unit. */
6580 static unsigned long
6581 size_of_pubnames (void)
6583 unsigned long size;
6584 unsigned i;
6586 size = DWARF_PUBNAMES_HEADER_SIZE;
6587 for (i = 0; i < pubname_table_in_use; i++)
6589 pubname_ref p = &pubname_table[i];
6590 size += DWARF_OFFSET_SIZE + strlen (p->name) + 1;
6593 size += DWARF_OFFSET_SIZE;
6594 return size;
6597 /* Return the size of the information in the .debug_aranges section. */
6599 static unsigned long
6600 size_of_aranges (void)
6602 unsigned long size;
6604 size = DWARF_ARANGES_HEADER_SIZE;
6606 /* Count the address/length pair for this compilation unit. */
6607 size += 2 * DWARF2_ADDR_SIZE;
6608 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6610 /* Count the two zero words used to terminated the address range table. */
6611 size += 2 * DWARF2_ADDR_SIZE;
6612 return size;
6615 /* Select the encoding of an attribute value. */
6617 static enum dwarf_form
6618 value_format (dw_attr_ref a)
6620 switch (a->dw_attr_val.val_class)
6622 case dw_val_class_addr:
6623 return DW_FORM_addr;
6624 case dw_val_class_range_list:
6625 case dw_val_class_offset:
6626 if (DWARF_OFFSET_SIZE == 4)
6627 return DW_FORM_data4;
6628 if (DWARF_OFFSET_SIZE == 8)
6629 return DW_FORM_data8;
6630 abort ();
6631 case dw_val_class_loc_list:
6632 /* FIXME: Could be DW_FORM_data8, with a > 32 bit size
6633 .debug_loc section */
6634 return DW_FORM_data4;
6635 case dw_val_class_loc:
6636 switch (constant_size (size_of_locs (AT_loc (a))))
6638 case 1:
6639 return DW_FORM_block1;
6640 case 2:
6641 return DW_FORM_block2;
6642 default:
6643 abort ();
6645 case dw_val_class_const:
6646 return DW_FORM_sdata;
6647 case dw_val_class_unsigned_const:
6648 switch (constant_size (AT_unsigned (a)))
6650 case 1:
6651 return DW_FORM_data1;
6652 case 2:
6653 return DW_FORM_data2;
6654 case 4:
6655 return DW_FORM_data4;
6656 case 8:
6657 return DW_FORM_data8;
6658 default:
6659 abort ();
6661 case dw_val_class_long_long:
6662 return DW_FORM_block1;
6663 case dw_val_class_vec:
6664 return DW_FORM_block1;
6665 case dw_val_class_flag:
6666 return DW_FORM_flag;
6667 case dw_val_class_die_ref:
6668 if (AT_ref_external (a))
6669 return DW_FORM_ref_addr;
6670 else
6671 return DW_FORM_ref;
6672 case dw_val_class_fde_ref:
6673 return DW_FORM_data;
6674 case dw_val_class_lbl_id:
6675 return DW_FORM_addr;
6676 case dw_val_class_lbl_offset:
6677 return DW_FORM_data;
6678 case dw_val_class_str:
6679 return AT_string_form (a);
6681 default:
6682 abort ();
6686 /* Output the encoding of an attribute value. */
6688 static void
6689 output_value_format (dw_attr_ref a)
6691 enum dwarf_form form = value_format (a);
6693 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6696 /* Output the .debug_abbrev section which defines the DIE abbreviation
6697 table. */
6699 static void
6700 output_abbrev_section (void)
6702 unsigned long abbrev_id;
6704 dw_attr_ref a_attr;
6706 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6708 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6710 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6711 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6712 dwarf_tag_name (abbrev->die_tag));
6714 if (abbrev->die_child != NULL)
6715 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6716 else
6717 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6719 for (a_attr = abbrev->die_attr; a_attr != NULL;
6720 a_attr = a_attr->dw_attr_next)
6722 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6723 dwarf_attr_name (a_attr->dw_attr));
6724 output_value_format (a_attr);
6727 dw2_asm_output_data (1, 0, NULL);
6728 dw2_asm_output_data (1, 0, NULL);
6731 /* Terminate the table. */
6732 dw2_asm_output_data (1, 0, NULL);
6735 /* Output a symbol we can use to refer to this DIE from another CU. */
6737 static inline void
6738 output_die_symbol (dw_die_ref die)
6740 char *sym = die->die_symbol;
6742 if (sym == 0)
6743 return;
6745 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6746 /* We make these global, not weak; if the target doesn't support
6747 .linkonce, it doesn't support combining the sections, so debugging
6748 will break. */
6749 targetm.asm_out.globalize_label (asm_out_file, sym);
6751 ASM_OUTPUT_LABEL (asm_out_file, sym);
6754 /* Return a new location list, given the begin and end range, and the
6755 expression. gensym tells us whether to generate a new internal symbol for
6756 this location list node, which is done for the head of the list only. */
6758 static inline dw_loc_list_ref
6759 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6760 const char *section, unsigned int gensym)
6762 dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
6764 retlist->begin = begin;
6765 retlist->end = end;
6766 retlist->expr = expr;
6767 retlist->section = section;
6768 if (gensym)
6769 retlist->ll_symbol = gen_internal_sym ("LLST");
6771 return retlist;
6774 /* Add a location description expression to a location list. */
6776 static inline void
6777 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6778 const char *begin, const char *end,
6779 const char *section)
6781 dw_loc_list_ref *d;
6783 /* Find the end of the chain. */
6784 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
6787 /* Add a new location list node to the list. */
6788 *d = new_loc_list (descr, begin, end, section, 0);
6791 /* Output the location list given to us. */
6793 static void
6794 output_loc_list (dw_loc_list_ref list_head)
6796 dw_loc_list_ref curr = list_head;
6798 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
6800 /* Walk the location list, and output each range + expression. */
6801 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
6803 unsigned long size;
6804 if (separate_line_info_table_in_use == 0)
6806 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
6807 "Location list begin address (%s)",
6808 list_head->ll_symbol);
6809 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
6810 "Location list end address (%s)",
6811 list_head->ll_symbol);
6813 else
6815 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
6816 "Location list begin address (%s)",
6817 list_head->ll_symbol);
6818 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
6819 "Location list end address (%s)",
6820 list_head->ll_symbol);
6822 size = size_of_locs (curr->expr);
6824 /* Output the block length for this list of location operations. */
6825 if (size > 0xffff)
6826 abort ();
6827 dw2_asm_output_data (2, size, "%s", "Location expression size");
6829 output_loc_sequence (curr->expr);
6832 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6833 "Location list terminator begin (%s)",
6834 list_head->ll_symbol);
6835 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6836 "Location list terminator end (%s)",
6837 list_head->ll_symbol);
6840 /* Output the DIE and its attributes. Called recursively to generate
6841 the definitions of each child DIE. */
6843 static void
6844 output_die (dw_die_ref die)
6846 dw_attr_ref a;
6847 dw_die_ref c;
6848 unsigned long size;
6850 /* If someone in another CU might refer to us, set up a symbol for
6851 them to point to. */
6852 if (die->die_symbol)
6853 output_die_symbol (die);
6855 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
6856 die->die_offset, dwarf_tag_name (die->die_tag));
6858 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6860 const char *name = dwarf_attr_name (a->dw_attr);
6862 switch (AT_class (a))
6864 case dw_val_class_addr:
6865 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
6866 break;
6868 case dw_val_class_offset:
6869 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
6870 "%s", name);
6871 break;
6873 case dw_val_class_range_list:
6875 char *p = strchr (ranges_section_label, '\0');
6877 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
6878 a->dw_attr_val.v.val_offset);
6879 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
6880 "%s", name);
6881 *p = '\0';
6883 break;
6885 case dw_val_class_loc:
6886 size = size_of_locs (AT_loc (a));
6888 /* Output the block length for this list of location operations. */
6889 dw2_asm_output_data (constant_size (size), size, "%s", name);
6891 output_loc_sequence (AT_loc (a));
6892 break;
6894 case dw_val_class_const:
6895 /* ??? It would be slightly more efficient to use a scheme like is
6896 used for unsigned constants below, but gdb 4.x does not sign
6897 extend. Gdb 5.x does sign extend. */
6898 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
6899 break;
6901 case dw_val_class_unsigned_const:
6902 dw2_asm_output_data (constant_size (AT_unsigned (a)),
6903 AT_unsigned (a), "%s", name);
6904 break;
6906 case dw_val_class_long_long:
6908 unsigned HOST_WIDE_INT first, second;
6910 dw2_asm_output_data (1,
6911 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6912 "%s", name);
6914 if (WORDS_BIG_ENDIAN)
6916 first = a->dw_attr_val.v.val_long_long.hi;
6917 second = a->dw_attr_val.v.val_long_long.low;
6919 else
6921 first = a->dw_attr_val.v.val_long_long.low;
6922 second = a->dw_attr_val.v.val_long_long.hi;
6925 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6926 first, "long long constant");
6927 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6928 second, NULL);
6930 break;
6932 case dw_val_class_vec:
6934 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
6935 unsigned int len = a->dw_attr_val.v.val_vec.length;
6936 unsigned int i;
6937 unsigned char *p;
6939 dw2_asm_output_data (1, len * elt_size, "%s", name);
6940 if (elt_size > sizeof (HOST_WIDE_INT))
6942 elt_size /= 2;
6943 len *= 2;
6945 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
6946 i < len;
6947 i++, p += elt_size)
6948 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
6949 "fp or vector constant word %u", i);
6950 break;
6953 case dw_val_class_flag:
6954 dw2_asm_output_data (1, AT_flag (a), "%s", name);
6955 break;
6957 case dw_val_class_loc_list:
6959 char *sym = AT_loc_list (a)->ll_symbol;
6961 if (sym == 0)
6962 abort ();
6963 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, "%s", name);
6965 break;
6967 case dw_val_class_die_ref:
6968 if (AT_ref_external (a))
6970 char *sym = AT_ref (a)->die_symbol;
6972 if (sym == 0)
6973 abort ();
6974 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, "%s", name);
6976 else if (AT_ref (a)->die_offset == 0)
6977 abort ();
6978 else
6979 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
6980 "%s", name);
6981 break;
6983 case dw_val_class_fde_ref:
6985 char l1[20];
6987 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
6988 a->dw_attr_val.v.val_fde_index * 2);
6989 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, "%s", name);
6991 break;
6993 case dw_val_class_lbl_id:
6994 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
6995 break;
6997 case dw_val_class_lbl_offset:
6998 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a), "%s", name);
6999 break;
7001 case dw_val_class_str:
7002 if (AT_string_form (a) == DW_FORM_strp)
7003 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7004 a->dw_attr_val.v.val_str->label,
7005 "%s: \"%s\"", name, AT_string (a));
7006 else
7007 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7008 break;
7010 default:
7011 abort ();
7015 for (c = die->die_child; c != NULL; c = c->die_sib)
7016 output_die (c);
7018 /* Add null byte to terminate sibling list. */
7019 if (die->die_child != NULL)
7020 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7021 die->die_offset);
7024 /* Output the compilation unit that appears at the beginning of the
7025 .debug_info section, and precedes the DIE descriptions. */
7027 static void
7028 output_compilation_unit_header (void)
7030 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7031 dw2_asm_output_data (4, 0xffffffff,
7032 "Initial length escape value indicating 64-bit DWARF extension");
7033 dw2_asm_output_data (DWARF_OFFSET_SIZE,
7034 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7035 "Length of Compilation Unit Info");
7036 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7037 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7038 "Offset Into Abbrev. Section");
7039 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7042 /* Output the compilation unit DIE and its children. */
7044 static void
7045 output_comp_unit (dw_die_ref die, int output_if_empty)
7047 const char *secname;
7048 char *oldsym, *tmp;
7050 /* Unless we are outputting main CU, we may throw away empty ones. */
7051 if (!output_if_empty && die->die_child == NULL)
7052 return;
7054 /* Even if there are no children of this DIE, we must output the information
7055 about the compilation unit. Otherwise, on an empty translation unit, we
7056 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
7057 will then complain when examining the file. First mark all the DIEs in
7058 this CU so we know which get local refs. */
7059 mark_dies (die);
7061 build_abbrev_table (die);
7063 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
7064 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7065 calc_die_sizes (die);
7067 oldsym = die->die_symbol;
7068 if (oldsym)
7070 tmp = alloca (strlen (oldsym) + 24);
7072 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7073 secname = tmp;
7074 die->die_symbol = NULL;
7076 else
7077 secname = (const char *) DEBUG_INFO_SECTION;
7079 /* Output debugging information. */
7080 named_section_flags (secname, SECTION_DEBUG);
7081 output_compilation_unit_header ();
7082 output_die (die);
7084 /* Leave the marks on the main CU, so we can check them in
7085 output_pubnames. */
7086 if (oldsym)
7088 unmark_dies (die);
7089 die->die_symbol = oldsym;
7093 /* The DWARF2 pubname for a nested thingy looks like "A::f". The
7094 output of lang_hooks.decl_printable_name for C++ looks like
7095 "A::f(int)". Let's drop the argument list, and maybe the scope. */
7097 static const char *
7098 dwarf2_name (tree decl, int scope)
7100 return lang_hooks.decl_printable_name (decl, scope ? 1 : 0);
7103 /* Add a new entry to .debug_pubnames if appropriate. */
7105 static void
7106 add_pubname (tree decl, dw_die_ref die)
7108 pubname_ref p;
7110 if (! TREE_PUBLIC (decl))
7111 return;
7113 if (pubname_table_in_use == pubname_table_allocated)
7115 pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
7116 pubname_table
7117 = ggc_realloc (pubname_table,
7118 (pubname_table_allocated * sizeof (pubname_entry)));
7119 memset (pubname_table + pubname_table_in_use, 0,
7120 PUBNAME_TABLE_INCREMENT * sizeof (pubname_entry));
7123 p = &pubname_table[pubname_table_in_use++];
7124 p->die = die;
7125 p->name = xstrdup (dwarf2_name (decl, 1));
7128 /* Output the public names table used to speed up access to externally
7129 visible names. For now, only generate entries for externally
7130 visible procedures. */
7132 static void
7133 output_pubnames (void)
7135 unsigned i;
7136 unsigned long pubnames_length = size_of_pubnames ();
7138 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7139 dw2_asm_output_data (4, 0xffffffff,
7140 "Initial length escape value indicating 64-bit DWARF extension");
7141 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7142 "Length of Public Names Info");
7143 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7144 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7145 "Offset of Compilation Unit Info");
7146 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7147 "Compilation Unit Length");
7149 for (i = 0; i < pubname_table_in_use; i++)
7151 pubname_ref pub = &pubname_table[i];
7153 /* We shouldn't see pubnames for DIEs outside of the main CU. */
7154 if (pub->die->die_mark == 0)
7155 abort ();
7157 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7158 "DIE offset");
7160 dw2_asm_output_nstring (pub->name, -1, "external name");
7163 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7166 /* Add a new entry to .debug_aranges if appropriate. */
7168 static void
7169 add_arange (tree decl, dw_die_ref die)
7171 if (! DECL_SECTION_NAME (decl))
7172 return;
7174 if (arange_table_in_use == arange_table_allocated)
7176 arange_table_allocated += ARANGE_TABLE_INCREMENT;
7177 arange_table = ggc_realloc (arange_table,
7178 (arange_table_allocated
7179 * sizeof (dw_die_ref)));
7180 memset (arange_table + arange_table_in_use, 0,
7181 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7184 arange_table[arange_table_in_use++] = die;
7187 /* Output the information that goes into the .debug_aranges table.
7188 Namely, define the beginning and ending address range of the
7189 text section generated for this compilation unit. */
7191 static void
7192 output_aranges (void)
7194 unsigned i;
7195 unsigned long aranges_length = size_of_aranges ();
7197 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7198 dw2_asm_output_data (4, 0xffffffff,
7199 "Initial length escape value indicating 64-bit DWARF extension");
7200 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7201 "Length of Address Ranges Info");
7202 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7203 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7204 "Offset of Compilation Unit Info");
7205 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7206 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7208 /* We need to align to twice the pointer size here. */
7209 if (DWARF_ARANGES_PAD_SIZE)
7211 /* Pad using a 2 byte words so that padding is correct for any
7212 pointer size. */
7213 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7214 2 * DWARF2_ADDR_SIZE);
7215 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7216 dw2_asm_output_data (2, 0, NULL);
7219 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7220 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7221 text_section_label, "Length");
7223 for (i = 0; i < arange_table_in_use; i++)
7225 dw_die_ref die = arange_table[i];
7227 /* We shouldn't see aranges for DIEs outside of the main CU. */
7228 if (die->die_mark == 0)
7229 abort ();
7231 if (die->die_tag == DW_TAG_subprogram)
7233 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7234 "Address");
7235 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7236 get_AT_low_pc (die), "Length");
7238 else
7240 /* A static variable; extract the symbol from DW_AT_location.
7241 Note that this code isn't currently hit, as we only emit
7242 aranges for functions (jason 9/23/99). */
7243 dw_attr_ref a = get_AT (die, DW_AT_location);
7244 dw_loc_descr_ref loc;
7246 if (! a || AT_class (a) != dw_val_class_loc)
7247 abort ();
7249 loc = AT_loc (a);
7250 if (loc->dw_loc_opc != DW_OP_addr)
7251 abort ();
7253 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7254 loc->dw_loc_oprnd1.v.val_addr, "Address");
7255 dw2_asm_output_data (DWARF2_ADDR_SIZE,
7256 get_AT_unsigned (die, DW_AT_byte_size),
7257 "Length");
7261 /* Output the terminator words. */
7262 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7263 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7266 /* Add a new entry to .debug_ranges. Return the offset at which it
7267 was placed. */
7269 static unsigned int
7270 add_ranges (tree block)
7272 unsigned int in_use = ranges_table_in_use;
7274 if (in_use == ranges_table_allocated)
7276 ranges_table_allocated += RANGES_TABLE_INCREMENT;
7277 ranges_table
7278 = ggc_realloc (ranges_table, (ranges_table_allocated
7279 * sizeof (struct dw_ranges_struct)));
7280 memset (ranges_table + ranges_table_in_use, 0,
7281 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7284 ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7285 ranges_table_in_use = in_use + 1;
7287 return in_use * 2 * DWARF2_ADDR_SIZE;
7290 static void
7291 output_ranges (void)
7293 unsigned i;
7294 static const char *const start_fmt = "Offset 0x%x";
7295 const char *fmt = start_fmt;
7297 for (i = 0; i < ranges_table_in_use; i++)
7299 int block_num = ranges_table[i].block_num;
7301 if (block_num)
7303 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7304 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7306 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7307 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7309 /* If all code is in the text section, then the compilation
7310 unit base address defaults to DW_AT_low_pc, which is the
7311 base of the text section. */
7312 if (separate_line_info_table_in_use == 0)
7314 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7315 text_section_label,
7316 fmt, i * 2 * DWARF2_ADDR_SIZE);
7317 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7318 text_section_label, NULL);
7321 /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7322 compilation unit base address to zero, which allows us to
7323 use absolute addresses, and not worry about whether the
7324 target supports cross-section arithmetic. */
7325 else
7327 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7328 fmt, i * 2 * DWARF2_ADDR_SIZE);
7329 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7332 fmt = NULL;
7334 else
7336 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7337 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7338 fmt = start_fmt;
7343 /* Data structure containing information about input files. */
7344 struct file_info
7346 char *path; /* Complete file name. */
7347 char *fname; /* File name part. */
7348 int length; /* Length of entire string. */
7349 int file_idx; /* Index in input file table. */
7350 int dir_idx; /* Index in directory table. */
7353 /* Data structure containing information about directories with source
7354 files. */
7355 struct dir_info
7357 char *path; /* Path including directory name. */
7358 int length; /* Path length. */
7359 int prefix; /* Index of directory entry which is a prefix. */
7360 int count; /* Number of files in this directory. */
7361 int dir_idx; /* Index of directory used as base. */
7362 int used; /* Used in the end? */
7365 /* Callback function for file_info comparison. We sort by looking at
7366 the directories in the path. */
7368 static int
7369 file_info_cmp (const void *p1, const void *p2)
7371 const struct file_info *s1 = p1;
7372 const struct file_info *s2 = p2;
7373 unsigned char *cp1;
7374 unsigned char *cp2;
7376 /* Take care of file names without directories. We need to make sure that
7377 we return consistent values to qsort since some will get confused if
7378 we return the same value when identical operands are passed in opposite
7379 orders. So if neither has a directory, return 0 and otherwise return
7380 1 or -1 depending on which one has the directory. */
7381 if ((s1->path == s1->fname || s2->path == s2->fname))
7382 return (s2->path == s2->fname) - (s1->path == s1->fname);
7384 cp1 = (unsigned char *) s1->path;
7385 cp2 = (unsigned char *) s2->path;
7387 while (1)
7389 ++cp1;
7390 ++cp2;
7391 /* Reached the end of the first path? If so, handle like above. */
7392 if ((cp1 == (unsigned char *) s1->fname)
7393 || (cp2 == (unsigned char *) s2->fname))
7394 return ((cp2 == (unsigned char *) s2->fname)
7395 - (cp1 == (unsigned char *) s1->fname));
7397 /* Character of current path component the same? */
7398 else if (*cp1 != *cp2)
7399 return *cp1 - *cp2;
7403 /* Output the directory table and the file name table. We try to minimize
7404 the total amount of memory needed. A heuristic is used to avoid large
7405 slowdowns with many input files. */
7407 static void
7408 output_file_names (void)
7410 struct file_info *files;
7411 struct dir_info *dirs;
7412 int *saved;
7413 int *savehere;
7414 int *backmap;
7415 size_t ndirs;
7416 int idx_offset;
7417 size_t i;
7418 int idx;
7420 /* Handle the case where file_table is empty. */
7421 if (VARRAY_ACTIVE_SIZE (file_table) <= 1)
7423 dw2_asm_output_data (1, 0, "End directory table");
7424 dw2_asm_output_data (1, 0, "End file name table");
7425 return;
7428 /* Allocate the various arrays we need. */
7429 files = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct file_info));
7430 dirs = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct dir_info));
7432 /* Sort the file names. */
7433 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7435 char *f;
7437 /* Skip all leading "./". */
7438 f = VARRAY_CHAR_PTR (file_table, i);
7439 while (f[0] == '.' && f[1] == '/')
7440 f += 2;
7442 /* Create a new array entry. */
7443 files[i].path = f;
7444 files[i].length = strlen (f);
7445 files[i].file_idx = i;
7447 /* Search for the file name part. */
7448 f = strrchr (f, '/');
7449 files[i].fname = f == NULL ? files[i].path : f + 1;
7452 qsort (files + 1, VARRAY_ACTIVE_SIZE (file_table) - 1,
7453 sizeof (files[0]), file_info_cmp);
7455 /* Find all the different directories used. */
7456 dirs[0].path = files[1].path;
7457 dirs[0].length = files[1].fname - files[1].path;
7458 dirs[0].prefix = -1;
7459 dirs[0].count = 1;
7460 dirs[0].dir_idx = 0;
7461 dirs[0].used = 0;
7462 files[1].dir_idx = 0;
7463 ndirs = 1;
7465 for (i = 2; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7466 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7467 && memcmp (dirs[ndirs - 1].path, files[i].path,
7468 dirs[ndirs - 1].length) == 0)
7470 /* Same directory as last entry. */
7471 files[i].dir_idx = ndirs - 1;
7472 ++dirs[ndirs - 1].count;
7474 else
7476 size_t j;
7478 /* This is a new directory. */
7479 dirs[ndirs].path = files[i].path;
7480 dirs[ndirs].length = files[i].fname - files[i].path;
7481 dirs[ndirs].count = 1;
7482 dirs[ndirs].dir_idx = ndirs;
7483 dirs[ndirs].used = 0;
7484 files[i].dir_idx = ndirs;
7486 /* Search for a prefix. */
7487 dirs[ndirs].prefix = -1;
7488 for (j = 0; j < ndirs; j++)
7489 if (dirs[j].length < dirs[ndirs].length
7490 && dirs[j].length > 1
7491 && (dirs[ndirs].prefix == -1
7492 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7493 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7494 dirs[ndirs].prefix = j;
7496 ++ndirs;
7499 /* Now to the actual work. We have to find a subset of the directories which
7500 allow expressing the file name using references to the directory table
7501 with the least amount of characters. We do not do an exhaustive search
7502 where we would have to check out every combination of every single
7503 possible prefix. Instead we use a heuristic which provides nearly optimal
7504 results in most cases and never is much off. */
7505 saved = alloca (ndirs * sizeof (int));
7506 savehere = alloca (ndirs * sizeof (int));
7508 memset (saved, '\0', ndirs * sizeof (saved[0]));
7509 for (i = 0; i < ndirs; i++)
7511 size_t j;
7512 int total;
7514 /* We can always save some space for the current directory. But this
7515 does not mean it will be enough to justify adding the directory. */
7516 savehere[i] = dirs[i].length;
7517 total = (savehere[i] - saved[i]) * dirs[i].count;
7519 for (j = i + 1; j < ndirs; j++)
7521 savehere[j] = 0;
7522 if (saved[j] < dirs[i].length)
7524 /* Determine whether the dirs[i] path is a prefix of the
7525 dirs[j] path. */
7526 int k;
7528 k = dirs[j].prefix;
7529 while (k != -1 && k != (int) i)
7530 k = dirs[k].prefix;
7532 if (k == (int) i)
7534 /* Yes it is. We can possibly safe some memory but
7535 writing the filenames in dirs[j] relative to
7536 dirs[i]. */
7537 savehere[j] = dirs[i].length;
7538 total += (savehere[j] - saved[j]) * dirs[j].count;
7543 /* Check whether we can safe enough to justify adding the dirs[i]
7544 directory. */
7545 if (total > dirs[i].length + 1)
7547 /* It's worthwhile adding. */
7548 for (j = i; j < ndirs; j++)
7549 if (savehere[j] > 0)
7551 /* Remember how much we saved for this directory so far. */
7552 saved[j] = savehere[j];
7554 /* Remember the prefix directory. */
7555 dirs[j].dir_idx = i;
7560 /* We have to emit them in the order they appear in the file_table array
7561 since the index is used in the debug info generation. To do this
7562 efficiently we generate a back-mapping of the indices first. */
7563 backmap = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (int));
7564 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7566 backmap[files[i].file_idx] = i;
7568 /* Mark this directory as used. */
7569 dirs[dirs[files[i].dir_idx].dir_idx].used = 1;
7572 /* That was it. We are ready to emit the information. First emit the
7573 directory name table. We have to make sure the first actually emitted
7574 directory name has index one; zero is reserved for the current working
7575 directory. Make sure we do not confuse these indices with the one for the
7576 constructed table (even though most of the time they are identical). */
7577 idx = 1;
7578 idx_offset = dirs[0].length > 0 ? 1 : 0;
7579 for (i = 1 - idx_offset; i < ndirs; i++)
7580 if (dirs[i].used != 0)
7582 dirs[i].used = idx++;
7583 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7584 "Directory Entry: 0x%x", dirs[i].used);
7587 dw2_asm_output_data (1, 0, "End directory table");
7589 /* Correct the index for the current working directory entry if it
7590 exists. */
7591 if (idx_offset == 0)
7592 dirs[0].used = 0;
7594 /* Now write all the file names. */
7595 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7597 int file_idx = backmap[i];
7598 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7600 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7601 "File Entry: 0x%lx", (unsigned long) i);
7603 /* Include directory index. */
7604 dw2_asm_output_data_uleb128 (dirs[dir_idx].used, NULL);
7606 /* Modification time. */
7607 dw2_asm_output_data_uleb128 (0, NULL);
7609 /* File length in bytes. */
7610 dw2_asm_output_data_uleb128 (0, NULL);
7613 dw2_asm_output_data (1, 0, "End file name table");
7617 /* Output the source line number correspondence information. This
7618 information goes into the .debug_line section. */
7620 static void
7621 output_line_info (void)
7623 char l1[20], l2[20], p1[20], p2[20];
7624 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7625 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7626 unsigned opc;
7627 unsigned n_op_args;
7628 unsigned long lt_index;
7629 unsigned long current_line;
7630 long line_offset;
7631 long line_delta;
7632 unsigned long current_file;
7633 unsigned long function;
7635 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7636 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7637 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7638 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7640 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7641 dw2_asm_output_data (4, 0xffffffff,
7642 "Initial length escape value indicating 64-bit DWARF extension");
7643 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7644 "Length of Source Line Info");
7645 ASM_OUTPUT_LABEL (asm_out_file, l1);
7647 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7648 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7649 ASM_OUTPUT_LABEL (asm_out_file, p1);
7651 /* Define the architecture-dependent minimum instruction length (in
7652 bytes). In this implementation of DWARF, this field is used for
7653 information purposes only. Since GCC generates assembly language,
7654 we have no a priori knowledge of how many instruction bytes are
7655 generated for each source line, and therefore can use only the
7656 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7657 commands. Accordingly, we fix this as `1', which is "correct
7658 enough" for all architectures, and don't let the target override. */
7659 dw2_asm_output_data (1, 1,
7660 "Minimum Instruction Length");
7662 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7663 "Default is_stmt_start flag");
7664 dw2_asm_output_data (1, DWARF_LINE_BASE,
7665 "Line Base Value (Special Opcodes)");
7666 dw2_asm_output_data (1, DWARF_LINE_RANGE,
7667 "Line Range Value (Special Opcodes)");
7668 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7669 "Special Opcode Base");
7671 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7673 switch (opc)
7675 case DW_LNS_advance_pc:
7676 case DW_LNS_advance_line:
7677 case DW_LNS_set_file:
7678 case DW_LNS_set_column:
7679 case DW_LNS_fixed_advance_pc:
7680 n_op_args = 1;
7681 break;
7682 default:
7683 n_op_args = 0;
7684 break;
7687 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
7688 opc, n_op_args);
7691 /* Write out the information about the files we use. */
7692 output_file_names ();
7693 ASM_OUTPUT_LABEL (asm_out_file, p2);
7695 /* We used to set the address register to the first location in the text
7696 section here, but that didn't accomplish anything since we already
7697 have a line note for the opening brace of the first function. */
7699 /* Generate the line number to PC correspondence table, encoded as
7700 a series of state machine operations. */
7701 current_file = 1;
7702 current_line = 1;
7703 strcpy (prev_line_label, text_section_label);
7704 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
7706 dw_line_info_ref line_info = &line_info_table[lt_index];
7708 #if 0
7709 /* Disable this optimization for now; GDB wants to see two line notes
7710 at the beginning of a function so it can find the end of the
7711 prologue. */
7713 /* Don't emit anything for redundant notes. Just updating the
7714 address doesn't accomplish anything, because we already assume
7715 that anything after the last address is this line. */
7716 if (line_info->dw_line_num == current_line
7717 && line_info->dw_file_num == current_file)
7718 continue;
7719 #endif
7721 /* Emit debug info for the address of the current line.
7723 Unfortunately, we have little choice here currently, and must always
7724 use the most general form. GCC does not know the address delta
7725 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
7726 attributes which will give an upper bound on the address range. We
7727 could perhaps use length attributes to determine when it is safe to
7728 use DW_LNS_fixed_advance_pc. */
7730 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
7731 if (0)
7733 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
7734 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7735 "DW_LNS_fixed_advance_pc");
7736 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7738 else
7740 /* This can handle any delta. This takes
7741 4+DWARF2_ADDR_SIZE bytes. */
7742 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7743 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7744 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7745 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7748 strcpy (prev_line_label, line_label);
7750 /* Emit debug info for the source file of the current line, if
7751 different from the previous line. */
7752 if (line_info->dw_file_num != current_file)
7754 current_file = line_info->dw_file_num;
7755 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7756 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7757 VARRAY_CHAR_PTR (file_table,
7758 current_file));
7761 /* Emit debug info for the current line number, choosing the encoding
7762 that uses the least amount of space. */
7763 if (line_info->dw_line_num != current_line)
7765 line_offset = line_info->dw_line_num - current_line;
7766 line_delta = line_offset - DWARF_LINE_BASE;
7767 current_line = line_info->dw_line_num;
7768 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7769 /* This can handle deltas from -10 to 234, using the current
7770 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
7771 takes 1 byte. */
7772 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7773 "line %lu", current_line);
7774 else
7776 /* This can handle any delta. This takes at least 4 bytes,
7777 depending on the value being encoded. */
7778 dw2_asm_output_data (1, DW_LNS_advance_line,
7779 "advance to line %lu", current_line);
7780 dw2_asm_output_data_sleb128 (line_offset, NULL);
7781 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7784 else
7785 /* We still need to start a new row, so output a copy insn. */
7786 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7789 /* Emit debug info for the address of the end of the function. */
7790 if (0)
7792 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7793 "DW_LNS_fixed_advance_pc");
7794 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
7796 else
7798 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7799 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7800 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7801 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
7804 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7805 dw2_asm_output_data_uleb128 (1, NULL);
7806 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7808 function = 0;
7809 current_file = 1;
7810 current_line = 1;
7811 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
7813 dw_separate_line_info_ref line_info
7814 = &separate_line_info_table[lt_index];
7816 #if 0
7817 /* Don't emit anything for redundant notes. */
7818 if (line_info->dw_line_num == current_line
7819 && line_info->dw_file_num == current_file
7820 && line_info->function == function)
7821 goto cont;
7822 #endif
7824 /* Emit debug info for the address of the current line. If this is
7825 a new function, or the first line of a function, then we need
7826 to handle it differently. */
7827 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
7828 lt_index);
7829 if (function != line_info->function)
7831 function = line_info->function;
7833 /* Set the address register to the first line in the function. */
7834 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7835 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7836 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7837 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7839 else
7841 /* ??? See the DW_LNS_advance_pc comment above. */
7842 if (0)
7844 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7845 "DW_LNS_fixed_advance_pc");
7846 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7848 else
7850 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7851 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7852 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7853 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7857 strcpy (prev_line_label, line_label);
7859 /* Emit debug info for the source file of the current line, if
7860 different from the previous line. */
7861 if (line_info->dw_file_num != current_file)
7863 current_file = line_info->dw_file_num;
7864 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7865 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7866 VARRAY_CHAR_PTR (file_table,
7867 current_file));
7870 /* Emit debug info for the current line number, choosing the encoding
7871 that uses the least amount of space. */
7872 if (line_info->dw_line_num != current_line)
7874 line_offset = line_info->dw_line_num - current_line;
7875 line_delta = line_offset - DWARF_LINE_BASE;
7876 current_line = line_info->dw_line_num;
7877 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7878 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7879 "line %lu", current_line);
7880 else
7882 dw2_asm_output_data (1, DW_LNS_advance_line,
7883 "advance to line %lu", current_line);
7884 dw2_asm_output_data_sleb128 (line_offset, NULL);
7885 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7888 else
7889 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7891 #if 0
7892 cont:
7893 #endif
7895 lt_index++;
7897 /* If we're done with a function, end its sequence. */
7898 if (lt_index == separate_line_info_table_in_use
7899 || separate_line_info_table[lt_index].function != function)
7901 current_file = 1;
7902 current_line = 1;
7904 /* Emit debug info for the address of the end of the function. */
7905 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
7906 if (0)
7908 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7909 "DW_LNS_fixed_advance_pc");
7910 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7912 else
7914 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7915 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7916 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7917 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7920 /* Output the marker for the end of this sequence. */
7921 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7922 dw2_asm_output_data_uleb128 (1, NULL);
7923 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7927 /* Output the marker for the end of the line number info. */
7928 ASM_OUTPUT_LABEL (asm_out_file, l2);
7931 /* Given a pointer to a tree node for some base type, return a pointer to
7932 a DIE that describes the given type.
7934 This routine must only be called for GCC type nodes that correspond to
7935 Dwarf base (fundamental) types. */
7937 static dw_die_ref
7938 base_type_die (tree type)
7940 dw_die_ref base_type_result;
7941 const char *type_name;
7942 enum dwarf_type encoding;
7943 tree name = TYPE_NAME (type);
7945 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
7946 return 0;
7948 if (name)
7950 if (TREE_CODE (name) == TYPE_DECL)
7951 name = DECL_NAME (name);
7953 type_name = IDENTIFIER_POINTER (name);
7955 else
7956 type_name = "__unknown__";
7958 switch (TREE_CODE (type))
7960 case INTEGER_TYPE:
7961 /* Carefully distinguish the C character types, without messing
7962 up if the language is not C. Note that we check only for the names
7963 that contain spaces; other names might occur by coincidence in other
7964 languages. */
7965 if (! (TYPE_PRECISION (type) == CHAR_TYPE_SIZE
7966 && (type == char_type_node
7967 || ! strcmp (type_name, "signed char")
7968 || ! strcmp (type_name, "unsigned char"))))
7970 if (TYPE_UNSIGNED (type))
7971 encoding = DW_ATE_unsigned;
7972 else
7973 encoding = DW_ATE_signed;
7974 break;
7976 /* else fall through. */
7978 case CHAR_TYPE:
7979 /* GNU Pascal/Ada CHAR type. Not used in C. */
7980 if (TYPE_UNSIGNED (type))
7981 encoding = DW_ATE_unsigned_char;
7982 else
7983 encoding = DW_ATE_signed_char;
7984 break;
7986 case REAL_TYPE:
7987 encoding = DW_ATE_float;
7988 break;
7990 /* Dwarf2 doesn't know anything about complex ints, so use
7991 a user defined type for it. */
7992 case COMPLEX_TYPE:
7993 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
7994 encoding = DW_ATE_complex_float;
7995 else
7996 encoding = DW_ATE_lo_user;
7997 break;
7999 case BOOLEAN_TYPE:
8000 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
8001 encoding = DW_ATE_boolean;
8002 break;
8004 default:
8005 /* No other TREE_CODEs are Dwarf fundamental types. */
8006 abort ();
8009 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8010 if (demangle_name_func)
8011 type_name = (*demangle_name_func) (type_name);
8013 add_AT_string (base_type_result, DW_AT_name, type_name);
8014 add_AT_unsigned (base_type_result, DW_AT_byte_size,
8015 int_size_in_bytes (type));
8016 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8018 return base_type_result;
8021 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
8022 the Dwarf "root" type for the given input type. The Dwarf "root" type of
8023 a given type is generally the same as the given type, except that if the
8024 given type is a pointer or reference type, then the root type of the given
8025 type is the root type of the "basis" type for the pointer or reference
8026 type. (This definition of the "root" type is recursive.) Also, the root
8027 type of a `const' qualified type or a `volatile' qualified type is the
8028 root type of the given type without the qualifiers. */
8030 static tree
8031 root_type (tree type)
8033 if (TREE_CODE (type) == ERROR_MARK)
8034 return error_mark_node;
8036 switch (TREE_CODE (type))
8038 case ERROR_MARK:
8039 return error_mark_node;
8041 case POINTER_TYPE:
8042 case REFERENCE_TYPE:
8043 return type_main_variant (root_type (TREE_TYPE (type)));
8045 default:
8046 return type_main_variant (type);
8050 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8051 given input type is a Dwarf "fundamental" type. Otherwise return null. */
8053 static inline int
8054 is_base_type (tree type)
8056 switch (TREE_CODE (type))
8058 case ERROR_MARK:
8059 case VOID_TYPE:
8060 case INTEGER_TYPE:
8061 case REAL_TYPE:
8062 case COMPLEX_TYPE:
8063 case BOOLEAN_TYPE:
8064 case CHAR_TYPE:
8065 return 1;
8067 case SET_TYPE:
8068 case ARRAY_TYPE:
8069 case RECORD_TYPE:
8070 case UNION_TYPE:
8071 case QUAL_UNION_TYPE:
8072 case ENUMERAL_TYPE:
8073 case FUNCTION_TYPE:
8074 case METHOD_TYPE:
8075 case POINTER_TYPE:
8076 case REFERENCE_TYPE:
8077 case FILE_TYPE:
8078 case OFFSET_TYPE:
8079 case LANG_TYPE:
8080 case VECTOR_TYPE:
8081 return 0;
8083 default:
8084 abort ();
8087 return 0;
8090 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8091 node, return the size in bits for the type if it is a constant, or else
8092 return the alignment for the type if the type's size is not constant, or
8093 else return BITS_PER_WORD if the type actually turns out to be an
8094 ERROR_MARK node. */
8096 static inline unsigned HOST_WIDE_INT
8097 simple_type_size_in_bits (tree type)
8099 if (TREE_CODE (type) == ERROR_MARK)
8100 return BITS_PER_WORD;
8101 else if (TYPE_SIZE (type) == NULL_TREE)
8102 return 0;
8103 else if (host_integerp (TYPE_SIZE (type), 1))
8104 return tree_low_cst (TYPE_SIZE (type), 1);
8105 else
8106 return TYPE_ALIGN (type);
8109 /* Return true if the debug information for the given type should be
8110 emitted as a subrange type. */
8112 static inline bool
8113 is_subrange_type (tree type)
8115 tree subtype = TREE_TYPE (type);
8117 /* Subrange types are identified by the fact that they are integer
8118 types, and that they have a subtype which is either an integer type
8119 or an enumeral type. */
8121 if (TREE_CODE (type) != INTEGER_TYPE
8122 || subtype == NULL_TREE)
8123 return false;
8125 if (TREE_CODE (subtype) != INTEGER_TYPE
8126 && TREE_CODE (subtype) != ENUMERAL_TYPE)
8127 return false;
8129 if (TREE_CODE (type) == TREE_CODE (subtype)
8130 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8131 && TYPE_MIN_VALUE (type) != NULL
8132 && TYPE_MIN_VALUE (subtype) != NULL
8133 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8134 && TYPE_MAX_VALUE (type) != NULL
8135 && TYPE_MAX_VALUE (subtype) != NULL
8136 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8138 /* The type and its subtype have the same representation. If in
8139 addition the two types also have the same name, then the given
8140 type is not a subrange type, but rather a plain base type. */
8141 /* FIXME: brobecker/2004-03-22:
8142 Sizetype INTEGER_CSTs nodes are canonicalized. It should
8143 therefore be sufficient to check the TYPE_SIZE node pointers
8144 rather than checking the actual size. Unfortunately, we have
8145 found some cases, such as in the Ada "integer" type, where
8146 this is not the case. Until this problem is solved, we need to
8147 keep checking the actual size. */
8148 tree type_name = TYPE_NAME (type);
8149 tree subtype_name = TYPE_NAME (subtype);
8151 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8152 type_name = DECL_NAME (type_name);
8154 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8155 subtype_name = DECL_NAME (subtype_name);
8157 if (type_name == subtype_name)
8158 return false;
8161 return true;
8164 /* Given a pointer to a tree node for a subrange type, return a pointer
8165 to a DIE that describes the given type. */
8167 static dw_die_ref
8168 subrange_type_die (tree type, dw_die_ref context_die)
8170 dw_die_ref subtype_die;
8171 dw_die_ref subrange_die;
8172 tree name = TYPE_NAME (type);
8173 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8174 tree subtype = TREE_TYPE (type);
8176 if (context_die == NULL)
8177 context_die = comp_unit_die;
8179 if (TREE_CODE (subtype) == ENUMERAL_TYPE)
8180 subtype_die = gen_enumeration_type_die (subtype, context_die);
8181 else
8182 subtype_die = base_type_die (subtype);
8184 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8186 if (name != NULL)
8188 if (TREE_CODE (name) == TYPE_DECL)
8189 name = DECL_NAME (name);
8190 add_name_attribute (subrange_die, IDENTIFIER_POINTER (name));
8193 if (int_size_in_bytes (subtype) != size_in_bytes)
8195 /* The size of the subrange type and its base type do not match,
8196 so we need to generate a size attribute for the subrange type. */
8197 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8200 if (TYPE_MIN_VALUE (type) != NULL)
8201 add_bound_info (subrange_die, DW_AT_lower_bound,
8202 TYPE_MIN_VALUE (type));
8203 if (TYPE_MAX_VALUE (type) != NULL)
8204 add_bound_info (subrange_die, DW_AT_upper_bound,
8205 TYPE_MAX_VALUE (type));
8206 add_AT_die_ref (subrange_die, DW_AT_type, subtype_die);
8208 return subrange_die;
8211 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8212 entry that chains various modifiers in front of the given type. */
8214 static dw_die_ref
8215 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8216 dw_die_ref context_die)
8218 enum tree_code code = TREE_CODE (type);
8219 dw_die_ref mod_type_die = NULL;
8220 dw_die_ref sub_die = NULL;
8221 tree item_type = NULL;
8223 if (code != ERROR_MARK)
8225 tree qualified_type;
8227 /* See if we already have the appropriately qualified variant of
8228 this type. */
8229 qualified_type
8230 = get_qualified_type (type,
8231 ((is_const_type ? TYPE_QUAL_CONST : 0)
8232 | (is_volatile_type
8233 ? TYPE_QUAL_VOLATILE : 0)));
8235 /* If we do, then we can just use its DIE, if it exists. */
8236 if (qualified_type)
8238 mod_type_die = lookup_type_die (qualified_type);
8239 if (mod_type_die)
8240 return mod_type_die;
8243 /* Handle C typedef types. */
8244 if (qualified_type && TYPE_NAME (qualified_type)
8245 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL
8246 && DECL_ORIGINAL_TYPE (TYPE_NAME (qualified_type)))
8248 tree type_name = TYPE_NAME (qualified_type);
8249 tree dtype = TREE_TYPE (type_name);
8251 if (qualified_type == dtype)
8253 /* For a named type, use the typedef. */
8254 gen_type_die (qualified_type, context_die);
8255 mod_type_die = lookup_type_die (qualified_type);
8257 else if (is_const_type < TYPE_READONLY (dtype)
8258 || is_volatile_type < TYPE_VOLATILE (dtype))
8259 /* cv-unqualified version of named type. Just use the unnamed
8260 type to which it refers. */
8261 mod_type_die
8262 = modified_type_die (DECL_ORIGINAL_TYPE (type_name),
8263 is_const_type, is_volatile_type,
8264 context_die);
8266 /* Else cv-qualified version of named type; fall through. */
8269 if (mod_type_die)
8270 /* OK. */
8272 else if (is_const_type)
8274 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8275 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8277 else if (is_volatile_type)
8279 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8280 sub_die = modified_type_die (type, 0, 0, context_die);
8282 else if (code == POINTER_TYPE)
8284 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8285 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8286 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8287 #if 0
8288 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8289 #endif
8290 item_type = TREE_TYPE (type);
8292 else if (code == REFERENCE_TYPE)
8294 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8295 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8296 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8297 #if 0
8298 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8299 #endif
8300 item_type = TREE_TYPE (type);
8302 else if (is_subrange_type (type))
8303 mod_type_die = subrange_type_die (type, context_die);
8304 else if (is_base_type (type))
8305 mod_type_die = base_type_die (type);
8306 else
8308 gen_type_die (type, context_die);
8310 /* We have to get the type_main_variant here (and pass that to the
8311 `lookup_type_die' routine) because the ..._TYPE node we have
8312 might simply be a *copy* of some original type node (where the
8313 copy was created to help us keep track of typedef names) and
8314 that copy might have a different TYPE_UID from the original
8315 ..._TYPE node. */
8316 if (TREE_CODE (type) != VECTOR_TYPE)
8317 mod_type_die = lookup_type_die (type_main_variant (type));
8318 else
8319 /* Vectors have the debugging information in the type,
8320 not the main variant. */
8321 mod_type_die = lookup_type_die (type);
8322 if (mod_type_die == NULL)
8323 abort ();
8326 /* We want to equate the qualified type to the die below. */
8327 type = qualified_type;
8330 if (type)
8331 equate_type_number_to_die (type, mod_type_die);
8332 if (item_type)
8333 /* We must do this after the equate_type_number_to_die call, in case
8334 this is a recursive type. This ensures that the modified_type_die
8335 recursion will terminate even if the type is recursive. Recursive
8336 types are possible in Ada. */
8337 sub_die = modified_type_die (item_type,
8338 TYPE_READONLY (item_type),
8339 TYPE_VOLATILE (item_type),
8340 context_die);
8342 if (sub_die != NULL)
8343 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8345 return mod_type_die;
8348 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8349 an enumerated type. */
8351 static inline int
8352 type_is_enum (tree type)
8354 return TREE_CODE (type) == ENUMERAL_TYPE;
8357 /* Return the DBX register number described by a given RTL node. */
8359 static unsigned int
8360 dbx_reg_number (rtx rtl)
8362 unsigned regno = REGNO (rtl);
8364 if (regno >= FIRST_PSEUDO_REGISTER)
8365 abort ();
8367 return DBX_REGISTER_NUMBER (regno);
8370 /* Return a location descriptor that designates a machine register or
8371 zero if there is none. */
8373 static dw_loc_descr_ref
8374 reg_loc_descriptor (rtx rtl)
8376 unsigned reg;
8377 rtx regs;
8379 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8380 return 0;
8382 reg = dbx_reg_number (rtl);
8383 regs = targetm.dwarf_register_span (rtl);
8385 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1
8386 || regs)
8387 return multiple_reg_loc_descriptor (rtl, regs);
8388 else
8389 return one_reg_loc_descriptor (reg);
8392 /* Return a location descriptor that designates a machine register for
8393 a given hard register number. */
8395 static dw_loc_descr_ref
8396 one_reg_loc_descriptor (unsigned int regno)
8398 if (regno <= 31)
8399 return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8400 else
8401 return new_loc_descr (DW_OP_regx, regno, 0);
8404 /* Given an RTL of a register, return a location descriptor that
8405 designates a value that spans more than one register. */
8407 static dw_loc_descr_ref
8408 multiple_reg_loc_descriptor (rtx rtl, rtx regs)
8410 int nregs, size, i;
8411 unsigned reg;
8412 dw_loc_descr_ref loc_result = NULL;
8414 reg = dbx_reg_number (rtl);
8415 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8417 /* Simple, contiguous registers. */
8418 if (regs == NULL_RTX)
8420 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8422 loc_result = NULL;
8423 while (nregs--)
8425 dw_loc_descr_ref t;
8427 t = one_reg_loc_descriptor (reg);
8428 add_loc_descr (&loc_result, t);
8429 add_loc_descr (&loc_result, new_loc_descr (DW_OP_piece, size, 0));
8430 ++reg;
8432 return loc_result;
8435 /* Now onto stupid register sets in non contiguous locations. */
8437 if (GET_CODE (regs) != PARALLEL)
8438 abort ();
8440 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8441 loc_result = NULL;
8443 for (i = 0; i < XVECLEN (regs, 0); ++i)
8445 dw_loc_descr_ref t;
8447 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8448 add_loc_descr (&loc_result, t);
8449 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8450 add_loc_descr (&loc_result, new_loc_descr (DW_OP_piece, size, 0));
8452 return loc_result;
8455 /* Return a location descriptor that designates a constant. */
8457 static dw_loc_descr_ref
8458 int_loc_descriptor (HOST_WIDE_INT i)
8460 enum dwarf_location_atom op;
8462 /* Pick the smallest representation of a constant, rather than just
8463 defaulting to the LEB encoding. */
8464 if (i >= 0)
8466 if (i <= 31)
8467 op = DW_OP_lit0 + i;
8468 else if (i <= 0xff)
8469 op = DW_OP_const1u;
8470 else if (i <= 0xffff)
8471 op = DW_OP_const2u;
8472 else if (HOST_BITS_PER_WIDE_INT == 32
8473 || i <= 0xffffffff)
8474 op = DW_OP_const4u;
8475 else
8476 op = DW_OP_constu;
8478 else
8480 if (i >= -0x80)
8481 op = DW_OP_const1s;
8482 else if (i >= -0x8000)
8483 op = DW_OP_const2s;
8484 else if (HOST_BITS_PER_WIDE_INT == 32
8485 || i >= -0x80000000)
8486 op = DW_OP_const4s;
8487 else
8488 op = DW_OP_consts;
8491 return new_loc_descr (op, i, 0);
8494 /* Return a location descriptor that designates a base+offset location. */
8496 static dw_loc_descr_ref
8497 based_loc_descr (unsigned int reg, HOST_WIDE_INT offset, bool can_use_fbreg)
8499 dw_loc_descr_ref loc_result;
8500 /* For the "frame base", we use the frame pointer or stack pointer
8501 registers, since the RTL for local variables is relative to one of
8502 them. */
8503 unsigned fp_reg = DBX_REGISTER_NUMBER (frame_pointer_needed
8504 ? HARD_FRAME_POINTER_REGNUM
8505 : STACK_POINTER_REGNUM);
8507 if (reg == fp_reg && can_use_fbreg)
8508 loc_result = new_loc_descr (DW_OP_fbreg, offset, 0);
8509 else if (reg <= 31)
8510 loc_result = new_loc_descr (DW_OP_breg0 + reg, offset, 0);
8511 else
8512 loc_result = new_loc_descr (DW_OP_bregx, reg, offset);
8514 return loc_result;
8517 /* Return true if this RTL expression describes a base+offset calculation. */
8519 static inline int
8520 is_based_loc (rtx rtl)
8522 return (GET_CODE (rtl) == PLUS
8523 && ((REG_P (XEXP (rtl, 0))
8524 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8525 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
8528 /* The following routine converts the RTL for a variable or parameter
8529 (resident in memory) into an equivalent Dwarf representation of a
8530 mechanism for getting the address of that same variable onto the top of a
8531 hypothetical "address evaluation" stack.
8533 When creating memory location descriptors, we are effectively transforming
8534 the RTL for a memory-resident object into its Dwarf postfix expression
8535 equivalent. This routine recursively descends an RTL tree, turning
8536 it into Dwarf postfix code as it goes.
8538 MODE is the mode of the memory reference, needed to handle some
8539 autoincrement addressing modes.
8541 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the location
8542 list for RTL. We can't use it when we are emitting location list for
8543 virtual variable frame_base_decl (i.e. a location list for DW_AT_frame_base)
8544 which describes how frame base changes when !frame_pointer_needed.
8546 Return 0 if we can't represent the location. */
8548 static dw_loc_descr_ref
8549 mem_loc_descriptor (rtx rtl, enum machine_mode mode, bool can_use_fbreg)
8551 dw_loc_descr_ref mem_loc_result = NULL;
8552 enum dwarf_location_atom op;
8554 /* Note that for a dynamically sized array, the location we will generate a
8555 description of here will be the lowest numbered location which is
8556 actually within the array. That's *not* necessarily the same as the
8557 zeroth element of the array. */
8559 rtl = targetm.delegitimize_address (rtl);
8561 switch (GET_CODE (rtl))
8563 case POST_INC:
8564 case POST_DEC:
8565 case POST_MODIFY:
8566 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
8567 just fall into the SUBREG code. */
8569 /* ... fall through ... */
8571 case SUBREG:
8572 /* The case of a subreg may arise when we have a local (register)
8573 variable or a formal (register) parameter which doesn't quite fill
8574 up an entire register. For now, just assume that it is
8575 legitimate to make the Dwarf info refer to the whole register which
8576 contains the given subreg. */
8577 rtl = SUBREG_REG (rtl);
8579 /* ... fall through ... */
8581 case REG:
8582 /* Whenever a register number forms a part of the description of the
8583 method for calculating the (dynamic) address of a memory resident
8584 object, DWARF rules require the register number be referred to as
8585 a "base register". This distinction is not based in any way upon
8586 what category of register the hardware believes the given register
8587 belongs to. This is strictly DWARF terminology we're dealing with
8588 here. Note that in cases where the location of a memory-resident
8589 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8590 OP_CONST (0)) the actual DWARF location descriptor that we generate
8591 may just be OP_BASEREG (basereg). This may look deceptively like
8592 the object in question was allocated to a register (rather than in
8593 memory) so DWARF consumers need to be aware of the subtle
8594 distinction between OP_REG and OP_BASEREG. */
8595 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8596 mem_loc_result = based_loc_descr (dbx_reg_number (rtl), 0,
8597 can_use_fbreg);
8598 break;
8600 case MEM:
8601 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
8602 can_use_fbreg);
8603 if (mem_loc_result != 0)
8604 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
8605 break;
8607 case LO_SUM:
8608 rtl = XEXP (rtl, 1);
8610 /* ... fall through ... */
8612 case LABEL_REF:
8613 /* Some ports can transform a symbol ref into a label ref, because
8614 the symbol ref is too far away and has to be dumped into a constant
8615 pool. */
8616 case CONST:
8617 case SYMBOL_REF:
8618 /* Alternatively, the symbol in the constant pool might be referenced
8619 by a different symbol. */
8620 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
8622 bool marked;
8623 rtx tmp = get_pool_constant_mark (rtl, &marked);
8625 if (GET_CODE (tmp) == SYMBOL_REF)
8627 rtl = tmp;
8628 if (CONSTANT_POOL_ADDRESS_P (tmp))
8629 get_pool_constant_mark (tmp, &marked);
8630 else
8631 marked = true;
8634 /* If all references to this pool constant were optimized away,
8635 it was not output and thus we can't represent it.
8636 FIXME: might try to use DW_OP_const_value here, though
8637 DW_OP_piece complicates it. */
8638 if (!marked)
8639 return 0;
8642 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8643 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
8644 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8645 VARRAY_PUSH_RTX (used_rtx_varray, rtl);
8646 break;
8648 case PRE_MODIFY:
8649 /* Extract the PLUS expression nested inside and fall into
8650 PLUS code below. */
8651 rtl = XEXP (rtl, 1);
8652 goto plus;
8654 case PRE_INC:
8655 case PRE_DEC:
8656 /* Turn these into a PLUS expression and fall into the PLUS code
8657 below. */
8658 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8659 GEN_INT (GET_CODE (rtl) == PRE_INC
8660 ? GET_MODE_UNIT_SIZE (mode)
8661 : -GET_MODE_UNIT_SIZE (mode)));
8663 /* ... fall through ... */
8665 case PLUS:
8666 plus:
8667 if (is_based_loc (rtl))
8668 mem_loc_result = based_loc_descr (dbx_reg_number (XEXP (rtl, 0)),
8669 INTVAL (XEXP (rtl, 1)),
8670 can_use_fbreg);
8671 else
8673 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
8674 can_use_fbreg);
8675 if (mem_loc_result == 0)
8676 break;
8678 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8679 && INTVAL (XEXP (rtl, 1)) >= 0)
8680 add_loc_descr (&mem_loc_result,
8681 new_loc_descr (DW_OP_plus_uconst,
8682 INTVAL (XEXP (rtl, 1)), 0));
8683 else
8685 add_loc_descr (&mem_loc_result,
8686 mem_loc_descriptor (XEXP (rtl, 1), mode,
8687 can_use_fbreg));
8688 add_loc_descr (&mem_loc_result,
8689 new_loc_descr (DW_OP_plus, 0, 0));
8692 break;
8694 /* If a pseudo-reg is optimized away, it is possible for it to
8695 be replaced with a MEM containing a multiply or shift. */
8696 case MULT:
8697 op = DW_OP_mul;
8698 goto do_binop;
8700 case ASHIFT:
8701 op = DW_OP_shl;
8702 goto do_binop;
8704 case ASHIFTRT:
8705 op = DW_OP_shra;
8706 goto do_binop;
8708 case LSHIFTRT:
8709 op = DW_OP_shr;
8710 goto do_binop;
8712 do_binop:
8714 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
8715 can_use_fbreg);
8716 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
8717 can_use_fbreg);
8719 if (op0 == 0 || op1 == 0)
8720 break;
8722 mem_loc_result = op0;
8723 add_loc_descr (&mem_loc_result, op1);
8724 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
8725 break;
8728 case CONST_INT:
8729 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
8730 break;
8732 default:
8733 abort ();
8736 return mem_loc_result;
8739 /* Return a descriptor that describes the concatenation of two locations.
8740 This is typically a complex variable. */
8742 static dw_loc_descr_ref
8743 concat_loc_descriptor (rtx x0, rtx x1)
8745 dw_loc_descr_ref cc_loc_result = NULL;
8746 dw_loc_descr_ref x0_ref = loc_descriptor (x0, true);
8747 dw_loc_descr_ref x1_ref = loc_descriptor (x1, true);
8749 if (x0_ref == 0 || x1_ref == 0)
8750 return 0;
8752 cc_loc_result = x0_ref;
8753 add_loc_descr (&cc_loc_result,
8754 new_loc_descr (DW_OP_piece,
8755 GET_MODE_SIZE (GET_MODE (x0)), 0));
8757 add_loc_descr (&cc_loc_result, x1_ref);
8758 add_loc_descr (&cc_loc_result,
8759 new_loc_descr (DW_OP_piece,
8760 GET_MODE_SIZE (GET_MODE (x1)), 0));
8762 return cc_loc_result;
8765 /* Output a proper Dwarf location descriptor for a variable or parameter
8766 which is either allocated in a register or in a memory location. For a
8767 register, we just generate an OP_REG and the register number. For a
8768 memory location we provide a Dwarf postfix expression describing how to
8769 generate the (dynamic) address of the object onto the address stack.
8771 If we don't know how to describe it, return 0. */
8773 static dw_loc_descr_ref
8774 loc_descriptor (rtx rtl, bool can_use_fbreg)
8776 dw_loc_descr_ref loc_result = NULL;
8778 switch (GET_CODE (rtl))
8780 case SUBREG:
8781 /* The case of a subreg may arise when we have a local (register)
8782 variable or a formal (register) parameter which doesn't quite fill
8783 up an entire register. For now, just assume that it is
8784 legitimate to make the Dwarf info refer to the whole register which
8785 contains the given subreg. */
8786 rtl = SUBREG_REG (rtl);
8788 /* ... fall through ... */
8790 case REG:
8791 loc_result = reg_loc_descriptor (rtl);
8792 break;
8794 case MEM:
8795 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
8796 can_use_fbreg);
8797 break;
8799 case CONCAT:
8800 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
8801 break;
8803 case VAR_LOCATION:
8804 /* Single part. */
8805 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
8807 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), can_use_fbreg);
8809 /* Multiple parts. */
8810 else
8812 rtvec par_elems = XVEC (XEXP (rtl, 1), 0);
8813 int num_elem = GET_NUM_ELEM (par_elems);
8814 enum machine_mode mode;
8815 int i;
8817 /* Create the first one, so we have something to add to. */
8818 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
8819 can_use_fbreg);
8820 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
8821 add_loc_descr (&loc_result,
8822 new_loc_descr (DW_OP_piece, GET_MODE_SIZE (mode), 0));
8823 for (i = 1; i < num_elem; i++)
8825 dw_loc_descr_ref temp;
8827 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
8828 can_use_fbreg);
8829 add_loc_descr (&loc_result, temp);
8830 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
8831 add_loc_descr (&loc_result,
8832 new_loc_descr (DW_OP_piece,
8833 GET_MODE_SIZE (mode), 0));
8836 break;
8838 default:
8839 abort ();
8842 return loc_result;
8845 /* Similar, but generate the descriptor from trees instead of rtl. This comes
8846 up particularly with variable length arrays. If ADDRESSP is nonzero, we are
8847 looking for an address. Otherwise, we return a value. If we can't make a
8848 descriptor, return 0. */
8850 static dw_loc_descr_ref
8851 loc_descriptor_from_tree (tree loc, int addressp)
8853 dw_loc_descr_ref ret, ret1;
8854 int indirect_p = 0;
8855 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
8856 enum dwarf_location_atom op;
8858 /* ??? Most of the time we do not take proper care for sign/zero
8859 extending the values properly. Hopefully this won't be a real
8860 problem... */
8862 switch (TREE_CODE (loc))
8864 case ERROR_MARK:
8865 return 0;
8867 case PLACEHOLDER_EXPR:
8868 /* This case involves extracting fields from an object to determine the
8869 position of other fields. We don't try to encode this here. The
8870 only user of this is Ada, which encodes the needed information using
8871 the names of types. */
8872 return 0;
8874 case CALL_EXPR:
8875 return 0;
8877 case PREINCREMENT_EXPR:
8878 case PREDECREMENT_EXPR:
8879 case POSTINCREMENT_EXPR:
8880 case POSTDECREMENT_EXPR:
8881 /* There are no opcodes for these operations. */
8882 return 0;
8884 case ADDR_EXPR:
8885 /* We can support this only if we can look through conversions and
8886 find an INDIRECT_EXPR. */
8887 for (loc = TREE_OPERAND (loc, 0);
8888 TREE_CODE (loc) == CONVERT_EXPR || TREE_CODE (loc) == NOP_EXPR
8889 || TREE_CODE (loc) == NON_LVALUE_EXPR
8890 || TREE_CODE (loc) == VIEW_CONVERT_EXPR
8891 || TREE_CODE (loc) == SAVE_EXPR;
8892 loc = TREE_OPERAND (loc, 0))
8895 return (TREE_CODE (loc) == INDIRECT_REF
8896 ? loc_descriptor_from_tree (TREE_OPERAND (loc, 0), addressp)
8897 : 0);
8899 case VAR_DECL:
8900 if (DECL_THREAD_LOCAL (loc))
8902 rtx rtl;
8904 #ifndef ASM_OUTPUT_DWARF_DTPREL
8905 /* If this is not defined, we have no way to emit the data. */
8906 return 0;
8907 #endif
8909 /* The way DW_OP_GNU_push_tls_address is specified, we can only
8910 look up addresses of objects in the current module. */
8911 if (DECL_EXTERNAL (loc))
8912 return 0;
8914 rtl = rtl_for_decl_location (loc);
8915 if (rtl == NULL_RTX)
8916 return 0;
8918 if (!MEM_P (rtl))
8919 return 0;
8920 rtl = XEXP (rtl, 0);
8921 if (! CONSTANT_P (rtl))
8922 return 0;
8924 ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
8925 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8926 ret->dw_loc_oprnd1.v.val_addr = rtl;
8928 ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
8929 add_loc_descr (&ret, ret1);
8931 indirect_p = 1;
8932 break;
8934 /* Fall through. */
8936 case PARM_DECL:
8937 case RESULT_DECL:
8939 rtx rtl = rtl_for_decl_location (loc);
8941 if (rtl == NULL_RTX)
8942 return 0;
8943 else if (CONSTANT_P (rtl))
8945 ret = new_loc_descr (DW_OP_addr, 0, 0);
8946 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8947 ret->dw_loc_oprnd1.v.val_addr = rtl;
8948 indirect_p = 1;
8950 else
8952 enum machine_mode mode = GET_MODE (rtl);
8954 if (MEM_P (rtl))
8956 indirect_p = 1;
8957 rtl = XEXP (rtl, 0);
8960 ret = mem_loc_descriptor (rtl, mode, true);
8963 break;
8965 case INDIRECT_REF:
8966 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8967 indirect_p = 1;
8968 break;
8970 case COMPOUND_EXPR:
8971 return loc_descriptor_from_tree (TREE_OPERAND (loc, 1), addressp);
8973 case NOP_EXPR:
8974 case CONVERT_EXPR:
8975 case NON_LVALUE_EXPR:
8976 case VIEW_CONVERT_EXPR:
8977 case SAVE_EXPR:
8978 case MODIFY_EXPR:
8979 return loc_descriptor_from_tree (TREE_OPERAND (loc, 0), addressp);
8981 case COMPONENT_REF:
8982 case BIT_FIELD_REF:
8983 case ARRAY_REF:
8984 case ARRAY_RANGE_REF:
8986 tree obj, offset;
8987 HOST_WIDE_INT bitsize, bitpos, bytepos;
8988 enum machine_mode mode;
8989 int volatilep;
8991 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
8992 &unsignedp, &volatilep);
8994 if (obj == loc)
8995 return 0;
8997 ret = loc_descriptor_from_tree (obj, 1);
8998 if (ret == 0
8999 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9000 return 0;
9002 if (offset != NULL_TREE)
9004 /* Variable offset. */
9005 add_loc_descr (&ret, loc_descriptor_from_tree (offset, 0));
9006 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9009 if (!addressp)
9010 indirect_p = 1;
9012 bytepos = bitpos / BITS_PER_UNIT;
9013 if (bytepos > 0)
9014 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9015 else if (bytepos < 0)
9017 add_loc_descr (&ret, int_loc_descriptor (bytepos));
9018 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9020 break;
9023 case INTEGER_CST:
9024 if (host_integerp (loc, 0))
9025 ret = int_loc_descriptor (tree_low_cst (loc, 0));
9026 else
9027 return 0;
9028 break;
9030 case CONSTRUCTOR:
9032 /* Get an RTL for this, if something has been emitted. */
9033 rtx rtl = lookup_constant_def (loc);
9034 enum machine_mode mode;
9036 if (!MEM_P (rtl))
9037 return 0;
9038 mode = GET_MODE (rtl);
9039 rtl = XEXP (rtl, 0);
9041 rtl = targetm.delegitimize_address (rtl);
9043 indirect_p = 1;
9044 ret = mem_loc_descriptor (rtl, mode, true);
9045 break;
9048 case TRUTH_AND_EXPR:
9049 case TRUTH_ANDIF_EXPR:
9050 case BIT_AND_EXPR:
9051 op = DW_OP_and;
9052 goto do_binop;
9054 case TRUTH_XOR_EXPR:
9055 case BIT_XOR_EXPR:
9056 op = DW_OP_xor;
9057 goto do_binop;
9059 case TRUTH_OR_EXPR:
9060 case TRUTH_ORIF_EXPR:
9061 case BIT_IOR_EXPR:
9062 op = DW_OP_or;
9063 goto do_binop;
9065 case FLOOR_DIV_EXPR:
9066 case CEIL_DIV_EXPR:
9067 case ROUND_DIV_EXPR:
9068 case TRUNC_DIV_EXPR:
9069 op = DW_OP_div;
9070 goto do_binop;
9072 case MINUS_EXPR:
9073 op = DW_OP_minus;
9074 goto do_binop;
9076 case FLOOR_MOD_EXPR:
9077 case CEIL_MOD_EXPR:
9078 case ROUND_MOD_EXPR:
9079 case TRUNC_MOD_EXPR:
9080 op = DW_OP_mod;
9081 goto do_binop;
9083 case MULT_EXPR:
9084 op = DW_OP_mul;
9085 goto do_binop;
9087 case LSHIFT_EXPR:
9088 op = DW_OP_shl;
9089 goto do_binop;
9091 case RSHIFT_EXPR:
9092 op = (unsignedp ? DW_OP_shr : DW_OP_shra);
9093 goto do_binop;
9095 case PLUS_EXPR:
9096 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9097 && host_integerp (TREE_OPERAND (loc, 1), 0))
9099 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
9100 if (ret == 0)
9101 return 0;
9103 add_loc_descr (&ret,
9104 new_loc_descr (DW_OP_plus_uconst,
9105 tree_low_cst (TREE_OPERAND (loc, 1),
9107 0));
9108 break;
9111 op = DW_OP_plus;
9112 goto do_binop;
9114 case LE_EXPR:
9115 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9116 return 0;
9118 op = DW_OP_le;
9119 goto do_binop;
9121 case GE_EXPR:
9122 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9123 return 0;
9125 op = DW_OP_ge;
9126 goto do_binop;
9128 case LT_EXPR:
9129 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9130 return 0;
9132 op = DW_OP_lt;
9133 goto do_binop;
9135 case GT_EXPR:
9136 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9137 return 0;
9139 op = DW_OP_gt;
9140 goto do_binop;
9142 case EQ_EXPR:
9143 op = DW_OP_eq;
9144 goto do_binop;
9146 case NE_EXPR:
9147 op = DW_OP_ne;
9148 goto do_binop;
9150 do_binop:
9151 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
9152 ret1 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
9153 if (ret == 0 || ret1 == 0)
9154 return 0;
9156 add_loc_descr (&ret, ret1);
9157 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9158 break;
9160 case TRUTH_NOT_EXPR:
9161 case BIT_NOT_EXPR:
9162 op = DW_OP_not;
9163 goto do_unop;
9165 case ABS_EXPR:
9166 op = DW_OP_abs;
9167 goto do_unop;
9169 case NEGATE_EXPR:
9170 op = DW_OP_neg;
9171 goto do_unop;
9173 do_unop:
9174 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
9175 if (ret == 0)
9176 return 0;
9178 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9179 break;
9181 case MIN_EXPR:
9182 case MAX_EXPR:
9184 const enum tree_code code =
9185 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9187 loc = build3 (COND_EXPR, TREE_TYPE (loc),
9188 build2 (code, integer_type_node,
9189 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9190 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9193 /* ... fall through ... */
9195 case COND_EXPR:
9197 dw_loc_descr_ref lhs
9198 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
9199 dw_loc_descr_ref rhs
9200 = loc_descriptor_from_tree (TREE_OPERAND (loc, 2), 0);
9201 dw_loc_descr_ref bra_node, jump_node, tmp;
9203 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
9204 if (ret == 0 || lhs == 0 || rhs == 0)
9205 return 0;
9207 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9208 add_loc_descr (&ret, bra_node);
9210 add_loc_descr (&ret, rhs);
9211 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9212 add_loc_descr (&ret, jump_node);
9214 add_loc_descr (&ret, lhs);
9215 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9216 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9218 /* ??? Need a node to point the skip at. Use a nop. */
9219 tmp = new_loc_descr (DW_OP_nop, 0, 0);
9220 add_loc_descr (&ret, tmp);
9221 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9222 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9224 break;
9226 default:
9227 /* Leave front-end specific codes as simply unknown. This comes
9228 up, for instance, with the C STMT_EXPR. */
9229 if ((unsigned int) TREE_CODE (loc)
9230 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9231 return 0;
9233 /* Otherwise this is a generic code; we should just lists all of
9234 these explicitly. Aborting means we forgot one. */
9235 abort ();
9238 /* Show if we can't fill the request for an address. */
9239 if (addressp && indirect_p == 0)
9240 return 0;
9242 /* If we've got an address and don't want one, dereference. */
9243 if (!addressp && indirect_p > 0)
9245 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9247 if (size > DWARF2_ADDR_SIZE || size == -1)
9248 return 0;
9249 else if (size == DWARF2_ADDR_SIZE)
9250 op = DW_OP_deref;
9251 else
9252 op = DW_OP_deref_size;
9254 add_loc_descr (&ret, new_loc_descr (op, size, 0));
9257 return ret;
9260 /* Given a value, round it up to the lowest multiple of `boundary'
9261 which is not less than the value itself. */
9263 static inline HOST_WIDE_INT
9264 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9266 return (((value + boundary - 1) / boundary) * boundary);
9269 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9270 pointer to the declared type for the relevant field variable, or return
9271 `integer_type_node' if the given node turns out to be an
9272 ERROR_MARK node. */
9274 static inline tree
9275 field_type (tree decl)
9277 tree type;
9279 if (TREE_CODE (decl) == ERROR_MARK)
9280 return integer_type_node;
9282 type = DECL_BIT_FIELD_TYPE (decl);
9283 if (type == NULL_TREE)
9284 type = TREE_TYPE (decl);
9286 return type;
9289 /* Given a pointer to a tree node, return the alignment in bits for
9290 it, or else return BITS_PER_WORD if the node actually turns out to
9291 be an ERROR_MARK node. */
9293 static inline unsigned
9294 simple_type_align_in_bits (tree type)
9296 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9299 static inline unsigned
9300 simple_decl_align_in_bits (tree decl)
9302 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9305 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9306 lowest addressed byte of the "containing object" for the given FIELD_DECL,
9307 or return 0 if we are unable to determine what that offset is, either
9308 because the argument turns out to be a pointer to an ERROR_MARK node, or
9309 because the offset is actually variable. (We can't handle the latter case
9310 just yet). */
9312 static HOST_WIDE_INT
9313 field_byte_offset (tree decl)
9315 unsigned int type_align_in_bits;
9316 unsigned int decl_align_in_bits;
9317 unsigned HOST_WIDE_INT type_size_in_bits;
9318 HOST_WIDE_INT object_offset_in_bits;
9319 tree type;
9320 tree field_size_tree;
9321 HOST_WIDE_INT bitpos_int;
9322 HOST_WIDE_INT deepest_bitpos;
9323 unsigned HOST_WIDE_INT field_size_in_bits;
9325 if (TREE_CODE (decl) == ERROR_MARK)
9326 return 0;
9327 else if (TREE_CODE (decl) != FIELD_DECL)
9328 abort ();
9330 type = field_type (decl);
9331 field_size_tree = DECL_SIZE (decl);
9333 /* The size could be unspecified if there was an error, or for
9334 a flexible array member. */
9335 if (! field_size_tree)
9336 field_size_tree = bitsize_zero_node;
9338 /* We cannot yet cope with fields whose positions are variable, so
9339 for now, when we see such things, we simply return 0. Someday, we may
9340 be able to handle such cases, but it will be damn difficult. */
9341 if (! host_integerp (bit_position (decl), 0))
9342 return 0;
9344 bitpos_int = int_bit_position (decl);
9346 /* If we don't know the size of the field, pretend it's a full word. */
9347 if (host_integerp (field_size_tree, 1))
9348 field_size_in_bits = tree_low_cst (field_size_tree, 1);
9349 else
9350 field_size_in_bits = BITS_PER_WORD;
9352 type_size_in_bits = simple_type_size_in_bits (type);
9353 type_align_in_bits = simple_type_align_in_bits (type);
9354 decl_align_in_bits = simple_decl_align_in_bits (decl);
9356 /* The GCC front-end doesn't make any attempt to keep track of the starting
9357 bit offset (relative to the start of the containing structure type) of the
9358 hypothetical "containing object" for a bit-field. Thus, when computing
9359 the byte offset value for the start of the "containing object" of a
9360 bit-field, we must deduce this information on our own. This can be rather
9361 tricky to do in some cases. For example, handling the following structure
9362 type definition when compiling for an i386/i486 target (which only aligns
9363 long long's to 32-bit boundaries) can be very tricky:
9365 struct S { int field1; long long field2:31; };
9367 Fortunately, there is a simple rule-of-thumb which can be used in such
9368 cases. When compiling for an i386/i486, GCC will allocate 8 bytes for the
9369 structure shown above. It decides to do this based upon one simple rule
9370 for bit-field allocation. GCC allocates each "containing object" for each
9371 bit-field at the first (i.e. lowest addressed) legitimate alignment
9372 boundary (based upon the required minimum alignment for the declared type
9373 of the field) which it can possibly use, subject to the condition that
9374 there is still enough available space remaining in the containing object
9375 (when allocated at the selected point) to fully accommodate all of the
9376 bits of the bit-field itself.
9378 This simple rule makes it obvious why GCC allocates 8 bytes for each
9379 object of the structure type shown above. When looking for a place to
9380 allocate the "containing object" for `field2', the compiler simply tries
9381 to allocate a 64-bit "containing object" at each successive 32-bit
9382 boundary (starting at zero) until it finds a place to allocate that 64-
9383 bit field such that at least 31 contiguous (and previously unallocated)
9384 bits remain within that selected 64 bit field. (As it turns out, for the
9385 example above, the compiler finds it is OK to allocate the "containing
9386 object" 64-bit field at bit-offset zero within the structure type.)
9388 Here we attempt to work backwards from the limited set of facts we're
9389 given, and we try to deduce from those facts, where GCC must have believed
9390 that the containing object started (within the structure type). The value
9391 we deduce is then used (by the callers of this routine) to generate
9392 DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
9393 and, in the case of DW_AT_location, regular fields as well). */
9395 /* Figure out the bit-distance from the start of the structure to the
9396 "deepest" bit of the bit-field. */
9397 deepest_bitpos = bitpos_int + field_size_in_bits;
9399 /* This is the tricky part. Use some fancy footwork to deduce where the
9400 lowest addressed bit of the containing object must be. */
9401 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9403 /* Round up to type_align by default. This works best for bitfields. */
9404 object_offset_in_bits += type_align_in_bits - 1;
9405 object_offset_in_bits /= type_align_in_bits;
9406 object_offset_in_bits *= type_align_in_bits;
9408 if (object_offset_in_bits > bitpos_int)
9410 /* Sigh, the decl must be packed. */
9411 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9413 /* Round up to decl_align instead. */
9414 object_offset_in_bits += decl_align_in_bits - 1;
9415 object_offset_in_bits /= decl_align_in_bits;
9416 object_offset_in_bits *= decl_align_in_bits;
9419 return object_offset_in_bits / BITS_PER_UNIT;
9422 /* The following routines define various Dwarf attributes and any data
9423 associated with them. */
9425 /* Add a location description attribute value to a DIE.
9427 This emits location attributes suitable for whole variables and
9428 whole parameters. Note that the location attributes for struct fields are
9429 generated by the routine `data_member_location_attribute' below. */
9431 static inline void
9432 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9433 dw_loc_descr_ref descr)
9435 if (descr != 0)
9436 add_AT_loc (die, attr_kind, descr);
9439 /* Attach the specialized form of location attribute used for data members of
9440 struct and union types. In the special case of a FIELD_DECL node which
9441 represents a bit-field, the "offset" part of this special location
9442 descriptor must indicate the distance in bytes from the lowest-addressed
9443 byte of the containing struct or union type to the lowest-addressed byte of
9444 the "containing object" for the bit-field. (See the `field_byte_offset'
9445 function above).
9447 For any given bit-field, the "containing object" is a hypothetical object
9448 (of some integral or enum type) within which the given bit-field lives. The
9449 type of this hypothetical "containing object" is always the same as the
9450 declared type of the individual bit-field itself (for GCC anyway... the
9451 DWARF spec doesn't actually mandate this). Note that it is the size (in
9452 bytes) of the hypothetical "containing object" which will be given in the
9453 DW_AT_byte_size attribute for this bit-field. (See the
9454 `byte_size_attribute' function below.) It is also used when calculating the
9455 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
9456 function below.) */
9458 static void
9459 add_data_member_location_attribute (dw_die_ref die, tree decl)
9461 HOST_WIDE_INT offset;
9462 dw_loc_descr_ref loc_descr = 0;
9464 if (TREE_CODE (decl) == TREE_BINFO)
9466 /* We're working on the TAG_inheritance for a base class. */
9467 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
9469 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9470 aren't at a fixed offset from all (sub)objects of the same
9471 type. We need to extract the appropriate offset from our
9472 vtable. The following dwarf expression means
9474 BaseAddr = ObAddr + *((*ObAddr) - Offset)
9476 This is specific to the V3 ABI, of course. */
9478 dw_loc_descr_ref tmp;
9480 /* Make a copy of the object address. */
9481 tmp = new_loc_descr (DW_OP_dup, 0, 0);
9482 add_loc_descr (&loc_descr, tmp);
9484 /* Extract the vtable address. */
9485 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9486 add_loc_descr (&loc_descr, tmp);
9488 /* Calculate the address of the offset. */
9489 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9490 if (offset >= 0)
9491 abort ();
9493 tmp = int_loc_descriptor (-offset);
9494 add_loc_descr (&loc_descr, tmp);
9495 tmp = new_loc_descr (DW_OP_minus, 0, 0);
9496 add_loc_descr (&loc_descr, tmp);
9498 /* Extract the offset. */
9499 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9500 add_loc_descr (&loc_descr, tmp);
9502 /* Add it to the object address. */
9503 tmp = new_loc_descr (DW_OP_plus, 0, 0);
9504 add_loc_descr (&loc_descr, tmp);
9506 else
9507 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9509 else
9510 offset = field_byte_offset (decl);
9512 if (! loc_descr)
9514 enum dwarf_location_atom op;
9516 /* The DWARF2 standard says that we should assume that the structure
9517 address is already on the stack, so we can specify a structure field
9518 address by using DW_OP_plus_uconst. */
9520 #ifdef MIPS_DEBUGGING_INFO
9521 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9522 operator correctly. It works only if we leave the offset on the
9523 stack. */
9524 op = DW_OP_constu;
9525 #else
9526 op = DW_OP_plus_uconst;
9527 #endif
9529 loc_descr = new_loc_descr (op, offset, 0);
9532 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9535 /* Writes integer values to dw_vec_const array. */
9537 static void
9538 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9540 while (size != 0)
9542 *dest++ = val & 0xff;
9543 val >>= 8;
9544 --size;
9548 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
9550 static HOST_WIDE_INT
9551 extract_int (const unsigned char *src, unsigned int size)
9553 HOST_WIDE_INT val = 0;
9555 src += size;
9556 while (size != 0)
9558 val <<= 8;
9559 val |= *--src & 0xff;
9560 --size;
9562 return val;
9565 /* Writes floating point values to dw_vec_const array. */
9567 static void
9568 insert_float (rtx rtl, unsigned char *array)
9570 REAL_VALUE_TYPE rv;
9571 long val[4];
9572 int i;
9574 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9575 real_to_target (val, &rv, GET_MODE (rtl));
9577 /* real_to_target puts 32-bit pieces in each long. Pack them. */
9578 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9580 insert_int (val[i], 4, array);
9581 array += 4;
9585 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
9586 does not have a "location" either in memory or in a register. These
9587 things can arise in GNU C when a constant is passed as an actual parameter
9588 to an inlined function. They can also arise in C++ where declared
9589 constants do not necessarily get memory "homes". */
9591 static void
9592 add_const_value_attribute (dw_die_ref die, rtx rtl)
9594 switch (GET_CODE (rtl))
9596 case CONST_INT:
9598 HOST_WIDE_INT val = INTVAL (rtl);
9600 if (val < 0)
9601 add_AT_int (die, DW_AT_const_value, val);
9602 else
9603 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
9605 break;
9607 case CONST_DOUBLE:
9608 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9609 floating-point constant. A CONST_DOUBLE is used whenever the
9610 constant requires more than one word in order to be adequately
9611 represented. We output CONST_DOUBLEs as blocks. */
9613 enum machine_mode mode = GET_MODE (rtl);
9615 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
9617 unsigned int length = GET_MODE_SIZE (mode);
9618 unsigned char *array = ggc_alloc (length);
9620 insert_float (rtl, array);
9621 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
9623 else
9625 /* ??? We really should be using HOST_WIDE_INT throughout. */
9626 if (HOST_BITS_PER_LONG != HOST_BITS_PER_WIDE_INT)
9627 abort ();
9629 add_AT_long_long (die, DW_AT_const_value,
9630 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
9633 break;
9635 case CONST_VECTOR:
9637 enum machine_mode mode = GET_MODE (rtl);
9638 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9639 unsigned int length = CONST_VECTOR_NUNITS (rtl);
9640 unsigned char *array = ggc_alloc (length * elt_size);
9641 unsigned int i;
9642 unsigned char *p;
9644 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
9646 for (i = 0, p = array; i < length; i++, p += elt_size)
9648 rtx elt = CONST_VECTOR_ELT (rtl, i);
9649 HOST_WIDE_INT lo, hi;
9650 if (GET_CODE (elt) == CONST_INT)
9652 lo = INTVAL (elt);
9653 hi = -(lo < 0);
9655 else if (GET_CODE (elt) == CONST_DOUBLE)
9657 lo = CONST_DOUBLE_LOW (elt);
9658 hi = CONST_DOUBLE_HIGH (elt);
9660 else
9661 abort ();
9663 if (elt_size <= sizeof (HOST_WIDE_INT))
9664 insert_int (lo, elt_size, p);
9665 else if (elt_size == 2 * sizeof (HOST_WIDE_INT))
9667 unsigned char *p0 = p;
9668 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9670 if (WORDS_BIG_ENDIAN)
9672 p0 = p1;
9673 p1 = p;
9675 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9676 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9678 else
9679 abort ();
9682 else if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
9684 for (i = 0, p = array; i < length; i++, p += elt_size)
9686 rtx elt = CONST_VECTOR_ELT (rtl, i);
9687 insert_float (elt, p);
9690 else
9691 abort ();
9693 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
9695 break;
9697 case CONST_STRING:
9698 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
9699 break;
9701 case SYMBOL_REF:
9702 case LABEL_REF:
9703 case CONST:
9704 add_AT_addr (die, DW_AT_const_value, rtl);
9705 VARRAY_PUSH_RTX (used_rtx_varray, rtl);
9706 break;
9708 case PLUS:
9709 /* In cases where an inlined instance of an inline function is passed
9710 the address of an `auto' variable (which is local to the caller) we
9711 can get a situation where the DECL_RTL of the artificial local
9712 variable (for the inlining) which acts as a stand-in for the
9713 corresponding formal parameter (of the inline function) will look
9714 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
9715 exactly a compile-time constant expression, but it isn't the address
9716 of the (artificial) local variable either. Rather, it represents the
9717 *value* which the artificial local variable always has during its
9718 lifetime. We currently have no way to represent such quasi-constant
9719 values in Dwarf, so for now we just punt and generate nothing. */
9720 break;
9722 default:
9723 /* No other kinds of rtx should be possible here. */
9724 abort ();
9729 static rtx
9730 rtl_for_decl_location (tree decl)
9732 rtx rtl;
9734 /* Here we have to decide where we are going to say the parameter "lives"
9735 (as far as the debugger is concerned). We only have a couple of
9736 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
9738 DECL_RTL normally indicates where the parameter lives during most of the
9739 activation of the function. If optimization is enabled however, this
9740 could be either NULL or else a pseudo-reg. Both of those cases indicate
9741 that the parameter doesn't really live anywhere (as far as the code
9742 generation parts of GCC are concerned) during most of the function's
9743 activation. That will happen (for example) if the parameter is never
9744 referenced within the function.
9746 We could just generate a location descriptor here for all non-NULL
9747 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
9748 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
9749 where DECL_RTL is NULL or is a pseudo-reg.
9751 Note however that we can only get away with using DECL_INCOMING_RTL as
9752 a backup substitute for DECL_RTL in certain limited cases. In cases
9753 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
9754 we can be sure that the parameter was passed using the same type as it is
9755 declared to have within the function, and that its DECL_INCOMING_RTL
9756 points us to a place where a value of that type is passed.
9758 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
9759 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
9760 because in these cases DECL_INCOMING_RTL points us to a value of some
9761 type which is *different* from the type of the parameter itself. Thus,
9762 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
9763 such cases, the debugger would end up (for example) trying to fetch a
9764 `float' from a place which actually contains the first part of a
9765 `double'. That would lead to really incorrect and confusing
9766 output at debug-time.
9768 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
9769 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
9770 are a couple of exceptions however. On little-endian machines we can
9771 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
9772 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
9773 an integral type that is smaller than TREE_TYPE (decl). These cases arise
9774 when (on a little-endian machine) a non-prototyped function has a
9775 parameter declared to be of type `short' or `char'. In such cases,
9776 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
9777 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
9778 passed `int' value. If the debugger then uses that address to fetch
9779 a `short' or a `char' (on a little-endian machine) the result will be
9780 the correct data, so we allow for such exceptional cases below.
9782 Note that our goal here is to describe the place where the given formal
9783 parameter lives during most of the function's activation (i.e. between the
9784 end of the prologue and the start of the epilogue). We'll do that as best
9785 as we can. Note however that if the given formal parameter is modified
9786 sometime during the execution of the function, then a stack backtrace (at
9787 debug-time) will show the function as having been called with the *new*
9788 value rather than the value which was originally passed in. This happens
9789 rarely enough that it is not a major problem, but it *is* a problem, and
9790 I'd like to fix it.
9792 A future version of dwarf2out.c may generate two additional attributes for
9793 any given DW_TAG_formal_parameter DIE which will describe the "passed
9794 type" and the "passed location" for the given formal parameter in addition
9795 to the attributes we now generate to indicate the "declared type" and the
9796 "active location" for each parameter. This additional set of attributes
9797 could be used by debuggers for stack backtraces. Separately, note that
9798 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
9799 This happens (for example) for inlined-instances of inline function formal
9800 parameters which are never referenced. This really shouldn't be
9801 happening. All PARM_DECL nodes should get valid non-NULL
9802 DECL_INCOMING_RTL values. FIXME. */
9804 /* Use DECL_RTL as the "location" unless we find something better. */
9805 rtl = DECL_RTL_IF_SET (decl);
9807 /* When generating abstract instances, ignore everything except
9808 constants, symbols living in memory, and symbols living in
9809 fixed registers. */
9810 if (! reload_completed)
9812 if (rtl
9813 && (CONSTANT_P (rtl)
9814 || (MEM_P (rtl)
9815 && CONSTANT_P (XEXP (rtl, 0)))
9816 || (REG_P (rtl)
9817 && TREE_CODE (decl) == VAR_DECL
9818 && TREE_STATIC (decl))))
9820 rtl = targetm.delegitimize_address (rtl);
9821 return rtl;
9823 rtl = NULL_RTX;
9825 else if (TREE_CODE (decl) == PARM_DECL)
9827 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
9829 tree declared_type = type_main_variant (TREE_TYPE (decl));
9830 tree passed_type = type_main_variant (DECL_ARG_TYPE (decl));
9832 /* This decl represents a formal parameter which was optimized out.
9833 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
9834 all cases where (rtl == NULL_RTX) just below. */
9835 if (declared_type == passed_type)
9836 rtl = DECL_INCOMING_RTL (decl);
9837 else if (! BYTES_BIG_ENDIAN
9838 && TREE_CODE (declared_type) == INTEGER_TYPE
9839 && (GET_MODE_SIZE (TYPE_MODE (declared_type))
9840 <= GET_MODE_SIZE (TYPE_MODE (passed_type))))
9841 rtl = DECL_INCOMING_RTL (decl);
9844 /* If the parm was passed in registers, but lives on the stack, then
9845 make a big endian correction if the mode of the type of the
9846 parameter is not the same as the mode of the rtl. */
9847 /* ??? This is the same series of checks that are made in dbxout.c before
9848 we reach the big endian correction code there. It isn't clear if all
9849 of these checks are necessary here, but keeping them all is the safe
9850 thing to do. */
9851 else if (MEM_P (rtl)
9852 && XEXP (rtl, 0) != const0_rtx
9853 && ! CONSTANT_P (XEXP (rtl, 0))
9854 /* Not passed in memory. */
9855 && !MEM_P (DECL_INCOMING_RTL (decl))
9856 /* Not passed by invisible reference. */
9857 && (!REG_P (XEXP (rtl, 0))
9858 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
9859 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
9860 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
9861 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
9862 #endif
9864 /* Big endian correction check. */
9865 && BYTES_BIG_ENDIAN
9866 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
9867 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
9868 < UNITS_PER_WORD))
9870 int offset = (UNITS_PER_WORD
9871 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
9873 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
9874 plus_constant (XEXP (rtl, 0), offset));
9877 else if (TREE_CODE (decl) == VAR_DECL
9878 && rtl
9879 && MEM_P (rtl)
9880 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
9881 && BYTES_BIG_ENDIAN)
9883 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
9884 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
9886 /* If a variable is declared "register" yet is smaller than
9887 a register, then if we store the variable to memory, it
9888 looks like we're storing a register-sized value, when in
9889 fact we are not. We need to adjust the offset of the
9890 storage location to reflect the actual value's bytes,
9891 else gdb will not be able to display it. */
9892 if (rsize > dsize)
9893 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
9894 plus_constant (XEXP (rtl, 0), rsize-dsize));
9897 if (rtl != NULL_RTX)
9899 rtl = eliminate_regs (rtl, 0, NULL_RTX);
9900 #ifdef LEAF_REG_REMAP
9901 if (current_function_uses_only_leaf_regs)
9902 leaf_renumber_regs_insn (rtl);
9903 #endif
9906 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
9907 and will have been substituted directly into all expressions that use it.
9908 C does not have such a concept, but C++ and other languages do. */
9909 else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
9911 /* If a variable is initialized with a string constant without embedded
9912 zeros, build CONST_STRING. */
9913 if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
9914 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9916 tree arrtype = TREE_TYPE (decl);
9917 tree enttype = TREE_TYPE (arrtype);
9918 tree domain = TYPE_DOMAIN (arrtype);
9919 tree init = DECL_INITIAL (decl);
9920 enum machine_mode mode = TYPE_MODE (enttype);
9922 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
9923 && domain
9924 && integer_zerop (TYPE_MIN_VALUE (domain))
9925 && compare_tree_int (TYPE_MAX_VALUE (domain),
9926 TREE_STRING_LENGTH (init) - 1) == 0
9927 && ((size_t) TREE_STRING_LENGTH (init)
9928 == strlen (TREE_STRING_POINTER (init)) + 1))
9929 rtl = gen_rtx_CONST_STRING (VOIDmode, TREE_STRING_POINTER (init));
9931 /* If the initializer is something that we know will expand into an
9932 immediate RTL constant, expand it now. Expanding anything else
9933 tends to produce unresolved symbols; see debug/5770 and c++/6381. */
9934 else if (TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST
9935 || TREE_CODE (DECL_INITIAL (decl)) == REAL_CST)
9937 rtl = expand_expr (DECL_INITIAL (decl), NULL_RTX, VOIDmode,
9938 EXPAND_INITIALIZER);
9939 /* If expand_expr returns a MEM, it wasn't immediate. */
9940 if (rtl && MEM_P (rtl))
9941 abort ();
9945 if (rtl)
9946 rtl = targetm.delegitimize_address (rtl);
9948 /* If we don't look past the constant pool, we risk emitting a
9949 reference to a constant pool entry that isn't referenced from
9950 code, and thus is not emitted. */
9951 if (rtl)
9952 rtl = avoid_constant_pool_reference (rtl);
9954 return rtl;
9957 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
9958 data attribute for a variable or a parameter. We generate the
9959 DW_AT_const_value attribute only in those cases where the given variable
9960 or parameter does not have a true "location" either in memory or in a
9961 register. This can happen (for example) when a constant is passed as an
9962 actual argument in a call to an inline function. (It's possible that
9963 these things can crop up in other ways also.) Note that one type of
9964 constant value which can be passed into an inlined function is a constant
9965 pointer. This can happen for example if an actual argument in an inlined
9966 function call evaluates to a compile-time constant address. */
9968 static void
9969 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
9970 enum dwarf_attribute attr)
9972 rtx rtl;
9973 dw_loc_descr_ref descr;
9974 var_loc_list *loc_list;
9976 if (TREE_CODE (decl) == ERROR_MARK)
9977 return;
9978 else if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != PARM_DECL
9979 && TREE_CODE (decl) != RESULT_DECL)
9980 abort ();
9982 /* See if we possibly have multiple locations for this variable. */
9983 loc_list = lookup_decl_loc (decl);
9985 /* If it truly has multiple locations, the first and last node will
9986 differ. */
9987 if (loc_list && loc_list->first != loc_list->last)
9989 const char *secname;
9990 const char *endname;
9991 dw_loc_list_ref list;
9992 rtx varloc;
9993 struct var_loc_node *node;
9995 /* We need to figure out what section we should use as the base
9996 for the address ranges where a given location is valid.
9997 1. If this particular DECL has a section associated with it,
9998 use that.
9999 2. If this function has a section associated with it, use
10000 that.
10001 3. Otherwise, use the text section.
10002 XXX: If you split a variable across multiple sections, this
10003 won't notice. */
10005 if (DECL_SECTION_NAME (decl))
10007 tree sectree = DECL_SECTION_NAME (decl);
10008 secname = TREE_STRING_POINTER (sectree);
10010 else if (current_function_decl
10011 && DECL_SECTION_NAME (current_function_decl))
10013 tree sectree = DECL_SECTION_NAME (current_function_decl);
10014 secname = TREE_STRING_POINTER (sectree);
10016 else
10017 secname = text_section_label;
10019 /* Now that we know what section we are using for a base,
10020 actually construct the list of locations.
10021 The first location information is what is passed to the
10022 function that creates the location list, and the remaining
10023 locations just get added on to that list.
10024 Note that we only know the start address for a location
10025 (IE location changes), so to build the range, we use
10026 the range [current location start, next location start].
10027 This means we have to special case the last node, and generate
10028 a range of [last location start, end of function label]. */
10030 node = loc_list->first;
10031 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10032 list = new_loc_list (loc_descriptor (varloc, attr != DW_AT_frame_base),
10033 node->label, node->next->label, secname, 1);
10034 node = node->next;
10036 for (; node->next; node = node->next)
10037 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10039 /* The variable has a location between NODE->LABEL and
10040 NODE->NEXT->LABEL. */
10041 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10042 add_loc_descr_to_loc_list (&list,
10043 loc_descriptor (varloc,
10044 attr != DW_AT_frame_base),
10045 node->label, node->next->label, secname);
10048 /* If the variable has a location at the last label
10049 it keeps its location until the end of function. */
10050 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10052 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10054 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10055 if (!current_function_decl)
10056 endname = text_end_label;
10057 else
10059 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10060 current_function_funcdef_no);
10061 endname = ggc_strdup (label_id);
10063 add_loc_descr_to_loc_list (&list,
10064 loc_descriptor (varloc,
10065 attr != DW_AT_frame_base),
10066 node->label, endname, secname);
10069 /* Finally, add the location list to the DIE, and we are done. */
10070 add_AT_loc_list (die, attr, list);
10071 return;
10074 rtl = rtl_for_decl_location (decl);
10075 if (rtl == NULL_RTX)
10076 return;
10078 switch (GET_CODE (rtl))
10080 case CONST_INT:
10081 case CONST_DOUBLE:
10082 case CONST_VECTOR:
10083 case CONST_STRING:
10084 case SYMBOL_REF:
10085 case LABEL_REF:
10086 case CONST:
10087 case PLUS:
10088 /* DECL_RTL could be (plus (reg ...) (const_int ...)) */
10089 add_const_value_attribute (die, rtl);
10090 break;
10092 case MEM:
10093 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL (decl))
10095 /* Need loc_descriptor_from_tree since that's where we know
10096 how to handle TLS variables. Want the object's address
10097 since the top-level DW_AT_location assumes such. See
10098 the confusion in loc_descriptor for reference. */
10099 descr = loc_descriptor_from_tree (decl, 1);
10101 else
10103 case REG:
10104 case SUBREG:
10105 case CONCAT:
10106 descr = loc_descriptor (rtl, true);
10108 add_AT_location_description (die, attr, descr);
10109 break;
10111 case PARALLEL:
10113 rtvec par_elems = XVEC (rtl, 0);
10114 int num_elem = GET_NUM_ELEM (par_elems);
10115 enum machine_mode mode;
10116 int i;
10118 /* Create the first one, so we have something to add to. */
10119 descr = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0), true);
10120 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10121 add_loc_descr (&descr,
10122 new_loc_descr (DW_OP_piece, GET_MODE_SIZE (mode), 0));
10123 for (i = 1; i < num_elem; i++)
10125 dw_loc_descr_ref temp;
10127 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0), true);
10128 add_loc_descr (&descr, temp);
10129 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10130 add_loc_descr (&descr,
10131 new_loc_descr (DW_OP_piece,
10132 GET_MODE_SIZE (mode), 0));
10135 add_AT_location_description (die, DW_AT_location, descr);
10136 break;
10138 default:
10139 abort ();
10143 /* If we don't have a copy of this variable in memory for some reason (such
10144 as a C++ member constant that doesn't have an out-of-line definition),
10145 we should tell the debugger about the constant value. */
10147 static void
10148 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10150 tree init = DECL_INITIAL (decl);
10151 tree type = TREE_TYPE (decl);
10153 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init
10154 && initializer_constant_valid_p (init, type) == null_pointer_node)
10155 /* OK */;
10156 else
10157 return;
10159 switch (TREE_CODE (type))
10161 case INTEGER_TYPE:
10162 if (host_integerp (init, 0))
10163 add_AT_unsigned (var_die, DW_AT_const_value,
10164 tree_low_cst (init, 0));
10165 else
10166 add_AT_long_long (var_die, DW_AT_const_value,
10167 TREE_INT_CST_HIGH (init),
10168 TREE_INT_CST_LOW (init));
10169 break;
10171 default:;
10175 /* Generate a DW_AT_name attribute given some string value to be included as
10176 the value of the attribute. */
10178 static void
10179 add_name_attribute (dw_die_ref die, const char *name_string)
10181 if (name_string != NULL && *name_string != 0)
10183 if (demangle_name_func)
10184 name_string = (*demangle_name_func) (name_string);
10186 add_AT_string (die, DW_AT_name, name_string);
10190 /* Generate a DW_AT_comp_dir attribute for DIE. */
10192 static void
10193 add_comp_dir_attribute (dw_die_ref die)
10195 const char *wd = get_src_pwd ();
10196 if (wd != NULL)
10197 add_AT_string (die, DW_AT_comp_dir, wd);
10200 /* Given a tree node describing an array bound (either lower or upper) output
10201 a representation for that bound. */
10203 static void
10204 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10206 switch (TREE_CODE (bound))
10208 case ERROR_MARK:
10209 return;
10211 /* All fixed-bounds are represented by INTEGER_CST nodes. */
10212 case INTEGER_CST:
10213 if (! host_integerp (bound, 0)
10214 || (bound_attr == DW_AT_lower_bound
10215 && (((is_c_family () || is_java ()) && integer_zerop (bound))
10216 || (is_fortran () && integer_onep (bound)))))
10217 /* Use the default. */
10219 else
10220 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
10221 break;
10223 case CONVERT_EXPR:
10224 case NOP_EXPR:
10225 case NON_LVALUE_EXPR:
10226 case VIEW_CONVERT_EXPR:
10227 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10228 break;
10230 case SAVE_EXPR:
10231 break;
10233 case VAR_DECL:
10234 case PARM_DECL:
10235 case RESULT_DECL:
10237 dw_die_ref decl_die = lookup_decl_die (bound);
10239 /* ??? Can this happen, or should the variable have been bound
10240 first? Probably it can, since I imagine that we try to create
10241 the types of parameters in the order in which they exist in
10242 the list, and won't have created a forward reference to a
10243 later parameter. */
10244 if (decl_die != NULL)
10245 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10246 break;
10249 default:
10251 /* Otherwise try to create a stack operation procedure to
10252 evaluate the value of the array bound. */
10254 dw_die_ref ctx, decl_die;
10255 dw_loc_descr_ref loc;
10257 loc = loc_descriptor_from_tree (bound, 0);
10258 if (loc == NULL)
10259 break;
10261 if (current_function_decl == 0)
10262 ctx = comp_unit_die;
10263 else
10264 ctx = lookup_decl_die (current_function_decl);
10266 decl_die = new_die (DW_TAG_variable, ctx, bound);
10267 add_AT_flag (decl_die, DW_AT_artificial, 1);
10268 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10269 add_AT_loc (decl_die, DW_AT_location, loc);
10271 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10272 break;
10277 /* Note that the block of subscript information for an array type also
10278 includes information about the element type of type given array type. */
10280 static void
10281 add_subscript_info (dw_die_ref type_die, tree type)
10283 #ifndef MIPS_DEBUGGING_INFO
10284 unsigned dimension_number;
10285 #endif
10286 tree lower, upper;
10287 dw_die_ref subrange_die;
10289 /* The GNU compilers represent multidimensional array types as sequences of
10290 one dimensional array types whose element types are themselves array
10291 types. Here we squish that down, so that each multidimensional array
10292 type gets only one array_type DIE in the Dwarf debugging info. The draft
10293 Dwarf specification say that we are allowed to do this kind of
10294 compression in C (because there is no difference between an array or
10295 arrays and a multidimensional array in C) but for other source languages
10296 (e.g. Ada) we probably shouldn't do this. */
10298 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10299 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10300 We work around this by disabling this feature. See also
10301 gen_array_type_die. */
10302 #ifndef MIPS_DEBUGGING_INFO
10303 for (dimension_number = 0;
10304 TREE_CODE (type) == ARRAY_TYPE;
10305 type = TREE_TYPE (type), dimension_number++)
10306 #endif
10308 tree domain = TYPE_DOMAIN (type);
10310 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
10311 and (in GNU C only) variable bounds. Handle all three forms
10312 here. */
10313 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
10314 if (domain)
10316 /* We have an array type with specified bounds. */
10317 lower = TYPE_MIN_VALUE (domain);
10318 upper = TYPE_MAX_VALUE (domain);
10320 /* Define the index type. */
10321 if (TREE_TYPE (domain))
10323 /* ??? This is probably an Ada unnamed subrange type. Ignore the
10324 TREE_TYPE field. We can't emit debug info for this
10325 because it is an unnamed integral type. */
10326 if (TREE_CODE (domain) == INTEGER_TYPE
10327 && TYPE_NAME (domain) == NULL_TREE
10328 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10329 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
10331 else
10332 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10333 type_die);
10336 /* ??? If upper is NULL, the array has unspecified length,
10337 but it does have a lower bound. This happens with Fortran
10338 dimension arr(N:*)
10339 Since the debugger is definitely going to need to know N
10340 to produce useful results, go ahead and output the lower
10341 bound solo, and hope the debugger can cope. */
10343 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
10344 if (upper)
10345 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
10348 /* Otherwise we have an array type with an unspecified length. The
10349 DWARF-2 spec does not say how to handle this; let's just leave out the
10350 bounds. */
10354 static void
10355 add_byte_size_attribute (dw_die_ref die, tree tree_node)
10357 unsigned size;
10359 switch (TREE_CODE (tree_node))
10361 case ERROR_MARK:
10362 size = 0;
10363 break;
10364 case ENUMERAL_TYPE:
10365 case RECORD_TYPE:
10366 case UNION_TYPE:
10367 case QUAL_UNION_TYPE:
10368 size = int_size_in_bytes (tree_node);
10369 break;
10370 case FIELD_DECL:
10371 /* For a data member of a struct or union, the DW_AT_byte_size is
10372 generally given as the number of bytes normally allocated for an
10373 object of the *declared* type of the member itself. This is true
10374 even for bit-fields. */
10375 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10376 break;
10377 default:
10378 abort ();
10381 /* Note that `size' might be -1 when we get to this point. If it is, that
10382 indicates that the byte size of the entity in question is variable. We
10383 have no good way of expressing this fact in Dwarf at the present time,
10384 so just let the -1 pass on through. */
10385 add_AT_unsigned (die, DW_AT_byte_size, size);
10388 /* For a FIELD_DECL node which represents a bit-field, output an attribute
10389 which specifies the distance in bits from the highest order bit of the
10390 "containing object" for the bit-field to the highest order bit of the
10391 bit-field itself.
10393 For any given bit-field, the "containing object" is a hypothetical object
10394 (of some integral or enum type) within which the given bit-field lives. The
10395 type of this hypothetical "containing object" is always the same as the
10396 declared type of the individual bit-field itself. The determination of the
10397 exact location of the "containing object" for a bit-field is rather
10398 complicated. It's handled by the `field_byte_offset' function (above).
10400 Note that it is the size (in bytes) of the hypothetical "containing object"
10401 which will be given in the DW_AT_byte_size attribute for this bit-field.
10402 (See `byte_size_attribute' above). */
10404 static inline void
10405 add_bit_offset_attribute (dw_die_ref die, tree decl)
10407 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10408 tree type = DECL_BIT_FIELD_TYPE (decl);
10409 HOST_WIDE_INT bitpos_int;
10410 HOST_WIDE_INT highest_order_object_bit_offset;
10411 HOST_WIDE_INT highest_order_field_bit_offset;
10412 HOST_WIDE_INT unsigned bit_offset;
10414 /* Must be a field and a bit field. */
10415 if (!type
10416 || TREE_CODE (decl) != FIELD_DECL)
10417 abort ();
10419 /* We can't yet handle bit-fields whose offsets are variable, so if we
10420 encounter such things, just return without generating any attribute
10421 whatsoever. Likewise for variable or too large size. */
10422 if (! host_integerp (bit_position (decl), 0)
10423 || ! host_integerp (DECL_SIZE (decl), 1))
10424 return;
10426 bitpos_int = int_bit_position (decl);
10428 /* Note that the bit offset is always the distance (in bits) from the
10429 highest-order bit of the "containing object" to the highest-order bit of
10430 the bit-field itself. Since the "high-order end" of any object or field
10431 is different on big-endian and little-endian machines, the computation
10432 below must take account of these differences. */
10433 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10434 highest_order_field_bit_offset = bitpos_int;
10436 if (! BYTES_BIG_ENDIAN)
10438 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
10439 highest_order_object_bit_offset += simple_type_size_in_bits (type);
10442 bit_offset
10443 = (! BYTES_BIG_ENDIAN
10444 ? highest_order_object_bit_offset - highest_order_field_bit_offset
10445 : highest_order_field_bit_offset - highest_order_object_bit_offset);
10447 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10450 /* For a FIELD_DECL node which represents a bit field, output an attribute
10451 which specifies the length in bits of the given field. */
10453 static inline void
10454 add_bit_size_attribute (dw_die_ref die, tree decl)
10456 /* Must be a field and a bit field. */
10457 if (TREE_CODE (decl) != FIELD_DECL
10458 || ! DECL_BIT_FIELD_TYPE (decl))
10459 abort ();
10461 if (host_integerp (DECL_SIZE (decl), 1))
10462 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
10465 /* If the compiled language is ANSI C, then add a 'prototyped'
10466 attribute, if arg types are given for the parameters of a function. */
10468 static inline void
10469 add_prototyped_attribute (dw_die_ref die, tree func_type)
10471 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10472 && TYPE_ARG_TYPES (func_type) != NULL)
10473 add_AT_flag (die, DW_AT_prototyped, 1);
10476 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
10477 by looking in either the type declaration or object declaration
10478 equate table. */
10480 static inline void
10481 add_abstract_origin_attribute (dw_die_ref die, tree origin)
10483 dw_die_ref origin_die = NULL;
10485 if (TREE_CODE (origin) != FUNCTION_DECL)
10487 /* We may have gotten separated from the block for the inlined
10488 function, if we're in an exception handler or some such; make
10489 sure that the abstract function has been written out.
10491 Doing this for nested functions is wrong, however; functions are
10492 distinct units, and our context might not even be inline. */
10493 tree fn = origin;
10495 if (TYPE_P (fn))
10496 fn = TYPE_STUB_DECL (fn);
10498 fn = decl_function_context (fn);
10499 if (fn)
10500 dwarf2out_abstract_function (fn);
10503 if (DECL_P (origin))
10504 origin_die = lookup_decl_die (origin);
10505 else if (TYPE_P (origin))
10506 origin_die = lookup_type_die (origin);
10508 if (origin_die == NULL)
10509 abort ();
10511 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
10514 /* We do not currently support the pure_virtual attribute. */
10516 static inline void
10517 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
10519 if (DECL_VINDEX (func_decl))
10521 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10523 if (host_integerp (DECL_VINDEX (func_decl), 0))
10524 add_AT_loc (die, DW_AT_vtable_elem_location,
10525 new_loc_descr (DW_OP_constu,
10526 tree_low_cst (DECL_VINDEX (func_decl), 0),
10527 0));
10529 /* GNU extension: Record what type this method came from originally. */
10530 if (debug_info_level > DINFO_LEVEL_TERSE)
10531 add_AT_die_ref (die, DW_AT_containing_type,
10532 lookup_type_die (DECL_CONTEXT (func_decl)));
10536 /* Add source coordinate attributes for the given decl. */
10538 static void
10539 add_src_coords_attributes (dw_die_ref die, tree decl)
10541 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
10542 unsigned file_index = lookup_filename (s.file);
10544 add_AT_unsigned (die, DW_AT_decl_file, file_index);
10545 add_AT_unsigned (die, DW_AT_decl_line, s.line);
10548 /* Add a DW_AT_name attribute and source coordinate attribute for the
10549 given decl, but only if it actually has a name. */
10551 static void
10552 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
10554 tree decl_name;
10556 decl_name = DECL_NAME (decl);
10557 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
10559 add_name_attribute (die, dwarf2_name (decl, 0));
10560 if (! DECL_ARTIFICIAL (decl))
10561 add_src_coords_attributes (die, decl);
10563 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
10564 && TREE_PUBLIC (decl)
10565 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
10566 && !DECL_ABSTRACT (decl))
10567 add_AT_string (die, DW_AT_MIPS_linkage_name,
10568 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
10571 #ifdef VMS_DEBUGGING_INFO
10572 /* Get the function's name, as described by its RTL. This may be different
10573 from the DECL_NAME name used in the source file. */
10574 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
10576 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
10577 XEXP (DECL_RTL (decl), 0));
10578 VARRAY_PUSH_RTX (used_rtx_varray, XEXP (DECL_RTL (decl), 0));
10580 #endif
10583 /* Push a new declaration scope. */
10585 static void
10586 push_decl_scope (tree scope)
10588 VARRAY_PUSH_TREE (decl_scope_table, scope);
10591 /* Pop a declaration scope. */
10593 static inline void
10594 pop_decl_scope (void)
10596 if (VARRAY_ACTIVE_SIZE (decl_scope_table) <= 0)
10597 abort ();
10599 VARRAY_POP (decl_scope_table);
10602 /* Return the DIE for the scope that immediately contains this type.
10603 Non-named types get global scope. Named types nested in other
10604 types get their containing scope if it's open, or global scope
10605 otherwise. All other types (i.e. function-local named types) get
10606 the current active scope. */
10608 static dw_die_ref
10609 scope_die_for (tree t, dw_die_ref context_die)
10611 dw_die_ref scope_die = NULL;
10612 tree containing_scope;
10613 int i;
10615 /* Non-types always go in the current scope. */
10616 if (! TYPE_P (t))
10617 abort ();
10619 containing_scope = TYPE_CONTEXT (t);
10621 /* Use the containing namespace if it was passed in (for a declaration). */
10622 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
10624 if (context_die == lookup_decl_die (containing_scope))
10625 /* OK */;
10626 else
10627 containing_scope = NULL_TREE;
10630 /* Ignore function type "scopes" from the C frontend. They mean that
10631 a tagged type is local to a parmlist of a function declarator, but
10632 that isn't useful to DWARF. */
10633 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
10634 containing_scope = NULL_TREE;
10636 if (containing_scope == NULL_TREE)
10637 scope_die = comp_unit_die;
10638 else if (TYPE_P (containing_scope))
10640 /* For types, we can just look up the appropriate DIE. But
10641 first we check to see if we're in the middle of emitting it
10642 so we know where the new DIE should go. */
10643 for (i = VARRAY_ACTIVE_SIZE (decl_scope_table) - 1; i >= 0; --i)
10644 if (VARRAY_TREE (decl_scope_table, i) == containing_scope)
10645 break;
10647 if (i < 0)
10649 if (debug_info_level > DINFO_LEVEL_TERSE
10650 && !TREE_ASM_WRITTEN (containing_scope))
10651 abort ();
10653 /* If none of the current dies are suitable, we get file scope. */
10654 scope_die = comp_unit_die;
10656 else
10657 scope_die = lookup_type_die (containing_scope);
10659 else
10660 scope_die = context_die;
10662 return scope_die;
10665 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
10667 static inline int
10668 local_scope_p (dw_die_ref context_die)
10670 for (; context_die; context_die = context_die->die_parent)
10671 if (context_die->die_tag == DW_TAG_inlined_subroutine
10672 || context_die->die_tag == DW_TAG_subprogram)
10673 return 1;
10675 return 0;
10678 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
10679 whether or not to treat a DIE in this context as a declaration. */
10681 static inline int
10682 class_or_namespace_scope_p (dw_die_ref context_die)
10684 return (context_die
10685 && (context_die->die_tag == DW_TAG_structure_type
10686 || context_die->die_tag == DW_TAG_union_type
10687 || context_die->die_tag == DW_TAG_namespace));
10690 /* Many forms of DIEs require a "type description" attribute. This
10691 routine locates the proper "type descriptor" die for the type given
10692 by 'type', and adds a DW_AT_type attribute below the given die. */
10694 static void
10695 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
10696 int decl_volatile, dw_die_ref context_die)
10698 enum tree_code code = TREE_CODE (type);
10699 dw_die_ref type_die = NULL;
10701 /* ??? If this type is an unnamed subrange type of an integral or
10702 floating-point type, use the inner type. This is because we have no
10703 support for unnamed types in base_type_die. This can happen if this is
10704 an Ada subrange type. Correct solution is emit a subrange type die. */
10705 if ((code == INTEGER_TYPE || code == REAL_TYPE)
10706 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
10707 type = TREE_TYPE (type), code = TREE_CODE (type);
10709 if (code == ERROR_MARK
10710 /* Handle a special case. For functions whose return type is void, we
10711 generate *no* type attribute. (Note that no object may have type
10712 `void', so this only applies to function return types). */
10713 || code == VOID_TYPE)
10714 return;
10716 type_die = modified_type_die (type,
10717 decl_const || TYPE_READONLY (type),
10718 decl_volatile || TYPE_VOLATILE (type),
10719 context_die);
10721 if (type_die != NULL)
10722 add_AT_die_ref (object_die, DW_AT_type, type_die);
10725 /* Given a tree pointer to a struct, class, union, or enum type node, return
10726 a pointer to the (string) tag name for the given type, or zero if the type
10727 was declared without a tag. */
10729 static const char *
10730 type_tag (tree type)
10732 const char *name = 0;
10734 if (TYPE_NAME (type) != 0)
10736 tree t = 0;
10738 /* Find the IDENTIFIER_NODE for the type name. */
10739 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
10740 t = TYPE_NAME (type);
10742 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
10743 a TYPE_DECL node, regardless of whether or not a `typedef' was
10744 involved. */
10745 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10746 && ! DECL_IGNORED_P (TYPE_NAME (type)))
10747 t = DECL_NAME (TYPE_NAME (type));
10749 /* Now get the name as a string, or invent one. */
10750 if (t != 0)
10751 name = IDENTIFIER_POINTER (t);
10754 return (name == 0 || *name == '\0') ? 0 : name;
10757 /* Return the type associated with a data member, make a special check
10758 for bit field types. */
10760 static inline tree
10761 member_declared_type (tree member)
10763 return (DECL_BIT_FIELD_TYPE (member)
10764 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
10767 /* Get the decl's label, as described by its RTL. This may be different
10768 from the DECL_NAME name used in the source file. */
10770 #if 0
10771 static const char *
10772 decl_start_label (tree decl)
10774 rtx x;
10775 const char *fnname;
10777 x = DECL_RTL (decl);
10778 if (!MEM_P (x))
10779 abort ();
10781 x = XEXP (x, 0);
10782 if (GET_CODE (x) != SYMBOL_REF)
10783 abort ();
10785 fnname = XSTR (x, 0);
10786 return fnname;
10788 #endif
10790 /* These routines generate the internal representation of the DIE's for
10791 the compilation unit. Debugging information is collected by walking
10792 the declaration trees passed in from dwarf2out_decl(). */
10794 static void
10795 gen_array_type_die (tree type, dw_die_ref context_die)
10797 dw_die_ref scope_die = scope_die_for (type, context_die);
10798 dw_die_ref array_die;
10799 tree element_type;
10801 /* ??? The SGI dwarf reader fails for array of array of enum types unless
10802 the inner array type comes before the outer array type. Thus we must
10803 call gen_type_die before we call new_die. See below also. */
10804 #ifdef MIPS_DEBUGGING_INFO
10805 gen_type_die (TREE_TYPE (type), context_die);
10806 #endif
10808 array_die = new_die (DW_TAG_array_type, scope_die, type);
10809 add_name_attribute (array_die, type_tag (type));
10810 equate_type_number_to_die (type, array_die);
10812 if (TREE_CODE (type) == VECTOR_TYPE)
10814 /* The frontend feeds us a representation for the vector as a struct
10815 containing an array. Pull out the array type. */
10816 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
10817 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
10820 #if 0
10821 /* We default the array ordering. SDB will probably do
10822 the right things even if DW_AT_ordering is not present. It's not even
10823 an issue until we start to get into multidimensional arrays anyway. If
10824 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
10825 then we'll have to put the DW_AT_ordering attribute back in. (But if
10826 and when we find out that we need to put these in, we will only do so
10827 for multidimensional arrays. */
10828 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
10829 #endif
10831 #ifdef MIPS_DEBUGGING_INFO
10832 /* The SGI compilers handle arrays of unknown bound by setting
10833 AT_declaration and not emitting any subrange DIEs. */
10834 if (! TYPE_DOMAIN (type))
10835 add_AT_flag (array_die, DW_AT_declaration, 1);
10836 else
10837 #endif
10838 add_subscript_info (array_die, type);
10840 /* Add representation of the type of the elements of this array type. */
10841 element_type = TREE_TYPE (type);
10843 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10844 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10845 We work around this by disabling this feature. See also
10846 add_subscript_info. */
10847 #ifndef MIPS_DEBUGGING_INFO
10848 while (TREE_CODE (element_type) == ARRAY_TYPE)
10849 element_type = TREE_TYPE (element_type);
10851 gen_type_die (element_type, context_die);
10852 #endif
10854 add_type_attribute (array_die, element_type, 0, 0, context_die);
10857 static void
10858 gen_set_type_die (tree type, dw_die_ref context_die)
10860 dw_die_ref type_die
10861 = new_die (DW_TAG_set_type, scope_die_for (type, context_die), type);
10863 equate_type_number_to_die (type, type_die);
10864 add_type_attribute (type_die, TREE_TYPE (type), 0, 0, context_die);
10867 #if 0
10868 static void
10869 gen_entry_point_die (tree decl, dw_die_ref context_die)
10871 tree origin = decl_ultimate_origin (decl);
10872 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
10874 if (origin != NULL)
10875 add_abstract_origin_attribute (decl_die, origin);
10876 else
10878 add_name_and_src_coords_attributes (decl_die, decl);
10879 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
10880 0, 0, context_die);
10883 if (DECL_ABSTRACT (decl))
10884 equate_decl_number_to_die (decl, decl_die);
10885 else
10886 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
10888 #endif
10890 /* Walk through the list of incomplete types again, trying once more to
10891 emit full debugging info for them. */
10893 static void
10894 retry_incomplete_types (void)
10896 int i;
10898 for (i = VARRAY_ACTIVE_SIZE (incomplete_types) - 1; i >= 0; i--)
10899 gen_type_die (VARRAY_TREE (incomplete_types, i), comp_unit_die);
10902 /* Generate a DIE to represent an inlined instance of an enumeration type. */
10904 static void
10905 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
10907 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
10909 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10910 be incomplete and such types are not marked. */
10911 add_abstract_origin_attribute (type_die, type);
10914 /* Generate a DIE to represent an inlined instance of a structure type. */
10916 static void
10917 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
10919 dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
10921 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10922 be incomplete and such types are not marked. */
10923 add_abstract_origin_attribute (type_die, type);
10926 /* Generate a DIE to represent an inlined instance of a union type. */
10928 static void
10929 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
10931 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
10933 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10934 be incomplete and such types are not marked. */
10935 add_abstract_origin_attribute (type_die, type);
10938 /* Generate a DIE to represent an enumeration type. Note that these DIEs
10939 include all of the information about the enumeration values also. Each
10940 enumerated type name/value is listed as a child of the enumerated type
10941 DIE. */
10943 static dw_die_ref
10944 gen_enumeration_type_die (tree type, dw_die_ref context_die)
10946 dw_die_ref type_die = lookup_type_die (type);
10948 if (type_die == NULL)
10950 type_die = new_die (DW_TAG_enumeration_type,
10951 scope_die_for (type, context_die), type);
10952 equate_type_number_to_die (type, type_die);
10953 add_name_attribute (type_die, type_tag (type));
10955 else if (! TYPE_SIZE (type))
10956 return type_die;
10957 else
10958 remove_AT (type_die, DW_AT_declaration);
10960 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
10961 given enum type is incomplete, do not generate the DW_AT_byte_size
10962 attribute or the DW_AT_element_list attribute. */
10963 if (TYPE_SIZE (type))
10965 tree link;
10967 TREE_ASM_WRITTEN (type) = 1;
10968 add_byte_size_attribute (type_die, type);
10969 if (TYPE_STUB_DECL (type) != NULL_TREE)
10970 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
10972 /* If the first reference to this type was as the return type of an
10973 inline function, then it may not have a parent. Fix this now. */
10974 if (type_die->die_parent == NULL)
10975 add_child_die (scope_die_for (type, context_die), type_die);
10977 for (link = TYPE_VALUES (type);
10978 link != NULL; link = TREE_CHAIN (link))
10980 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
10981 tree value = TREE_VALUE (link);
10983 add_name_attribute (enum_die,
10984 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
10986 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
10987 /* DWARF2 does not provide a way of indicating whether or
10988 not enumeration constants are signed or unsigned. GDB
10989 always assumes the values are signed, so we output all
10990 values as if they were signed. That means that
10991 enumeration constants with very large unsigned values
10992 will appear to have negative values in the debugger. */
10993 add_AT_int (enum_die, DW_AT_const_value,
10994 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
10997 else
10998 add_AT_flag (type_die, DW_AT_declaration, 1);
11000 return type_die;
11003 /* Generate a DIE to represent either a real live formal parameter decl or to
11004 represent just the type of some formal parameter position in some function
11005 type.
11007 Note that this routine is a bit unusual because its argument may be a
11008 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
11009 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
11010 node. If it's the former then this function is being called to output a
11011 DIE to represent a formal parameter object (or some inlining thereof). If
11012 it's the latter, then this function is only being called to output a
11013 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
11014 argument type of some subprogram type. */
11016 static dw_die_ref
11017 gen_formal_parameter_die (tree node, dw_die_ref context_die)
11019 dw_die_ref parm_die
11020 = new_die (DW_TAG_formal_parameter, context_die, node);
11021 tree origin;
11023 switch (TREE_CODE_CLASS (TREE_CODE (node)))
11025 case 'd':
11026 origin = decl_ultimate_origin (node);
11027 if (origin != NULL)
11028 add_abstract_origin_attribute (parm_die, origin);
11029 else
11031 add_name_and_src_coords_attributes (parm_die, node);
11032 add_type_attribute (parm_die, TREE_TYPE (node),
11033 TREE_READONLY (node),
11034 TREE_THIS_VOLATILE (node),
11035 context_die);
11036 if (DECL_ARTIFICIAL (node))
11037 add_AT_flag (parm_die, DW_AT_artificial, 1);
11040 equate_decl_number_to_die (node, parm_die);
11041 if (! DECL_ABSTRACT (node))
11042 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
11044 break;
11046 case 't':
11047 /* We were called with some kind of a ..._TYPE node. */
11048 add_type_attribute (parm_die, node, 0, 0, context_die);
11049 break;
11051 default:
11052 abort ();
11055 return parm_die;
11058 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11059 at the end of an (ANSI prototyped) formal parameters list. */
11061 static void
11062 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11064 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11067 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11068 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11069 parameters as specified in some function type specification (except for
11070 those which appear as part of a function *definition*). */
11072 static void
11073 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11075 tree link;
11076 tree formal_type = NULL;
11077 tree first_parm_type;
11078 tree arg;
11080 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11082 arg = DECL_ARGUMENTS (function_or_method_type);
11083 function_or_method_type = TREE_TYPE (function_or_method_type);
11085 else
11086 arg = NULL_TREE;
11088 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11090 /* Make our first pass over the list of formal parameter types and output a
11091 DW_TAG_formal_parameter DIE for each one. */
11092 for (link = first_parm_type; link; )
11094 dw_die_ref parm_die;
11096 formal_type = TREE_VALUE (link);
11097 if (formal_type == void_type_node)
11098 break;
11100 /* Output a (nameless) DIE to represent the formal parameter itself. */
11101 parm_die = gen_formal_parameter_die (formal_type, context_die);
11102 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11103 && link == first_parm_type)
11104 || (arg && DECL_ARTIFICIAL (arg)))
11105 add_AT_flag (parm_die, DW_AT_artificial, 1);
11107 link = TREE_CHAIN (link);
11108 if (arg)
11109 arg = TREE_CHAIN (arg);
11112 /* If this function type has an ellipsis, add a
11113 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
11114 if (formal_type != void_type_node)
11115 gen_unspecified_parameters_die (function_or_method_type, context_die);
11117 /* Make our second (and final) pass over the list of formal parameter types
11118 and output DIEs to represent those types (as necessary). */
11119 for (link = TYPE_ARG_TYPES (function_or_method_type);
11120 link && TREE_VALUE (link);
11121 link = TREE_CHAIN (link))
11122 gen_type_die (TREE_VALUE (link), context_die);
11125 /* We want to generate the DIE for TYPE so that we can generate the
11126 die for MEMBER, which has been defined; we will need to refer back
11127 to the member declaration nested within TYPE. If we're trying to
11128 generate minimal debug info for TYPE, processing TYPE won't do the
11129 trick; we need to attach the member declaration by hand. */
11131 static void
11132 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11134 gen_type_die (type, context_die);
11136 /* If we're trying to avoid duplicate debug info, we may not have
11137 emitted the member decl for this function. Emit it now. */
11138 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11139 && ! lookup_decl_die (member))
11141 if (decl_ultimate_origin (member))
11142 abort ();
11144 push_decl_scope (type);
11145 if (TREE_CODE (member) == FUNCTION_DECL)
11146 gen_subprogram_die (member, lookup_type_die (type));
11147 else
11148 gen_variable_die (member, lookup_type_die (type));
11150 pop_decl_scope ();
11154 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11155 may later generate inlined and/or out-of-line instances of. */
11157 static void
11158 dwarf2out_abstract_function (tree decl)
11160 dw_die_ref old_die;
11161 tree save_fn;
11162 tree context;
11163 int was_abstract = DECL_ABSTRACT (decl);
11165 /* Make sure we have the actual abstract inline, not a clone. */
11166 decl = DECL_ORIGIN (decl);
11168 old_die = lookup_decl_die (decl);
11169 if (old_die && get_AT (old_die, DW_AT_inline))
11170 /* We've already generated the abstract instance. */
11171 return;
11173 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11174 we don't get confused by DECL_ABSTRACT. */
11175 if (debug_info_level > DINFO_LEVEL_TERSE)
11177 context = decl_class_context (decl);
11178 if (context)
11179 gen_type_die_for_member
11180 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11183 /* Pretend we've just finished compiling this function. */
11184 save_fn = current_function_decl;
11185 current_function_decl = decl;
11187 set_decl_abstract_flags (decl, 1);
11188 dwarf2out_decl (decl);
11189 if (! was_abstract)
11190 set_decl_abstract_flags (decl, 0);
11192 current_function_decl = save_fn;
11195 /* Generate a DIE to represent a declared function (either file-scope or
11196 block-local). */
11198 static void
11199 gen_subprogram_die (tree decl, dw_die_ref context_die)
11201 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11202 tree origin = decl_ultimate_origin (decl);
11203 dw_die_ref subr_die;
11204 rtx fp_reg;
11205 tree fn_arg_types;
11206 tree outer_scope;
11207 dw_die_ref old_die = lookup_decl_die (decl);
11208 int declaration = (current_function_decl != decl
11209 || class_or_namespace_scope_p (context_die));
11211 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11212 started to generate the abstract instance of an inline, decided to output
11213 its containing class, and proceeded to emit the declaration of the inline
11214 from the member list for the class. If so, DECLARATION takes priority;
11215 we'll get back to the abstract instance when done with the class. */
11217 /* The class-scope declaration DIE must be the primary DIE. */
11218 if (origin && declaration && class_or_namespace_scope_p (context_die))
11220 origin = NULL;
11221 if (old_die)
11222 abort ();
11225 if (origin != NULL)
11227 if (declaration && ! local_scope_p (context_die))
11228 abort ();
11230 /* Fixup die_parent for the abstract instance of a nested
11231 inline function. */
11232 if (old_die && old_die->die_parent == NULL)
11233 add_child_die (context_die, old_die);
11235 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11236 add_abstract_origin_attribute (subr_die, origin);
11238 else if (old_die)
11240 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11241 unsigned file_index = lookup_filename (s.file);
11243 if (!get_AT_flag (old_die, DW_AT_declaration)
11244 /* We can have a normal definition following an inline one in the
11245 case of redefinition of GNU C extern inlines.
11246 It seems reasonable to use AT_specification in this case. */
11247 && !get_AT (old_die, DW_AT_inline))
11249 /* ??? This can happen if there is a bug in the program, for
11250 instance, if it has duplicate function definitions. Ideally,
11251 we should detect this case and ignore it. For now, if we have
11252 already reported an error, any error at all, then assume that
11253 we got here because of an input error, not a dwarf2 bug. */
11254 if (errorcount)
11255 return;
11256 abort ();
11259 /* If the definition comes from the same place as the declaration,
11260 maybe use the old DIE. We always want the DIE for this function
11261 that has the *_pc attributes to be under comp_unit_die so the
11262 debugger can find it. We also need to do this for abstract
11263 instances of inlines, since the spec requires the out-of-line copy
11264 to have the same parent. For local class methods, this doesn't
11265 apply; we just use the old DIE. */
11266 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
11267 && (DECL_ARTIFICIAL (decl)
11268 || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
11269 && (get_AT_unsigned (old_die, DW_AT_decl_line)
11270 == (unsigned) s.line))))
11272 subr_die = old_die;
11274 /* Clear out the declaration attribute and the formal parameters.
11275 Do not remove all children, because it is possible that this
11276 declaration die was forced using force_decl_die(). In such
11277 cases die that forced declaration die (e.g. TAG_imported_module)
11278 is one of the children that we do not want to remove. */
11279 remove_AT (subr_die, DW_AT_declaration);
11280 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
11282 else
11284 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11285 add_AT_specification (subr_die, old_die);
11286 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11287 add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
11288 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11289 != (unsigned) s.line)
11290 add_AT_unsigned
11291 (subr_die, DW_AT_decl_line, s.line);
11294 else
11296 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11298 if (TREE_PUBLIC (decl))
11299 add_AT_flag (subr_die, DW_AT_external, 1);
11301 add_name_and_src_coords_attributes (subr_die, decl);
11302 if (debug_info_level > DINFO_LEVEL_TERSE)
11304 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11305 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11306 0, 0, context_die);
11309 add_pure_or_virtual_attribute (subr_die, decl);
11310 if (DECL_ARTIFICIAL (decl))
11311 add_AT_flag (subr_die, DW_AT_artificial, 1);
11313 if (TREE_PROTECTED (decl))
11314 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11315 else if (TREE_PRIVATE (decl))
11316 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
11319 if (declaration)
11321 if (!old_die || !get_AT (old_die, DW_AT_inline))
11323 add_AT_flag (subr_die, DW_AT_declaration, 1);
11325 /* The first time we see a member function, it is in the context of
11326 the class to which it belongs. We make sure of this by emitting
11327 the class first. The next time is the definition, which is
11328 handled above. The two may come from the same source text.
11330 Note that force_decl_die() forces function declaration die. It is
11331 later reused to represent definition. */
11332 equate_decl_number_to_die (decl, subr_die);
11335 else if (DECL_ABSTRACT (decl))
11337 if (DECL_DECLARED_INLINE_P (decl))
11339 if (cgraph_function_possibly_inlined_p (decl))
11340 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11341 else
11342 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
11344 else
11346 if (cgraph_function_possibly_inlined_p (decl))
11347 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11348 else
11349 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11352 equate_decl_number_to_die (decl, subr_die);
11354 else if (!DECL_EXTERNAL (decl))
11356 if (!old_die || !get_AT (old_die, DW_AT_inline))
11357 equate_decl_number_to_die (decl, subr_die);
11359 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11360 current_function_funcdef_no);
11361 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11362 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11363 current_function_funcdef_no);
11364 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11366 add_pubname (decl, subr_die);
11367 add_arange (decl, subr_die);
11369 #ifdef MIPS_DEBUGGING_INFO
11370 /* Add a reference to the FDE for this routine. */
11371 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11372 #endif
11374 /* Define the "frame base" location for this routine. We use the
11375 frame pointer or stack pointer registers, since the RTL for local
11376 variables is relative to one of them. */
11377 if (frame_base_decl && lookup_decl_loc (frame_base_decl) != NULL)
11379 add_location_or_const_value_attribute (subr_die, frame_base_decl,
11380 DW_AT_frame_base);
11382 else
11384 fp_reg
11385 = frame_pointer_needed ? hard_frame_pointer_rtx : stack_pointer_rtx;
11386 add_AT_loc (subr_die, DW_AT_frame_base, reg_loc_descriptor (fp_reg));
11389 if (cfun->static_chain_decl)
11390 add_AT_location_description (subr_die, DW_AT_static_link,
11391 loc_descriptor_from_tree (cfun->static_chain_decl, 0));
11394 /* Now output descriptions of the arguments for this function. This gets
11395 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
11396 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
11397 `...' at the end of the formal parameter list. In order to find out if
11398 there was a trailing ellipsis or not, we must instead look at the type
11399 associated with the FUNCTION_DECL. This will be a node of type
11400 FUNCTION_TYPE. If the chain of type nodes hanging off of this
11401 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
11402 an ellipsis at the end. */
11404 /* In the case where we are describing a mere function declaration, all we
11405 need to do here (and all we *can* do here) is to describe the *types* of
11406 its formal parameters. */
11407 if (debug_info_level <= DINFO_LEVEL_TERSE)
11409 else if (declaration)
11410 gen_formal_types_die (decl, subr_die);
11411 else
11413 /* Generate DIEs to represent all known formal parameters. */
11414 tree arg_decls = DECL_ARGUMENTS (decl);
11415 tree parm;
11417 /* When generating DIEs, generate the unspecified_parameters DIE
11418 instead if we come across the arg "__builtin_va_alist" */
11419 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
11420 if (TREE_CODE (parm) == PARM_DECL)
11422 if (DECL_NAME (parm)
11423 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
11424 "__builtin_va_alist"))
11425 gen_unspecified_parameters_die (parm, subr_die);
11426 else
11427 gen_decl_die (parm, subr_die);
11430 /* Decide whether we need an unspecified_parameters DIE at the end.
11431 There are 2 more cases to do this for: 1) the ansi ... declaration -
11432 this is detectable when the end of the arg list is not a
11433 void_type_node 2) an unprototyped function declaration (not a
11434 definition). This just means that we have no info about the
11435 parameters at all. */
11436 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
11437 if (fn_arg_types != NULL)
11439 /* This is the prototyped case, check for.... */
11440 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
11441 gen_unspecified_parameters_die (decl, subr_die);
11443 else if (DECL_INITIAL (decl) == NULL_TREE)
11444 gen_unspecified_parameters_die (decl, subr_die);
11447 /* Output Dwarf info for all of the stuff within the body of the function
11448 (if it has one - it may be just a declaration). */
11449 outer_scope = DECL_INITIAL (decl);
11451 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
11452 a function. This BLOCK actually represents the outermost binding contour
11453 for the function, i.e. the contour in which the function's formal
11454 parameters and labels get declared. Curiously, it appears that the front
11455 end doesn't actually put the PARM_DECL nodes for the current function onto
11456 the BLOCK_VARS list for this outer scope, but are strung off of the
11457 DECL_ARGUMENTS list for the function instead.
11459 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
11460 the LABEL_DECL nodes for the function however, and we output DWARF info
11461 for those in decls_for_scope. Just within the `outer_scope' there will be
11462 a BLOCK node representing the function's outermost pair of curly braces,
11463 and any blocks used for the base and member initializers of a C++
11464 constructor function. */
11465 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
11467 /* Emit a DW_TAG_variable DIE for a named return value. */
11468 if (DECL_NAME (DECL_RESULT (decl)))
11469 gen_decl_die (DECL_RESULT (decl), subr_die);
11471 current_function_has_inlines = 0;
11472 decls_for_scope (outer_scope, subr_die, 0);
11474 #if 0 && defined (MIPS_DEBUGGING_INFO)
11475 if (current_function_has_inlines)
11477 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
11478 if (! comp_unit_has_inlines)
11480 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
11481 comp_unit_has_inlines = 1;
11484 #endif
11488 /* Generate a DIE to represent a declared data object. */
11490 static void
11491 gen_variable_die (tree decl, dw_die_ref context_die)
11493 tree origin = decl_ultimate_origin (decl);
11494 dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
11496 dw_die_ref old_die = lookup_decl_die (decl);
11497 int declaration = (DECL_EXTERNAL (decl)
11498 || class_or_namespace_scope_p (context_die));
11500 if (origin != NULL)
11501 add_abstract_origin_attribute (var_die, origin);
11503 /* Loop unrolling can create multiple blocks that refer to the same
11504 static variable, so we must test for the DW_AT_declaration flag.
11506 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
11507 copy decls and set the DECL_ABSTRACT flag on them instead of
11508 sharing them.
11510 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
11511 else if (old_die && TREE_STATIC (decl)
11512 && get_AT_flag (old_die, DW_AT_declaration) == 1)
11514 /* This is a definition of a C++ class level static. */
11515 add_AT_specification (var_die, old_die);
11516 if (DECL_NAME (decl))
11518 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11519 unsigned file_index = lookup_filename (s.file);
11521 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11522 add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
11524 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11525 != (unsigned) s.line)
11527 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
11530 else
11532 add_name_and_src_coords_attributes (var_die, decl);
11533 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
11534 TREE_THIS_VOLATILE (decl), context_die);
11536 if (TREE_PUBLIC (decl))
11537 add_AT_flag (var_die, DW_AT_external, 1);
11539 if (DECL_ARTIFICIAL (decl))
11540 add_AT_flag (var_die, DW_AT_artificial, 1);
11542 if (TREE_PROTECTED (decl))
11543 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
11544 else if (TREE_PRIVATE (decl))
11545 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
11548 if (declaration)
11549 add_AT_flag (var_die, DW_AT_declaration, 1);
11551 if (DECL_ABSTRACT (decl) || declaration)
11552 equate_decl_number_to_die (decl, var_die);
11554 if (! declaration && ! DECL_ABSTRACT (decl))
11556 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
11557 add_pubname (decl, var_die);
11559 else
11560 tree_add_const_value_attribute (var_die, decl);
11563 /* Generate a DIE to represent a label identifier. */
11565 static void
11566 gen_label_die (tree decl, dw_die_ref context_die)
11568 tree origin = decl_ultimate_origin (decl);
11569 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
11570 rtx insn;
11571 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11573 if (origin != NULL)
11574 add_abstract_origin_attribute (lbl_die, origin);
11575 else
11576 add_name_and_src_coords_attributes (lbl_die, decl);
11578 if (DECL_ABSTRACT (decl))
11579 equate_decl_number_to_die (decl, lbl_die);
11580 else
11582 insn = DECL_RTL_IF_SET (decl);
11584 /* Deleted labels are programmer specified labels which have been
11585 eliminated because of various optimizations. We still emit them
11586 here so that it is possible to put breakpoints on them. */
11587 if (insn
11588 && (LABEL_P (insn)
11589 || ((NOTE_P (insn)
11590 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
11592 /* When optimization is enabled (via -O) some parts of the compiler
11593 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
11594 represent source-level labels which were explicitly declared by
11595 the user. This really shouldn't be happening though, so catch
11596 it if it ever does happen. */
11597 if (INSN_DELETED_P (insn))
11598 abort ();
11600 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
11601 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
11606 /* Generate a DIE for a lexical block. */
11608 static void
11609 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
11611 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
11612 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11614 if (! BLOCK_ABSTRACT (stmt))
11616 if (BLOCK_FRAGMENT_CHAIN (stmt))
11618 tree chain;
11620 add_AT_range_list (stmt_die, DW_AT_ranges, add_ranges (stmt));
11622 chain = BLOCK_FRAGMENT_CHAIN (stmt);
11625 add_ranges (chain);
11626 chain = BLOCK_FRAGMENT_CHAIN (chain);
11628 while (chain);
11629 add_ranges (NULL);
11631 else
11633 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
11634 BLOCK_NUMBER (stmt));
11635 add_AT_lbl_id (stmt_die, DW_AT_low_pc, label);
11636 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
11637 BLOCK_NUMBER (stmt));
11638 add_AT_lbl_id (stmt_die, DW_AT_high_pc, label);
11642 decls_for_scope (stmt, stmt_die, depth);
11645 /* Generate a DIE for an inlined subprogram. */
11647 static void
11648 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
11650 tree decl = block_ultimate_origin (stmt);
11652 /* Emit info for the abstract instance first, if we haven't yet. We
11653 must emit this even if the block is abstract, otherwise when we
11654 emit the block below (or elsewhere), we may end up trying to emit
11655 a die whose origin die hasn't been emitted, and crashing. */
11656 dwarf2out_abstract_function (decl);
11658 if (! BLOCK_ABSTRACT (stmt))
11660 dw_die_ref subr_die
11661 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
11662 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11664 add_abstract_origin_attribute (subr_die, decl);
11665 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
11666 BLOCK_NUMBER (stmt));
11667 add_AT_lbl_id (subr_die, DW_AT_low_pc, label);
11668 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
11669 BLOCK_NUMBER (stmt));
11670 add_AT_lbl_id (subr_die, DW_AT_high_pc, label);
11671 decls_for_scope (stmt, subr_die, depth);
11672 current_function_has_inlines = 1;
11674 else
11675 /* We may get here if we're the outer block of function A that was
11676 inlined into function B that was inlined into function C. When
11677 generating debugging info for C, dwarf2out_abstract_function(B)
11678 would mark all inlined blocks as abstract, including this one.
11679 So, we wouldn't (and shouldn't) expect labels to be generated
11680 for this one. Instead, just emit debugging info for
11681 declarations within the block. This is particularly important
11682 in the case of initializers of arguments passed from B to us:
11683 if they're statement expressions containing declarations, we
11684 wouldn't generate dies for their abstract variables, and then,
11685 when generating dies for the real variables, we'd die (pun
11686 intended :-) */
11687 gen_lexical_block_die (stmt, context_die, depth);
11690 /* Generate a DIE for a field in a record, or structure. */
11692 static void
11693 gen_field_die (tree decl, dw_die_ref context_die)
11695 dw_die_ref decl_die;
11697 if (TREE_TYPE (decl) == error_mark_node)
11698 return;
11700 decl_die = new_die (DW_TAG_member, context_die, decl);
11701 add_name_and_src_coords_attributes (decl_die, decl);
11702 add_type_attribute (decl_die, member_declared_type (decl),
11703 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
11704 context_die);
11706 if (DECL_BIT_FIELD_TYPE (decl))
11708 add_byte_size_attribute (decl_die, decl);
11709 add_bit_size_attribute (decl_die, decl);
11710 add_bit_offset_attribute (decl_die, decl);
11713 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
11714 add_data_member_location_attribute (decl_die, decl);
11716 if (DECL_ARTIFICIAL (decl))
11717 add_AT_flag (decl_die, DW_AT_artificial, 1);
11719 if (TREE_PROTECTED (decl))
11720 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
11721 else if (TREE_PRIVATE (decl))
11722 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
11725 #if 0
11726 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
11727 Use modified_type_die instead.
11728 We keep this code here just in case these types of DIEs may be needed to
11729 represent certain things in other languages (e.g. Pascal) someday. */
11731 static void
11732 gen_pointer_type_die (tree type, dw_die_ref context_die)
11734 dw_die_ref ptr_die
11735 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
11737 equate_type_number_to_die (type, ptr_die);
11738 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
11739 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
11742 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
11743 Use modified_type_die instead.
11744 We keep this code here just in case these types of DIEs may be needed to
11745 represent certain things in other languages (e.g. Pascal) someday. */
11747 static void
11748 gen_reference_type_die (tree type, dw_die_ref context_die)
11750 dw_die_ref ref_die
11751 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
11753 equate_type_number_to_die (type, ref_die);
11754 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
11755 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
11757 #endif
11759 /* Generate a DIE for a pointer to a member type. */
11761 static void
11762 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
11764 dw_die_ref ptr_die
11765 = new_die (DW_TAG_ptr_to_member_type,
11766 scope_die_for (type, context_die), type);
11768 equate_type_number_to_die (type, ptr_die);
11769 add_AT_die_ref (ptr_die, DW_AT_containing_type,
11770 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
11771 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
11774 /* Generate the DIE for the compilation unit. */
11776 static dw_die_ref
11777 gen_compile_unit_die (const char *filename)
11779 dw_die_ref die;
11780 char producer[250];
11781 const char *language_string = lang_hooks.name;
11782 int language;
11784 die = new_die (DW_TAG_compile_unit, NULL, NULL);
11786 if (filename)
11788 add_name_attribute (die, filename);
11789 /* Don't add cwd for <built-in>. */
11790 if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
11791 add_comp_dir_attribute (die);
11794 sprintf (producer, "%s %s", language_string, version_string);
11796 #ifdef MIPS_DEBUGGING_INFO
11797 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
11798 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
11799 not appear in the producer string, the debugger reaches the conclusion
11800 that the object file is stripped and has no debugging information.
11801 To get the MIPS/SGI debugger to believe that there is debugging
11802 information in the object file, we add a -g to the producer string. */
11803 if (debug_info_level > DINFO_LEVEL_TERSE)
11804 strcat (producer, " -g");
11805 #endif
11807 add_AT_string (die, DW_AT_producer, producer);
11809 if (strcmp (language_string, "GNU C++") == 0)
11810 language = DW_LANG_C_plus_plus;
11811 else if (strcmp (language_string, "GNU Ada") == 0)
11812 language = DW_LANG_Ada95;
11813 else if (strcmp (language_string, "GNU F77") == 0)
11814 language = DW_LANG_Fortran77;
11815 else if (strcmp (language_string, "GNU F95") == 0)
11816 language = DW_LANG_Fortran95;
11817 else if (strcmp (language_string, "GNU Pascal") == 0)
11818 language = DW_LANG_Pascal83;
11819 else if (strcmp (language_string, "GNU Java") == 0)
11820 language = DW_LANG_Java;
11821 else
11822 language = DW_LANG_C89;
11824 add_AT_unsigned (die, DW_AT_language, language);
11825 return die;
11828 /* Generate a DIE for a string type. */
11830 static void
11831 gen_string_type_die (tree type, dw_die_ref context_die)
11833 dw_die_ref type_die
11834 = new_die (DW_TAG_string_type, scope_die_for (type, context_die), type);
11836 equate_type_number_to_die (type, type_die);
11838 /* ??? Fudge the string length attribute for now.
11839 TODO: add string length info. */
11840 #if 0
11841 string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
11842 bound_representation (upper_bound, 0, 'u');
11843 #endif
11846 /* Generate the DIE for a base class. */
11848 static void
11849 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
11851 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
11853 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
11854 add_data_member_location_attribute (die, binfo);
11856 if (BINFO_VIRTUAL_P (binfo))
11857 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11859 if (access == access_public_node)
11860 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
11861 else if (access == access_protected_node)
11862 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
11865 /* Generate a DIE for a class member. */
11867 static void
11868 gen_member_die (tree type, dw_die_ref context_die)
11870 tree member;
11871 tree binfo = TYPE_BINFO (type);
11872 dw_die_ref child;
11874 /* If this is not an incomplete type, output descriptions of each of its
11875 members. Note that as we output the DIEs necessary to represent the
11876 members of this record or union type, we will also be trying to output
11877 DIEs to represent the *types* of those members. However the `type'
11878 function (above) will specifically avoid generating type DIEs for member
11879 types *within* the list of member DIEs for this (containing) type except
11880 for those types (of members) which are explicitly marked as also being
11881 members of this (containing) type themselves. The g++ front- end can
11882 force any given type to be treated as a member of some other (containing)
11883 type by setting the TYPE_CONTEXT of the given (member) type to point to
11884 the TREE node representing the appropriate (containing) type. */
11886 /* First output info about the base classes. */
11887 if (binfo)
11889 VEC (tree) *accesses = BINFO_BASE_ACCESSES (binfo);
11890 int i;
11891 tree base;
11893 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
11894 gen_inheritance_die (base,
11895 (accesses ? VEC_index (tree, accesses, i)
11896 : access_public_node), context_die);
11899 /* Now output info about the data members and type members. */
11900 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
11902 /* If we thought we were generating minimal debug info for TYPE
11903 and then changed our minds, some of the member declarations
11904 may have already been defined. Don't define them again, but
11905 do put them in the right order. */
11907 child = lookup_decl_die (member);
11908 if (child)
11909 splice_child_die (context_die, child);
11910 else
11911 gen_decl_die (member, context_die);
11914 /* Now output info about the function members (if any). */
11915 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
11917 /* Don't include clones in the member list. */
11918 if (DECL_ABSTRACT_ORIGIN (member))
11919 continue;
11921 child = lookup_decl_die (member);
11922 if (child)
11923 splice_child_die (context_die, child);
11924 else
11925 gen_decl_die (member, context_die);
11929 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
11930 is set, we pretend that the type was never defined, so we only get the
11931 member DIEs needed by later specification DIEs. */
11933 static void
11934 gen_struct_or_union_type_die (tree type, dw_die_ref context_die)
11936 dw_die_ref type_die = lookup_type_die (type);
11937 dw_die_ref scope_die = 0;
11938 int nested = 0;
11939 int complete = (TYPE_SIZE (type)
11940 && (! TYPE_STUB_DECL (type)
11941 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
11942 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
11944 if (type_die && ! complete)
11945 return;
11947 if (TYPE_CONTEXT (type) != NULL_TREE
11948 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
11949 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
11950 nested = 1;
11952 scope_die = scope_die_for (type, context_die);
11954 if (! type_die || (nested && scope_die == comp_unit_die))
11955 /* First occurrence of type or toplevel definition of nested class. */
11957 dw_die_ref old_die = type_die;
11959 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
11960 ? DW_TAG_structure_type : DW_TAG_union_type,
11961 scope_die, type);
11962 equate_type_number_to_die (type, type_die);
11963 if (old_die)
11964 add_AT_specification (type_die, old_die);
11965 else
11966 add_name_attribute (type_die, type_tag (type));
11968 else
11969 remove_AT (type_die, DW_AT_declaration);
11971 /* If this type has been completed, then give it a byte_size attribute and
11972 then give a list of members. */
11973 if (complete && !ns_decl)
11975 /* Prevent infinite recursion in cases where the type of some member of
11976 this type is expressed in terms of this type itself. */
11977 TREE_ASM_WRITTEN (type) = 1;
11978 add_byte_size_attribute (type_die, type);
11979 if (TYPE_STUB_DECL (type) != NULL_TREE)
11980 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11982 /* If the first reference to this type was as the return type of an
11983 inline function, then it may not have a parent. Fix this now. */
11984 if (type_die->die_parent == NULL)
11985 add_child_die (scope_die, type_die);
11987 push_decl_scope (type);
11988 gen_member_die (type, type_die);
11989 pop_decl_scope ();
11991 /* GNU extension: Record what type our vtable lives in. */
11992 if (TYPE_VFIELD (type))
11994 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
11996 gen_type_die (vtype, context_die);
11997 add_AT_die_ref (type_die, DW_AT_containing_type,
11998 lookup_type_die (vtype));
12001 else
12003 add_AT_flag (type_die, DW_AT_declaration, 1);
12005 /* We don't need to do this for function-local types. */
12006 if (TYPE_STUB_DECL (type)
12007 && ! decl_function_context (TYPE_STUB_DECL (type)))
12008 VARRAY_PUSH_TREE (incomplete_types, type);
12012 /* Generate a DIE for a subroutine _type_. */
12014 static void
12015 gen_subroutine_type_die (tree type, dw_die_ref context_die)
12017 tree return_type = TREE_TYPE (type);
12018 dw_die_ref subr_die
12019 = new_die (DW_TAG_subroutine_type,
12020 scope_die_for (type, context_die), type);
12022 equate_type_number_to_die (type, subr_die);
12023 add_prototyped_attribute (subr_die, type);
12024 add_type_attribute (subr_die, return_type, 0, 0, context_die);
12025 gen_formal_types_die (type, subr_die);
12028 /* Generate a DIE for a type definition. */
12030 static void
12031 gen_typedef_die (tree decl, dw_die_ref context_die)
12033 dw_die_ref type_die;
12034 tree origin;
12036 if (TREE_ASM_WRITTEN (decl))
12037 return;
12039 TREE_ASM_WRITTEN (decl) = 1;
12040 type_die = new_die (DW_TAG_typedef, context_die, decl);
12041 origin = decl_ultimate_origin (decl);
12042 if (origin != NULL)
12043 add_abstract_origin_attribute (type_die, origin);
12044 else
12046 tree type;
12048 add_name_and_src_coords_attributes (type_die, decl);
12049 if (DECL_ORIGINAL_TYPE (decl))
12051 type = DECL_ORIGINAL_TYPE (decl);
12053 if (type == TREE_TYPE (decl))
12054 abort ();
12055 else
12056 equate_type_number_to_die (TREE_TYPE (decl), type_die);
12058 else
12059 type = TREE_TYPE (decl);
12061 add_type_attribute (type_die, type, TREE_READONLY (decl),
12062 TREE_THIS_VOLATILE (decl), context_die);
12065 if (DECL_ABSTRACT (decl))
12066 equate_decl_number_to_die (decl, type_die);
12069 /* Generate a type description DIE. */
12071 static void
12072 gen_type_die (tree type, dw_die_ref context_die)
12074 int need_pop;
12076 if (type == NULL_TREE || type == error_mark_node)
12077 return;
12079 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12080 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
12082 if (TREE_ASM_WRITTEN (type))
12083 return;
12085 /* Prevent broken recursion; we can't hand off to the same type. */
12086 if (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) == type)
12087 abort ();
12089 TREE_ASM_WRITTEN (type) = 1;
12090 gen_decl_die (TYPE_NAME (type), context_die);
12091 return;
12094 /* We are going to output a DIE to represent the unqualified version
12095 of this type (i.e. without any const or volatile qualifiers) so
12096 get the main variant (i.e. the unqualified version) of this type
12097 now. (Vectors are special because the debugging info is in the
12098 cloned type itself). */
12099 if (TREE_CODE (type) != VECTOR_TYPE)
12100 type = type_main_variant (type);
12102 if (TREE_ASM_WRITTEN (type))
12103 return;
12105 switch (TREE_CODE (type))
12107 case ERROR_MARK:
12108 break;
12110 case POINTER_TYPE:
12111 case REFERENCE_TYPE:
12112 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
12113 ensures that the gen_type_die recursion will terminate even if the
12114 type is recursive. Recursive types are possible in Ada. */
12115 /* ??? We could perhaps do this for all types before the switch
12116 statement. */
12117 TREE_ASM_WRITTEN (type) = 1;
12119 /* For these types, all that is required is that we output a DIE (or a
12120 set of DIEs) to represent the "basis" type. */
12121 gen_type_die (TREE_TYPE (type), context_die);
12122 break;
12124 case OFFSET_TYPE:
12125 /* This code is used for C++ pointer-to-data-member types.
12126 Output a description of the relevant class type. */
12127 gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
12129 /* Output a description of the type of the object pointed to. */
12130 gen_type_die (TREE_TYPE (type), context_die);
12132 /* Now output a DIE to represent this pointer-to-data-member type
12133 itself. */
12134 gen_ptr_to_mbr_type_die (type, context_die);
12135 break;
12137 case SET_TYPE:
12138 gen_type_die (TYPE_DOMAIN (type), context_die);
12139 gen_set_type_die (type, context_die);
12140 break;
12142 case FILE_TYPE:
12143 gen_type_die (TREE_TYPE (type), context_die);
12144 abort (); /* No way to represent these in Dwarf yet! */
12145 break;
12147 case FUNCTION_TYPE:
12148 /* Force out return type (in case it wasn't forced out already). */
12149 gen_type_die (TREE_TYPE (type), context_die);
12150 gen_subroutine_type_die (type, context_die);
12151 break;
12153 case METHOD_TYPE:
12154 /* Force out return type (in case it wasn't forced out already). */
12155 gen_type_die (TREE_TYPE (type), context_die);
12156 gen_subroutine_type_die (type, context_die);
12157 break;
12159 case ARRAY_TYPE:
12160 if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
12162 gen_type_die (TREE_TYPE (type), context_die);
12163 gen_string_type_die (type, context_die);
12165 else
12166 gen_array_type_die (type, context_die);
12167 break;
12169 case VECTOR_TYPE:
12170 gen_array_type_die (type, context_die);
12171 break;
12173 case ENUMERAL_TYPE:
12174 case RECORD_TYPE:
12175 case UNION_TYPE:
12176 case QUAL_UNION_TYPE:
12177 /* If this is a nested type whose containing class hasn't been written
12178 out yet, writing it out will cover this one, too. This does not apply
12179 to instantiations of member class templates; they need to be added to
12180 the containing class as they are generated. FIXME: This hurts the
12181 idea of combining type decls from multiple TUs, since we can't predict
12182 what set of template instantiations we'll get. */
12183 if (TYPE_CONTEXT (type)
12184 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12185 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
12187 gen_type_die (TYPE_CONTEXT (type), context_die);
12189 if (TREE_ASM_WRITTEN (type))
12190 return;
12192 /* If that failed, attach ourselves to the stub. */
12193 push_decl_scope (TYPE_CONTEXT (type));
12194 context_die = lookup_type_die (TYPE_CONTEXT (type));
12195 need_pop = 1;
12197 else
12199 declare_in_namespace (type, context_die);
12200 need_pop = 0;
12203 if (TREE_CODE (type) == ENUMERAL_TYPE)
12204 gen_enumeration_type_die (type, context_die);
12205 else
12206 gen_struct_or_union_type_die (type, context_die);
12208 if (need_pop)
12209 pop_decl_scope ();
12211 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
12212 it up if it is ever completed. gen_*_type_die will set it for us
12213 when appropriate. */
12214 return;
12216 case VOID_TYPE:
12217 case INTEGER_TYPE:
12218 case REAL_TYPE:
12219 case COMPLEX_TYPE:
12220 case BOOLEAN_TYPE:
12221 case CHAR_TYPE:
12222 /* No DIEs needed for fundamental types. */
12223 break;
12225 case LANG_TYPE:
12226 /* No Dwarf representation currently defined. */
12227 break;
12229 default:
12230 abort ();
12233 TREE_ASM_WRITTEN (type) = 1;
12236 /* Generate a DIE for a tagged type instantiation. */
12238 static void
12239 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
12241 if (type == NULL_TREE || type == error_mark_node)
12242 return;
12244 /* We are going to output a DIE to represent the unqualified version of
12245 this type (i.e. without any const or volatile qualifiers) so make sure
12246 that we have the main variant (i.e. the unqualified version) of this
12247 type now. */
12248 if (type != type_main_variant (type))
12249 abort ();
12251 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
12252 an instance of an unresolved type. */
12254 switch (TREE_CODE (type))
12256 case ERROR_MARK:
12257 break;
12259 case ENUMERAL_TYPE:
12260 gen_inlined_enumeration_type_die (type, context_die);
12261 break;
12263 case RECORD_TYPE:
12264 gen_inlined_structure_type_die (type, context_die);
12265 break;
12267 case UNION_TYPE:
12268 case QUAL_UNION_TYPE:
12269 gen_inlined_union_type_die (type, context_die);
12270 break;
12272 default:
12273 abort ();
12277 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12278 things which are local to the given block. */
12280 static void
12281 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
12283 int must_output_die = 0;
12284 tree origin;
12285 tree decl;
12286 enum tree_code origin_code;
12288 /* Ignore blocks never really used to make RTL. */
12289 if (stmt == NULL_TREE || !TREE_USED (stmt)
12290 || (!TREE_ASM_WRITTEN (stmt) && !BLOCK_ABSTRACT (stmt)))
12291 return;
12293 /* If the block is one fragment of a non-contiguous block, do not
12294 process the variables, since they will have been done by the
12295 origin block. Do process subblocks. */
12296 if (BLOCK_FRAGMENT_ORIGIN (stmt))
12298 tree sub;
12300 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
12301 gen_block_die (sub, context_die, depth + 1);
12303 return;
12306 /* Determine the "ultimate origin" of this block. This block may be an
12307 inlined instance of an inlined instance of inline function, so we have
12308 to trace all of the way back through the origin chain to find out what
12309 sort of node actually served as the original seed for the creation of
12310 the current block. */
12311 origin = block_ultimate_origin (stmt);
12312 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
12314 /* Determine if we need to output any Dwarf DIEs at all to represent this
12315 block. */
12316 if (origin_code == FUNCTION_DECL)
12317 /* The outer scopes for inlinings *must* always be represented. We
12318 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
12319 must_output_die = 1;
12320 else
12322 /* In the case where the current block represents an inlining of the
12323 "body block" of an inline function, we must *NOT* output any DIE for
12324 this block because we have already output a DIE to represent the whole
12325 inlined function scope and the "body block" of any function doesn't
12326 really represent a different scope according to ANSI C rules. So we
12327 check here to make sure that this block does not represent a "body
12328 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
12329 if (! is_body_block (origin ? origin : stmt))
12331 /* Determine if this block directly contains any "significant"
12332 local declarations which we will need to output DIEs for. */
12333 if (debug_info_level > DINFO_LEVEL_TERSE)
12334 /* We are not in terse mode so *any* local declaration counts
12335 as being a "significant" one. */
12336 must_output_die = (BLOCK_VARS (stmt) != NULL);
12337 else
12338 /* We are in terse mode, so only local (nested) function
12339 definitions count as "significant" local declarations. */
12340 for (decl = BLOCK_VARS (stmt);
12341 decl != NULL; decl = TREE_CHAIN (decl))
12342 if (TREE_CODE (decl) == FUNCTION_DECL
12343 && DECL_INITIAL (decl))
12345 must_output_die = 1;
12346 break;
12351 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
12352 DIE for any block which contains no significant local declarations at
12353 all. Rather, in such cases we just call `decls_for_scope' so that any
12354 needed Dwarf info for any sub-blocks will get properly generated. Note
12355 that in terse mode, our definition of what constitutes a "significant"
12356 local declaration gets restricted to include only inlined function
12357 instances and local (nested) function definitions. */
12358 if (must_output_die)
12360 if (origin_code == FUNCTION_DECL)
12361 gen_inlined_subroutine_die (stmt, context_die, depth);
12362 else
12363 gen_lexical_block_die (stmt, context_die, depth);
12365 else
12366 decls_for_scope (stmt, context_die, depth);
12369 /* Generate all of the decls declared within a given scope and (recursively)
12370 all of its sub-blocks. */
12372 static void
12373 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
12375 tree decl;
12376 tree subblocks;
12378 /* Ignore blocks never really used to make RTL. */
12379 if (stmt == NULL_TREE || ! TREE_USED (stmt))
12380 return;
12382 /* Output the DIEs to represent all of the data objects and typedefs
12383 declared directly within this block but not within any nested
12384 sub-blocks. Also, nested function and tag DIEs have been
12385 generated with a parent of NULL; fix that up now. */
12386 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
12388 dw_die_ref die;
12390 if (TREE_CODE (decl) == FUNCTION_DECL)
12391 die = lookup_decl_die (decl);
12392 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
12393 die = lookup_type_die (TREE_TYPE (decl));
12394 else
12395 die = NULL;
12397 if (die != NULL && die->die_parent == NULL)
12398 add_child_die (context_die, die);
12399 else
12400 gen_decl_die (decl, context_die);
12403 /* If we're at -g1, we're not interested in subblocks. */
12404 if (debug_info_level <= DINFO_LEVEL_TERSE)
12405 return;
12407 /* Output the DIEs to represent all sub-blocks (and the items declared
12408 therein) of this block. */
12409 for (subblocks = BLOCK_SUBBLOCKS (stmt);
12410 subblocks != NULL;
12411 subblocks = BLOCK_CHAIN (subblocks))
12412 gen_block_die (subblocks, context_die, depth + 1);
12415 /* Is this a typedef we can avoid emitting? */
12417 static inline int
12418 is_redundant_typedef (tree decl)
12420 if (TYPE_DECL_IS_STUB (decl))
12421 return 1;
12423 if (DECL_ARTIFICIAL (decl)
12424 && DECL_CONTEXT (decl)
12425 && is_tagged_type (DECL_CONTEXT (decl))
12426 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
12427 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
12428 /* Also ignore the artificial member typedef for the class name. */
12429 return 1;
12431 return 0;
12434 /* Returns the DIE for decl or aborts. */
12436 static dw_die_ref
12437 force_decl_die (tree decl)
12439 dw_die_ref decl_die;
12440 unsigned saved_external_flag;
12441 tree save_fn = NULL_TREE;
12442 decl_die = lookup_decl_die (decl);
12443 if (!decl_die)
12445 dw_die_ref context_die;
12446 tree decl_context = DECL_CONTEXT (decl);
12447 if (decl_context)
12449 /* Find die that represents this context. */
12450 if (TYPE_P (decl_context))
12451 context_die = force_type_die (decl_context);
12452 else
12453 context_die = force_decl_die (decl_context);
12455 else
12456 context_die = comp_unit_die;
12458 switch (TREE_CODE (decl))
12460 case FUNCTION_DECL:
12461 /* Clear current_function_decl, so that gen_subprogram_die thinks
12462 that this is a declaration. At this point, we just want to force
12463 declaration die. */
12464 save_fn = current_function_decl;
12465 current_function_decl = NULL_TREE;
12466 gen_subprogram_die (decl, context_die);
12467 current_function_decl = save_fn;
12468 break;
12470 case VAR_DECL:
12471 /* Set external flag to force declaration die. Restore it after
12472 gen_decl_die() call. */
12473 saved_external_flag = DECL_EXTERNAL (decl);
12474 DECL_EXTERNAL (decl) = 1;
12475 gen_decl_die (decl, context_die);
12476 DECL_EXTERNAL (decl) = saved_external_flag;
12477 break;
12479 case NAMESPACE_DECL:
12480 dwarf2out_decl (decl);
12481 break;
12483 default:
12484 abort ();
12487 /* See if we can find the die for this deci now.
12488 If not then abort. */
12489 if (!decl_die)
12490 decl_die = lookup_decl_die (decl);
12491 if (!decl_die)
12492 abort ();
12495 return decl_die;
12498 /* Returns the DIE for decl or aborts. */
12500 static dw_die_ref
12501 force_type_die (tree type)
12503 dw_die_ref type_die;
12505 type_die = lookup_type_die (type);
12506 if (!type_die)
12508 dw_die_ref context_die;
12509 if (TYPE_CONTEXT (type))
12510 if (TYPE_P (TYPE_CONTEXT (type)))
12511 context_die = force_type_die (TYPE_CONTEXT (type));
12512 else
12513 context_die = force_decl_die (TYPE_CONTEXT (type));
12514 else
12515 context_die = comp_unit_die;
12517 gen_type_die (type, context_die);
12518 type_die = lookup_type_die (type);
12519 if (!type_die)
12520 abort();
12522 return type_die;
12525 /* Force out any required namespaces to be able to output DECL,
12526 and return the new context_die for it, if it's changed. */
12528 static dw_die_ref
12529 setup_namespace_context (tree thing, dw_die_ref context_die)
12531 tree context = DECL_P (thing) ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing);
12532 if (context && TREE_CODE (context) == NAMESPACE_DECL)
12533 /* Force out the namespace. */
12534 context_die = force_decl_die (context);
12536 return context_die;
12539 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
12540 type) within its namespace, if appropriate.
12542 For compatibility with older debuggers, namespace DIEs only contain
12543 declarations; all definitions are emitted at CU scope. */
12545 static void
12546 declare_in_namespace (tree thing, dw_die_ref context_die)
12548 dw_die_ref ns_context;
12550 if (debug_info_level <= DINFO_LEVEL_TERSE)
12551 return;
12553 ns_context = setup_namespace_context (thing, context_die);
12555 if (ns_context != context_die)
12557 if (DECL_P (thing))
12558 gen_decl_die (thing, ns_context);
12559 else
12560 gen_type_die (thing, ns_context);
12564 /* Generate a DIE for a namespace or namespace alias. */
12566 static void
12567 gen_namespace_die (tree decl)
12569 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
12571 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
12572 they are an alias of. */
12573 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
12575 /* Output a real namespace. */
12576 dw_die_ref namespace_die
12577 = new_die (DW_TAG_namespace, context_die, decl);
12578 add_name_and_src_coords_attributes (namespace_die, decl);
12579 equate_decl_number_to_die (decl, namespace_die);
12581 else
12583 /* Output a namespace alias. */
12585 /* Force out the namespace we are an alias of, if necessary. */
12586 dw_die_ref origin_die
12587 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
12589 /* Now create the namespace alias DIE. */
12590 dw_die_ref namespace_die
12591 = new_die (DW_TAG_imported_declaration, context_die, decl);
12592 add_name_and_src_coords_attributes (namespace_die, decl);
12593 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
12594 equate_decl_number_to_die (decl, namespace_die);
12598 /* Generate Dwarf debug information for a decl described by DECL. */
12600 static void
12601 gen_decl_die (tree decl, dw_die_ref context_die)
12603 tree origin;
12605 if (DECL_P (decl) && DECL_IGNORED_P (decl))
12606 return;
12608 switch (TREE_CODE (decl))
12610 case ERROR_MARK:
12611 break;
12613 case CONST_DECL:
12614 /* The individual enumerators of an enum type get output when we output
12615 the Dwarf representation of the relevant enum type itself. */
12616 break;
12618 case FUNCTION_DECL:
12619 /* Don't output any DIEs to represent mere function declarations,
12620 unless they are class members or explicit block externs. */
12621 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
12622 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
12623 break;
12625 #if 0
12626 /* FIXME */
12627 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
12628 on local redeclarations of global functions. That seems broken. */
12629 if (current_function_decl != decl)
12630 /* This is only a declaration. */;
12631 #endif
12633 /* If we're emitting a clone, emit info for the abstract instance. */
12634 if (DECL_ORIGIN (decl) != decl)
12635 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
12637 /* If we're emitting an out-of-line copy of an inline function,
12638 emit info for the abstract instance and set up to refer to it. */
12639 else if (cgraph_function_possibly_inlined_p (decl)
12640 && ! DECL_ABSTRACT (decl)
12641 && ! class_or_namespace_scope_p (context_die)
12642 /* dwarf2out_abstract_function won't emit a die if this is just
12643 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
12644 that case, because that works only if we have a die. */
12645 && DECL_INITIAL (decl) != NULL_TREE)
12647 dwarf2out_abstract_function (decl);
12648 set_decl_origin_self (decl);
12651 /* Otherwise we're emitting the primary DIE for this decl. */
12652 else if (debug_info_level > DINFO_LEVEL_TERSE)
12654 /* Before we describe the FUNCTION_DECL itself, make sure that we
12655 have described its return type. */
12656 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
12658 /* And its virtual context. */
12659 if (DECL_VINDEX (decl) != NULL_TREE)
12660 gen_type_die (DECL_CONTEXT (decl), context_die);
12662 /* And its containing type. */
12663 origin = decl_class_context (decl);
12664 if (origin != NULL_TREE)
12665 gen_type_die_for_member (origin, decl, context_die);
12667 /* And its containing namespace. */
12668 declare_in_namespace (decl, context_die);
12671 /* Now output a DIE to represent the function itself. */
12672 gen_subprogram_die (decl, context_die);
12673 break;
12675 case TYPE_DECL:
12676 /* If we are in terse mode, don't generate any DIEs to represent any
12677 actual typedefs. */
12678 if (debug_info_level <= DINFO_LEVEL_TERSE)
12679 break;
12681 /* In the special case of a TYPE_DECL node representing the declaration
12682 of some type tag, if the given TYPE_DECL is marked as having been
12683 instantiated from some other (original) TYPE_DECL node (e.g. one which
12684 was generated within the original definition of an inline function) we
12685 have to generate a special (abbreviated) DW_TAG_structure_type,
12686 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
12687 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
12689 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
12690 break;
12693 if (is_redundant_typedef (decl))
12694 gen_type_die (TREE_TYPE (decl), context_die);
12695 else
12696 /* Output a DIE to represent the typedef itself. */
12697 gen_typedef_die (decl, context_die);
12698 break;
12700 case LABEL_DECL:
12701 if (debug_info_level >= DINFO_LEVEL_NORMAL)
12702 gen_label_die (decl, context_die);
12703 break;
12705 case VAR_DECL:
12706 case RESULT_DECL:
12707 /* If we are in terse mode, don't generate any DIEs to represent any
12708 variable declarations or definitions. */
12709 if (debug_info_level <= DINFO_LEVEL_TERSE)
12710 break;
12712 /* Output any DIEs that are needed to specify the type of this data
12713 object. */
12714 gen_type_die (TREE_TYPE (decl), context_die);
12716 /* And its containing type. */
12717 origin = decl_class_context (decl);
12718 if (origin != NULL_TREE)
12719 gen_type_die_for_member (origin, decl, context_die);
12721 /* And its containing namespace. */
12722 declare_in_namespace (decl, context_die);
12724 /* Now output the DIE to represent the data object itself. This gets
12725 complicated because of the possibility that the VAR_DECL really
12726 represents an inlined instance of a formal parameter for an inline
12727 function. */
12728 origin = decl_ultimate_origin (decl);
12729 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
12730 gen_formal_parameter_die (decl, context_die);
12731 else
12732 gen_variable_die (decl, context_die);
12733 break;
12735 case FIELD_DECL:
12736 /* Ignore the nameless fields that are used to skip bits but handle C++
12737 anonymous unions and structs. */
12738 if (DECL_NAME (decl) != NULL_TREE
12739 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
12740 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
12742 gen_type_die (member_declared_type (decl), context_die);
12743 gen_field_die (decl, context_die);
12745 break;
12747 case PARM_DECL:
12748 gen_type_die (TREE_TYPE (decl), context_die);
12749 gen_formal_parameter_die (decl, context_die);
12750 break;
12752 case NAMESPACE_DECL:
12753 gen_namespace_die (decl);
12754 break;
12756 default:
12757 if ((int)TREE_CODE (decl) > NUM_TREE_CODES)
12758 /* Probably some frontend-internal decl. Assume we don't care. */
12759 break;
12760 abort ();
12764 /* Add Ada "use" clause information for SGI Workshop debugger. */
12766 void
12767 dwarf2out_add_library_unit_info (const char *filename, const char *context_list)
12769 unsigned int file_index;
12771 if (filename != NULL)
12773 dw_die_ref unit_die = new_die (DW_TAG_module, comp_unit_die, NULL);
12774 tree context_list_decl
12775 = build_decl (LABEL_DECL, get_identifier (context_list),
12776 void_type_node);
12778 TREE_PUBLIC (context_list_decl) = TRUE;
12779 add_name_attribute (unit_die, context_list);
12780 file_index = lookup_filename (filename);
12781 add_AT_unsigned (unit_die, DW_AT_decl_file, file_index);
12782 add_pubname (context_list_decl, unit_die);
12786 /* Output debug information for global decl DECL. Called from toplev.c after
12787 compilation proper has finished. */
12789 static void
12790 dwarf2out_global_decl (tree decl)
12792 /* Output DWARF2 information for file-scope tentative data object
12793 declarations, file-scope (extern) function declarations (which had no
12794 corresponding body) and file-scope tagged type declarations and
12795 definitions which have not yet been forced out. */
12796 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
12797 dwarf2out_decl (decl);
12800 /* Output debug information for type decl DECL. Called from toplev.c
12801 and from language front ends (to record built-in types). */
12802 static void
12803 dwarf2out_type_decl (tree decl, int local)
12805 if (!local)
12806 dwarf2out_decl (decl);
12809 /* Output debug information for imported module or decl. */
12811 static void
12812 dwarf2out_imported_module_or_decl (tree decl, tree context)
12814 dw_die_ref imported_die, at_import_die;
12815 dw_die_ref scope_die;
12816 unsigned file_index;
12817 expanded_location xloc;
12819 if (debug_info_level <= DINFO_LEVEL_TERSE)
12820 return;
12822 if (!decl)
12823 abort ();
12825 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
12826 We need decl DIE for reference and scope die. First, get DIE for the decl
12827 itself. */
12829 /* Get the scope die for decl context. Use comp_unit_die for global module
12830 or decl. If die is not found for non globals, force new die. */
12831 if (!context)
12832 scope_die = comp_unit_die;
12833 else if (TYPE_P (context))
12834 scope_die = force_type_die (context);
12835 else
12836 scope_die = force_decl_die (context);
12838 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
12839 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
12840 at_import_die = force_type_die (TREE_TYPE (decl));
12841 else
12842 at_import_die = force_decl_die (decl);
12844 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
12845 if (TREE_CODE (decl) == NAMESPACE_DECL)
12846 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
12847 else
12848 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
12850 xloc = expand_location (input_location);
12851 file_index = lookup_filename (xloc.file);
12852 add_AT_unsigned (imported_die, DW_AT_decl_file, file_index);
12853 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
12854 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
12857 /* Write the debugging output for DECL. */
12859 void
12860 dwarf2out_decl (tree decl)
12862 dw_die_ref context_die = comp_unit_die;
12864 switch (TREE_CODE (decl))
12866 case ERROR_MARK:
12867 return;
12869 case FUNCTION_DECL:
12870 /* What we would really like to do here is to filter out all mere
12871 file-scope declarations of file-scope functions which are never
12872 referenced later within this translation unit (and keep all of ones
12873 that *are* referenced later on) but we aren't clairvoyant, so we have
12874 no idea which functions will be referenced in the future (i.e. later
12875 on within the current translation unit). So here we just ignore all
12876 file-scope function declarations which are not also definitions. If
12877 and when the debugger needs to know something about these functions,
12878 it will have to hunt around and find the DWARF information associated
12879 with the definition of the function.
12881 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
12882 nodes represent definitions and which ones represent mere
12883 declarations. We have to check DECL_INITIAL instead. That's because
12884 the C front-end supports some weird semantics for "extern inline"
12885 function definitions. These can get inlined within the current
12886 translation unit (an thus, we need to generate Dwarf info for their
12887 abstract instances so that the Dwarf info for the concrete inlined
12888 instances can have something to refer to) but the compiler never
12889 generates any out-of-lines instances of such things (despite the fact
12890 that they *are* definitions).
12892 The important point is that the C front-end marks these "extern
12893 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
12894 them anyway. Note that the C++ front-end also plays some similar games
12895 for inline function definitions appearing within include files which
12896 also contain `#pragma interface' pragmas. */
12897 if (DECL_INITIAL (decl) == NULL_TREE)
12898 return;
12900 /* If we're a nested function, initially use a parent of NULL; if we're
12901 a plain function, this will be fixed up in decls_for_scope. If
12902 we're a method, it will be ignored, since we already have a DIE. */
12903 if (decl_function_context (decl)
12904 /* But if we're in terse mode, we don't care about scope. */
12905 && debug_info_level > DINFO_LEVEL_TERSE)
12906 context_die = NULL;
12907 break;
12909 case VAR_DECL:
12910 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
12911 declaration and if the declaration was never even referenced from
12912 within this entire compilation unit. We suppress these DIEs in
12913 order to save space in the .debug section (by eliminating entries
12914 which are probably useless). Note that we must not suppress
12915 block-local extern declarations (whether used or not) because that
12916 would screw-up the debugger's name lookup mechanism and cause it to
12917 miss things which really ought to be in scope at a given point. */
12918 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
12919 return;
12921 /* If we are in terse mode, don't generate any DIEs to represent any
12922 variable declarations or definitions. */
12923 if (debug_info_level <= DINFO_LEVEL_TERSE)
12924 return;
12925 break;
12927 case NAMESPACE_DECL:
12928 if (debug_info_level <= DINFO_LEVEL_TERSE)
12929 return;
12930 if (lookup_decl_die (decl) != NULL)
12931 return;
12932 break;
12934 case TYPE_DECL:
12935 /* Don't emit stubs for types unless they are needed by other DIEs. */
12936 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
12937 return;
12939 /* Don't bother trying to generate any DIEs to represent any of the
12940 normal built-in types for the language we are compiling. */
12941 if (DECL_IS_BUILTIN (decl))
12943 /* OK, we need to generate one for `bool' so GDB knows what type
12944 comparisons have. */
12945 if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
12946 == DW_LANG_C_plus_plus)
12947 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
12948 && ! DECL_IGNORED_P (decl))
12949 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
12951 return;
12954 /* If we are in terse mode, don't generate any DIEs for types. */
12955 if (debug_info_level <= DINFO_LEVEL_TERSE)
12956 return;
12958 /* If we're a function-scope tag, initially use a parent of NULL;
12959 this will be fixed up in decls_for_scope. */
12960 if (decl_function_context (decl))
12961 context_die = NULL;
12963 break;
12965 default:
12966 return;
12969 gen_decl_die (decl, context_die);
12972 /* Output a marker (i.e. a label) for the beginning of the generated code for
12973 a lexical block. */
12975 static void
12976 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
12977 unsigned int blocknum)
12979 function_section (current_function_decl);
12980 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
12983 /* Output a marker (i.e. a label) for the end of the generated code for a
12984 lexical block. */
12986 static void
12987 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
12989 function_section (current_function_decl);
12990 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
12993 /* Returns nonzero if it is appropriate not to emit any debugging
12994 information for BLOCK, because it doesn't contain any instructions.
12996 Don't allow this for blocks with nested functions or local classes
12997 as we would end up with orphans, and in the presence of scheduling
12998 we may end up calling them anyway. */
13000 static bool
13001 dwarf2out_ignore_block (tree block)
13003 tree decl;
13005 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
13006 if (TREE_CODE (decl) == FUNCTION_DECL
13007 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
13008 return 0;
13010 return 1;
13013 /* Lookup FILE_NAME (in the list of filenames that we know about here in
13014 dwarf2out.c) and return its "index". The index of each (known) filename is
13015 just a unique number which is associated with only that one filename. We
13016 need such numbers for the sake of generating labels (in the .debug_sfnames
13017 section) and references to those files numbers (in the .debug_srcinfo
13018 and.debug_macinfo sections). If the filename given as an argument is not
13019 found in our current list, add it to the list and assign it the next
13020 available unique index number. In order to speed up searches, we remember
13021 the index of the filename was looked up last. This handles the majority of
13022 all searches. */
13024 static unsigned
13025 lookup_filename (const char *file_name)
13027 size_t i, n;
13028 char *save_file_name;
13030 /* Check to see if the file name that was searched on the previous
13031 call matches this file name. If so, return the index. */
13032 if (file_table_last_lookup_index != 0)
13034 const char *last
13035 = VARRAY_CHAR_PTR (file_table, file_table_last_lookup_index);
13036 if (strcmp (file_name, last) == 0)
13037 return file_table_last_lookup_index;
13040 /* Didn't match the previous lookup, search the table */
13041 n = VARRAY_ACTIVE_SIZE (file_table);
13042 for (i = 1; i < n; i++)
13043 if (strcmp (file_name, VARRAY_CHAR_PTR (file_table, i)) == 0)
13045 file_table_last_lookup_index = i;
13046 return i;
13049 /* Add the new entry to the end of the filename table. */
13050 file_table_last_lookup_index = n;
13051 save_file_name = (char *) ggc_strdup (file_name);
13052 VARRAY_PUSH_CHAR_PTR (file_table, save_file_name);
13053 VARRAY_PUSH_UINT (file_table_emitted, 0);
13055 return i;
13058 static int
13059 maybe_emit_file (int fileno)
13061 if (DWARF2_ASM_LINE_DEBUG_INFO && fileno > 0)
13063 if (!VARRAY_UINT (file_table_emitted, fileno))
13065 VARRAY_UINT (file_table_emitted, fileno) = ++emitcount;
13066 fprintf (asm_out_file, "\t.file %u ",
13067 VARRAY_UINT (file_table_emitted, fileno));
13068 output_quoted_string (asm_out_file,
13069 VARRAY_CHAR_PTR (file_table, fileno));
13070 fputc ('\n', asm_out_file);
13072 return VARRAY_UINT (file_table_emitted, fileno);
13074 else
13075 return fileno;
13078 static void
13079 init_file_table (void)
13081 /* Allocate the initial hunk of the file_table. */
13082 VARRAY_CHAR_PTR_INIT (file_table, 64, "file_table");
13083 VARRAY_UINT_INIT (file_table_emitted, 64, "file_table_emitted");
13085 /* Skip the first entry - file numbers begin at 1. */
13086 VARRAY_PUSH_CHAR_PTR (file_table, NULL);
13087 VARRAY_PUSH_UINT (file_table_emitted, 0);
13088 file_table_last_lookup_index = 0;
13091 /* Called by the final INSN scan whenever we see a var location. We
13092 use it to drop labels in the right places, and throw the location in
13093 our lookup table. */
13095 static void
13096 dwarf2out_var_location (rtx loc_note)
13098 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13099 struct var_loc_node *newloc;
13100 rtx prev_insn;
13101 static rtx last_insn;
13102 static const char *last_label;
13104 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13105 return;
13106 prev_insn = PREV_INSN (loc_note);
13108 newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13109 /* If the insn we processed last time is the previous insn
13110 and it is also a var location note, use the label we emitted
13111 last time. */
13112 if (last_insn != NULL_RTX
13113 && last_insn == prev_insn
13114 && NOTE_P (prev_insn)
13115 && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13117 newloc->label = last_label;
13119 else
13121 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13122 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13123 loclabel_num++;
13124 newloc->label = ggc_strdup (loclabel);
13126 newloc->var_loc_note = loc_note;
13127 newloc->next = NULL;
13129 last_insn = loc_note;
13130 last_label = newloc->label;
13132 add_var_loc_to_decl (NOTE_VAR_LOCATION_DECL (loc_note), newloc);
13135 /* We need to reset the locations at the beginning of each
13136 function. We can't do this in the end_function hook, because the
13137 declarations that use the locations won't have been outputted when
13138 that hook is called. */
13140 static void
13141 dwarf2out_begin_function (tree unused ATTRIBUTE_UNUSED)
13143 htab_empty (decl_loc_table);
13146 /* Output a label to mark the beginning of a source code line entry
13147 and record information relating to this source line, in
13148 'line_info_table' for later output of the .debug_line section. */
13150 static void
13151 dwarf2out_source_line (unsigned int line, const char *filename)
13153 if (debug_info_level >= DINFO_LEVEL_NORMAL
13154 && line != 0)
13156 function_section (current_function_decl);
13158 /* If requested, emit something human-readable. */
13159 if (flag_debug_asm)
13160 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13161 filename, line);
13163 if (DWARF2_ASM_LINE_DEBUG_INFO)
13165 unsigned file_num = lookup_filename (filename);
13167 file_num = maybe_emit_file (file_num);
13169 /* Emit the .loc directive understood by GNU as. */
13170 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
13172 /* Indicate that line number info exists. */
13173 line_info_table_in_use++;
13175 /* Indicate that multiple line number tables exist. */
13176 if (DECL_SECTION_NAME (current_function_decl))
13177 separate_line_info_table_in_use++;
13179 else if (DECL_SECTION_NAME (current_function_decl))
13181 dw_separate_line_info_ref line_info;
13182 targetm.asm_out.internal_label (asm_out_file, SEPARATE_LINE_CODE_LABEL,
13183 separate_line_info_table_in_use);
13185 /* Expand the line info table if necessary. */
13186 if (separate_line_info_table_in_use
13187 == separate_line_info_table_allocated)
13189 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13190 separate_line_info_table
13191 = ggc_realloc (separate_line_info_table,
13192 separate_line_info_table_allocated
13193 * sizeof (dw_separate_line_info_entry));
13194 memset (separate_line_info_table
13195 + separate_line_info_table_in_use,
13197 (LINE_INFO_TABLE_INCREMENT
13198 * sizeof (dw_separate_line_info_entry)));
13201 /* Add the new entry at the end of the line_info_table. */
13202 line_info
13203 = &separate_line_info_table[separate_line_info_table_in_use++];
13204 line_info->dw_file_num = lookup_filename (filename);
13205 line_info->dw_line_num = line;
13206 line_info->function = current_function_funcdef_no;
13208 else
13210 dw_line_info_ref line_info;
13212 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
13213 line_info_table_in_use);
13215 /* Expand the line info table if necessary. */
13216 if (line_info_table_in_use == line_info_table_allocated)
13218 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13219 line_info_table
13220 = ggc_realloc (line_info_table,
13221 (line_info_table_allocated
13222 * sizeof (dw_line_info_entry)));
13223 memset (line_info_table + line_info_table_in_use, 0,
13224 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
13227 /* Add the new entry at the end of the line_info_table. */
13228 line_info = &line_info_table[line_info_table_in_use++];
13229 line_info->dw_file_num = lookup_filename (filename);
13230 line_info->dw_line_num = line;
13235 /* Record the beginning of a new source file. */
13237 static void
13238 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
13240 if (flag_eliminate_dwarf2_dups)
13242 /* Record the beginning of the file for break_out_includes. */
13243 dw_die_ref bincl_die;
13245 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
13246 add_AT_string (bincl_die, DW_AT_name, filename);
13249 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13251 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13252 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
13253 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13254 lineno);
13255 maybe_emit_file (lookup_filename (filename));
13256 dw2_asm_output_data_uleb128 (lookup_filename (filename),
13257 "Filename we just started");
13261 /* Record the end of a source file. */
13263 static void
13264 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
13266 if (flag_eliminate_dwarf2_dups)
13267 /* Record the end of the file for break_out_includes. */
13268 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
13270 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13272 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13273 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13277 /* Called from debug_define in toplev.c. The `buffer' parameter contains
13278 the tail part of the directive line, i.e. the part which is past the
13279 initial whitespace, #, whitespace, directive-name, whitespace part. */
13281 static void
13282 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13283 const char *buffer ATTRIBUTE_UNUSED)
13285 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13287 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13288 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
13289 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13290 dw2_asm_output_nstring (buffer, -1, "The macro");
13294 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
13295 the tail part of the directive line, i.e. the part which is past the
13296 initial whitespace, #, whitespace, directive-name, whitespace part. */
13298 static void
13299 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13300 const char *buffer ATTRIBUTE_UNUSED)
13302 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13304 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13305 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
13306 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13307 dw2_asm_output_nstring (buffer, -1, "The macro");
13311 /* Set up for Dwarf output at the start of compilation. */
13313 static void
13314 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
13316 init_file_table ();
13318 /* Allocate the decl_die_table. */
13319 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13320 decl_die_table_eq, NULL);
13322 /* Allocate the decl_loc_table. */
13323 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13324 decl_loc_table_eq, NULL);
13326 /* Allocate the initial hunk of the decl_scope_table. */
13327 VARRAY_TREE_INIT (decl_scope_table, 256, "decl_scope_table");
13329 /* Allocate the initial hunk of the abbrev_die_table. */
13330 abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
13331 * sizeof (dw_die_ref));
13332 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
13333 /* Zero-th entry is allocated, but unused */
13334 abbrev_die_table_in_use = 1;
13336 /* Allocate the initial hunk of the line_info_table. */
13337 line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
13338 * sizeof (dw_line_info_entry));
13339 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
13341 /* Zero-th entry is allocated, but unused */
13342 line_info_table_in_use = 1;
13344 /* Generate the initial DIE for the .debug section. Note that the (string)
13345 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
13346 will (typically) be a relative pathname and that this pathname should be
13347 taken as being relative to the directory from which the compiler was
13348 invoked when the given (base) source file was compiled. We will fill
13349 in this value in dwarf2out_finish. */
13350 comp_unit_die = gen_compile_unit_die (NULL);
13352 VARRAY_TREE_INIT (incomplete_types, 64, "incomplete_types");
13354 VARRAY_RTX_INIT (used_rtx_varray, 32, "used_rtx_varray");
13356 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
13357 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
13358 DEBUG_ABBREV_SECTION_LABEL, 0);
13359 if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
13360 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
13361 else
13362 strcpy (text_section_label, stripattributes (TEXT_SECTION_NAME));
13364 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
13365 DEBUG_INFO_SECTION_LABEL, 0);
13366 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
13367 DEBUG_LINE_SECTION_LABEL, 0);
13368 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
13369 DEBUG_RANGES_SECTION_LABEL, 0);
13370 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13371 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
13372 named_section_flags (DEBUG_INFO_SECTION, SECTION_DEBUG);
13373 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
13374 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13375 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
13377 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13379 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13380 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
13381 DEBUG_MACINFO_SECTION_LABEL, 0);
13382 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
13385 if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
13387 text_section ();
13388 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
13392 /* A helper function for dwarf2out_finish called through
13393 ht_forall. Emit one queued .debug_str string. */
13395 static int
13396 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
13398 struct indirect_string_node *node = (struct indirect_string_node *) *h;
13400 if (node->form == DW_FORM_strp)
13402 named_section_flags (DEBUG_STR_SECTION, DEBUG_STR_SECTION_FLAGS);
13403 ASM_OUTPUT_LABEL (asm_out_file, node->label);
13404 assemble_string (node->str, strlen (node->str) + 1);
13407 return 1;
13412 /* Clear the marks for a die and its children.
13413 Be cool if the mark isn't set. */
13415 static void
13416 prune_unmark_dies (dw_die_ref die)
13418 dw_die_ref c;
13419 die->die_mark = 0;
13420 for (c = die->die_child; c; c = c->die_sib)
13421 prune_unmark_dies (c);
13425 /* Given DIE that we're marking as used, find any other dies
13426 it references as attributes and mark them as used. */
13428 static void
13429 prune_unused_types_walk_attribs (dw_die_ref die)
13431 dw_attr_ref a;
13433 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
13435 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
13437 /* A reference to another DIE.
13438 Make sure that it will get emitted. */
13439 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
13441 else if (a->dw_attr == DW_AT_decl_file)
13443 /* A reference to a file. Make sure the file name is emitted. */
13444 a->dw_attr_val.v.val_unsigned =
13445 maybe_emit_file (a->dw_attr_val.v.val_unsigned);
13451 /* Mark DIE as being used. If DOKIDS is true, then walk down
13452 to DIE's children. */
13454 static void
13455 prune_unused_types_mark (dw_die_ref die, int dokids)
13457 dw_die_ref c;
13459 if (die->die_mark == 0)
13461 /* We haven't done this node yet. Mark it as used. */
13462 die->die_mark = 1;
13464 /* We also have to mark its parents as used.
13465 (But we don't want to mark our parents' kids due to this.) */
13466 if (die->die_parent)
13467 prune_unused_types_mark (die->die_parent, 0);
13469 /* Mark any referenced nodes. */
13470 prune_unused_types_walk_attribs (die);
13472 /* If this node is a specification,
13473 also mark the definition, if it exists. */
13474 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
13475 prune_unused_types_mark (die->die_definition, 1);
13478 if (dokids && die->die_mark != 2)
13480 /* We need to walk the children, but haven't done so yet.
13481 Remember that we've walked the kids. */
13482 die->die_mark = 2;
13484 /* Walk them. */
13485 for (c = die->die_child; c; c = c->die_sib)
13487 /* If this is an array type, we need to make sure our
13488 kids get marked, even if they're types. */
13489 if (die->die_tag == DW_TAG_array_type)
13490 prune_unused_types_mark (c, 1);
13491 else
13492 prune_unused_types_walk (c);
13498 /* Walk the tree DIE and mark types that we actually use. */
13500 static void
13501 prune_unused_types_walk (dw_die_ref die)
13503 dw_die_ref c;
13505 /* Don't do anything if this node is already marked. */
13506 if (die->die_mark)
13507 return;
13509 switch (die->die_tag) {
13510 case DW_TAG_const_type:
13511 case DW_TAG_packed_type:
13512 case DW_TAG_pointer_type:
13513 case DW_TAG_reference_type:
13514 case DW_TAG_volatile_type:
13515 case DW_TAG_typedef:
13516 case DW_TAG_array_type:
13517 case DW_TAG_structure_type:
13518 case DW_TAG_union_type:
13519 case DW_TAG_class_type:
13520 case DW_TAG_friend:
13521 case DW_TAG_variant_part:
13522 case DW_TAG_enumeration_type:
13523 case DW_TAG_subroutine_type:
13524 case DW_TAG_string_type:
13525 case DW_TAG_set_type:
13526 case DW_TAG_subrange_type:
13527 case DW_TAG_ptr_to_member_type:
13528 case DW_TAG_file_type:
13529 /* It's a type node --- don't mark it. */
13530 return;
13532 default:
13533 /* Mark everything else. */
13534 break;
13537 die->die_mark = 1;
13539 /* Now, mark any dies referenced from here. */
13540 prune_unused_types_walk_attribs (die);
13542 /* Mark children. */
13543 for (c = die->die_child; c; c = c->die_sib)
13544 prune_unused_types_walk (c);
13548 /* Remove from the tree DIE any dies that aren't marked. */
13550 static void
13551 prune_unused_types_prune (dw_die_ref die)
13553 dw_die_ref c, p, n;
13554 if (!die->die_mark)
13555 abort();
13557 p = NULL;
13558 for (c = die->die_child; c; c = n)
13560 n = c->die_sib;
13561 if (c->die_mark)
13563 prune_unused_types_prune (c);
13564 p = c;
13566 else
13568 if (p)
13569 p->die_sib = n;
13570 else
13571 die->die_child = n;
13572 free_die (c);
13578 /* Remove dies representing declarations that we never use. */
13580 static void
13581 prune_unused_types (void)
13583 unsigned int i;
13584 limbo_die_node *node;
13586 /* Clear all the marks. */
13587 prune_unmark_dies (comp_unit_die);
13588 for (node = limbo_die_list; node; node = node->next)
13589 prune_unmark_dies (node->die);
13591 /* Set the mark on nodes that are actually used. */
13592 prune_unused_types_walk (comp_unit_die);
13593 for (node = limbo_die_list; node; node = node->next)
13594 prune_unused_types_walk (node->die);
13596 /* Also set the mark on nodes referenced from the
13597 pubname_table or arange_table. */
13598 for (i = 0; i < pubname_table_in_use; i++)
13599 prune_unused_types_mark (pubname_table[i].die, 1);
13600 for (i = 0; i < arange_table_in_use; i++)
13601 prune_unused_types_mark (arange_table[i], 1);
13603 /* Get rid of nodes that aren't marked. */
13604 prune_unused_types_prune (comp_unit_die);
13605 for (node = limbo_die_list; node; node = node->next)
13606 prune_unused_types_prune (node->die);
13608 /* Leave the marks clear. */
13609 prune_unmark_dies (comp_unit_die);
13610 for (node = limbo_die_list; node; node = node->next)
13611 prune_unmark_dies (node->die);
13614 /* Output stuff that dwarf requires at the end of every file,
13615 and generate the DWARF-2 debugging info. */
13617 static void
13618 dwarf2out_finish (const char *filename)
13620 limbo_die_node *node, *next_node;
13621 dw_die_ref die = 0;
13623 /* Add the name for the main input file now. We delayed this from
13624 dwarf2out_init to avoid complications with PCH. */
13625 add_name_attribute (comp_unit_die, filename);
13626 if (filename[0] != DIR_SEPARATOR)
13627 add_comp_dir_attribute (comp_unit_die);
13628 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
13630 size_t i;
13631 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
13632 if (VARRAY_CHAR_PTR (file_table, i)[0] != DIR_SEPARATOR
13633 /* Don't add cwd for <built-in>. */
13634 && VARRAY_CHAR_PTR (file_table, i)[0] != '<')
13636 add_comp_dir_attribute (comp_unit_die);
13637 break;
13641 /* Traverse the limbo die list, and add parent/child links. The only
13642 dies without parents that should be here are concrete instances of
13643 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
13644 For concrete instances, we can get the parent die from the abstract
13645 instance. */
13646 for (node = limbo_die_list; node; node = next_node)
13648 next_node = node->next;
13649 die = node->die;
13651 if (die->die_parent == NULL)
13653 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
13654 tree context;
13656 if (origin)
13657 add_child_die (origin->die_parent, die);
13658 else if (die == comp_unit_die)
13660 else if (errorcount > 0 || sorrycount > 0)
13661 /* It's OK to be confused by errors in the input. */
13662 add_child_die (comp_unit_die, die);
13663 else if (node->created_for
13664 && ((DECL_P (node->created_for)
13665 && (context = DECL_CONTEXT (node->created_for)))
13666 || (TYPE_P (node->created_for)
13667 && (context = TYPE_CONTEXT (node->created_for))))
13668 && TREE_CODE (context) == FUNCTION_DECL)
13670 /* In certain situations, the lexical block containing a
13671 nested function can be optimized away, which results
13672 in the nested function die being orphaned. Likewise
13673 with the return type of that nested function. Force
13674 this to be a child of the containing function. */
13675 origin = lookup_decl_die (context);
13676 if (! origin)
13677 abort ();
13678 add_child_die (origin, die);
13680 else
13681 abort ();
13685 limbo_die_list = NULL;
13687 /* Walk through the list of incomplete types again, trying once more to
13688 emit full debugging info for them. */
13689 retry_incomplete_types ();
13691 /* We need to reverse all the dies before break_out_includes, or
13692 we'll see the end of an include file before the beginning. */
13693 reverse_all_dies (comp_unit_die);
13695 if (flag_eliminate_unused_debug_types)
13696 prune_unused_types ();
13698 /* Generate separate CUs for each of the include files we've seen.
13699 They will go into limbo_die_list. */
13700 if (flag_eliminate_dwarf2_dups)
13701 break_out_includes (comp_unit_die);
13703 /* Traverse the DIE's and add add sibling attributes to those DIE's
13704 that have children. */
13705 add_sibling_attributes (comp_unit_die);
13706 for (node = limbo_die_list; node; node = node->next)
13707 add_sibling_attributes (node->die);
13709 /* Output a terminator label for the .text section. */
13710 text_section ();
13711 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
13713 /* Output the source line correspondence table. We must do this
13714 even if there is no line information. Otherwise, on an empty
13715 translation unit, we will generate a present, but empty,
13716 .debug_info section. IRIX 6.5 `nm' will then complain when
13717 examining the file. */
13718 if (! DWARF2_ASM_LINE_DEBUG_INFO)
13720 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13721 output_line_info ();
13724 /* Output location list section if necessary. */
13725 if (have_location_lists)
13727 /* Output the location lists info. */
13728 named_section_flags (DEBUG_LOC_SECTION, SECTION_DEBUG);
13729 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
13730 DEBUG_LOC_SECTION_LABEL, 0);
13731 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
13732 output_location_lists (die);
13733 have_location_lists = 0;
13736 /* We can only use the low/high_pc attributes if all of the code was
13737 in .text. */
13738 if (separate_line_info_table_in_use == 0)
13740 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
13741 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
13744 /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
13745 "base address". Use zero so that these addresses become absolute. */
13746 else if (have_location_lists || ranges_table_in_use)
13747 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
13749 if (debug_info_level >= DINFO_LEVEL_NORMAL)
13750 add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
13751 debug_line_section_label);
13753 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13754 add_AT_lbl_offset (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
13756 /* Output all of the compilation units. We put the main one last so that
13757 the offsets are available to output_pubnames. */
13758 for (node = limbo_die_list; node; node = node->next)
13759 output_comp_unit (node->die, 0);
13761 output_comp_unit (comp_unit_die, 0);
13763 /* Output the abbreviation table. */
13764 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13765 output_abbrev_section ();
13767 /* Output public names table if necessary. */
13768 if (pubname_table_in_use)
13770 named_section_flags (DEBUG_PUBNAMES_SECTION, SECTION_DEBUG);
13771 output_pubnames ();
13774 /* Output the address range information. We only put functions in the arange
13775 table, so don't write it out if we don't have any. */
13776 if (fde_table_in_use)
13778 named_section_flags (DEBUG_ARANGES_SECTION, SECTION_DEBUG);
13779 output_aranges ();
13782 /* Output ranges section if necessary. */
13783 if (ranges_table_in_use)
13785 named_section_flags (DEBUG_RANGES_SECTION, SECTION_DEBUG);
13786 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
13787 output_ranges ();
13790 /* Have to end the primary source file. */
13791 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13793 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13794 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13795 dw2_asm_output_data (1, 0, "End compilation unit");
13798 /* If we emitted any DW_FORM_strp form attribute, output the string
13799 table too. */
13800 if (debug_str_hash)
13801 htab_traverse (debug_str_hash, output_indirect_string, NULL);
13803 #else
13805 /* This should never be used, but its address is needed for comparisons. */
13806 const struct gcc_debug_hooks dwarf2_debug_hooks;
13808 #endif /* DWARF2_DEBUGGING_INFO */
13810 #include "gt-dwarf2out.h"