* arm.h (REVERSE_CONDITION): Define.
[official-gcc.git] / gcc / dwarf2out.c
blob02cbef3df9387216d256fc7694db569a22f59c48
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 #ifndef TARGET_UNWIND_INFO
2462 /* Output another copy for the unwinder. */
2463 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2464 output_call_frame_info (1);
2465 #endif
2467 #endif
2469 /* And now, the subset of the debugging information support code necessary
2470 for emitting location expressions. */
2472 /* We need some way to distinguish DW_OP_addr with a direct symbol
2473 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
2474 #define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
2477 typedef struct dw_val_struct *dw_val_ref;
2478 typedef struct die_struct *dw_die_ref;
2479 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2480 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2482 /* Each DIE may have a series of attribute/value pairs. Values
2483 can take on several forms. The forms that are used in this
2484 implementation are listed below. */
2486 enum dw_val_class
2488 dw_val_class_addr,
2489 dw_val_class_offset,
2490 dw_val_class_loc,
2491 dw_val_class_loc_list,
2492 dw_val_class_range_list,
2493 dw_val_class_const,
2494 dw_val_class_unsigned_const,
2495 dw_val_class_long_long,
2496 dw_val_class_vec,
2497 dw_val_class_flag,
2498 dw_val_class_die_ref,
2499 dw_val_class_fde_ref,
2500 dw_val_class_lbl_id,
2501 dw_val_class_lbl_offset,
2502 dw_val_class_str
2505 /* Describe a double word constant value. */
2506 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
2508 typedef struct dw_long_long_struct GTY(())
2510 unsigned long hi;
2511 unsigned long low;
2513 dw_long_long_const;
2515 /* Describe a floating point constant value, or a vector constant value. */
2517 typedef struct dw_vec_struct GTY(())
2519 unsigned char * GTY((length ("%h.length"))) array;
2520 unsigned length;
2521 unsigned elt_size;
2523 dw_vec_const;
2525 /* The dw_val_node describes an attribute's value, as it is
2526 represented internally. */
2528 typedef struct dw_val_struct GTY(())
2530 enum dw_val_class val_class;
2531 union dw_val_struct_union
2533 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2534 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2535 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2536 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2537 HOST_WIDE_INT GTY ((default)) val_int;
2538 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2539 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2540 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2541 struct dw_val_die_union
2543 dw_die_ref die;
2544 int external;
2545 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2546 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2547 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2548 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2549 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2551 GTY ((desc ("%1.val_class"))) v;
2553 dw_val_node;
2555 /* Locations in memory are described using a sequence of stack machine
2556 operations. */
2558 typedef struct dw_loc_descr_struct GTY(())
2560 dw_loc_descr_ref dw_loc_next;
2561 enum dwarf_location_atom dw_loc_opc;
2562 dw_val_node dw_loc_oprnd1;
2563 dw_val_node dw_loc_oprnd2;
2564 int dw_loc_addr;
2566 dw_loc_descr_node;
2568 /* Location lists are ranges + location descriptions for that range,
2569 so you can track variables that are in different places over
2570 their entire life. */
2571 typedef struct dw_loc_list_struct GTY(())
2573 dw_loc_list_ref dw_loc_next;
2574 const char *begin; /* Label for begin address of range */
2575 const char *end; /* Label for end address of range */
2576 char *ll_symbol; /* Label for beginning of location list.
2577 Only on head of list */
2578 const char *section; /* Section this loclist is relative to */
2579 dw_loc_descr_ref expr;
2580 } dw_loc_list_node;
2582 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2584 static const char *dwarf_stack_op_name (unsigned);
2585 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2586 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2587 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2588 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2589 static unsigned long size_of_locs (dw_loc_descr_ref);
2590 static void output_loc_operands (dw_loc_descr_ref);
2591 static void output_loc_sequence (dw_loc_descr_ref);
2593 /* Convert a DWARF stack opcode into its string name. */
2595 static const char *
2596 dwarf_stack_op_name (unsigned int op)
2598 switch (op)
2600 case DW_OP_addr:
2601 case INTERNAL_DW_OP_tls_addr:
2602 return "DW_OP_addr";
2603 case DW_OP_deref:
2604 return "DW_OP_deref";
2605 case DW_OP_const1u:
2606 return "DW_OP_const1u";
2607 case DW_OP_const1s:
2608 return "DW_OP_const1s";
2609 case DW_OP_const2u:
2610 return "DW_OP_const2u";
2611 case DW_OP_const2s:
2612 return "DW_OP_const2s";
2613 case DW_OP_const4u:
2614 return "DW_OP_const4u";
2615 case DW_OP_const4s:
2616 return "DW_OP_const4s";
2617 case DW_OP_const8u:
2618 return "DW_OP_const8u";
2619 case DW_OP_const8s:
2620 return "DW_OP_const8s";
2621 case DW_OP_constu:
2622 return "DW_OP_constu";
2623 case DW_OP_consts:
2624 return "DW_OP_consts";
2625 case DW_OP_dup:
2626 return "DW_OP_dup";
2627 case DW_OP_drop:
2628 return "DW_OP_drop";
2629 case DW_OP_over:
2630 return "DW_OP_over";
2631 case DW_OP_pick:
2632 return "DW_OP_pick";
2633 case DW_OP_swap:
2634 return "DW_OP_swap";
2635 case DW_OP_rot:
2636 return "DW_OP_rot";
2637 case DW_OP_xderef:
2638 return "DW_OP_xderef";
2639 case DW_OP_abs:
2640 return "DW_OP_abs";
2641 case DW_OP_and:
2642 return "DW_OP_and";
2643 case DW_OP_div:
2644 return "DW_OP_div";
2645 case DW_OP_minus:
2646 return "DW_OP_minus";
2647 case DW_OP_mod:
2648 return "DW_OP_mod";
2649 case DW_OP_mul:
2650 return "DW_OP_mul";
2651 case DW_OP_neg:
2652 return "DW_OP_neg";
2653 case DW_OP_not:
2654 return "DW_OP_not";
2655 case DW_OP_or:
2656 return "DW_OP_or";
2657 case DW_OP_plus:
2658 return "DW_OP_plus";
2659 case DW_OP_plus_uconst:
2660 return "DW_OP_plus_uconst";
2661 case DW_OP_shl:
2662 return "DW_OP_shl";
2663 case DW_OP_shr:
2664 return "DW_OP_shr";
2665 case DW_OP_shra:
2666 return "DW_OP_shra";
2667 case DW_OP_xor:
2668 return "DW_OP_xor";
2669 case DW_OP_bra:
2670 return "DW_OP_bra";
2671 case DW_OP_eq:
2672 return "DW_OP_eq";
2673 case DW_OP_ge:
2674 return "DW_OP_ge";
2675 case DW_OP_gt:
2676 return "DW_OP_gt";
2677 case DW_OP_le:
2678 return "DW_OP_le";
2679 case DW_OP_lt:
2680 return "DW_OP_lt";
2681 case DW_OP_ne:
2682 return "DW_OP_ne";
2683 case DW_OP_skip:
2684 return "DW_OP_skip";
2685 case DW_OP_lit0:
2686 return "DW_OP_lit0";
2687 case DW_OP_lit1:
2688 return "DW_OP_lit1";
2689 case DW_OP_lit2:
2690 return "DW_OP_lit2";
2691 case DW_OP_lit3:
2692 return "DW_OP_lit3";
2693 case DW_OP_lit4:
2694 return "DW_OP_lit4";
2695 case DW_OP_lit5:
2696 return "DW_OP_lit5";
2697 case DW_OP_lit6:
2698 return "DW_OP_lit6";
2699 case DW_OP_lit7:
2700 return "DW_OP_lit7";
2701 case DW_OP_lit8:
2702 return "DW_OP_lit8";
2703 case DW_OP_lit9:
2704 return "DW_OP_lit9";
2705 case DW_OP_lit10:
2706 return "DW_OP_lit10";
2707 case DW_OP_lit11:
2708 return "DW_OP_lit11";
2709 case DW_OP_lit12:
2710 return "DW_OP_lit12";
2711 case DW_OP_lit13:
2712 return "DW_OP_lit13";
2713 case DW_OP_lit14:
2714 return "DW_OP_lit14";
2715 case DW_OP_lit15:
2716 return "DW_OP_lit15";
2717 case DW_OP_lit16:
2718 return "DW_OP_lit16";
2719 case DW_OP_lit17:
2720 return "DW_OP_lit17";
2721 case DW_OP_lit18:
2722 return "DW_OP_lit18";
2723 case DW_OP_lit19:
2724 return "DW_OP_lit19";
2725 case DW_OP_lit20:
2726 return "DW_OP_lit20";
2727 case DW_OP_lit21:
2728 return "DW_OP_lit21";
2729 case DW_OP_lit22:
2730 return "DW_OP_lit22";
2731 case DW_OP_lit23:
2732 return "DW_OP_lit23";
2733 case DW_OP_lit24:
2734 return "DW_OP_lit24";
2735 case DW_OP_lit25:
2736 return "DW_OP_lit25";
2737 case DW_OP_lit26:
2738 return "DW_OP_lit26";
2739 case DW_OP_lit27:
2740 return "DW_OP_lit27";
2741 case DW_OP_lit28:
2742 return "DW_OP_lit28";
2743 case DW_OP_lit29:
2744 return "DW_OP_lit29";
2745 case DW_OP_lit30:
2746 return "DW_OP_lit30";
2747 case DW_OP_lit31:
2748 return "DW_OP_lit31";
2749 case DW_OP_reg0:
2750 return "DW_OP_reg0";
2751 case DW_OP_reg1:
2752 return "DW_OP_reg1";
2753 case DW_OP_reg2:
2754 return "DW_OP_reg2";
2755 case DW_OP_reg3:
2756 return "DW_OP_reg3";
2757 case DW_OP_reg4:
2758 return "DW_OP_reg4";
2759 case DW_OP_reg5:
2760 return "DW_OP_reg5";
2761 case DW_OP_reg6:
2762 return "DW_OP_reg6";
2763 case DW_OP_reg7:
2764 return "DW_OP_reg7";
2765 case DW_OP_reg8:
2766 return "DW_OP_reg8";
2767 case DW_OP_reg9:
2768 return "DW_OP_reg9";
2769 case DW_OP_reg10:
2770 return "DW_OP_reg10";
2771 case DW_OP_reg11:
2772 return "DW_OP_reg11";
2773 case DW_OP_reg12:
2774 return "DW_OP_reg12";
2775 case DW_OP_reg13:
2776 return "DW_OP_reg13";
2777 case DW_OP_reg14:
2778 return "DW_OP_reg14";
2779 case DW_OP_reg15:
2780 return "DW_OP_reg15";
2781 case DW_OP_reg16:
2782 return "DW_OP_reg16";
2783 case DW_OP_reg17:
2784 return "DW_OP_reg17";
2785 case DW_OP_reg18:
2786 return "DW_OP_reg18";
2787 case DW_OP_reg19:
2788 return "DW_OP_reg19";
2789 case DW_OP_reg20:
2790 return "DW_OP_reg20";
2791 case DW_OP_reg21:
2792 return "DW_OP_reg21";
2793 case DW_OP_reg22:
2794 return "DW_OP_reg22";
2795 case DW_OP_reg23:
2796 return "DW_OP_reg23";
2797 case DW_OP_reg24:
2798 return "DW_OP_reg24";
2799 case DW_OP_reg25:
2800 return "DW_OP_reg25";
2801 case DW_OP_reg26:
2802 return "DW_OP_reg26";
2803 case DW_OP_reg27:
2804 return "DW_OP_reg27";
2805 case DW_OP_reg28:
2806 return "DW_OP_reg28";
2807 case DW_OP_reg29:
2808 return "DW_OP_reg29";
2809 case DW_OP_reg30:
2810 return "DW_OP_reg30";
2811 case DW_OP_reg31:
2812 return "DW_OP_reg31";
2813 case DW_OP_breg0:
2814 return "DW_OP_breg0";
2815 case DW_OP_breg1:
2816 return "DW_OP_breg1";
2817 case DW_OP_breg2:
2818 return "DW_OP_breg2";
2819 case DW_OP_breg3:
2820 return "DW_OP_breg3";
2821 case DW_OP_breg4:
2822 return "DW_OP_breg4";
2823 case DW_OP_breg5:
2824 return "DW_OP_breg5";
2825 case DW_OP_breg6:
2826 return "DW_OP_breg6";
2827 case DW_OP_breg7:
2828 return "DW_OP_breg7";
2829 case DW_OP_breg8:
2830 return "DW_OP_breg8";
2831 case DW_OP_breg9:
2832 return "DW_OP_breg9";
2833 case DW_OP_breg10:
2834 return "DW_OP_breg10";
2835 case DW_OP_breg11:
2836 return "DW_OP_breg11";
2837 case DW_OP_breg12:
2838 return "DW_OP_breg12";
2839 case DW_OP_breg13:
2840 return "DW_OP_breg13";
2841 case DW_OP_breg14:
2842 return "DW_OP_breg14";
2843 case DW_OP_breg15:
2844 return "DW_OP_breg15";
2845 case DW_OP_breg16:
2846 return "DW_OP_breg16";
2847 case DW_OP_breg17:
2848 return "DW_OP_breg17";
2849 case DW_OP_breg18:
2850 return "DW_OP_breg18";
2851 case DW_OP_breg19:
2852 return "DW_OP_breg19";
2853 case DW_OP_breg20:
2854 return "DW_OP_breg20";
2855 case DW_OP_breg21:
2856 return "DW_OP_breg21";
2857 case DW_OP_breg22:
2858 return "DW_OP_breg22";
2859 case DW_OP_breg23:
2860 return "DW_OP_breg23";
2861 case DW_OP_breg24:
2862 return "DW_OP_breg24";
2863 case DW_OP_breg25:
2864 return "DW_OP_breg25";
2865 case DW_OP_breg26:
2866 return "DW_OP_breg26";
2867 case DW_OP_breg27:
2868 return "DW_OP_breg27";
2869 case DW_OP_breg28:
2870 return "DW_OP_breg28";
2871 case DW_OP_breg29:
2872 return "DW_OP_breg29";
2873 case DW_OP_breg30:
2874 return "DW_OP_breg30";
2875 case DW_OP_breg31:
2876 return "DW_OP_breg31";
2877 case DW_OP_regx:
2878 return "DW_OP_regx";
2879 case DW_OP_fbreg:
2880 return "DW_OP_fbreg";
2881 case DW_OP_bregx:
2882 return "DW_OP_bregx";
2883 case DW_OP_piece:
2884 return "DW_OP_piece";
2885 case DW_OP_deref_size:
2886 return "DW_OP_deref_size";
2887 case DW_OP_xderef_size:
2888 return "DW_OP_xderef_size";
2889 case DW_OP_nop:
2890 return "DW_OP_nop";
2891 case DW_OP_push_object_address:
2892 return "DW_OP_push_object_address";
2893 case DW_OP_call2:
2894 return "DW_OP_call2";
2895 case DW_OP_call4:
2896 return "DW_OP_call4";
2897 case DW_OP_call_ref:
2898 return "DW_OP_call_ref";
2899 case DW_OP_GNU_push_tls_address:
2900 return "DW_OP_GNU_push_tls_address";
2901 default:
2902 return "OP_<unknown>";
2906 /* Return a pointer to a newly allocated location description. Location
2907 descriptions are simple expression terms that can be strung
2908 together to form more complicated location (address) descriptions. */
2910 static inline dw_loc_descr_ref
2911 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
2912 unsigned HOST_WIDE_INT oprnd2)
2914 dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
2916 descr->dw_loc_opc = op;
2917 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
2918 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
2919 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
2920 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
2922 return descr;
2926 /* Add a location description term to a location description expression. */
2928 static inline void
2929 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
2931 dw_loc_descr_ref *d;
2933 /* Find the end of the chain. */
2934 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
2937 *d = descr;
2940 /* Return the size of a location descriptor. */
2942 static unsigned long
2943 size_of_loc_descr (dw_loc_descr_ref loc)
2945 unsigned long size = 1;
2947 switch (loc->dw_loc_opc)
2949 case DW_OP_addr:
2950 case INTERNAL_DW_OP_tls_addr:
2951 size += DWARF2_ADDR_SIZE;
2952 break;
2953 case DW_OP_const1u:
2954 case DW_OP_const1s:
2955 size += 1;
2956 break;
2957 case DW_OP_const2u:
2958 case DW_OP_const2s:
2959 size += 2;
2960 break;
2961 case DW_OP_const4u:
2962 case DW_OP_const4s:
2963 size += 4;
2964 break;
2965 case DW_OP_const8u:
2966 case DW_OP_const8s:
2967 size += 8;
2968 break;
2969 case DW_OP_constu:
2970 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2971 break;
2972 case DW_OP_consts:
2973 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2974 break;
2975 case DW_OP_pick:
2976 size += 1;
2977 break;
2978 case DW_OP_plus_uconst:
2979 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2980 break;
2981 case DW_OP_skip:
2982 case DW_OP_bra:
2983 size += 2;
2984 break;
2985 case DW_OP_breg0:
2986 case DW_OP_breg1:
2987 case DW_OP_breg2:
2988 case DW_OP_breg3:
2989 case DW_OP_breg4:
2990 case DW_OP_breg5:
2991 case DW_OP_breg6:
2992 case DW_OP_breg7:
2993 case DW_OP_breg8:
2994 case DW_OP_breg9:
2995 case DW_OP_breg10:
2996 case DW_OP_breg11:
2997 case DW_OP_breg12:
2998 case DW_OP_breg13:
2999 case DW_OP_breg14:
3000 case DW_OP_breg15:
3001 case DW_OP_breg16:
3002 case DW_OP_breg17:
3003 case DW_OP_breg18:
3004 case DW_OP_breg19:
3005 case DW_OP_breg20:
3006 case DW_OP_breg21:
3007 case DW_OP_breg22:
3008 case DW_OP_breg23:
3009 case DW_OP_breg24:
3010 case DW_OP_breg25:
3011 case DW_OP_breg26:
3012 case DW_OP_breg27:
3013 case DW_OP_breg28:
3014 case DW_OP_breg29:
3015 case DW_OP_breg30:
3016 case DW_OP_breg31:
3017 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3018 break;
3019 case DW_OP_regx:
3020 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3021 break;
3022 case DW_OP_fbreg:
3023 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3024 break;
3025 case DW_OP_bregx:
3026 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3027 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3028 break;
3029 case DW_OP_piece:
3030 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3031 break;
3032 case DW_OP_deref_size:
3033 case DW_OP_xderef_size:
3034 size += 1;
3035 break;
3036 case DW_OP_call2:
3037 size += 2;
3038 break;
3039 case DW_OP_call4:
3040 size += 4;
3041 break;
3042 case DW_OP_call_ref:
3043 size += DWARF2_ADDR_SIZE;
3044 break;
3045 default:
3046 break;
3049 return size;
3052 /* Return the size of a series of location descriptors. */
3054 static unsigned long
3055 size_of_locs (dw_loc_descr_ref loc)
3057 unsigned long size;
3059 for (size = 0; loc != NULL; loc = loc->dw_loc_next)
3061 loc->dw_loc_addr = size;
3062 size += size_of_loc_descr (loc);
3065 return size;
3068 /* Output location description stack opcode's operands (if any). */
3070 static void
3071 output_loc_operands (dw_loc_descr_ref loc)
3073 dw_val_ref val1 = &loc->dw_loc_oprnd1;
3074 dw_val_ref val2 = &loc->dw_loc_oprnd2;
3076 switch (loc->dw_loc_opc)
3078 #ifdef DWARF2_DEBUGGING_INFO
3079 case DW_OP_addr:
3080 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3081 break;
3082 case DW_OP_const2u:
3083 case DW_OP_const2s:
3084 dw2_asm_output_data (2, val1->v.val_int, NULL);
3085 break;
3086 case DW_OP_const4u:
3087 case DW_OP_const4s:
3088 dw2_asm_output_data (4, val1->v.val_int, NULL);
3089 break;
3090 case DW_OP_const8u:
3091 case DW_OP_const8s:
3092 if (HOST_BITS_PER_LONG < 64)
3093 abort ();
3094 dw2_asm_output_data (8, val1->v.val_int, NULL);
3095 break;
3096 case DW_OP_skip:
3097 case DW_OP_bra:
3099 int offset;
3101 if (val1->val_class == dw_val_class_loc)
3102 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3103 else
3104 abort ();
3106 dw2_asm_output_data (2, offset, NULL);
3108 break;
3109 #else
3110 case DW_OP_addr:
3111 case DW_OP_const2u:
3112 case DW_OP_const2s:
3113 case DW_OP_const4u:
3114 case DW_OP_const4s:
3115 case DW_OP_const8u:
3116 case DW_OP_const8s:
3117 case DW_OP_skip:
3118 case DW_OP_bra:
3119 /* We currently don't make any attempt to make sure these are
3120 aligned properly like we do for the main unwind info, so
3121 don't support emitting things larger than a byte if we're
3122 only doing unwinding. */
3123 abort ();
3124 #endif
3125 case DW_OP_const1u:
3126 case DW_OP_const1s:
3127 dw2_asm_output_data (1, val1->v.val_int, NULL);
3128 break;
3129 case DW_OP_constu:
3130 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3131 break;
3132 case DW_OP_consts:
3133 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3134 break;
3135 case DW_OP_pick:
3136 dw2_asm_output_data (1, val1->v.val_int, NULL);
3137 break;
3138 case DW_OP_plus_uconst:
3139 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3140 break;
3141 case DW_OP_breg0:
3142 case DW_OP_breg1:
3143 case DW_OP_breg2:
3144 case DW_OP_breg3:
3145 case DW_OP_breg4:
3146 case DW_OP_breg5:
3147 case DW_OP_breg6:
3148 case DW_OP_breg7:
3149 case DW_OP_breg8:
3150 case DW_OP_breg9:
3151 case DW_OP_breg10:
3152 case DW_OP_breg11:
3153 case DW_OP_breg12:
3154 case DW_OP_breg13:
3155 case DW_OP_breg14:
3156 case DW_OP_breg15:
3157 case DW_OP_breg16:
3158 case DW_OP_breg17:
3159 case DW_OP_breg18:
3160 case DW_OP_breg19:
3161 case DW_OP_breg20:
3162 case DW_OP_breg21:
3163 case DW_OP_breg22:
3164 case DW_OP_breg23:
3165 case DW_OP_breg24:
3166 case DW_OP_breg25:
3167 case DW_OP_breg26:
3168 case DW_OP_breg27:
3169 case DW_OP_breg28:
3170 case DW_OP_breg29:
3171 case DW_OP_breg30:
3172 case DW_OP_breg31:
3173 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3174 break;
3175 case DW_OP_regx:
3176 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3177 break;
3178 case DW_OP_fbreg:
3179 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3180 break;
3181 case DW_OP_bregx:
3182 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3183 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3184 break;
3185 case DW_OP_piece:
3186 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3187 break;
3188 case DW_OP_deref_size:
3189 case DW_OP_xderef_size:
3190 dw2_asm_output_data (1, val1->v.val_int, NULL);
3191 break;
3193 case INTERNAL_DW_OP_tls_addr:
3194 #ifdef ASM_OUTPUT_DWARF_DTPREL
3195 ASM_OUTPUT_DWARF_DTPREL (asm_out_file, DWARF2_ADDR_SIZE,
3196 val1->v.val_addr);
3197 fputc ('\n', asm_out_file);
3198 #else
3199 abort ();
3200 #endif
3201 break;
3203 default:
3204 /* Other codes have no operands. */
3205 break;
3209 /* Output a sequence of location operations. */
3211 static void
3212 output_loc_sequence (dw_loc_descr_ref loc)
3214 for (; loc != NULL; loc = loc->dw_loc_next)
3216 /* Output the opcode. */
3217 dw2_asm_output_data (1, loc->dw_loc_opc,
3218 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3220 /* Output the operand(s) (if any). */
3221 output_loc_operands (loc);
3225 /* This routine will generate the correct assembly data for a location
3226 description based on a cfi entry with a complex address. */
3228 static void
3229 output_cfa_loc (dw_cfi_ref cfi)
3231 dw_loc_descr_ref loc;
3232 unsigned long size;
3234 /* Output the size of the block. */
3235 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3236 size = size_of_locs (loc);
3237 dw2_asm_output_data_uleb128 (size, NULL);
3239 /* Now output the operations themselves. */
3240 output_loc_sequence (loc);
3243 /* This function builds a dwarf location descriptor sequence from
3244 a dw_cfa_location. */
3246 static struct dw_loc_descr_struct *
3247 build_cfa_loc (dw_cfa_location *cfa)
3249 struct dw_loc_descr_struct *head, *tmp;
3251 if (cfa->indirect == 0)
3252 abort ();
3254 if (cfa->base_offset)
3256 if (cfa->reg <= 31)
3257 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3258 else
3259 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3261 else if (cfa->reg <= 31)
3262 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3263 else
3264 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3266 head->dw_loc_oprnd1.val_class = dw_val_class_const;
3267 tmp = new_loc_descr (DW_OP_deref, 0, 0);
3268 add_loc_descr (&head, tmp);
3269 if (cfa->offset != 0)
3271 tmp = new_loc_descr (DW_OP_plus_uconst, cfa->offset, 0);
3272 add_loc_descr (&head, tmp);
3275 return head;
3278 /* This function fills in aa dw_cfa_location structure from a dwarf location
3279 descriptor sequence. */
3281 static void
3282 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3284 struct dw_loc_descr_struct *ptr;
3285 cfa->offset = 0;
3286 cfa->base_offset = 0;
3287 cfa->indirect = 0;
3288 cfa->reg = -1;
3290 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3292 enum dwarf_location_atom op = ptr->dw_loc_opc;
3294 switch (op)
3296 case DW_OP_reg0:
3297 case DW_OP_reg1:
3298 case DW_OP_reg2:
3299 case DW_OP_reg3:
3300 case DW_OP_reg4:
3301 case DW_OP_reg5:
3302 case DW_OP_reg6:
3303 case DW_OP_reg7:
3304 case DW_OP_reg8:
3305 case DW_OP_reg9:
3306 case DW_OP_reg10:
3307 case DW_OP_reg11:
3308 case DW_OP_reg12:
3309 case DW_OP_reg13:
3310 case DW_OP_reg14:
3311 case DW_OP_reg15:
3312 case DW_OP_reg16:
3313 case DW_OP_reg17:
3314 case DW_OP_reg18:
3315 case DW_OP_reg19:
3316 case DW_OP_reg20:
3317 case DW_OP_reg21:
3318 case DW_OP_reg22:
3319 case DW_OP_reg23:
3320 case DW_OP_reg24:
3321 case DW_OP_reg25:
3322 case DW_OP_reg26:
3323 case DW_OP_reg27:
3324 case DW_OP_reg28:
3325 case DW_OP_reg29:
3326 case DW_OP_reg30:
3327 case DW_OP_reg31:
3328 cfa->reg = op - DW_OP_reg0;
3329 break;
3330 case DW_OP_regx:
3331 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3332 break;
3333 case DW_OP_breg0:
3334 case DW_OP_breg1:
3335 case DW_OP_breg2:
3336 case DW_OP_breg3:
3337 case DW_OP_breg4:
3338 case DW_OP_breg5:
3339 case DW_OP_breg6:
3340 case DW_OP_breg7:
3341 case DW_OP_breg8:
3342 case DW_OP_breg9:
3343 case DW_OP_breg10:
3344 case DW_OP_breg11:
3345 case DW_OP_breg12:
3346 case DW_OP_breg13:
3347 case DW_OP_breg14:
3348 case DW_OP_breg15:
3349 case DW_OP_breg16:
3350 case DW_OP_breg17:
3351 case DW_OP_breg18:
3352 case DW_OP_breg19:
3353 case DW_OP_breg20:
3354 case DW_OP_breg21:
3355 case DW_OP_breg22:
3356 case DW_OP_breg23:
3357 case DW_OP_breg24:
3358 case DW_OP_breg25:
3359 case DW_OP_breg26:
3360 case DW_OP_breg27:
3361 case DW_OP_breg28:
3362 case DW_OP_breg29:
3363 case DW_OP_breg30:
3364 case DW_OP_breg31:
3365 cfa->reg = op - DW_OP_breg0;
3366 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3367 break;
3368 case DW_OP_bregx:
3369 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3370 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3371 break;
3372 case DW_OP_deref:
3373 cfa->indirect = 1;
3374 break;
3375 case DW_OP_plus_uconst:
3376 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3377 break;
3378 default:
3379 internal_error ("DW_LOC_OP %s not implemented\n",
3380 dwarf_stack_op_name (ptr->dw_loc_opc));
3384 #endif /* .debug_frame support */
3386 /* And now, the support for symbolic debugging information. */
3387 #ifdef DWARF2_DEBUGGING_INFO
3389 /* .debug_str support. */
3390 static int output_indirect_string (void **, void *);
3392 static void dwarf2out_init (const char *);
3393 static void dwarf2out_finish (const char *);
3394 static void dwarf2out_define (unsigned int, const char *);
3395 static void dwarf2out_undef (unsigned int, const char *);
3396 static void dwarf2out_start_source_file (unsigned, const char *);
3397 static void dwarf2out_end_source_file (unsigned);
3398 static void dwarf2out_begin_block (unsigned, unsigned);
3399 static void dwarf2out_end_block (unsigned, unsigned);
3400 static bool dwarf2out_ignore_block (tree);
3401 static void dwarf2out_global_decl (tree);
3402 static void dwarf2out_type_decl (tree, int);
3403 static void dwarf2out_imported_module_or_decl (tree, tree);
3404 static void dwarf2out_abstract_function (tree);
3405 static void dwarf2out_var_location (rtx);
3406 static void dwarf2out_begin_function (tree);
3408 /* The debug hooks structure. */
3410 const struct gcc_debug_hooks dwarf2_debug_hooks =
3412 dwarf2out_init,
3413 dwarf2out_finish,
3414 dwarf2out_define,
3415 dwarf2out_undef,
3416 dwarf2out_start_source_file,
3417 dwarf2out_end_source_file,
3418 dwarf2out_begin_block,
3419 dwarf2out_end_block,
3420 dwarf2out_ignore_block,
3421 dwarf2out_source_line,
3422 dwarf2out_begin_prologue,
3423 debug_nothing_int_charstar, /* end_prologue */
3424 dwarf2out_end_epilogue,
3425 dwarf2out_begin_function,
3426 debug_nothing_int, /* end_function */
3427 dwarf2out_decl, /* function_decl */
3428 dwarf2out_global_decl,
3429 dwarf2out_type_decl, /* type_decl */
3430 dwarf2out_imported_module_or_decl,
3431 debug_nothing_tree, /* deferred_inline_function */
3432 /* The DWARF 2 backend tries to reduce debugging bloat by not
3433 emitting the abstract description of inline functions until
3434 something tries to reference them. */
3435 dwarf2out_abstract_function, /* outlining_inline_function */
3436 debug_nothing_rtx, /* label */
3437 debug_nothing_int, /* handle_pch */
3438 dwarf2out_var_location
3440 #endif
3442 /* NOTE: In the comments in this file, many references are made to
3443 "Debugging Information Entries". This term is abbreviated as `DIE'
3444 throughout the remainder of this file. */
3446 /* An internal representation of the DWARF output is built, and then
3447 walked to generate the DWARF debugging info. The walk of the internal
3448 representation is done after the entire program has been compiled.
3449 The types below are used to describe the internal representation. */
3451 /* Various DIE's use offsets relative to the beginning of the
3452 .debug_info section to refer to each other. */
3454 typedef long int dw_offset;
3456 /* Define typedefs here to avoid circular dependencies. */
3458 typedef struct dw_attr_struct *dw_attr_ref;
3459 typedef struct dw_line_info_struct *dw_line_info_ref;
3460 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3461 typedef struct pubname_struct *pubname_ref;
3462 typedef struct dw_ranges_struct *dw_ranges_ref;
3464 /* Each entry in the line_info_table maintains the file and
3465 line number associated with the label generated for that
3466 entry. The label gives the PC value associated with
3467 the line number entry. */
3469 typedef struct dw_line_info_struct GTY(())
3471 unsigned long dw_file_num;
3472 unsigned long dw_line_num;
3474 dw_line_info_entry;
3476 /* Line information for functions in separate sections; each one gets its
3477 own sequence. */
3478 typedef struct dw_separate_line_info_struct GTY(())
3480 unsigned long dw_file_num;
3481 unsigned long dw_line_num;
3482 unsigned long function;
3484 dw_separate_line_info_entry;
3486 /* Each DIE attribute has a field specifying the attribute kind,
3487 a link to the next attribute in the chain, and an attribute value.
3488 Attributes are typically linked below the DIE they modify. */
3490 typedef struct dw_attr_struct GTY(())
3492 enum dwarf_attribute dw_attr;
3493 dw_attr_ref dw_attr_next;
3494 dw_val_node dw_attr_val;
3496 dw_attr_node;
3498 /* The Debugging Information Entry (DIE) structure */
3500 typedef struct die_struct GTY(())
3502 enum dwarf_tag die_tag;
3503 char *die_symbol;
3504 dw_attr_ref die_attr;
3505 dw_die_ref die_parent;
3506 dw_die_ref die_child;
3507 dw_die_ref die_sib;
3508 dw_die_ref die_definition; /* ref from a specification to its definition */
3509 dw_offset die_offset;
3510 unsigned long die_abbrev;
3511 int die_mark;
3512 unsigned int decl_id;
3514 die_node;
3516 /* The pubname structure */
3518 typedef struct pubname_struct GTY(())
3520 dw_die_ref die;
3521 char *name;
3523 pubname_entry;
3525 struct dw_ranges_struct GTY(())
3527 int block_num;
3530 /* The limbo die list structure. */
3531 typedef struct limbo_die_struct GTY(())
3533 dw_die_ref die;
3534 tree created_for;
3535 struct limbo_die_struct *next;
3537 limbo_die_node;
3539 /* How to start an assembler comment. */
3540 #ifndef ASM_COMMENT_START
3541 #define ASM_COMMENT_START ";#"
3542 #endif
3544 /* Define a macro which returns nonzero for a TYPE_DECL which was
3545 implicitly generated for a tagged type.
3547 Note that unlike the gcc front end (which generates a NULL named
3548 TYPE_DECL node for each complete tagged type, each array type, and
3549 each function type node created) the g++ front end generates a
3550 _named_ TYPE_DECL node for each tagged type node created.
3551 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3552 generate a DW_TAG_typedef DIE for them. */
3554 #define TYPE_DECL_IS_STUB(decl) \
3555 (DECL_NAME (decl) == NULL_TREE \
3556 || (DECL_ARTIFICIAL (decl) \
3557 && is_tagged_type (TREE_TYPE (decl)) \
3558 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3559 /* This is necessary for stub decls that \
3560 appear in nested inline functions. */ \
3561 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3562 && (decl_ultimate_origin (decl) \
3563 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3565 /* Information concerning the compilation unit's programming
3566 language, and compiler version. */
3568 /* Fixed size portion of the DWARF compilation unit header. */
3569 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3570 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3572 /* Fixed size portion of public names info. */
3573 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3575 /* Fixed size portion of the address range info. */
3576 #define DWARF_ARANGES_HEADER_SIZE \
3577 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3578 DWARF2_ADDR_SIZE * 2) \
3579 - DWARF_INITIAL_LENGTH_SIZE)
3581 /* Size of padding portion in the address range info. It must be
3582 aligned to twice the pointer size. */
3583 #define DWARF_ARANGES_PAD_SIZE \
3584 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3585 DWARF2_ADDR_SIZE * 2) \
3586 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3588 /* Use assembler line directives if available. */
3589 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3590 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3591 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3592 #else
3593 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3594 #endif
3595 #endif
3597 /* Minimum line offset in a special line info. opcode.
3598 This value was chosen to give a reasonable range of values. */
3599 #define DWARF_LINE_BASE -10
3601 /* First special line opcode - leave room for the standard opcodes. */
3602 #define DWARF_LINE_OPCODE_BASE 10
3604 /* Range of line offsets in a special line info. opcode. */
3605 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3607 /* Flag that indicates the initial value of the is_stmt_start flag.
3608 In the present implementation, we do not mark any lines as
3609 the beginning of a source statement, because that information
3610 is not made available by the GCC front-end. */
3611 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3613 #ifdef DWARF2_DEBUGGING_INFO
3614 /* This location is used by calc_die_sizes() to keep track
3615 the offset of each DIE within the .debug_info section. */
3616 static unsigned long next_die_offset;
3617 #endif
3619 /* Record the root of the DIE's built for the current compilation unit. */
3620 static GTY(()) dw_die_ref comp_unit_die;
3622 /* A list of DIEs with a NULL parent waiting to be relocated. */
3623 static GTY(()) limbo_die_node *limbo_die_list;
3625 /* Filenames referenced by this compilation unit. */
3626 static GTY(()) varray_type file_table;
3627 static GTY(()) varray_type file_table_emitted;
3628 static GTY(()) size_t file_table_last_lookup_index;
3630 /* A hash table of references to DIE's that describe declarations.
3631 The key is a DECL_UID() which is a unique number identifying each decl. */
3632 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3634 /* Node of the variable location list. */
3635 struct var_loc_node GTY ((chain_next ("%h.next")))
3637 rtx GTY (()) var_loc_note;
3638 const char * GTY (()) label;
3639 struct var_loc_node * GTY (()) next;
3642 /* Variable location list. */
3643 struct var_loc_list_def GTY (())
3645 struct var_loc_node * GTY (()) first;
3647 /* Do not mark the last element of the chained list because
3648 it is marked through the chain. */
3649 struct var_loc_node * GTY ((skip ("%h"))) last;
3651 /* DECL_UID of the variable decl. */
3652 unsigned int decl_id;
3654 typedef struct var_loc_list_def var_loc_list;
3657 /* Table of decl location linked lists. */
3658 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3660 /* A pointer to the base of a list of references to DIE's that
3661 are uniquely identified by their tag, presence/absence of
3662 children DIE's, and list of attribute/value pairs. */
3663 static GTY((length ("abbrev_die_table_allocated")))
3664 dw_die_ref *abbrev_die_table;
3666 /* Number of elements currently allocated for abbrev_die_table. */
3667 static GTY(()) unsigned abbrev_die_table_allocated;
3669 /* Number of elements in type_die_table currently in use. */
3670 static GTY(()) unsigned abbrev_die_table_in_use;
3672 /* Size (in elements) of increments by which we may expand the
3673 abbrev_die_table. */
3674 #define ABBREV_DIE_TABLE_INCREMENT 256
3676 /* A pointer to the base of a table that contains line information
3677 for each source code line in .text in the compilation unit. */
3678 static GTY((length ("line_info_table_allocated")))
3679 dw_line_info_ref line_info_table;
3681 /* Number of elements currently allocated for line_info_table. */
3682 static GTY(()) unsigned line_info_table_allocated;
3684 /* Number of elements in line_info_table currently in use. */
3685 static GTY(()) unsigned line_info_table_in_use;
3687 /* A pointer to the base of a table that contains line information
3688 for each source code line outside of .text in the compilation unit. */
3689 static GTY ((length ("separate_line_info_table_allocated")))
3690 dw_separate_line_info_ref separate_line_info_table;
3692 /* Number of elements currently allocated for separate_line_info_table. */
3693 static GTY(()) unsigned separate_line_info_table_allocated;
3695 /* Number of elements in separate_line_info_table currently in use. */
3696 static GTY(()) unsigned separate_line_info_table_in_use;
3698 /* Size (in elements) of increments by which we may expand the
3699 line_info_table. */
3700 #define LINE_INFO_TABLE_INCREMENT 1024
3702 /* A pointer to the base of a table that contains a list of publicly
3703 accessible names. */
3704 static GTY ((length ("pubname_table_allocated"))) pubname_ref pubname_table;
3706 /* Number of elements currently allocated for pubname_table. */
3707 static GTY(()) unsigned pubname_table_allocated;
3709 /* Number of elements in pubname_table currently in use. */
3710 static GTY(()) unsigned pubname_table_in_use;
3712 /* Size (in elements) of increments by which we may expand the
3713 pubname_table. */
3714 #define PUBNAME_TABLE_INCREMENT 64
3716 /* Array of dies for which we should generate .debug_arange info. */
3717 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3719 /* Number of elements currently allocated for arange_table. */
3720 static GTY(()) unsigned arange_table_allocated;
3722 /* Number of elements in arange_table currently in use. */
3723 static GTY(()) unsigned arange_table_in_use;
3725 /* Size (in elements) of increments by which we may expand the
3726 arange_table. */
3727 #define ARANGE_TABLE_INCREMENT 64
3729 /* Array of dies for which we should generate .debug_ranges info. */
3730 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3732 /* Number of elements currently allocated for ranges_table. */
3733 static GTY(()) unsigned ranges_table_allocated;
3735 /* Number of elements in ranges_table currently in use. */
3736 static GTY(()) unsigned ranges_table_in_use;
3738 /* Size (in elements) of increments by which we may expand the
3739 ranges_table. */
3740 #define RANGES_TABLE_INCREMENT 64
3742 /* Whether we have location lists that need outputting */
3743 static GTY(()) unsigned have_location_lists;
3745 /* Unique label counter. */
3746 static GTY(()) unsigned int loclabel_num;
3748 #ifdef DWARF2_DEBUGGING_INFO
3749 /* Record whether the function being analyzed contains inlined functions. */
3750 static int current_function_has_inlines;
3751 #endif
3752 #if 0 && defined (MIPS_DEBUGGING_INFO)
3753 static int comp_unit_has_inlines;
3754 #endif
3756 /* Number of file tables emitted in maybe_emit_file(). */
3757 static GTY(()) int emitcount = 0;
3759 /* Number of internal labels generated by gen_internal_sym(). */
3760 static GTY(()) int label_num;
3762 #ifdef DWARF2_DEBUGGING_INFO
3764 /* Forward declarations for functions defined in this file. */
3766 static int is_pseudo_reg (rtx);
3767 static tree type_main_variant (tree);
3768 static int is_tagged_type (tree);
3769 static const char *dwarf_tag_name (unsigned);
3770 static const char *dwarf_attr_name (unsigned);
3771 static const char *dwarf_form_name (unsigned);
3772 #if 0
3773 static const char *dwarf_type_encoding_name (unsigned);
3774 #endif
3775 static tree decl_ultimate_origin (tree);
3776 static tree block_ultimate_origin (tree);
3777 static tree decl_class_context (tree);
3778 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3779 static inline enum dw_val_class AT_class (dw_attr_ref);
3780 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3781 static inline unsigned AT_flag (dw_attr_ref);
3782 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3783 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3784 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3785 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3786 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
3787 unsigned long);
3788 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3789 unsigned int, unsigned char *);
3790 static hashval_t debug_str_do_hash (const void *);
3791 static int debug_str_eq (const void *, const void *);
3792 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3793 static inline const char *AT_string (dw_attr_ref);
3794 static int AT_string_form (dw_attr_ref);
3795 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3796 static void add_AT_specification (dw_die_ref, dw_die_ref);
3797 static inline dw_die_ref AT_ref (dw_attr_ref);
3798 static inline int AT_ref_external (dw_attr_ref);
3799 static inline void set_AT_ref_external (dw_attr_ref, int);
3800 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3801 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3802 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3803 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3804 dw_loc_list_ref);
3805 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3806 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3807 static inline rtx AT_addr (dw_attr_ref);
3808 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3809 static void add_AT_lbl_offset (dw_die_ref, enum dwarf_attribute, const char *);
3810 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3811 unsigned HOST_WIDE_INT);
3812 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3813 unsigned long);
3814 static inline const char *AT_lbl (dw_attr_ref);
3815 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3816 static const char *get_AT_low_pc (dw_die_ref);
3817 static const char *get_AT_hi_pc (dw_die_ref);
3818 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3819 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3820 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3821 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3822 static bool is_c_family (void);
3823 static bool is_cxx (void);
3824 static bool is_java (void);
3825 static bool is_fortran (void);
3826 static bool is_ada (void);
3827 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3828 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3829 static inline void free_die (dw_die_ref);
3830 static void remove_children (dw_die_ref);
3831 static void add_child_die (dw_die_ref, dw_die_ref);
3832 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3833 static dw_die_ref lookup_type_die (tree);
3834 static void equate_type_number_to_die (tree, dw_die_ref);
3835 static hashval_t decl_die_table_hash (const void *);
3836 static int decl_die_table_eq (const void *, const void *);
3837 static dw_die_ref lookup_decl_die (tree);
3838 static hashval_t decl_loc_table_hash (const void *);
3839 static int decl_loc_table_eq (const void *, const void *);
3840 static var_loc_list *lookup_decl_loc (tree);
3841 static void equate_decl_number_to_die (tree, dw_die_ref);
3842 static void add_var_loc_to_decl (tree, struct var_loc_node *);
3843 static void print_spaces (FILE *);
3844 static void print_die (dw_die_ref, FILE *);
3845 static void print_dwarf_line_table (FILE *);
3846 static void reverse_die_lists (dw_die_ref);
3847 static void reverse_all_dies (dw_die_ref);
3848 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3849 static dw_die_ref pop_compile_unit (dw_die_ref);
3850 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3851 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3852 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3853 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3854 static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
3855 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3856 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3857 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3858 static void compute_section_prefix (dw_die_ref);
3859 static int is_type_die (dw_die_ref);
3860 static int is_comdat_die (dw_die_ref);
3861 static int is_symbol_die (dw_die_ref);
3862 static void assign_symbol_names (dw_die_ref);
3863 static void break_out_includes (dw_die_ref);
3864 static hashval_t htab_cu_hash (const void *);
3865 static int htab_cu_eq (const void *, const void *);
3866 static void htab_cu_del (void *);
3867 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3868 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3869 static void add_sibling_attributes (dw_die_ref);
3870 static void build_abbrev_table (dw_die_ref);
3871 static void output_location_lists (dw_die_ref);
3872 static int constant_size (long unsigned);
3873 static unsigned long size_of_die (dw_die_ref);
3874 static void calc_die_sizes (dw_die_ref);
3875 static void mark_dies (dw_die_ref);
3876 static void unmark_dies (dw_die_ref);
3877 static void unmark_all_dies (dw_die_ref);
3878 static unsigned long size_of_pubnames (void);
3879 static unsigned long size_of_aranges (void);
3880 static enum dwarf_form value_format (dw_attr_ref);
3881 static void output_value_format (dw_attr_ref);
3882 static void output_abbrev_section (void);
3883 static void output_die_symbol (dw_die_ref);
3884 static void output_die (dw_die_ref);
3885 static void output_compilation_unit_header (void);
3886 static void output_comp_unit (dw_die_ref, int);
3887 static const char *dwarf2_name (tree, int);
3888 static void add_pubname (tree, dw_die_ref);
3889 static void output_pubnames (void);
3890 static void add_arange (tree, dw_die_ref);
3891 static void output_aranges (void);
3892 static unsigned int add_ranges (tree);
3893 static void output_ranges (void);
3894 static void output_line_info (void);
3895 static void output_file_names (void);
3896 static dw_die_ref base_type_die (tree);
3897 static tree root_type (tree);
3898 static int is_base_type (tree);
3899 static bool is_subrange_type (tree);
3900 static dw_die_ref subrange_type_die (tree, dw_die_ref);
3901 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3902 static int type_is_enum (tree);
3903 static unsigned int dbx_reg_number (rtx);
3904 static dw_loc_descr_ref reg_loc_descriptor (rtx);
3905 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
3906 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
3907 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3908 static dw_loc_descr_ref based_loc_descr (unsigned, HOST_WIDE_INT, bool);
3909 static int is_based_loc (rtx);
3910 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode, bool);
3911 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
3912 static dw_loc_descr_ref loc_descriptor (rtx, bool);
3913 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
3914 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
3915 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3916 static tree field_type (tree);
3917 static unsigned int simple_type_align_in_bits (tree);
3918 static unsigned int simple_decl_align_in_bits (tree);
3919 static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
3920 static HOST_WIDE_INT field_byte_offset (tree);
3921 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3922 dw_loc_descr_ref);
3923 static void add_data_member_location_attribute (dw_die_ref, tree);
3924 static void add_const_value_attribute (dw_die_ref, rtx);
3925 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3926 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
3927 static void insert_float (rtx, unsigned char *);
3928 static rtx rtl_for_decl_location (tree);
3929 static void add_location_or_const_value_attribute (dw_die_ref, tree,
3930 enum dwarf_attribute);
3931 static void tree_add_const_value_attribute (dw_die_ref, tree);
3932 static void add_name_attribute (dw_die_ref, const char *);
3933 static void add_comp_dir_attribute (dw_die_ref);
3934 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3935 static void add_subscript_info (dw_die_ref, tree);
3936 static void add_byte_size_attribute (dw_die_ref, tree);
3937 static void add_bit_offset_attribute (dw_die_ref, tree);
3938 static void add_bit_size_attribute (dw_die_ref, tree);
3939 static void add_prototyped_attribute (dw_die_ref, tree);
3940 static void add_abstract_origin_attribute (dw_die_ref, tree);
3941 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3942 static void add_src_coords_attributes (dw_die_ref, tree);
3943 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3944 static void push_decl_scope (tree);
3945 static void pop_decl_scope (void);
3946 static dw_die_ref scope_die_for (tree, dw_die_ref);
3947 static inline int local_scope_p (dw_die_ref);
3948 static inline int class_or_namespace_scope_p (dw_die_ref);
3949 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3950 static const char *type_tag (tree);
3951 static tree member_declared_type (tree);
3952 #if 0
3953 static const char *decl_start_label (tree);
3954 #endif
3955 static void gen_array_type_die (tree, dw_die_ref);
3956 static void gen_set_type_die (tree, dw_die_ref);
3957 #if 0
3958 static void gen_entry_point_die (tree, dw_die_ref);
3959 #endif
3960 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
3961 static void gen_inlined_structure_type_die (tree, dw_die_ref);
3962 static void gen_inlined_union_type_die (tree, dw_die_ref);
3963 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3964 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
3965 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3966 static void gen_formal_types_die (tree, dw_die_ref);
3967 static void gen_subprogram_die (tree, dw_die_ref);
3968 static void gen_variable_die (tree, dw_die_ref);
3969 static void gen_label_die (tree, dw_die_ref);
3970 static void gen_lexical_block_die (tree, dw_die_ref, int);
3971 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3972 static void gen_field_die (tree, dw_die_ref);
3973 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3974 static dw_die_ref gen_compile_unit_die (const char *);
3975 static void gen_string_type_die (tree, dw_die_ref);
3976 static void gen_inheritance_die (tree, tree, dw_die_ref);
3977 static void gen_member_die (tree, dw_die_ref);
3978 static void gen_struct_or_union_type_die (tree, dw_die_ref);
3979 static void gen_subroutine_type_die (tree, dw_die_ref);
3980 static void gen_typedef_die (tree, dw_die_ref);
3981 static void gen_type_die (tree, dw_die_ref);
3982 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
3983 static void gen_block_die (tree, dw_die_ref, int);
3984 static void decls_for_scope (tree, dw_die_ref, int);
3985 static int is_redundant_typedef (tree);
3986 static void gen_namespace_die (tree);
3987 static void gen_decl_die (tree, dw_die_ref);
3988 static dw_die_ref force_decl_die (tree);
3989 static dw_die_ref force_type_die (tree);
3990 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3991 static void declare_in_namespace (tree, dw_die_ref);
3992 static unsigned lookup_filename (const char *);
3993 static void init_file_table (void);
3994 static void retry_incomplete_types (void);
3995 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3996 static void splice_child_die (dw_die_ref, dw_die_ref);
3997 static int file_info_cmp (const void *, const void *);
3998 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3999 const char *, const char *, unsigned);
4000 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4001 const char *, const char *,
4002 const char *);
4003 static void output_loc_list (dw_loc_list_ref);
4004 static char *gen_internal_sym (const char *);
4006 static void prune_unmark_dies (dw_die_ref);
4007 static void prune_unused_types_mark (dw_die_ref, int);
4008 static void prune_unused_types_walk (dw_die_ref);
4009 static void prune_unused_types_walk_attribs (dw_die_ref);
4010 static void prune_unused_types_prune (dw_die_ref);
4011 static void prune_unused_types (void);
4012 static int maybe_emit_file (int);
4014 /* Section names used to hold DWARF debugging information. */
4015 #ifndef DEBUG_INFO_SECTION
4016 #define DEBUG_INFO_SECTION ".debug_info"
4017 #endif
4018 #ifndef DEBUG_ABBREV_SECTION
4019 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4020 #endif
4021 #ifndef DEBUG_ARANGES_SECTION
4022 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4023 #endif
4024 #ifndef DEBUG_MACINFO_SECTION
4025 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4026 #endif
4027 #ifndef DEBUG_LINE_SECTION
4028 #define DEBUG_LINE_SECTION ".debug_line"
4029 #endif
4030 #ifndef DEBUG_LOC_SECTION
4031 #define DEBUG_LOC_SECTION ".debug_loc"
4032 #endif
4033 #ifndef DEBUG_PUBNAMES_SECTION
4034 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
4035 #endif
4036 #ifndef DEBUG_STR_SECTION
4037 #define DEBUG_STR_SECTION ".debug_str"
4038 #endif
4039 #ifndef DEBUG_RANGES_SECTION
4040 #define DEBUG_RANGES_SECTION ".debug_ranges"
4041 #endif
4043 /* Standard ELF section names for compiled code and data. */
4044 #ifndef TEXT_SECTION_NAME
4045 #define TEXT_SECTION_NAME ".text"
4046 #endif
4048 /* Section flags for .debug_str section. */
4049 #define DEBUG_STR_SECTION_FLAGS \
4050 (HAVE_GAS_SHF_MERGE && flag_merge_constants \
4051 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4052 : SECTION_DEBUG)
4054 /* Labels we insert at beginning sections we can reference instead of
4055 the section names themselves. */
4057 #ifndef TEXT_SECTION_LABEL
4058 #define TEXT_SECTION_LABEL "Ltext"
4059 #endif
4060 #ifndef DEBUG_LINE_SECTION_LABEL
4061 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4062 #endif
4063 #ifndef DEBUG_INFO_SECTION_LABEL
4064 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4065 #endif
4066 #ifndef DEBUG_ABBREV_SECTION_LABEL
4067 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4068 #endif
4069 #ifndef DEBUG_LOC_SECTION_LABEL
4070 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4071 #endif
4072 #ifndef DEBUG_RANGES_SECTION_LABEL
4073 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4074 #endif
4075 #ifndef DEBUG_MACINFO_SECTION_LABEL
4076 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4077 #endif
4079 /* Definitions of defaults for formats and names of various special
4080 (artificial) labels which may be generated within this file (when the -g
4081 options is used and DWARF2_DEBUGGING_INFO is in effect.
4082 If necessary, these may be overridden from within the tm.h file, but
4083 typically, overriding these defaults is unnecessary. */
4085 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4086 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4087 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4088 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4089 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4090 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4091 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4092 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4094 #ifndef TEXT_END_LABEL
4095 #define TEXT_END_LABEL "Letext"
4096 #endif
4097 #ifndef BLOCK_BEGIN_LABEL
4098 #define BLOCK_BEGIN_LABEL "LBB"
4099 #endif
4100 #ifndef BLOCK_END_LABEL
4101 #define BLOCK_END_LABEL "LBE"
4102 #endif
4103 #ifndef LINE_CODE_LABEL
4104 #define LINE_CODE_LABEL "LM"
4105 #endif
4106 #ifndef SEPARATE_LINE_CODE_LABEL
4107 #define SEPARATE_LINE_CODE_LABEL "LSM"
4108 #endif
4110 /* We allow a language front-end to designate a function that is to be
4111 called to "demangle" any name before it it put into a DIE. */
4113 static const char *(*demangle_name_func) (const char *);
4115 void
4116 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4118 demangle_name_func = func;
4121 /* Test if rtl node points to a pseudo register. */
4123 static inline int
4124 is_pseudo_reg (rtx rtl)
4126 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4127 || (GET_CODE (rtl) == SUBREG
4128 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4131 /* Return a reference to a type, with its const and volatile qualifiers
4132 removed. */
4134 static inline tree
4135 type_main_variant (tree type)
4137 type = TYPE_MAIN_VARIANT (type);
4139 /* ??? There really should be only one main variant among any group of
4140 variants of a given type (and all of the MAIN_VARIANT values for all
4141 members of the group should point to that one type) but sometimes the C
4142 front-end messes this up for array types, so we work around that bug
4143 here. */
4144 if (TREE_CODE (type) == ARRAY_TYPE)
4145 while (type != TYPE_MAIN_VARIANT (type))
4146 type = TYPE_MAIN_VARIANT (type);
4148 return type;
4151 /* Return nonzero if the given type node represents a tagged type. */
4153 static inline int
4154 is_tagged_type (tree type)
4156 enum tree_code code = TREE_CODE (type);
4158 return (code == RECORD_TYPE || code == UNION_TYPE
4159 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4162 /* Convert a DIE tag into its string name. */
4164 static const char *
4165 dwarf_tag_name (unsigned int tag)
4167 switch (tag)
4169 case DW_TAG_padding:
4170 return "DW_TAG_padding";
4171 case DW_TAG_array_type:
4172 return "DW_TAG_array_type";
4173 case DW_TAG_class_type:
4174 return "DW_TAG_class_type";
4175 case DW_TAG_entry_point:
4176 return "DW_TAG_entry_point";
4177 case DW_TAG_enumeration_type:
4178 return "DW_TAG_enumeration_type";
4179 case DW_TAG_formal_parameter:
4180 return "DW_TAG_formal_parameter";
4181 case DW_TAG_imported_declaration:
4182 return "DW_TAG_imported_declaration";
4183 case DW_TAG_label:
4184 return "DW_TAG_label";
4185 case DW_TAG_lexical_block:
4186 return "DW_TAG_lexical_block";
4187 case DW_TAG_member:
4188 return "DW_TAG_member";
4189 case DW_TAG_pointer_type:
4190 return "DW_TAG_pointer_type";
4191 case DW_TAG_reference_type:
4192 return "DW_TAG_reference_type";
4193 case DW_TAG_compile_unit:
4194 return "DW_TAG_compile_unit";
4195 case DW_TAG_string_type:
4196 return "DW_TAG_string_type";
4197 case DW_TAG_structure_type:
4198 return "DW_TAG_structure_type";
4199 case DW_TAG_subroutine_type:
4200 return "DW_TAG_subroutine_type";
4201 case DW_TAG_typedef:
4202 return "DW_TAG_typedef";
4203 case DW_TAG_union_type:
4204 return "DW_TAG_union_type";
4205 case DW_TAG_unspecified_parameters:
4206 return "DW_TAG_unspecified_parameters";
4207 case DW_TAG_variant:
4208 return "DW_TAG_variant";
4209 case DW_TAG_common_block:
4210 return "DW_TAG_common_block";
4211 case DW_TAG_common_inclusion:
4212 return "DW_TAG_common_inclusion";
4213 case DW_TAG_inheritance:
4214 return "DW_TAG_inheritance";
4215 case DW_TAG_inlined_subroutine:
4216 return "DW_TAG_inlined_subroutine";
4217 case DW_TAG_module:
4218 return "DW_TAG_module";
4219 case DW_TAG_ptr_to_member_type:
4220 return "DW_TAG_ptr_to_member_type";
4221 case DW_TAG_set_type:
4222 return "DW_TAG_set_type";
4223 case DW_TAG_subrange_type:
4224 return "DW_TAG_subrange_type";
4225 case DW_TAG_with_stmt:
4226 return "DW_TAG_with_stmt";
4227 case DW_TAG_access_declaration:
4228 return "DW_TAG_access_declaration";
4229 case DW_TAG_base_type:
4230 return "DW_TAG_base_type";
4231 case DW_TAG_catch_block:
4232 return "DW_TAG_catch_block";
4233 case DW_TAG_const_type:
4234 return "DW_TAG_const_type";
4235 case DW_TAG_constant:
4236 return "DW_TAG_constant";
4237 case DW_TAG_enumerator:
4238 return "DW_TAG_enumerator";
4239 case DW_TAG_file_type:
4240 return "DW_TAG_file_type";
4241 case DW_TAG_friend:
4242 return "DW_TAG_friend";
4243 case DW_TAG_namelist:
4244 return "DW_TAG_namelist";
4245 case DW_TAG_namelist_item:
4246 return "DW_TAG_namelist_item";
4247 case DW_TAG_namespace:
4248 return "DW_TAG_namespace";
4249 case DW_TAG_packed_type:
4250 return "DW_TAG_packed_type";
4251 case DW_TAG_subprogram:
4252 return "DW_TAG_subprogram";
4253 case DW_TAG_template_type_param:
4254 return "DW_TAG_template_type_param";
4255 case DW_TAG_template_value_param:
4256 return "DW_TAG_template_value_param";
4257 case DW_TAG_thrown_type:
4258 return "DW_TAG_thrown_type";
4259 case DW_TAG_try_block:
4260 return "DW_TAG_try_block";
4261 case DW_TAG_variant_part:
4262 return "DW_TAG_variant_part";
4263 case DW_TAG_variable:
4264 return "DW_TAG_variable";
4265 case DW_TAG_volatile_type:
4266 return "DW_TAG_volatile_type";
4267 case DW_TAG_imported_module:
4268 return "DW_TAG_imported_module";
4269 case DW_TAG_MIPS_loop:
4270 return "DW_TAG_MIPS_loop";
4271 case DW_TAG_format_label:
4272 return "DW_TAG_format_label";
4273 case DW_TAG_function_template:
4274 return "DW_TAG_function_template";
4275 case DW_TAG_class_template:
4276 return "DW_TAG_class_template";
4277 case DW_TAG_GNU_BINCL:
4278 return "DW_TAG_GNU_BINCL";
4279 case DW_TAG_GNU_EINCL:
4280 return "DW_TAG_GNU_EINCL";
4281 default:
4282 return "DW_TAG_<unknown>";
4286 /* Convert a DWARF attribute code into its string name. */
4288 static const char *
4289 dwarf_attr_name (unsigned int attr)
4291 switch (attr)
4293 case DW_AT_sibling:
4294 return "DW_AT_sibling";
4295 case DW_AT_location:
4296 return "DW_AT_location";
4297 case DW_AT_name:
4298 return "DW_AT_name";
4299 case DW_AT_ordering:
4300 return "DW_AT_ordering";
4301 case DW_AT_subscr_data:
4302 return "DW_AT_subscr_data";
4303 case DW_AT_byte_size:
4304 return "DW_AT_byte_size";
4305 case DW_AT_bit_offset:
4306 return "DW_AT_bit_offset";
4307 case DW_AT_bit_size:
4308 return "DW_AT_bit_size";
4309 case DW_AT_element_list:
4310 return "DW_AT_element_list";
4311 case DW_AT_stmt_list:
4312 return "DW_AT_stmt_list";
4313 case DW_AT_low_pc:
4314 return "DW_AT_low_pc";
4315 case DW_AT_high_pc:
4316 return "DW_AT_high_pc";
4317 case DW_AT_language:
4318 return "DW_AT_language";
4319 case DW_AT_member:
4320 return "DW_AT_member";
4321 case DW_AT_discr:
4322 return "DW_AT_discr";
4323 case DW_AT_discr_value:
4324 return "DW_AT_discr_value";
4325 case DW_AT_visibility:
4326 return "DW_AT_visibility";
4327 case DW_AT_import:
4328 return "DW_AT_import";
4329 case DW_AT_string_length:
4330 return "DW_AT_string_length";
4331 case DW_AT_common_reference:
4332 return "DW_AT_common_reference";
4333 case DW_AT_comp_dir:
4334 return "DW_AT_comp_dir";
4335 case DW_AT_const_value:
4336 return "DW_AT_const_value";
4337 case DW_AT_containing_type:
4338 return "DW_AT_containing_type";
4339 case DW_AT_default_value:
4340 return "DW_AT_default_value";
4341 case DW_AT_inline:
4342 return "DW_AT_inline";
4343 case DW_AT_is_optional:
4344 return "DW_AT_is_optional";
4345 case DW_AT_lower_bound:
4346 return "DW_AT_lower_bound";
4347 case DW_AT_producer:
4348 return "DW_AT_producer";
4349 case DW_AT_prototyped:
4350 return "DW_AT_prototyped";
4351 case DW_AT_return_addr:
4352 return "DW_AT_return_addr";
4353 case DW_AT_start_scope:
4354 return "DW_AT_start_scope";
4355 case DW_AT_stride_size:
4356 return "DW_AT_stride_size";
4357 case DW_AT_upper_bound:
4358 return "DW_AT_upper_bound";
4359 case DW_AT_abstract_origin:
4360 return "DW_AT_abstract_origin";
4361 case DW_AT_accessibility:
4362 return "DW_AT_accessibility";
4363 case DW_AT_address_class:
4364 return "DW_AT_address_class";
4365 case DW_AT_artificial:
4366 return "DW_AT_artificial";
4367 case DW_AT_base_types:
4368 return "DW_AT_base_types";
4369 case DW_AT_calling_convention:
4370 return "DW_AT_calling_convention";
4371 case DW_AT_count:
4372 return "DW_AT_count";
4373 case DW_AT_data_member_location:
4374 return "DW_AT_data_member_location";
4375 case DW_AT_decl_column:
4376 return "DW_AT_decl_column";
4377 case DW_AT_decl_file:
4378 return "DW_AT_decl_file";
4379 case DW_AT_decl_line:
4380 return "DW_AT_decl_line";
4381 case DW_AT_declaration:
4382 return "DW_AT_declaration";
4383 case DW_AT_discr_list:
4384 return "DW_AT_discr_list";
4385 case DW_AT_encoding:
4386 return "DW_AT_encoding";
4387 case DW_AT_external:
4388 return "DW_AT_external";
4389 case DW_AT_frame_base:
4390 return "DW_AT_frame_base";
4391 case DW_AT_friend:
4392 return "DW_AT_friend";
4393 case DW_AT_identifier_case:
4394 return "DW_AT_identifier_case";
4395 case DW_AT_macro_info:
4396 return "DW_AT_macro_info";
4397 case DW_AT_namelist_items:
4398 return "DW_AT_namelist_items";
4399 case DW_AT_priority:
4400 return "DW_AT_priority";
4401 case DW_AT_segment:
4402 return "DW_AT_segment";
4403 case DW_AT_specification:
4404 return "DW_AT_specification";
4405 case DW_AT_static_link:
4406 return "DW_AT_static_link";
4407 case DW_AT_type:
4408 return "DW_AT_type";
4409 case DW_AT_use_location:
4410 return "DW_AT_use_location";
4411 case DW_AT_variable_parameter:
4412 return "DW_AT_variable_parameter";
4413 case DW_AT_virtuality:
4414 return "DW_AT_virtuality";
4415 case DW_AT_vtable_elem_location:
4416 return "DW_AT_vtable_elem_location";
4418 case DW_AT_allocated:
4419 return "DW_AT_allocated";
4420 case DW_AT_associated:
4421 return "DW_AT_associated";
4422 case DW_AT_data_location:
4423 return "DW_AT_data_location";
4424 case DW_AT_stride:
4425 return "DW_AT_stride";
4426 case DW_AT_entry_pc:
4427 return "DW_AT_entry_pc";
4428 case DW_AT_use_UTF8:
4429 return "DW_AT_use_UTF8";
4430 case DW_AT_extension:
4431 return "DW_AT_extension";
4432 case DW_AT_ranges:
4433 return "DW_AT_ranges";
4434 case DW_AT_trampoline:
4435 return "DW_AT_trampoline";
4436 case DW_AT_call_column:
4437 return "DW_AT_call_column";
4438 case DW_AT_call_file:
4439 return "DW_AT_call_file";
4440 case DW_AT_call_line:
4441 return "DW_AT_call_line";
4443 case DW_AT_MIPS_fde:
4444 return "DW_AT_MIPS_fde";
4445 case DW_AT_MIPS_loop_begin:
4446 return "DW_AT_MIPS_loop_begin";
4447 case DW_AT_MIPS_tail_loop_begin:
4448 return "DW_AT_MIPS_tail_loop_begin";
4449 case DW_AT_MIPS_epilog_begin:
4450 return "DW_AT_MIPS_epilog_begin";
4451 case DW_AT_MIPS_loop_unroll_factor:
4452 return "DW_AT_MIPS_loop_unroll_factor";
4453 case DW_AT_MIPS_software_pipeline_depth:
4454 return "DW_AT_MIPS_software_pipeline_depth";
4455 case DW_AT_MIPS_linkage_name:
4456 return "DW_AT_MIPS_linkage_name";
4457 case DW_AT_MIPS_stride:
4458 return "DW_AT_MIPS_stride";
4459 case DW_AT_MIPS_abstract_name:
4460 return "DW_AT_MIPS_abstract_name";
4461 case DW_AT_MIPS_clone_origin:
4462 return "DW_AT_MIPS_clone_origin";
4463 case DW_AT_MIPS_has_inlines:
4464 return "DW_AT_MIPS_has_inlines";
4466 case DW_AT_sf_names:
4467 return "DW_AT_sf_names";
4468 case DW_AT_src_info:
4469 return "DW_AT_src_info";
4470 case DW_AT_mac_info:
4471 return "DW_AT_mac_info";
4472 case DW_AT_src_coords:
4473 return "DW_AT_src_coords";
4474 case DW_AT_body_begin:
4475 return "DW_AT_body_begin";
4476 case DW_AT_body_end:
4477 return "DW_AT_body_end";
4478 case DW_AT_GNU_vector:
4479 return "DW_AT_GNU_vector";
4481 case DW_AT_VMS_rtnbeg_pd_address:
4482 return "DW_AT_VMS_rtnbeg_pd_address";
4484 default:
4485 return "DW_AT_<unknown>";
4489 /* Convert a DWARF value form code into its string name. */
4491 static const char *
4492 dwarf_form_name (unsigned int form)
4494 switch (form)
4496 case DW_FORM_addr:
4497 return "DW_FORM_addr";
4498 case DW_FORM_block2:
4499 return "DW_FORM_block2";
4500 case DW_FORM_block4:
4501 return "DW_FORM_block4";
4502 case DW_FORM_data2:
4503 return "DW_FORM_data2";
4504 case DW_FORM_data4:
4505 return "DW_FORM_data4";
4506 case DW_FORM_data8:
4507 return "DW_FORM_data8";
4508 case DW_FORM_string:
4509 return "DW_FORM_string";
4510 case DW_FORM_block:
4511 return "DW_FORM_block";
4512 case DW_FORM_block1:
4513 return "DW_FORM_block1";
4514 case DW_FORM_data1:
4515 return "DW_FORM_data1";
4516 case DW_FORM_flag:
4517 return "DW_FORM_flag";
4518 case DW_FORM_sdata:
4519 return "DW_FORM_sdata";
4520 case DW_FORM_strp:
4521 return "DW_FORM_strp";
4522 case DW_FORM_udata:
4523 return "DW_FORM_udata";
4524 case DW_FORM_ref_addr:
4525 return "DW_FORM_ref_addr";
4526 case DW_FORM_ref1:
4527 return "DW_FORM_ref1";
4528 case DW_FORM_ref2:
4529 return "DW_FORM_ref2";
4530 case DW_FORM_ref4:
4531 return "DW_FORM_ref4";
4532 case DW_FORM_ref8:
4533 return "DW_FORM_ref8";
4534 case DW_FORM_ref_udata:
4535 return "DW_FORM_ref_udata";
4536 case DW_FORM_indirect:
4537 return "DW_FORM_indirect";
4538 default:
4539 return "DW_FORM_<unknown>";
4543 /* Convert a DWARF type code into its string name. */
4545 #if 0
4546 static const char *
4547 dwarf_type_encoding_name (unsigned enc)
4549 switch (enc)
4551 case DW_ATE_address:
4552 return "DW_ATE_address";
4553 case DW_ATE_boolean:
4554 return "DW_ATE_boolean";
4555 case DW_ATE_complex_float:
4556 return "DW_ATE_complex_float";
4557 case DW_ATE_float:
4558 return "DW_ATE_float";
4559 case DW_ATE_signed:
4560 return "DW_ATE_signed";
4561 case DW_ATE_signed_char:
4562 return "DW_ATE_signed_char";
4563 case DW_ATE_unsigned:
4564 return "DW_ATE_unsigned";
4565 case DW_ATE_unsigned_char:
4566 return "DW_ATE_unsigned_char";
4567 default:
4568 return "DW_ATE_<unknown>";
4571 #endif
4573 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4574 instance of an inlined instance of a decl which is local to an inline
4575 function, so we have to trace all of the way back through the origin chain
4576 to find out what sort of node actually served as the original seed for the
4577 given block. */
4579 static tree
4580 decl_ultimate_origin (tree decl)
4582 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4583 nodes in the function to point to themselves; ignore that if
4584 we're trying to output the abstract instance of this function. */
4585 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4586 return NULL_TREE;
4588 #ifdef ENABLE_CHECKING
4589 if (DECL_FROM_INLINE (DECL_ORIGIN (decl)))
4590 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4591 most distant ancestor, this should never happen. */
4592 abort ();
4593 #endif
4595 return DECL_ABSTRACT_ORIGIN (decl);
4598 /* Determine the "ultimate origin" of a block. The block may be an inlined
4599 instance of an inlined instance of a block which is local to an inline
4600 function, so we have to trace all of the way back through the origin chain
4601 to find out what sort of node actually served as the original seed for the
4602 given block. */
4604 static tree
4605 block_ultimate_origin (tree block)
4607 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4609 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4610 nodes in the function to point to themselves; ignore that if
4611 we're trying to output the abstract instance of this function. */
4612 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4613 return NULL_TREE;
4615 if (immediate_origin == NULL_TREE)
4616 return NULL_TREE;
4617 else
4619 tree ret_val;
4620 tree lookahead = immediate_origin;
4624 ret_val = lookahead;
4625 lookahead = (TREE_CODE (ret_val) == BLOCK
4626 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4628 while (lookahead != NULL && lookahead != ret_val);
4630 return ret_val;
4634 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4635 of a virtual function may refer to a base class, so we check the 'this'
4636 parameter. */
4638 static tree
4639 decl_class_context (tree decl)
4641 tree context = NULL_TREE;
4643 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4644 context = DECL_CONTEXT (decl);
4645 else
4646 context = TYPE_MAIN_VARIANT
4647 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4649 if (context && !TYPE_P (context))
4650 context = NULL_TREE;
4652 return context;
4655 /* Add an attribute/value pair to a DIE. We build the lists up in reverse
4656 addition order, and correct that in reverse_all_dies. */
4658 static inline void
4659 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4661 if (die != NULL && attr != NULL)
4663 attr->dw_attr_next = die->die_attr;
4664 die->die_attr = attr;
4668 static inline enum dw_val_class
4669 AT_class (dw_attr_ref a)
4671 return a->dw_attr_val.val_class;
4674 /* Add a flag value attribute to a DIE. */
4676 static inline void
4677 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4679 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4681 attr->dw_attr_next = NULL;
4682 attr->dw_attr = attr_kind;
4683 attr->dw_attr_val.val_class = dw_val_class_flag;
4684 attr->dw_attr_val.v.val_flag = flag;
4685 add_dwarf_attr (die, attr);
4688 static inline unsigned
4689 AT_flag (dw_attr_ref a)
4691 if (a && AT_class (a) == dw_val_class_flag)
4692 return a->dw_attr_val.v.val_flag;
4694 abort ();
4697 /* Add a signed integer attribute value to a DIE. */
4699 static inline void
4700 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4702 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4704 attr->dw_attr_next = NULL;
4705 attr->dw_attr = attr_kind;
4706 attr->dw_attr_val.val_class = dw_val_class_const;
4707 attr->dw_attr_val.v.val_int = int_val;
4708 add_dwarf_attr (die, attr);
4711 static inline HOST_WIDE_INT
4712 AT_int (dw_attr_ref a)
4714 if (a && AT_class (a) == dw_val_class_const)
4715 return a->dw_attr_val.v.val_int;
4717 abort ();
4720 /* Add an unsigned integer attribute value to a DIE. */
4722 static inline void
4723 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4724 unsigned HOST_WIDE_INT unsigned_val)
4726 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4728 attr->dw_attr_next = NULL;
4729 attr->dw_attr = attr_kind;
4730 attr->dw_attr_val.val_class = dw_val_class_unsigned_const;
4731 attr->dw_attr_val.v.val_unsigned = unsigned_val;
4732 add_dwarf_attr (die, attr);
4735 static inline unsigned HOST_WIDE_INT
4736 AT_unsigned (dw_attr_ref a)
4738 if (a && AT_class (a) == dw_val_class_unsigned_const)
4739 return a->dw_attr_val.v.val_unsigned;
4741 abort ();
4744 /* Add an unsigned double integer attribute value to a DIE. */
4746 static inline void
4747 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4748 long unsigned int val_hi, long unsigned int val_low)
4750 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4752 attr->dw_attr_next = NULL;
4753 attr->dw_attr = attr_kind;
4754 attr->dw_attr_val.val_class = dw_val_class_long_long;
4755 attr->dw_attr_val.v.val_long_long.hi = val_hi;
4756 attr->dw_attr_val.v.val_long_long.low = val_low;
4757 add_dwarf_attr (die, attr);
4760 /* Add a floating point attribute value to a DIE and return it. */
4762 static inline void
4763 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4764 unsigned int length, unsigned int elt_size, unsigned char *array)
4766 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4768 attr->dw_attr_next = NULL;
4769 attr->dw_attr = attr_kind;
4770 attr->dw_attr_val.val_class = dw_val_class_vec;
4771 attr->dw_attr_val.v.val_vec.length = length;
4772 attr->dw_attr_val.v.val_vec.elt_size = elt_size;
4773 attr->dw_attr_val.v.val_vec.array = array;
4774 add_dwarf_attr (die, attr);
4777 /* Hash and equality functions for debug_str_hash. */
4779 static hashval_t
4780 debug_str_do_hash (const void *x)
4782 return htab_hash_string (((const struct indirect_string_node *)x)->str);
4785 static int
4786 debug_str_eq (const void *x1, const void *x2)
4788 return strcmp ((((const struct indirect_string_node *)x1)->str),
4789 (const char *)x2) == 0;
4792 /* Add a string attribute value to a DIE. */
4794 static inline void
4795 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4797 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4798 struct indirect_string_node *node;
4799 void **slot;
4801 if (! debug_str_hash)
4802 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4803 debug_str_eq, NULL);
4805 slot = htab_find_slot_with_hash (debug_str_hash, str,
4806 htab_hash_string (str), INSERT);
4807 if (*slot == NULL)
4808 *slot = ggc_alloc_cleared (sizeof (struct indirect_string_node));
4809 node = (struct indirect_string_node *) *slot;
4810 node->str = ggc_strdup (str);
4811 node->refcount++;
4813 attr->dw_attr_next = NULL;
4814 attr->dw_attr = attr_kind;
4815 attr->dw_attr_val.val_class = dw_val_class_str;
4816 attr->dw_attr_val.v.val_str = node;
4817 add_dwarf_attr (die, attr);
4820 static inline const char *
4821 AT_string (dw_attr_ref a)
4823 if (a && AT_class (a) == dw_val_class_str)
4824 return a->dw_attr_val.v.val_str->str;
4826 abort ();
4829 /* Find out whether a string should be output inline in DIE
4830 or out-of-line in .debug_str section. */
4832 static int
4833 AT_string_form (dw_attr_ref a)
4835 if (a && AT_class (a) == dw_val_class_str)
4837 struct indirect_string_node *node;
4838 unsigned int len;
4839 char label[32];
4841 node = a->dw_attr_val.v.val_str;
4842 if (node->form)
4843 return node->form;
4845 len = strlen (node->str) + 1;
4847 /* If the string is shorter or equal to the size of the reference, it is
4848 always better to put it inline. */
4849 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4850 return node->form = DW_FORM_string;
4852 /* If we cannot expect the linker to merge strings in .debug_str
4853 section, only put it into .debug_str if it is worth even in this
4854 single module. */
4855 if ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) == 0
4856 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
4857 return node->form = DW_FORM_string;
4859 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4860 ++dw2_string_counter;
4861 node->label = xstrdup (label);
4863 return node->form = DW_FORM_strp;
4866 abort ();
4869 /* Add a DIE reference attribute value to a DIE. */
4871 static inline void
4872 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4874 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4876 attr->dw_attr_next = NULL;
4877 attr->dw_attr = attr_kind;
4878 attr->dw_attr_val.val_class = dw_val_class_die_ref;
4879 attr->dw_attr_val.v.val_die_ref.die = targ_die;
4880 attr->dw_attr_val.v.val_die_ref.external = 0;
4881 add_dwarf_attr (die, attr);
4884 /* Add an AT_specification attribute to a DIE, and also make the back
4885 pointer from the specification to the definition. */
4887 static inline void
4888 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4890 add_AT_die_ref (die, DW_AT_specification, targ_die);
4891 if (targ_die->die_definition)
4892 abort ();
4893 targ_die->die_definition = die;
4896 static inline dw_die_ref
4897 AT_ref (dw_attr_ref a)
4899 if (a && AT_class (a) == dw_val_class_die_ref)
4900 return a->dw_attr_val.v.val_die_ref.die;
4902 abort ();
4905 static inline int
4906 AT_ref_external (dw_attr_ref a)
4908 if (a && AT_class (a) == dw_val_class_die_ref)
4909 return a->dw_attr_val.v.val_die_ref.external;
4911 return 0;
4914 static inline void
4915 set_AT_ref_external (dw_attr_ref a, int i)
4917 if (a && AT_class (a) == dw_val_class_die_ref)
4918 a->dw_attr_val.v.val_die_ref.external = i;
4919 else
4920 abort ();
4923 /* Add an FDE reference attribute value to a DIE. */
4925 static inline void
4926 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4928 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4930 attr->dw_attr_next = NULL;
4931 attr->dw_attr = attr_kind;
4932 attr->dw_attr_val.val_class = dw_val_class_fde_ref;
4933 attr->dw_attr_val.v.val_fde_index = targ_fde;
4934 add_dwarf_attr (die, attr);
4937 /* Add a location description attribute value to a DIE. */
4939 static inline void
4940 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4942 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4944 attr->dw_attr_next = NULL;
4945 attr->dw_attr = attr_kind;
4946 attr->dw_attr_val.val_class = dw_val_class_loc;
4947 attr->dw_attr_val.v.val_loc = loc;
4948 add_dwarf_attr (die, attr);
4951 static inline dw_loc_descr_ref
4952 AT_loc (dw_attr_ref a)
4954 if (a && AT_class (a) == dw_val_class_loc)
4955 return a->dw_attr_val.v.val_loc;
4957 abort ();
4960 static inline void
4961 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4963 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4965 attr->dw_attr_next = NULL;
4966 attr->dw_attr = attr_kind;
4967 attr->dw_attr_val.val_class = dw_val_class_loc_list;
4968 attr->dw_attr_val.v.val_loc_list = loc_list;
4969 add_dwarf_attr (die, attr);
4970 have_location_lists = 1;
4973 static inline dw_loc_list_ref
4974 AT_loc_list (dw_attr_ref a)
4976 if (a && AT_class (a) == dw_val_class_loc_list)
4977 return a->dw_attr_val.v.val_loc_list;
4979 abort ();
4982 /* Add an address constant attribute value to a DIE. */
4984 static inline void
4985 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
4987 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4989 attr->dw_attr_next = NULL;
4990 attr->dw_attr = attr_kind;
4991 attr->dw_attr_val.val_class = dw_val_class_addr;
4992 attr->dw_attr_val.v.val_addr = addr;
4993 add_dwarf_attr (die, attr);
4996 static inline rtx
4997 AT_addr (dw_attr_ref a)
4999 if (a && AT_class (a) == dw_val_class_addr)
5000 return a->dw_attr_val.v.val_addr;
5002 abort ();
5005 /* Add a label identifier attribute value to a DIE. */
5007 static inline void
5008 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5010 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5012 attr->dw_attr_next = NULL;
5013 attr->dw_attr = attr_kind;
5014 attr->dw_attr_val.val_class = dw_val_class_lbl_id;
5015 attr->dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5016 add_dwarf_attr (die, attr);
5019 /* Add a section offset attribute value to a DIE. */
5021 static inline void
5022 add_AT_lbl_offset (dw_die_ref die, enum dwarf_attribute attr_kind, const char *label)
5024 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5026 attr->dw_attr_next = NULL;
5027 attr->dw_attr = attr_kind;
5028 attr->dw_attr_val.val_class = dw_val_class_lbl_offset;
5029 attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
5030 add_dwarf_attr (die, attr);
5033 /* Add an offset attribute value to a DIE. */
5035 static inline void
5036 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5037 unsigned HOST_WIDE_INT offset)
5039 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5041 attr->dw_attr_next = NULL;
5042 attr->dw_attr = attr_kind;
5043 attr->dw_attr_val.val_class = dw_val_class_offset;
5044 attr->dw_attr_val.v.val_offset = offset;
5045 add_dwarf_attr (die, attr);
5048 /* Add an range_list attribute value to a DIE. */
5050 static void
5051 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5052 long unsigned int offset)
5054 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5056 attr->dw_attr_next = NULL;
5057 attr->dw_attr = attr_kind;
5058 attr->dw_attr_val.val_class = dw_val_class_range_list;
5059 attr->dw_attr_val.v.val_offset = offset;
5060 add_dwarf_attr (die, attr);
5063 static inline const char *
5064 AT_lbl (dw_attr_ref a)
5066 if (a && (AT_class (a) == dw_val_class_lbl_id
5067 || AT_class (a) == dw_val_class_lbl_offset))
5068 return a->dw_attr_val.v.val_lbl_id;
5070 abort ();
5073 /* Get the attribute of type attr_kind. */
5075 static dw_attr_ref
5076 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5078 dw_attr_ref a;
5079 dw_die_ref spec = NULL;
5081 if (die != NULL)
5083 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5084 if (a->dw_attr == attr_kind)
5085 return a;
5086 else if (a->dw_attr == DW_AT_specification
5087 || a->dw_attr == DW_AT_abstract_origin)
5088 spec = AT_ref (a);
5090 if (spec)
5091 return get_AT (spec, attr_kind);
5094 return NULL;
5097 /* Return the "low pc" attribute value, typically associated with a subprogram
5098 DIE. Return null if the "low pc" attribute is either not present, or if it
5099 cannot be represented as an assembler label identifier. */
5101 static inline const char *
5102 get_AT_low_pc (dw_die_ref die)
5104 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5106 return a ? AT_lbl (a) : NULL;
5109 /* Return the "high pc" attribute value, typically associated with a subprogram
5110 DIE. Return null if the "high pc" attribute is either not present, or if it
5111 cannot be represented as an assembler label identifier. */
5113 static inline const char *
5114 get_AT_hi_pc (dw_die_ref die)
5116 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5118 return a ? AT_lbl (a) : NULL;
5121 /* Return the value of the string attribute designated by ATTR_KIND, or
5122 NULL if it is not present. */
5124 static inline const char *
5125 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5127 dw_attr_ref a = get_AT (die, attr_kind);
5129 return a ? AT_string (a) : NULL;
5132 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5133 if it is not present. */
5135 static inline int
5136 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5138 dw_attr_ref a = get_AT (die, attr_kind);
5140 return a ? AT_flag (a) : 0;
5143 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5144 if it is not present. */
5146 static inline unsigned
5147 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5149 dw_attr_ref a = get_AT (die, attr_kind);
5151 return a ? AT_unsigned (a) : 0;
5154 static inline dw_die_ref
5155 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5157 dw_attr_ref a = get_AT (die, attr_kind);
5159 return a ? AT_ref (a) : NULL;
5162 /* Return TRUE if the language is C or C++. */
5164 static inline bool
5165 is_c_family (void)
5167 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5169 return (lang == DW_LANG_C || lang == DW_LANG_C89
5170 || lang == DW_LANG_C_plus_plus);
5173 /* Return TRUE if the language is C++. */
5175 static inline bool
5176 is_cxx (void)
5178 return (get_AT_unsigned (comp_unit_die, DW_AT_language)
5179 == DW_LANG_C_plus_plus);
5182 /* Return TRUE if the language is Fortran. */
5184 static inline bool
5185 is_fortran (void)
5187 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5189 return (lang == DW_LANG_Fortran77
5190 || lang == DW_LANG_Fortran90
5191 || lang == DW_LANG_Fortran95);
5194 /* Return TRUE if the language is Java. */
5196 static inline bool
5197 is_java (void)
5199 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5201 return lang == DW_LANG_Java;
5204 /* Return TRUE if the language is Ada. */
5206 static inline bool
5207 is_ada (void)
5209 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5211 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5214 /* Free up the memory used by A. */
5216 static inline void free_AT (dw_attr_ref);
5217 static inline void
5218 free_AT (dw_attr_ref a)
5220 if (AT_class (a) == dw_val_class_str)
5221 if (a->dw_attr_val.v.val_str->refcount)
5222 a->dw_attr_val.v.val_str->refcount--;
5225 /* Remove the specified attribute if present. */
5227 static void
5228 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5230 dw_attr_ref *p;
5231 dw_attr_ref removed = NULL;
5233 if (die != NULL)
5235 for (p = &(die->die_attr); *p; p = &((*p)->dw_attr_next))
5236 if ((*p)->dw_attr == attr_kind)
5238 removed = *p;
5239 *p = (*p)->dw_attr_next;
5240 break;
5243 if (removed != 0)
5244 free_AT (removed);
5248 /* Remove child die whose die_tag is specified tag. */
5250 static void
5251 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5253 dw_die_ref current, prev, next;
5254 current = die->die_child;
5255 prev = NULL;
5256 while (current != NULL)
5258 if (current->die_tag == tag)
5260 next = current->die_sib;
5261 if (prev == NULL)
5262 die->die_child = next;
5263 else
5264 prev->die_sib = next;
5265 free_die (current);
5266 current = next;
5268 else
5270 prev = current;
5271 current = current->die_sib;
5276 /* Free up the memory used by DIE. */
5278 static inline void
5279 free_die (dw_die_ref die)
5281 remove_children (die);
5284 /* Discard the children of this DIE. */
5286 static void
5287 remove_children (dw_die_ref die)
5289 dw_die_ref child_die = die->die_child;
5291 die->die_child = NULL;
5293 while (child_die != NULL)
5295 dw_die_ref tmp_die = child_die;
5296 dw_attr_ref a;
5298 child_die = child_die->die_sib;
5300 for (a = tmp_die->die_attr; a != NULL;)
5302 dw_attr_ref tmp_a = a;
5304 a = a->dw_attr_next;
5305 free_AT (tmp_a);
5308 free_die (tmp_die);
5312 /* Add a child DIE below its parent. We build the lists up in reverse
5313 addition order, and correct that in reverse_all_dies. */
5315 static inline void
5316 add_child_die (dw_die_ref die, dw_die_ref child_die)
5318 if (die != NULL && child_die != NULL)
5320 if (die == child_die)
5321 abort ();
5323 child_die->die_parent = die;
5324 child_die->die_sib = die->die_child;
5325 die->die_child = child_die;
5329 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5330 is the specification, to the front of PARENT's list of children. */
5332 static void
5333 splice_child_die (dw_die_ref parent, dw_die_ref child)
5335 dw_die_ref *p;
5337 /* We want the declaration DIE from inside the class, not the
5338 specification DIE at toplevel. */
5339 if (child->die_parent != parent)
5341 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5343 if (tmp)
5344 child = tmp;
5347 if (child->die_parent != parent
5348 && child->die_parent != get_AT_ref (parent, DW_AT_specification))
5349 abort ();
5351 for (p = &(child->die_parent->die_child); *p; p = &((*p)->die_sib))
5352 if (*p == child)
5354 *p = child->die_sib;
5355 break;
5358 child->die_parent = parent;
5359 child->die_sib = parent->die_child;
5360 parent->die_child = child;
5363 /* Return a pointer to a newly created DIE node. */
5365 static inline dw_die_ref
5366 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5368 dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5370 die->die_tag = tag_value;
5372 if (parent_die != NULL)
5373 add_child_die (parent_die, die);
5374 else
5376 limbo_die_node *limbo_node;
5378 limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5379 limbo_node->die = die;
5380 limbo_node->created_for = t;
5381 limbo_node->next = limbo_die_list;
5382 limbo_die_list = limbo_node;
5385 return die;
5388 /* Return the DIE associated with the given type specifier. */
5390 static inline dw_die_ref
5391 lookup_type_die (tree type)
5393 return TYPE_SYMTAB_DIE (type);
5396 /* Equate a DIE to a given type specifier. */
5398 static inline void
5399 equate_type_number_to_die (tree type, dw_die_ref type_die)
5401 TYPE_SYMTAB_DIE (type) = type_die;
5404 /* Returns a hash value for X (which really is a die_struct). */
5406 static hashval_t
5407 decl_die_table_hash (const void *x)
5409 return (hashval_t) ((const dw_die_ref) x)->decl_id;
5412 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5414 static int
5415 decl_die_table_eq (const void *x, const void *y)
5417 return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5420 /* Return the DIE associated with a given declaration. */
5422 static inline dw_die_ref
5423 lookup_decl_die (tree decl)
5425 return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5428 /* Returns a hash value for X (which really is a var_loc_list). */
5430 static hashval_t
5431 decl_loc_table_hash (const void *x)
5433 return (hashval_t) ((const var_loc_list *) x)->decl_id;
5436 /* Return nonzero if decl_id of var_loc_list X is the same as
5437 UID of decl *Y. */
5439 static int
5440 decl_loc_table_eq (const void *x, const void *y)
5442 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5445 /* Return the var_loc list associated with a given declaration. */
5447 static inline var_loc_list *
5448 lookup_decl_loc (tree decl)
5450 return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5453 /* Equate a DIE to a particular declaration. */
5455 static void
5456 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5458 unsigned int decl_id = DECL_UID (decl);
5459 void **slot;
5461 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5462 *slot = decl_die;
5463 decl_die->decl_id = decl_id;
5466 /* Add a variable location node to the linked list for DECL. */
5468 static void
5469 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5471 unsigned int decl_id = DECL_UID (decl);
5472 var_loc_list *temp;
5473 void **slot;
5475 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5476 if (*slot == NULL)
5478 temp = ggc_alloc_cleared (sizeof (var_loc_list));
5479 temp->decl_id = decl_id;
5480 *slot = temp;
5482 else
5483 temp = *slot;
5485 if (temp->last)
5487 /* If the current location is the same as the end of the list,
5488 we have nothing to do. */
5489 if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5490 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5492 /* Add LOC to the end of list and update LAST. */
5493 temp->last->next = loc;
5494 temp->last = loc;
5497 /* Do not add empty location to the beginning of the list. */
5498 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5500 temp->first = loc;
5501 temp->last = loc;
5505 /* Keep track of the number of spaces used to indent the
5506 output of the debugging routines that print the structure of
5507 the DIE internal representation. */
5508 static int print_indent;
5510 /* Indent the line the number of spaces given by print_indent. */
5512 static inline void
5513 print_spaces (FILE *outfile)
5515 fprintf (outfile, "%*s", print_indent, "");
5518 /* Print the information associated with a given DIE, and its children.
5519 This routine is a debugging aid only. */
5521 static void
5522 print_die (dw_die_ref die, FILE *outfile)
5524 dw_attr_ref a;
5525 dw_die_ref c;
5527 print_spaces (outfile);
5528 fprintf (outfile, "DIE %4lu: %s\n",
5529 die->die_offset, dwarf_tag_name (die->die_tag));
5530 print_spaces (outfile);
5531 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5532 fprintf (outfile, " offset: %lu\n", die->die_offset);
5534 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5536 print_spaces (outfile);
5537 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5539 switch (AT_class (a))
5541 case dw_val_class_addr:
5542 fprintf (outfile, "address");
5543 break;
5544 case dw_val_class_offset:
5545 fprintf (outfile, "offset");
5546 break;
5547 case dw_val_class_loc:
5548 fprintf (outfile, "location descriptor");
5549 break;
5550 case dw_val_class_loc_list:
5551 fprintf (outfile, "location list -> label:%s",
5552 AT_loc_list (a)->ll_symbol);
5553 break;
5554 case dw_val_class_range_list:
5555 fprintf (outfile, "range list");
5556 break;
5557 case dw_val_class_const:
5558 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5559 break;
5560 case dw_val_class_unsigned_const:
5561 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5562 break;
5563 case dw_val_class_long_long:
5564 fprintf (outfile, "constant (%lu,%lu)",
5565 a->dw_attr_val.v.val_long_long.hi,
5566 a->dw_attr_val.v.val_long_long.low);
5567 break;
5568 case dw_val_class_vec:
5569 fprintf (outfile, "floating-point or vector constant");
5570 break;
5571 case dw_val_class_flag:
5572 fprintf (outfile, "%u", AT_flag (a));
5573 break;
5574 case dw_val_class_die_ref:
5575 if (AT_ref (a) != NULL)
5577 if (AT_ref (a)->die_symbol)
5578 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5579 else
5580 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5582 else
5583 fprintf (outfile, "die -> <null>");
5584 break;
5585 case dw_val_class_lbl_id:
5586 case dw_val_class_lbl_offset:
5587 fprintf (outfile, "label: %s", AT_lbl (a));
5588 break;
5589 case dw_val_class_str:
5590 if (AT_string (a) != NULL)
5591 fprintf (outfile, "\"%s\"", AT_string (a));
5592 else
5593 fprintf (outfile, "<null>");
5594 break;
5595 default:
5596 break;
5599 fprintf (outfile, "\n");
5602 if (die->die_child != NULL)
5604 print_indent += 4;
5605 for (c = die->die_child; c != NULL; c = c->die_sib)
5606 print_die (c, outfile);
5608 print_indent -= 4;
5610 if (print_indent == 0)
5611 fprintf (outfile, "\n");
5614 /* Print the contents of the source code line number correspondence table.
5615 This routine is a debugging aid only. */
5617 static void
5618 print_dwarf_line_table (FILE *outfile)
5620 unsigned i;
5621 dw_line_info_ref line_info;
5623 fprintf (outfile, "\n\nDWARF source line information\n");
5624 for (i = 1; i < line_info_table_in_use; i++)
5626 line_info = &line_info_table[i];
5627 fprintf (outfile, "%5d: ", i);
5628 fprintf (outfile, "%-20s",
5629 VARRAY_CHAR_PTR (file_table, line_info->dw_file_num));
5630 fprintf (outfile, "%6ld", line_info->dw_line_num);
5631 fprintf (outfile, "\n");
5634 fprintf (outfile, "\n\n");
5637 /* Print the information collected for a given DIE. */
5639 void
5640 debug_dwarf_die (dw_die_ref die)
5642 print_die (die, stderr);
5645 /* Print all DWARF information collected for the compilation unit.
5646 This routine is a debugging aid only. */
5648 void
5649 debug_dwarf (void)
5651 print_indent = 0;
5652 print_die (comp_unit_die, stderr);
5653 if (! DWARF2_ASM_LINE_DEBUG_INFO)
5654 print_dwarf_line_table (stderr);
5657 /* We build up the lists of children and attributes by pushing new ones
5658 onto the beginning of the list. Reverse the lists for DIE so that
5659 they are in order of addition. */
5661 static void
5662 reverse_die_lists (dw_die_ref die)
5664 dw_die_ref c, cp, cn;
5665 dw_attr_ref a, ap, an;
5667 for (a = die->die_attr, ap = 0; a; a = an)
5669 an = a->dw_attr_next;
5670 a->dw_attr_next = ap;
5671 ap = a;
5674 die->die_attr = ap;
5676 for (c = die->die_child, cp = 0; c; c = cn)
5678 cn = c->die_sib;
5679 c->die_sib = cp;
5680 cp = c;
5683 die->die_child = cp;
5686 /* reverse_die_lists only reverses the single die you pass it. Since we used to
5687 reverse all dies in add_sibling_attributes, which runs through all the dies,
5688 it would reverse all the dies. Now, however, since we don't call
5689 reverse_die_lists in add_sibling_attributes, we need a routine to
5690 recursively reverse all the dies. This is that routine. */
5692 static void
5693 reverse_all_dies (dw_die_ref die)
5695 dw_die_ref c;
5697 reverse_die_lists (die);
5699 for (c = die->die_child; c; c = c->die_sib)
5700 reverse_all_dies (c);
5703 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5704 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5705 DIE that marks the start of the DIEs for this include file. */
5707 static dw_die_ref
5708 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5710 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5711 dw_die_ref new_unit = gen_compile_unit_die (filename);
5713 new_unit->die_sib = old_unit;
5714 return new_unit;
5717 /* Close an include-file CU and reopen the enclosing one. */
5719 static dw_die_ref
5720 pop_compile_unit (dw_die_ref old_unit)
5722 dw_die_ref new_unit = old_unit->die_sib;
5724 old_unit->die_sib = NULL;
5725 return new_unit;
5728 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5729 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5731 /* Calculate the checksum of a location expression. */
5733 static inline void
5734 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5736 CHECKSUM (loc->dw_loc_opc);
5737 CHECKSUM (loc->dw_loc_oprnd1);
5738 CHECKSUM (loc->dw_loc_oprnd2);
5741 /* Calculate the checksum of an attribute. */
5743 static void
5744 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5746 dw_loc_descr_ref loc;
5747 rtx r;
5749 CHECKSUM (at->dw_attr);
5751 /* We don't care about differences in file numbering. */
5752 if (at->dw_attr == DW_AT_decl_file
5753 /* Or that this was compiled with a different compiler snapshot; if
5754 the output is the same, that's what matters. */
5755 || at->dw_attr == DW_AT_producer)
5756 return;
5758 switch (AT_class (at))
5760 case dw_val_class_const:
5761 CHECKSUM (at->dw_attr_val.v.val_int);
5762 break;
5763 case dw_val_class_unsigned_const:
5764 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5765 break;
5766 case dw_val_class_long_long:
5767 CHECKSUM (at->dw_attr_val.v.val_long_long);
5768 break;
5769 case dw_val_class_vec:
5770 CHECKSUM (at->dw_attr_val.v.val_vec);
5771 break;
5772 case dw_val_class_flag:
5773 CHECKSUM (at->dw_attr_val.v.val_flag);
5774 break;
5775 case dw_val_class_str:
5776 CHECKSUM_STRING (AT_string (at));
5777 break;
5779 case dw_val_class_addr:
5780 r = AT_addr (at);
5781 switch (GET_CODE (r))
5783 case SYMBOL_REF:
5784 CHECKSUM_STRING (XSTR (r, 0));
5785 break;
5787 default:
5788 abort ();
5790 break;
5792 case dw_val_class_offset:
5793 CHECKSUM (at->dw_attr_val.v.val_offset);
5794 break;
5796 case dw_val_class_loc:
5797 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5798 loc_checksum (loc, ctx);
5799 break;
5801 case dw_val_class_die_ref:
5802 die_checksum (AT_ref (at), ctx, mark);
5803 break;
5805 case dw_val_class_fde_ref:
5806 case dw_val_class_lbl_id:
5807 case dw_val_class_lbl_offset:
5808 break;
5810 default:
5811 break;
5815 /* Calculate the checksum of a DIE. */
5817 static void
5818 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5820 dw_die_ref c;
5821 dw_attr_ref a;
5823 /* To avoid infinite recursion. */
5824 if (die->die_mark)
5826 CHECKSUM (die->die_mark);
5827 return;
5829 die->die_mark = ++(*mark);
5831 CHECKSUM (die->die_tag);
5833 for (a = die->die_attr; a; a = a->dw_attr_next)
5834 attr_checksum (a, ctx, mark);
5836 for (c = die->die_child; c; c = c->die_sib)
5837 die_checksum (c, ctx, mark);
5840 #undef CHECKSUM
5841 #undef CHECKSUM_STRING
5843 /* Do the location expressions look same? */
5844 static inline int
5845 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
5847 return loc1->dw_loc_opc == loc2->dw_loc_opc
5848 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
5849 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
5852 /* Do the values look the same? */
5853 static int
5854 same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
5856 dw_loc_descr_ref loc1, loc2;
5857 rtx r1, r2;
5859 if (v1->val_class != v2->val_class)
5860 return 0;
5862 switch (v1->val_class)
5864 case dw_val_class_const:
5865 return v1->v.val_int == v2->v.val_int;
5866 case dw_val_class_unsigned_const:
5867 return v1->v.val_unsigned == v2->v.val_unsigned;
5868 case dw_val_class_long_long:
5869 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
5870 && v1->v.val_long_long.low == v2->v.val_long_long.low;
5871 case dw_val_class_vec:
5872 if (v1->v.val_vec.length != v2->v.val_vec.length
5873 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
5874 return 0;
5875 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
5876 v1->v.val_vec.length * v1->v.val_vec.elt_size))
5877 return 0;
5878 return 1;
5879 case dw_val_class_flag:
5880 return v1->v.val_flag == v2->v.val_flag;
5881 case dw_val_class_str:
5882 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
5884 case dw_val_class_addr:
5885 r1 = v1->v.val_addr;
5886 r2 = v2->v.val_addr;
5887 if (GET_CODE (r1) != GET_CODE (r2))
5888 return 0;
5889 switch (GET_CODE (r1))
5891 case SYMBOL_REF:
5892 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
5894 default:
5895 abort ();
5898 case dw_val_class_offset:
5899 return v1->v.val_offset == v2->v.val_offset;
5901 case dw_val_class_loc:
5902 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
5903 loc1 && loc2;
5904 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
5905 if (!same_loc_p (loc1, loc2, mark))
5906 return 0;
5907 return !loc1 && !loc2;
5909 case dw_val_class_die_ref:
5910 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
5912 case dw_val_class_fde_ref:
5913 case dw_val_class_lbl_id:
5914 case dw_val_class_lbl_offset:
5915 return 1;
5917 default:
5918 return 1;
5922 /* Do the attributes look the same? */
5924 static int
5925 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
5927 if (at1->dw_attr != at2->dw_attr)
5928 return 0;
5930 /* We don't care about differences in file numbering. */
5931 if (at1->dw_attr == DW_AT_decl_file
5932 /* Or that this was compiled with a different compiler snapshot; if
5933 the output is the same, that's what matters. */
5934 || at1->dw_attr == DW_AT_producer)
5935 return 1;
5937 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
5940 /* Do the dies look the same? */
5942 static int
5943 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
5945 dw_die_ref c1, c2;
5946 dw_attr_ref a1, a2;
5948 /* To avoid infinite recursion. */
5949 if (die1->die_mark)
5950 return die1->die_mark == die2->die_mark;
5951 die1->die_mark = die2->die_mark = ++(*mark);
5953 if (die1->die_tag != die2->die_tag)
5954 return 0;
5956 for (a1 = die1->die_attr, a2 = die2->die_attr;
5957 a1 && a2;
5958 a1 = a1->dw_attr_next, a2 = a2->dw_attr_next)
5959 if (!same_attr_p (a1, a2, mark))
5960 return 0;
5961 if (a1 || a2)
5962 return 0;
5964 for (c1 = die1->die_child, c2 = die2->die_child;
5965 c1 && c2;
5966 c1 = c1->die_sib, c2 = c2->die_sib)
5967 if (!same_die_p (c1, c2, mark))
5968 return 0;
5969 if (c1 || c2)
5970 return 0;
5972 return 1;
5975 /* Do the dies look the same? Wrapper around same_die_p. */
5977 static int
5978 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
5980 int mark = 0;
5981 int ret = same_die_p (die1, die2, &mark);
5983 unmark_all_dies (die1);
5984 unmark_all_dies (die2);
5986 return ret;
5989 /* The prefix to attach to symbols on DIEs in the current comdat debug
5990 info section. */
5991 static char *comdat_symbol_id;
5993 /* The index of the current symbol within the current comdat CU. */
5994 static unsigned int comdat_symbol_number;
5996 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
5997 children, and set comdat_symbol_id accordingly. */
5999 static void
6000 compute_section_prefix (dw_die_ref unit_die)
6002 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6003 const char *base = die_name ? lbasename (die_name) : "anonymous";
6004 char *name = alloca (strlen (base) + 64);
6005 char *p;
6006 int i, mark;
6007 unsigned char checksum[16];
6008 struct md5_ctx ctx;
6010 /* Compute the checksum of the DIE, then append part of it as hex digits to
6011 the name filename of the unit. */
6013 md5_init_ctx (&ctx);
6014 mark = 0;
6015 die_checksum (unit_die, &ctx, &mark);
6016 unmark_all_dies (unit_die);
6017 md5_finish_ctx (&ctx, checksum);
6019 sprintf (name, "%s.", base);
6020 clean_symbol_name (name);
6022 p = name + strlen (name);
6023 for (i = 0; i < 4; i++)
6025 sprintf (p, "%.2x", checksum[i]);
6026 p += 2;
6029 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6030 comdat_symbol_number = 0;
6033 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6035 static int
6036 is_type_die (dw_die_ref die)
6038 switch (die->die_tag)
6040 case DW_TAG_array_type:
6041 case DW_TAG_class_type:
6042 case DW_TAG_enumeration_type:
6043 case DW_TAG_pointer_type:
6044 case DW_TAG_reference_type:
6045 case DW_TAG_string_type:
6046 case DW_TAG_structure_type:
6047 case DW_TAG_subroutine_type:
6048 case DW_TAG_union_type:
6049 case DW_TAG_ptr_to_member_type:
6050 case DW_TAG_set_type:
6051 case DW_TAG_subrange_type:
6052 case DW_TAG_base_type:
6053 case DW_TAG_const_type:
6054 case DW_TAG_file_type:
6055 case DW_TAG_packed_type:
6056 case DW_TAG_volatile_type:
6057 case DW_TAG_typedef:
6058 return 1;
6059 default:
6060 return 0;
6064 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6065 Basically, we want to choose the bits that are likely to be shared between
6066 compilations (types) and leave out the bits that are specific to individual
6067 compilations (functions). */
6069 static int
6070 is_comdat_die (dw_die_ref c)
6072 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6073 we do for stabs. The advantage is a greater likelihood of sharing between
6074 objects that don't include headers in the same order (and therefore would
6075 put the base types in a different comdat). jason 8/28/00 */
6077 if (c->die_tag == DW_TAG_base_type)
6078 return 0;
6080 if (c->die_tag == DW_TAG_pointer_type
6081 || c->die_tag == DW_TAG_reference_type
6082 || c->die_tag == DW_TAG_const_type
6083 || c->die_tag == DW_TAG_volatile_type)
6085 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6087 return t ? is_comdat_die (t) : 0;
6090 return is_type_die (c);
6093 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6094 compilation unit. */
6096 static int
6097 is_symbol_die (dw_die_ref c)
6099 return (is_type_die (c)
6100 || (get_AT (c, DW_AT_declaration)
6101 && !get_AT (c, DW_AT_specification)));
6104 static char *
6105 gen_internal_sym (const char *prefix)
6107 char buf[256];
6109 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6110 return xstrdup (buf);
6113 /* Assign symbols to all worthy DIEs under DIE. */
6115 static void
6116 assign_symbol_names (dw_die_ref die)
6118 dw_die_ref c;
6120 if (is_symbol_die (die))
6122 if (comdat_symbol_id)
6124 char *p = alloca (strlen (comdat_symbol_id) + 64);
6126 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6127 comdat_symbol_id, comdat_symbol_number++);
6128 die->die_symbol = xstrdup (p);
6130 else
6131 die->die_symbol = gen_internal_sym ("LDIE");
6134 for (c = die->die_child; c != NULL; c = c->die_sib)
6135 assign_symbol_names (c);
6138 struct cu_hash_table_entry
6140 dw_die_ref cu;
6141 unsigned min_comdat_num, max_comdat_num;
6142 struct cu_hash_table_entry *next;
6145 /* Routines to manipulate hash table of CUs. */
6146 static hashval_t
6147 htab_cu_hash (const void *of)
6149 const struct cu_hash_table_entry *entry = of;
6151 return htab_hash_string (entry->cu->die_symbol);
6154 static int
6155 htab_cu_eq (const void *of1, const void *of2)
6157 const struct cu_hash_table_entry *entry1 = of1;
6158 const struct die_struct *entry2 = of2;
6160 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6163 static void
6164 htab_cu_del (void *what)
6166 struct cu_hash_table_entry *next, *entry = what;
6168 while (entry)
6170 next = entry->next;
6171 free (entry);
6172 entry = next;
6176 /* Check whether we have already seen this CU and set up SYM_NUM
6177 accordingly. */
6178 static int
6179 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6181 struct cu_hash_table_entry dummy;
6182 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6184 dummy.max_comdat_num = 0;
6186 slot = (struct cu_hash_table_entry **)
6187 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6188 INSERT);
6189 entry = *slot;
6191 for (; entry; last = entry, entry = entry->next)
6193 if (same_die_p_wrap (cu, entry->cu))
6194 break;
6197 if (entry)
6199 *sym_num = entry->min_comdat_num;
6200 return 1;
6203 entry = xcalloc (1, sizeof (struct cu_hash_table_entry));
6204 entry->cu = cu;
6205 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6206 entry->next = *slot;
6207 *slot = entry;
6209 return 0;
6212 /* Record SYM_NUM to record of CU in HTABLE. */
6213 static void
6214 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6216 struct cu_hash_table_entry **slot, *entry;
6218 slot = (struct cu_hash_table_entry **)
6219 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6220 NO_INSERT);
6221 entry = *slot;
6223 entry->max_comdat_num = sym_num;
6226 /* Traverse the DIE (which is always comp_unit_die), and set up
6227 additional compilation units for each of the include files we see
6228 bracketed by BINCL/EINCL. */
6230 static void
6231 break_out_includes (dw_die_ref die)
6233 dw_die_ref *ptr;
6234 dw_die_ref unit = NULL;
6235 limbo_die_node *node, **pnode;
6236 htab_t cu_hash_table;
6238 for (ptr = &(die->die_child); *ptr;)
6240 dw_die_ref c = *ptr;
6242 if (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6243 || (unit && is_comdat_die (c)))
6245 /* This DIE is for a secondary CU; remove it from the main one. */
6246 *ptr = c->die_sib;
6248 if (c->die_tag == DW_TAG_GNU_BINCL)
6250 unit = push_new_compile_unit (unit, c);
6251 free_die (c);
6253 else if (c->die_tag == DW_TAG_GNU_EINCL)
6255 unit = pop_compile_unit (unit);
6256 free_die (c);
6258 else
6259 add_child_die (unit, c);
6261 else
6263 /* Leave this DIE in the main CU. */
6264 ptr = &(c->die_sib);
6265 continue;
6269 #if 0
6270 /* We can only use this in debugging, since the frontend doesn't check
6271 to make sure that we leave every include file we enter. */
6272 if (unit != NULL)
6273 abort ();
6274 #endif
6276 assign_symbol_names (die);
6277 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6278 for (node = limbo_die_list, pnode = &limbo_die_list;
6279 node;
6280 node = node->next)
6282 int is_dupl;
6284 compute_section_prefix (node->die);
6285 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6286 &comdat_symbol_number);
6287 assign_symbol_names (node->die);
6288 if (is_dupl)
6289 *pnode = node->next;
6290 else
6292 pnode = &node->next;
6293 record_comdat_symbol_number (node->die, cu_hash_table,
6294 comdat_symbol_number);
6297 htab_delete (cu_hash_table);
6300 /* Traverse the DIE and add a sibling attribute if it may have the
6301 effect of speeding up access to siblings. To save some space,
6302 avoid generating sibling attributes for DIE's without children. */
6304 static void
6305 add_sibling_attributes (dw_die_ref die)
6307 dw_die_ref c;
6309 if (die->die_tag != DW_TAG_compile_unit
6310 && die->die_sib && die->die_child != NULL)
6311 /* Add the sibling link to the front of the attribute list. */
6312 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6314 for (c = die->die_child; c != NULL; c = c->die_sib)
6315 add_sibling_attributes (c);
6318 /* Output all location lists for the DIE and its children. */
6320 static void
6321 output_location_lists (dw_die_ref die)
6323 dw_die_ref c;
6324 dw_attr_ref d_attr;
6326 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6327 if (AT_class (d_attr) == dw_val_class_loc_list)
6328 output_loc_list (AT_loc_list (d_attr));
6330 for (c = die->die_child; c != NULL; c = c->die_sib)
6331 output_location_lists (c);
6335 /* The format of each DIE (and its attribute value pairs) is encoded in an
6336 abbreviation table. This routine builds the abbreviation table and assigns
6337 a unique abbreviation id for each abbreviation entry. The children of each
6338 die are visited recursively. */
6340 static void
6341 build_abbrev_table (dw_die_ref die)
6343 unsigned long abbrev_id;
6344 unsigned int n_alloc;
6345 dw_die_ref c;
6346 dw_attr_ref d_attr, a_attr;
6348 /* Scan the DIE references, and mark as external any that refer to
6349 DIEs from other CUs (i.e. those which are not marked). */
6350 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6351 if (AT_class (d_attr) == dw_val_class_die_ref
6352 && AT_ref (d_attr)->die_mark == 0)
6354 if (AT_ref (d_attr)->die_symbol == 0)
6355 abort ();
6357 set_AT_ref_external (d_attr, 1);
6360 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6362 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6364 if (abbrev->die_tag == die->die_tag)
6366 if ((abbrev->die_child != NULL) == (die->die_child != NULL))
6368 a_attr = abbrev->die_attr;
6369 d_attr = die->die_attr;
6371 while (a_attr != NULL && d_attr != NULL)
6373 if ((a_attr->dw_attr != d_attr->dw_attr)
6374 || (value_format (a_attr) != value_format (d_attr)))
6375 break;
6377 a_attr = a_attr->dw_attr_next;
6378 d_attr = d_attr->dw_attr_next;
6381 if (a_attr == NULL && d_attr == NULL)
6382 break;
6387 if (abbrev_id >= abbrev_die_table_in_use)
6389 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6391 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6392 abbrev_die_table = ggc_realloc (abbrev_die_table,
6393 sizeof (dw_die_ref) * n_alloc);
6395 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6396 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6397 abbrev_die_table_allocated = n_alloc;
6400 ++abbrev_die_table_in_use;
6401 abbrev_die_table[abbrev_id] = die;
6404 die->die_abbrev = abbrev_id;
6405 for (c = die->die_child; c != NULL; c = c->die_sib)
6406 build_abbrev_table (c);
6409 /* Return the power-of-two number of bytes necessary to represent VALUE. */
6411 static int
6412 constant_size (long unsigned int value)
6414 int log;
6416 if (value == 0)
6417 log = 0;
6418 else
6419 log = floor_log2 (value);
6421 log = log / 8;
6422 log = 1 << (floor_log2 (log) + 1);
6424 return log;
6427 /* Return the size of a DIE as it is represented in the
6428 .debug_info section. */
6430 static unsigned long
6431 size_of_die (dw_die_ref die)
6433 unsigned long size = 0;
6434 dw_attr_ref a;
6436 size += size_of_uleb128 (die->die_abbrev);
6437 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6439 switch (AT_class (a))
6441 case dw_val_class_addr:
6442 size += DWARF2_ADDR_SIZE;
6443 break;
6444 case dw_val_class_offset:
6445 size += DWARF_OFFSET_SIZE;
6446 break;
6447 case dw_val_class_loc:
6449 unsigned long lsize = size_of_locs (AT_loc (a));
6451 /* Block length. */
6452 size += constant_size (lsize);
6453 size += lsize;
6455 break;
6456 case dw_val_class_loc_list:
6457 size += DWARF_OFFSET_SIZE;
6458 break;
6459 case dw_val_class_range_list:
6460 size += DWARF_OFFSET_SIZE;
6461 break;
6462 case dw_val_class_const:
6463 size += size_of_sleb128 (AT_int (a));
6464 break;
6465 case dw_val_class_unsigned_const:
6466 size += constant_size (AT_unsigned (a));
6467 break;
6468 case dw_val_class_long_long:
6469 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6470 break;
6471 case dw_val_class_vec:
6472 size += 1 + (a->dw_attr_val.v.val_vec.length
6473 * a->dw_attr_val.v.val_vec.elt_size); /* block */
6474 break;
6475 case dw_val_class_flag:
6476 size += 1;
6477 break;
6478 case dw_val_class_die_ref:
6479 if (AT_ref_external (a))
6480 size += DWARF2_ADDR_SIZE;
6481 else
6482 size += DWARF_OFFSET_SIZE;
6483 break;
6484 case dw_val_class_fde_ref:
6485 size += DWARF_OFFSET_SIZE;
6486 break;
6487 case dw_val_class_lbl_id:
6488 size += DWARF2_ADDR_SIZE;
6489 break;
6490 case dw_val_class_lbl_offset:
6491 size += DWARF_OFFSET_SIZE;
6492 break;
6493 case dw_val_class_str:
6494 if (AT_string_form (a) == DW_FORM_strp)
6495 size += DWARF_OFFSET_SIZE;
6496 else
6497 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6498 break;
6499 default:
6500 abort ();
6504 return size;
6507 /* Size the debugging information associated with a given DIE. Visits the
6508 DIE's children recursively. Updates the global variable next_die_offset, on
6509 each time through. Uses the current value of next_die_offset to update the
6510 die_offset field in each DIE. */
6512 static void
6513 calc_die_sizes (dw_die_ref die)
6515 dw_die_ref c;
6517 die->die_offset = next_die_offset;
6518 next_die_offset += size_of_die (die);
6520 for (c = die->die_child; c != NULL; c = c->die_sib)
6521 calc_die_sizes (c);
6523 if (die->die_child != NULL)
6524 /* Count the null byte used to terminate sibling lists. */
6525 next_die_offset += 1;
6528 /* Set the marks for a die and its children. We do this so
6529 that we know whether or not a reference needs to use FORM_ref_addr; only
6530 DIEs in the same CU will be marked. We used to clear out the offset
6531 and use that as the flag, but ran into ordering problems. */
6533 static void
6534 mark_dies (dw_die_ref die)
6536 dw_die_ref c;
6538 if (die->die_mark)
6539 abort ();
6541 die->die_mark = 1;
6542 for (c = die->die_child; c; c = c->die_sib)
6543 mark_dies (c);
6546 /* Clear the marks for a die and its children. */
6548 static void
6549 unmark_dies (dw_die_ref die)
6551 dw_die_ref c;
6553 if (!die->die_mark)
6554 abort ();
6556 die->die_mark = 0;
6557 for (c = die->die_child; c; c = c->die_sib)
6558 unmark_dies (c);
6561 /* Clear the marks for a die, its children and referred dies. */
6563 static void
6564 unmark_all_dies (dw_die_ref die)
6566 dw_die_ref c;
6567 dw_attr_ref a;
6569 if (!die->die_mark)
6570 return;
6571 die->die_mark = 0;
6573 for (c = die->die_child; c; c = c->die_sib)
6574 unmark_all_dies (c);
6576 for (a = die->die_attr; a; a = a->dw_attr_next)
6577 if (AT_class (a) == dw_val_class_die_ref)
6578 unmark_all_dies (AT_ref (a));
6581 /* Return the size of the .debug_pubnames table generated for the
6582 compilation unit. */
6584 static unsigned long
6585 size_of_pubnames (void)
6587 unsigned long size;
6588 unsigned i;
6590 size = DWARF_PUBNAMES_HEADER_SIZE;
6591 for (i = 0; i < pubname_table_in_use; i++)
6593 pubname_ref p = &pubname_table[i];
6594 size += DWARF_OFFSET_SIZE + strlen (p->name) + 1;
6597 size += DWARF_OFFSET_SIZE;
6598 return size;
6601 /* Return the size of the information in the .debug_aranges section. */
6603 static unsigned long
6604 size_of_aranges (void)
6606 unsigned long size;
6608 size = DWARF_ARANGES_HEADER_SIZE;
6610 /* Count the address/length pair for this compilation unit. */
6611 size += 2 * DWARF2_ADDR_SIZE;
6612 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6614 /* Count the two zero words used to terminated the address range table. */
6615 size += 2 * DWARF2_ADDR_SIZE;
6616 return size;
6619 /* Select the encoding of an attribute value. */
6621 static enum dwarf_form
6622 value_format (dw_attr_ref a)
6624 switch (a->dw_attr_val.val_class)
6626 case dw_val_class_addr:
6627 return DW_FORM_addr;
6628 case dw_val_class_range_list:
6629 case dw_val_class_offset:
6630 if (DWARF_OFFSET_SIZE == 4)
6631 return DW_FORM_data4;
6632 if (DWARF_OFFSET_SIZE == 8)
6633 return DW_FORM_data8;
6634 abort ();
6635 case dw_val_class_loc_list:
6636 /* FIXME: Could be DW_FORM_data8, with a > 32 bit size
6637 .debug_loc section */
6638 return DW_FORM_data4;
6639 case dw_val_class_loc:
6640 switch (constant_size (size_of_locs (AT_loc (a))))
6642 case 1:
6643 return DW_FORM_block1;
6644 case 2:
6645 return DW_FORM_block2;
6646 default:
6647 abort ();
6649 case dw_val_class_const:
6650 return DW_FORM_sdata;
6651 case dw_val_class_unsigned_const:
6652 switch (constant_size (AT_unsigned (a)))
6654 case 1:
6655 return DW_FORM_data1;
6656 case 2:
6657 return DW_FORM_data2;
6658 case 4:
6659 return DW_FORM_data4;
6660 case 8:
6661 return DW_FORM_data8;
6662 default:
6663 abort ();
6665 case dw_val_class_long_long:
6666 return DW_FORM_block1;
6667 case dw_val_class_vec:
6668 return DW_FORM_block1;
6669 case dw_val_class_flag:
6670 return DW_FORM_flag;
6671 case dw_val_class_die_ref:
6672 if (AT_ref_external (a))
6673 return DW_FORM_ref_addr;
6674 else
6675 return DW_FORM_ref;
6676 case dw_val_class_fde_ref:
6677 return DW_FORM_data;
6678 case dw_val_class_lbl_id:
6679 return DW_FORM_addr;
6680 case dw_val_class_lbl_offset:
6681 return DW_FORM_data;
6682 case dw_val_class_str:
6683 return AT_string_form (a);
6685 default:
6686 abort ();
6690 /* Output the encoding of an attribute value. */
6692 static void
6693 output_value_format (dw_attr_ref a)
6695 enum dwarf_form form = value_format (a);
6697 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6700 /* Output the .debug_abbrev section which defines the DIE abbreviation
6701 table. */
6703 static void
6704 output_abbrev_section (void)
6706 unsigned long abbrev_id;
6708 dw_attr_ref a_attr;
6710 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6712 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6714 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6715 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6716 dwarf_tag_name (abbrev->die_tag));
6718 if (abbrev->die_child != NULL)
6719 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6720 else
6721 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6723 for (a_attr = abbrev->die_attr; a_attr != NULL;
6724 a_attr = a_attr->dw_attr_next)
6726 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6727 dwarf_attr_name (a_attr->dw_attr));
6728 output_value_format (a_attr);
6731 dw2_asm_output_data (1, 0, NULL);
6732 dw2_asm_output_data (1, 0, NULL);
6735 /* Terminate the table. */
6736 dw2_asm_output_data (1, 0, NULL);
6739 /* Output a symbol we can use to refer to this DIE from another CU. */
6741 static inline void
6742 output_die_symbol (dw_die_ref die)
6744 char *sym = die->die_symbol;
6746 if (sym == 0)
6747 return;
6749 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6750 /* We make these global, not weak; if the target doesn't support
6751 .linkonce, it doesn't support combining the sections, so debugging
6752 will break. */
6753 targetm.asm_out.globalize_label (asm_out_file, sym);
6755 ASM_OUTPUT_LABEL (asm_out_file, sym);
6758 /* Return a new location list, given the begin and end range, and the
6759 expression. gensym tells us whether to generate a new internal symbol for
6760 this location list node, which is done for the head of the list only. */
6762 static inline dw_loc_list_ref
6763 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6764 const char *section, unsigned int gensym)
6766 dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
6768 retlist->begin = begin;
6769 retlist->end = end;
6770 retlist->expr = expr;
6771 retlist->section = section;
6772 if (gensym)
6773 retlist->ll_symbol = gen_internal_sym ("LLST");
6775 return retlist;
6778 /* Add a location description expression to a location list. */
6780 static inline void
6781 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6782 const char *begin, const char *end,
6783 const char *section)
6785 dw_loc_list_ref *d;
6787 /* Find the end of the chain. */
6788 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
6791 /* Add a new location list node to the list. */
6792 *d = new_loc_list (descr, begin, end, section, 0);
6795 /* Output the location list given to us. */
6797 static void
6798 output_loc_list (dw_loc_list_ref list_head)
6800 dw_loc_list_ref curr = list_head;
6802 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
6804 /* Walk the location list, and output each range + expression. */
6805 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
6807 unsigned long size;
6808 if (separate_line_info_table_in_use == 0)
6810 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
6811 "Location list begin address (%s)",
6812 list_head->ll_symbol);
6813 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
6814 "Location list end address (%s)",
6815 list_head->ll_symbol);
6817 else
6819 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
6820 "Location list begin address (%s)",
6821 list_head->ll_symbol);
6822 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
6823 "Location list end address (%s)",
6824 list_head->ll_symbol);
6826 size = size_of_locs (curr->expr);
6828 /* Output the block length for this list of location operations. */
6829 if (size > 0xffff)
6830 abort ();
6831 dw2_asm_output_data (2, size, "%s", "Location expression size");
6833 output_loc_sequence (curr->expr);
6836 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6837 "Location list terminator begin (%s)",
6838 list_head->ll_symbol);
6839 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6840 "Location list terminator end (%s)",
6841 list_head->ll_symbol);
6844 /* Output the DIE and its attributes. Called recursively to generate
6845 the definitions of each child DIE. */
6847 static void
6848 output_die (dw_die_ref die)
6850 dw_attr_ref a;
6851 dw_die_ref c;
6852 unsigned long size;
6854 /* If someone in another CU might refer to us, set up a symbol for
6855 them to point to. */
6856 if (die->die_symbol)
6857 output_die_symbol (die);
6859 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
6860 die->die_offset, dwarf_tag_name (die->die_tag));
6862 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6864 const char *name = dwarf_attr_name (a->dw_attr);
6866 switch (AT_class (a))
6868 case dw_val_class_addr:
6869 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
6870 break;
6872 case dw_val_class_offset:
6873 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
6874 "%s", name);
6875 break;
6877 case dw_val_class_range_list:
6879 char *p = strchr (ranges_section_label, '\0');
6881 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
6882 a->dw_attr_val.v.val_offset);
6883 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
6884 "%s", name);
6885 *p = '\0';
6887 break;
6889 case dw_val_class_loc:
6890 size = size_of_locs (AT_loc (a));
6892 /* Output the block length for this list of location operations. */
6893 dw2_asm_output_data (constant_size (size), size, "%s", name);
6895 output_loc_sequence (AT_loc (a));
6896 break;
6898 case dw_val_class_const:
6899 /* ??? It would be slightly more efficient to use a scheme like is
6900 used for unsigned constants below, but gdb 4.x does not sign
6901 extend. Gdb 5.x does sign extend. */
6902 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
6903 break;
6905 case dw_val_class_unsigned_const:
6906 dw2_asm_output_data (constant_size (AT_unsigned (a)),
6907 AT_unsigned (a), "%s", name);
6908 break;
6910 case dw_val_class_long_long:
6912 unsigned HOST_WIDE_INT first, second;
6914 dw2_asm_output_data (1,
6915 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6916 "%s", name);
6918 if (WORDS_BIG_ENDIAN)
6920 first = a->dw_attr_val.v.val_long_long.hi;
6921 second = a->dw_attr_val.v.val_long_long.low;
6923 else
6925 first = a->dw_attr_val.v.val_long_long.low;
6926 second = a->dw_attr_val.v.val_long_long.hi;
6929 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6930 first, "long long constant");
6931 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6932 second, NULL);
6934 break;
6936 case dw_val_class_vec:
6938 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
6939 unsigned int len = a->dw_attr_val.v.val_vec.length;
6940 unsigned int i;
6941 unsigned char *p;
6943 dw2_asm_output_data (1, len * elt_size, "%s", name);
6944 if (elt_size > sizeof (HOST_WIDE_INT))
6946 elt_size /= 2;
6947 len *= 2;
6949 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
6950 i < len;
6951 i++, p += elt_size)
6952 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
6953 "fp or vector constant word %u", i);
6954 break;
6957 case dw_val_class_flag:
6958 dw2_asm_output_data (1, AT_flag (a), "%s", name);
6959 break;
6961 case dw_val_class_loc_list:
6963 char *sym = AT_loc_list (a)->ll_symbol;
6965 if (sym == 0)
6966 abort ();
6967 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, "%s", name);
6969 break;
6971 case dw_val_class_die_ref:
6972 if (AT_ref_external (a))
6974 char *sym = AT_ref (a)->die_symbol;
6976 if (sym == 0)
6977 abort ();
6978 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, "%s", name);
6980 else if (AT_ref (a)->die_offset == 0)
6981 abort ();
6982 else
6983 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
6984 "%s", name);
6985 break;
6987 case dw_val_class_fde_ref:
6989 char l1[20];
6991 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
6992 a->dw_attr_val.v.val_fde_index * 2);
6993 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, "%s", name);
6995 break;
6997 case dw_val_class_lbl_id:
6998 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
6999 break;
7001 case dw_val_class_lbl_offset:
7002 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a), "%s", name);
7003 break;
7005 case dw_val_class_str:
7006 if (AT_string_form (a) == DW_FORM_strp)
7007 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7008 a->dw_attr_val.v.val_str->label,
7009 "%s: \"%s\"", name, AT_string (a));
7010 else
7011 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7012 break;
7014 default:
7015 abort ();
7019 for (c = die->die_child; c != NULL; c = c->die_sib)
7020 output_die (c);
7022 /* Add null byte to terminate sibling list. */
7023 if (die->die_child != NULL)
7024 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7025 die->die_offset);
7028 /* Output the compilation unit that appears at the beginning of the
7029 .debug_info section, and precedes the DIE descriptions. */
7031 static void
7032 output_compilation_unit_header (void)
7034 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7035 dw2_asm_output_data (4, 0xffffffff,
7036 "Initial length escape value indicating 64-bit DWARF extension");
7037 dw2_asm_output_data (DWARF_OFFSET_SIZE,
7038 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7039 "Length of Compilation Unit Info");
7040 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7041 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7042 "Offset Into Abbrev. Section");
7043 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7046 /* Output the compilation unit DIE and its children. */
7048 static void
7049 output_comp_unit (dw_die_ref die, int output_if_empty)
7051 const char *secname;
7052 char *oldsym, *tmp;
7054 /* Unless we are outputting main CU, we may throw away empty ones. */
7055 if (!output_if_empty && die->die_child == NULL)
7056 return;
7058 /* Even if there are no children of this DIE, we must output the information
7059 about the compilation unit. Otherwise, on an empty translation unit, we
7060 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
7061 will then complain when examining the file. First mark all the DIEs in
7062 this CU so we know which get local refs. */
7063 mark_dies (die);
7065 build_abbrev_table (die);
7067 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
7068 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7069 calc_die_sizes (die);
7071 oldsym = die->die_symbol;
7072 if (oldsym)
7074 tmp = alloca (strlen (oldsym) + 24);
7076 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7077 secname = tmp;
7078 die->die_symbol = NULL;
7080 else
7081 secname = (const char *) DEBUG_INFO_SECTION;
7083 /* Output debugging information. */
7084 named_section_flags (secname, SECTION_DEBUG);
7085 output_compilation_unit_header ();
7086 output_die (die);
7088 /* Leave the marks on the main CU, so we can check them in
7089 output_pubnames. */
7090 if (oldsym)
7092 unmark_dies (die);
7093 die->die_symbol = oldsym;
7097 /* The DWARF2 pubname for a nested thingy looks like "A::f". The
7098 output of lang_hooks.decl_printable_name for C++ looks like
7099 "A::f(int)". Let's drop the argument list, and maybe the scope. */
7101 static const char *
7102 dwarf2_name (tree decl, int scope)
7104 return lang_hooks.decl_printable_name (decl, scope ? 1 : 0);
7107 /* Add a new entry to .debug_pubnames if appropriate. */
7109 static void
7110 add_pubname (tree decl, dw_die_ref die)
7112 pubname_ref p;
7114 if (! TREE_PUBLIC (decl))
7115 return;
7117 if (pubname_table_in_use == pubname_table_allocated)
7119 pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
7120 pubname_table
7121 = ggc_realloc (pubname_table,
7122 (pubname_table_allocated * sizeof (pubname_entry)));
7123 memset (pubname_table + pubname_table_in_use, 0,
7124 PUBNAME_TABLE_INCREMENT * sizeof (pubname_entry));
7127 p = &pubname_table[pubname_table_in_use++];
7128 p->die = die;
7129 p->name = xstrdup (dwarf2_name (decl, 1));
7132 /* Output the public names table used to speed up access to externally
7133 visible names. For now, only generate entries for externally
7134 visible procedures. */
7136 static void
7137 output_pubnames (void)
7139 unsigned i;
7140 unsigned long pubnames_length = size_of_pubnames ();
7142 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7143 dw2_asm_output_data (4, 0xffffffff,
7144 "Initial length escape value indicating 64-bit DWARF extension");
7145 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7146 "Length of Public Names Info");
7147 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7148 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7149 "Offset of Compilation Unit Info");
7150 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7151 "Compilation Unit Length");
7153 for (i = 0; i < pubname_table_in_use; i++)
7155 pubname_ref pub = &pubname_table[i];
7157 /* We shouldn't see pubnames for DIEs outside of the main CU. */
7158 if (pub->die->die_mark == 0)
7159 abort ();
7161 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7162 "DIE offset");
7164 dw2_asm_output_nstring (pub->name, -1, "external name");
7167 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7170 /* Add a new entry to .debug_aranges if appropriate. */
7172 static void
7173 add_arange (tree decl, dw_die_ref die)
7175 if (! DECL_SECTION_NAME (decl))
7176 return;
7178 if (arange_table_in_use == arange_table_allocated)
7180 arange_table_allocated += ARANGE_TABLE_INCREMENT;
7181 arange_table = ggc_realloc (arange_table,
7182 (arange_table_allocated
7183 * sizeof (dw_die_ref)));
7184 memset (arange_table + arange_table_in_use, 0,
7185 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7188 arange_table[arange_table_in_use++] = die;
7191 /* Output the information that goes into the .debug_aranges table.
7192 Namely, define the beginning and ending address range of the
7193 text section generated for this compilation unit. */
7195 static void
7196 output_aranges (void)
7198 unsigned i;
7199 unsigned long aranges_length = size_of_aranges ();
7201 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7202 dw2_asm_output_data (4, 0xffffffff,
7203 "Initial length escape value indicating 64-bit DWARF extension");
7204 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7205 "Length of Address Ranges Info");
7206 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7207 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7208 "Offset of Compilation Unit Info");
7209 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7210 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7212 /* We need to align to twice the pointer size here. */
7213 if (DWARF_ARANGES_PAD_SIZE)
7215 /* Pad using a 2 byte words so that padding is correct for any
7216 pointer size. */
7217 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7218 2 * DWARF2_ADDR_SIZE);
7219 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7220 dw2_asm_output_data (2, 0, NULL);
7223 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7224 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7225 text_section_label, "Length");
7227 for (i = 0; i < arange_table_in_use; i++)
7229 dw_die_ref die = arange_table[i];
7231 /* We shouldn't see aranges for DIEs outside of the main CU. */
7232 if (die->die_mark == 0)
7233 abort ();
7235 if (die->die_tag == DW_TAG_subprogram)
7237 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7238 "Address");
7239 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7240 get_AT_low_pc (die), "Length");
7242 else
7244 /* A static variable; extract the symbol from DW_AT_location.
7245 Note that this code isn't currently hit, as we only emit
7246 aranges for functions (jason 9/23/99). */
7247 dw_attr_ref a = get_AT (die, DW_AT_location);
7248 dw_loc_descr_ref loc;
7250 if (! a || AT_class (a) != dw_val_class_loc)
7251 abort ();
7253 loc = AT_loc (a);
7254 if (loc->dw_loc_opc != DW_OP_addr)
7255 abort ();
7257 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7258 loc->dw_loc_oprnd1.v.val_addr, "Address");
7259 dw2_asm_output_data (DWARF2_ADDR_SIZE,
7260 get_AT_unsigned (die, DW_AT_byte_size),
7261 "Length");
7265 /* Output the terminator words. */
7266 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7267 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7270 /* Add a new entry to .debug_ranges. Return the offset at which it
7271 was placed. */
7273 static unsigned int
7274 add_ranges (tree block)
7276 unsigned int in_use = ranges_table_in_use;
7278 if (in_use == ranges_table_allocated)
7280 ranges_table_allocated += RANGES_TABLE_INCREMENT;
7281 ranges_table
7282 = ggc_realloc (ranges_table, (ranges_table_allocated
7283 * sizeof (struct dw_ranges_struct)));
7284 memset (ranges_table + ranges_table_in_use, 0,
7285 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7288 ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7289 ranges_table_in_use = in_use + 1;
7291 return in_use * 2 * DWARF2_ADDR_SIZE;
7294 static void
7295 output_ranges (void)
7297 unsigned i;
7298 static const char *const start_fmt = "Offset 0x%x";
7299 const char *fmt = start_fmt;
7301 for (i = 0; i < ranges_table_in_use; i++)
7303 int block_num = ranges_table[i].block_num;
7305 if (block_num)
7307 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7308 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7310 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7311 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7313 /* If all code is in the text section, then the compilation
7314 unit base address defaults to DW_AT_low_pc, which is the
7315 base of the text section. */
7316 if (separate_line_info_table_in_use == 0)
7318 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7319 text_section_label,
7320 fmt, i * 2 * DWARF2_ADDR_SIZE);
7321 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7322 text_section_label, NULL);
7325 /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7326 compilation unit base address to zero, which allows us to
7327 use absolute addresses, and not worry about whether the
7328 target supports cross-section arithmetic. */
7329 else
7331 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7332 fmt, i * 2 * DWARF2_ADDR_SIZE);
7333 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7336 fmt = NULL;
7338 else
7340 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7341 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7342 fmt = start_fmt;
7347 /* Data structure containing information about input files. */
7348 struct file_info
7350 char *path; /* Complete file name. */
7351 char *fname; /* File name part. */
7352 int length; /* Length of entire string. */
7353 int file_idx; /* Index in input file table. */
7354 int dir_idx; /* Index in directory table. */
7357 /* Data structure containing information about directories with source
7358 files. */
7359 struct dir_info
7361 char *path; /* Path including directory name. */
7362 int length; /* Path length. */
7363 int prefix; /* Index of directory entry which is a prefix. */
7364 int count; /* Number of files in this directory. */
7365 int dir_idx; /* Index of directory used as base. */
7366 int used; /* Used in the end? */
7369 /* Callback function for file_info comparison. We sort by looking at
7370 the directories in the path. */
7372 static int
7373 file_info_cmp (const void *p1, const void *p2)
7375 const struct file_info *s1 = p1;
7376 const struct file_info *s2 = p2;
7377 unsigned char *cp1;
7378 unsigned char *cp2;
7380 /* Take care of file names without directories. We need to make sure that
7381 we return consistent values to qsort since some will get confused if
7382 we return the same value when identical operands are passed in opposite
7383 orders. So if neither has a directory, return 0 and otherwise return
7384 1 or -1 depending on which one has the directory. */
7385 if ((s1->path == s1->fname || s2->path == s2->fname))
7386 return (s2->path == s2->fname) - (s1->path == s1->fname);
7388 cp1 = (unsigned char *) s1->path;
7389 cp2 = (unsigned char *) s2->path;
7391 while (1)
7393 ++cp1;
7394 ++cp2;
7395 /* Reached the end of the first path? If so, handle like above. */
7396 if ((cp1 == (unsigned char *) s1->fname)
7397 || (cp2 == (unsigned char *) s2->fname))
7398 return ((cp2 == (unsigned char *) s2->fname)
7399 - (cp1 == (unsigned char *) s1->fname));
7401 /* Character of current path component the same? */
7402 else if (*cp1 != *cp2)
7403 return *cp1 - *cp2;
7407 /* Output the directory table and the file name table. We try to minimize
7408 the total amount of memory needed. A heuristic is used to avoid large
7409 slowdowns with many input files. */
7411 static void
7412 output_file_names (void)
7414 struct file_info *files;
7415 struct dir_info *dirs;
7416 int *saved;
7417 int *savehere;
7418 int *backmap;
7419 size_t ndirs;
7420 int idx_offset;
7421 size_t i;
7422 int idx;
7424 /* Handle the case where file_table is empty. */
7425 if (VARRAY_ACTIVE_SIZE (file_table) <= 1)
7427 dw2_asm_output_data (1, 0, "End directory table");
7428 dw2_asm_output_data (1, 0, "End file name table");
7429 return;
7432 /* Allocate the various arrays we need. */
7433 files = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct file_info));
7434 dirs = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct dir_info));
7436 /* Sort the file names. */
7437 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7439 char *f;
7441 /* Skip all leading "./". */
7442 f = VARRAY_CHAR_PTR (file_table, i);
7443 while (f[0] == '.' && f[1] == '/')
7444 f += 2;
7446 /* Create a new array entry. */
7447 files[i].path = f;
7448 files[i].length = strlen (f);
7449 files[i].file_idx = i;
7451 /* Search for the file name part. */
7452 f = strrchr (f, '/');
7453 files[i].fname = f == NULL ? files[i].path : f + 1;
7456 qsort (files + 1, VARRAY_ACTIVE_SIZE (file_table) - 1,
7457 sizeof (files[0]), file_info_cmp);
7459 /* Find all the different directories used. */
7460 dirs[0].path = files[1].path;
7461 dirs[0].length = files[1].fname - files[1].path;
7462 dirs[0].prefix = -1;
7463 dirs[0].count = 1;
7464 dirs[0].dir_idx = 0;
7465 dirs[0].used = 0;
7466 files[1].dir_idx = 0;
7467 ndirs = 1;
7469 for (i = 2; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7470 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7471 && memcmp (dirs[ndirs - 1].path, files[i].path,
7472 dirs[ndirs - 1].length) == 0)
7474 /* Same directory as last entry. */
7475 files[i].dir_idx = ndirs - 1;
7476 ++dirs[ndirs - 1].count;
7478 else
7480 size_t j;
7482 /* This is a new directory. */
7483 dirs[ndirs].path = files[i].path;
7484 dirs[ndirs].length = files[i].fname - files[i].path;
7485 dirs[ndirs].count = 1;
7486 dirs[ndirs].dir_idx = ndirs;
7487 dirs[ndirs].used = 0;
7488 files[i].dir_idx = ndirs;
7490 /* Search for a prefix. */
7491 dirs[ndirs].prefix = -1;
7492 for (j = 0; j < ndirs; j++)
7493 if (dirs[j].length < dirs[ndirs].length
7494 && dirs[j].length > 1
7495 && (dirs[ndirs].prefix == -1
7496 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7497 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7498 dirs[ndirs].prefix = j;
7500 ++ndirs;
7503 /* Now to the actual work. We have to find a subset of the directories which
7504 allow expressing the file name using references to the directory table
7505 with the least amount of characters. We do not do an exhaustive search
7506 where we would have to check out every combination of every single
7507 possible prefix. Instead we use a heuristic which provides nearly optimal
7508 results in most cases and never is much off. */
7509 saved = alloca (ndirs * sizeof (int));
7510 savehere = alloca (ndirs * sizeof (int));
7512 memset (saved, '\0', ndirs * sizeof (saved[0]));
7513 for (i = 0; i < ndirs; i++)
7515 size_t j;
7516 int total;
7518 /* We can always save some space for the current directory. But this
7519 does not mean it will be enough to justify adding the directory. */
7520 savehere[i] = dirs[i].length;
7521 total = (savehere[i] - saved[i]) * dirs[i].count;
7523 for (j = i + 1; j < ndirs; j++)
7525 savehere[j] = 0;
7526 if (saved[j] < dirs[i].length)
7528 /* Determine whether the dirs[i] path is a prefix of the
7529 dirs[j] path. */
7530 int k;
7532 k = dirs[j].prefix;
7533 while (k != -1 && k != (int) i)
7534 k = dirs[k].prefix;
7536 if (k == (int) i)
7538 /* Yes it is. We can possibly safe some memory but
7539 writing the filenames in dirs[j] relative to
7540 dirs[i]. */
7541 savehere[j] = dirs[i].length;
7542 total += (savehere[j] - saved[j]) * dirs[j].count;
7547 /* Check whether we can safe enough to justify adding the dirs[i]
7548 directory. */
7549 if (total > dirs[i].length + 1)
7551 /* It's worthwhile adding. */
7552 for (j = i; j < ndirs; j++)
7553 if (savehere[j] > 0)
7555 /* Remember how much we saved for this directory so far. */
7556 saved[j] = savehere[j];
7558 /* Remember the prefix directory. */
7559 dirs[j].dir_idx = i;
7564 /* We have to emit them in the order they appear in the file_table array
7565 since the index is used in the debug info generation. To do this
7566 efficiently we generate a back-mapping of the indices first. */
7567 backmap = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (int));
7568 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7570 backmap[files[i].file_idx] = i;
7572 /* Mark this directory as used. */
7573 dirs[dirs[files[i].dir_idx].dir_idx].used = 1;
7576 /* That was it. We are ready to emit the information. First emit the
7577 directory name table. We have to make sure the first actually emitted
7578 directory name has index one; zero is reserved for the current working
7579 directory. Make sure we do not confuse these indices with the one for the
7580 constructed table (even though most of the time they are identical). */
7581 idx = 1;
7582 idx_offset = dirs[0].length > 0 ? 1 : 0;
7583 for (i = 1 - idx_offset; i < ndirs; i++)
7584 if (dirs[i].used != 0)
7586 dirs[i].used = idx++;
7587 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7588 "Directory Entry: 0x%x", dirs[i].used);
7591 dw2_asm_output_data (1, 0, "End directory table");
7593 /* Correct the index for the current working directory entry if it
7594 exists. */
7595 if (idx_offset == 0)
7596 dirs[0].used = 0;
7598 /* Now write all the file names. */
7599 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7601 int file_idx = backmap[i];
7602 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7604 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7605 "File Entry: 0x%lx", (unsigned long) i);
7607 /* Include directory index. */
7608 dw2_asm_output_data_uleb128 (dirs[dir_idx].used, NULL);
7610 /* Modification time. */
7611 dw2_asm_output_data_uleb128 (0, NULL);
7613 /* File length in bytes. */
7614 dw2_asm_output_data_uleb128 (0, NULL);
7617 dw2_asm_output_data (1, 0, "End file name table");
7621 /* Output the source line number correspondence information. This
7622 information goes into the .debug_line section. */
7624 static void
7625 output_line_info (void)
7627 char l1[20], l2[20], p1[20], p2[20];
7628 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7629 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7630 unsigned opc;
7631 unsigned n_op_args;
7632 unsigned long lt_index;
7633 unsigned long current_line;
7634 long line_offset;
7635 long line_delta;
7636 unsigned long current_file;
7637 unsigned long function;
7639 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7640 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7641 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7642 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7644 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7645 dw2_asm_output_data (4, 0xffffffff,
7646 "Initial length escape value indicating 64-bit DWARF extension");
7647 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7648 "Length of Source Line Info");
7649 ASM_OUTPUT_LABEL (asm_out_file, l1);
7651 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7652 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7653 ASM_OUTPUT_LABEL (asm_out_file, p1);
7655 /* Define the architecture-dependent minimum instruction length (in
7656 bytes). In this implementation of DWARF, this field is used for
7657 information purposes only. Since GCC generates assembly language,
7658 we have no a priori knowledge of how many instruction bytes are
7659 generated for each source line, and therefore can use only the
7660 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7661 commands. Accordingly, we fix this as `1', which is "correct
7662 enough" for all architectures, and don't let the target override. */
7663 dw2_asm_output_data (1, 1,
7664 "Minimum Instruction Length");
7666 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7667 "Default is_stmt_start flag");
7668 dw2_asm_output_data (1, DWARF_LINE_BASE,
7669 "Line Base Value (Special Opcodes)");
7670 dw2_asm_output_data (1, DWARF_LINE_RANGE,
7671 "Line Range Value (Special Opcodes)");
7672 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7673 "Special Opcode Base");
7675 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7677 switch (opc)
7679 case DW_LNS_advance_pc:
7680 case DW_LNS_advance_line:
7681 case DW_LNS_set_file:
7682 case DW_LNS_set_column:
7683 case DW_LNS_fixed_advance_pc:
7684 n_op_args = 1;
7685 break;
7686 default:
7687 n_op_args = 0;
7688 break;
7691 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
7692 opc, n_op_args);
7695 /* Write out the information about the files we use. */
7696 output_file_names ();
7697 ASM_OUTPUT_LABEL (asm_out_file, p2);
7699 /* We used to set the address register to the first location in the text
7700 section here, but that didn't accomplish anything since we already
7701 have a line note for the opening brace of the first function. */
7703 /* Generate the line number to PC correspondence table, encoded as
7704 a series of state machine operations. */
7705 current_file = 1;
7706 current_line = 1;
7707 strcpy (prev_line_label, text_section_label);
7708 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
7710 dw_line_info_ref line_info = &line_info_table[lt_index];
7712 #if 0
7713 /* Disable this optimization for now; GDB wants to see two line notes
7714 at the beginning of a function so it can find the end of the
7715 prologue. */
7717 /* Don't emit anything for redundant notes. Just updating the
7718 address doesn't accomplish anything, because we already assume
7719 that anything after the last address is this line. */
7720 if (line_info->dw_line_num == current_line
7721 && line_info->dw_file_num == current_file)
7722 continue;
7723 #endif
7725 /* Emit debug info for the address of the current line.
7727 Unfortunately, we have little choice here currently, and must always
7728 use the most general form. GCC does not know the address delta
7729 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
7730 attributes which will give an upper bound on the address range. We
7731 could perhaps use length attributes to determine when it is safe to
7732 use DW_LNS_fixed_advance_pc. */
7734 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
7735 if (0)
7737 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
7738 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7739 "DW_LNS_fixed_advance_pc");
7740 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7742 else
7744 /* This can handle any delta. This takes
7745 4+DWARF2_ADDR_SIZE bytes. */
7746 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7747 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7748 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7749 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7752 strcpy (prev_line_label, line_label);
7754 /* Emit debug info for the source file of the current line, if
7755 different from the previous line. */
7756 if (line_info->dw_file_num != current_file)
7758 current_file = line_info->dw_file_num;
7759 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7760 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7761 VARRAY_CHAR_PTR (file_table,
7762 current_file));
7765 /* Emit debug info for the current line number, choosing the encoding
7766 that uses the least amount of space. */
7767 if (line_info->dw_line_num != current_line)
7769 line_offset = line_info->dw_line_num - current_line;
7770 line_delta = line_offset - DWARF_LINE_BASE;
7771 current_line = line_info->dw_line_num;
7772 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7773 /* This can handle deltas from -10 to 234, using the current
7774 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
7775 takes 1 byte. */
7776 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7777 "line %lu", current_line);
7778 else
7780 /* This can handle any delta. This takes at least 4 bytes,
7781 depending on the value being encoded. */
7782 dw2_asm_output_data (1, DW_LNS_advance_line,
7783 "advance to line %lu", current_line);
7784 dw2_asm_output_data_sleb128 (line_offset, NULL);
7785 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7788 else
7789 /* We still need to start a new row, so output a copy insn. */
7790 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7793 /* Emit debug info for the address of the end of the function. */
7794 if (0)
7796 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7797 "DW_LNS_fixed_advance_pc");
7798 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
7800 else
7802 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7803 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7804 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7805 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
7808 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7809 dw2_asm_output_data_uleb128 (1, NULL);
7810 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7812 function = 0;
7813 current_file = 1;
7814 current_line = 1;
7815 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
7817 dw_separate_line_info_ref line_info
7818 = &separate_line_info_table[lt_index];
7820 #if 0
7821 /* Don't emit anything for redundant notes. */
7822 if (line_info->dw_line_num == current_line
7823 && line_info->dw_file_num == current_file
7824 && line_info->function == function)
7825 goto cont;
7826 #endif
7828 /* Emit debug info for the address of the current line. If this is
7829 a new function, or the first line of a function, then we need
7830 to handle it differently. */
7831 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
7832 lt_index);
7833 if (function != line_info->function)
7835 function = line_info->function;
7837 /* Set the address register to the first line in the function. */
7838 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7839 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7840 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7841 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7843 else
7845 /* ??? See the DW_LNS_advance_pc comment above. */
7846 if (0)
7848 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7849 "DW_LNS_fixed_advance_pc");
7850 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7852 else
7854 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7855 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7856 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7857 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7861 strcpy (prev_line_label, line_label);
7863 /* Emit debug info for the source file of the current line, if
7864 different from the previous line. */
7865 if (line_info->dw_file_num != current_file)
7867 current_file = line_info->dw_file_num;
7868 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7869 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7870 VARRAY_CHAR_PTR (file_table,
7871 current_file));
7874 /* Emit debug info for the current line number, choosing the encoding
7875 that uses the least amount of space. */
7876 if (line_info->dw_line_num != current_line)
7878 line_offset = line_info->dw_line_num - current_line;
7879 line_delta = line_offset - DWARF_LINE_BASE;
7880 current_line = line_info->dw_line_num;
7881 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7882 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7883 "line %lu", current_line);
7884 else
7886 dw2_asm_output_data (1, DW_LNS_advance_line,
7887 "advance to line %lu", current_line);
7888 dw2_asm_output_data_sleb128 (line_offset, NULL);
7889 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7892 else
7893 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7895 #if 0
7896 cont:
7897 #endif
7899 lt_index++;
7901 /* If we're done with a function, end its sequence. */
7902 if (lt_index == separate_line_info_table_in_use
7903 || separate_line_info_table[lt_index].function != function)
7905 current_file = 1;
7906 current_line = 1;
7908 /* Emit debug info for the address of the end of the function. */
7909 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
7910 if (0)
7912 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7913 "DW_LNS_fixed_advance_pc");
7914 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7916 else
7918 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7919 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7920 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7921 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7924 /* Output the marker for the end of this sequence. */
7925 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7926 dw2_asm_output_data_uleb128 (1, NULL);
7927 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7931 /* Output the marker for the end of the line number info. */
7932 ASM_OUTPUT_LABEL (asm_out_file, l2);
7935 /* Given a pointer to a tree node for some base type, return a pointer to
7936 a DIE that describes the given type.
7938 This routine must only be called for GCC type nodes that correspond to
7939 Dwarf base (fundamental) types. */
7941 static dw_die_ref
7942 base_type_die (tree type)
7944 dw_die_ref base_type_result;
7945 const char *type_name;
7946 enum dwarf_type encoding;
7947 tree name = TYPE_NAME (type);
7949 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
7950 return 0;
7952 if (name)
7954 if (TREE_CODE (name) == TYPE_DECL)
7955 name = DECL_NAME (name);
7957 type_name = IDENTIFIER_POINTER (name);
7959 else
7960 type_name = "__unknown__";
7962 switch (TREE_CODE (type))
7964 case INTEGER_TYPE:
7965 /* Carefully distinguish the C character types, without messing
7966 up if the language is not C. Note that we check only for the names
7967 that contain spaces; other names might occur by coincidence in other
7968 languages. */
7969 if (! (TYPE_PRECISION (type) == CHAR_TYPE_SIZE
7970 && (type == char_type_node
7971 || ! strcmp (type_name, "signed char")
7972 || ! strcmp (type_name, "unsigned char"))))
7974 if (TYPE_UNSIGNED (type))
7975 encoding = DW_ATE_unsigned;
7976 else
7977 encoding = DW_ATE_signed;
7978 break;
7980 /* else fall through. */
7982 case CHAR_TYPE:
7983 /* GNU Pascal/Ada CHAR type. Not used in C. */
7984 if (TYPE_UNSIGNED (type))
7985 encoding = DW_ATE_unsigned_char;
7986 else
7987 encoding = DW_ATE_signed_char;
7988 break;
7990 case REAL_TYPE:
7991 encoding = DW_ATE_float;
7992 break;
7994 /* Dwarf2 doesn't know anything about complex ints, so use
7995 a user defined type for it. */
7996 case COMPLEX_TYPE:
7997 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
7998 encoding = DW_ATE_complex_float;
7999 else
8000 encoding = DW_ATE_lo_user;
8001 break;
8003 case BOOLEAN_TYPE:
8004 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
8005 encoding = DW_ATE_boolean;
8006 break;
8008 default:
8009 /* No other TREE_CODEs are Dwarf fundamental types. */
8010 abort ();
8013 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8014 if (demangle_name_func)
8015 type_name = (*demangle_name_func) (type_name);
8017 add_AT_string (base_type_result, DW_AT_name, type_name);
8018 add_AT_unsigned (base_type_result, DW_AT_byte_size,
8019 int_size_in_bytes (type));
8020 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8022 return base_type_result;
8025 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
8026 the Dwarf "root" type for the given input type. The Dwarf "root" type of
8027 a given type is generally the same as the given type, except that if the
8028 given type is a pointer or reference type, then the root type of the given
8029 type is the root type of the "basis" type for the pointer or reference
8030 type. (This definition of the "root" type is recursive.) Also, the root
8031 type of a `const' qualified type or a `volatile' qualified type is the
8032 root type of the given type without the qualifiers. */
8034 static tree
8035 root_type (tree type)
8037 if (TREE_CODE (type) == ERROR_MARK)
8038 return error_mark_node;
8040 switch (TREE_CODE (type))
8042 case ERROR_MARK:
8043 return error_mark_node;
8045 case POINTER_TYPE:
8046 case REFERENCE_TYPE:
8047 return type_main_variant (root_type (TREE_TYPE (type)));
8049 default:
8050 return type_main_variant (type);
8054 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8055 given input type is a Dwarf "fundamental" type. Otherwise return null. */
8057 static inline int
8058 is_base_type (tree type)
8060 switch (TREE_CODE (type))
8062 case ERROR_MARK:
8063 case VOID_TYPE:
8064 case INTEGER_TYPE:
8065 case REAL_TYPE:
8066 case COMPLEX_TYPE:
8067 case BOOLEAN_TYPE:
8068 case CHAR_TYPE:
8069 return 1;
8071 case SET_TYPE:
8072 case ARRAY_TYPE:
8073 case RECORD_TYPE:
8074 case UNION_TYPE:
8075 case QUAL_UNION_TYPE:
8076 case ENUMERAL_TYPE:
8077 case FUNCTION_TYPE:
8078 case METHOD_TYPE:
8079 case POINTER_TYPE:
8080 case REFERENCE_TYPE:
8081 case FILE_TYPE:
8082 case OFFSET_TYPE:
8083 case LANG_TYPE:
8084 case VECTOR_TYPE:
8085 return 0;
8087 default:
8088 abort ();
8091 return 0;
8094 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8095 node, return the size in bits for the type if it is a constant, or else
8096 return the alignment for the type if the type's size is not constant, or
8097 else return BITS_PER_WORD if the type actually turns out to be an
8098 ERROR_MARK node. */
8100 static inline unsigned HOST_WIDE_INT
8101 simple_type_size_in_bits (tree type)
8103 if (TREE_CODE (type) == ERROR_MARK)
8104 return BITS_PER_WORD;
8105 else if (TYPE_SIZE (type) == NULL_TREE)
8106 return 0;
8107 else if (host_integerp (TYPE_SIZE (type), 1))
8108 return tree_low_cst (TYPE_SIZE (type), 1);
8109 else
8110 return TYPE_ALIGN (type);
8113 /* Return true if the debug information for the given type should be
8114 emitted as a subrange type. */
8116 static inline bool
8117 is_subrange_type (tree type)
8119 tree subtype = TREE_TYPE (type);
8121 /* Subrange types are identified by the fact that they are integer
8122 types, and that they have a subtype which is either an integer type
8123 or an enumeral type. */
8125 if (TREE_CODE (type) != INTEGER_TYPE
8126 || subtype == NULL_TREE)
8127 return false;
8129 if (TREE_CODE (subtype) != INTEGER_TYPE
8130 && TREE_CODE (subtype) != ENUMERAL_TYPE)
8131 return false;
8133 if (TREE_CODE (type) == TREE_CODE (subtype)
8134 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8135 && TYPE_MIN_VALUE (type) != NULL
8136 && TYPE_MIN_VALUE (subtype) != NULL
8137 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8138 && TYPE_MAX_VALUE (type) != NULL
8139 && TYPE_MAX_VALUE (subtype) != NULL
8140 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8142 /* The type and its subtype have the same representation. If in
8143 addition the two types also have the same name, then the given
8144 type is not a subrange type, but rather a plain base type. */
8145 /* FIXME: brobecker/2004-03-22:
8146 Sizetype INTEGER_CSTs nodes are canonicalized. It should
8147 therefore be sufficient to check the TYPE_SIZE node pointers
8148 rather than checking the actual size. Unfortunately, we have
8149 found some cases, such as in the Ada "integer" type, where
8150 this is not the case. Until this problem is solved, we need to
8151 keep checking the actual size. */
8152 tree type_name = TYPE_NAME (type);
8153 tree subtype_name = TYPE_NAME (subtype);
8155 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8156 type_name = DECL_NAME (type_name);
8158 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8159 subtype_name = DECL_NAME (subtype_name);
8161 if (type_name == subtype_name)
8162 return false;
8165 return true;
8168 /* Given a pointer to a tree node for a subrange type, return a pointer
8169 to a DIE that describes the given type. */
8171 static dw_die_ref
8172 subrange_type_die (tree type, dw_die_ref context_die)
8174 dw_die_ref subtype_die;
8175 dw_die_ref subrange_die;
8176 tree name = TYPE_NAME (type);
8177 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8178 tree subtype = TREE_TYPE (type);
8180 if (context_die == NULL)
8181 context_die = comp_unit_die;
8183 if (TREE_CODE (subtype) == ENUMERAL_TYPE)
8184 subtype_die = gen_enumeration_type_die (subtype, context_die);
8185 else
8186 subtype_die = base_type_die (subtype);
8188 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8190 if (name != NULL)
8192 if (TREE_CODE (name) == TYPE_DECL)
8193 name = DECL_NAME (name);
8194 add_name_attribute (subrange_die, IDENTIFIER_POINTER (name));
8197 if (int_size_in_bytes (subtype) != size_in_bytes)
8199 /* The size of the subrange type and its base type do not match,
8200 so we need to generate a size attribute for the subrange type. */
8201 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8204 if (TYPE_MIN_VALUE (type) != NULL)
8205 add_bound_info (subrange_die, DW_AT_lower_bound,
8206 TYPE_MIN_VALUE (type));
8207 if (TYPE_MAX_VALUE (type) != NULL)
8208 add_bound_info (subrange_die, DW_AT_upper_bound,
8209 TYPE_MAX_VALUE (type));
8210 add_AT_die_ref (subrange_die, DW_AT_type, subtype_die);
8212 return subrange_die;
8215 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8216 entry that chains various modifiers in front of the given type. */
8218 static dw_die_ref
8219 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8220 dw_die_ref context_die)
8222 enum tree_code code = TREE_CODE (type);
8223 dw_die_ref mod_type_die = NULL;
8224 dw_die_ref sub_die = NULL;
8225 tree item_type = NULL;
8227 if (code != ERROR_MARK)
8229 tree qualified_type;
8231 /* See if we already have the appropriately qualified variant of
8232 this type. */
8233 qualified_type
8234 = get_qualified_type (type,
8235 ((is_const_type ? TYPE_QUAL_CONST : 0)
8236 | (is_volatile_type
8237 ? TYPE_QUAL_VOLATILE : 0)));
8239 /* If we do, then we can just use its DIE, if it exists. */
8240 if (qualified_type)
8242 mod_type_die = lookup_type_die (qualified_type);
8243 if (mod_type_die)
8244 return mod_type_die;
8247 /* Handle C typedef types. */
8248 if (qualified_type && TYPE_NAME (qualified_type)
8249 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL
8250 && DECL_ORIGINAL_TYPE (TYPE_NAME (qualified_type)))
8252 tree type_name = TYPE_NAME (qualified_type);
8253 tree dtype = TREE_TYPE (type_name);
8255 if (qualified_type == dtype)
8257 /* For a named type, use the typedef. */
8258 gen_type_die (qualified_type, context_die);
8259 mod_type_die = lookup_type_die (qualified_type);
8261 else if (is_const_type < TYPE_READONLY (dtype)
8262 || is_volatile_type < TYPE_VOLATILE (dtype))
8263 /* cv-unqualified version of named type. Just use the unnamed
8264 type to which it refers. */
8265 mod_type_die
8266 = modified_type_die (DECL_ORIGINAL_TYPE (type_name),
8267 is_const_type, is_volatile_type,
8268 context_die);
8270 /* Else cv-qualified version of named type; fall through. */
8273 if (mod_type_die)
8274 /* OK. */
8276 else if (is_const_type)
8278 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8279 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8281 else if (is_volatile_type)
8283 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8284 sub_die = modified_type_die (type, 0, 0, context_die);
8286 else if (code == POINTER_TYPE)
8288 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8289 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8290 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8291 #if 0
8292 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8293 #endif
8294 item_type = TREE_TYPE (type);
8296 else if (code == REFERENCE_TYPE)
8298 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8299 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8300 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8301 #if 0
8302 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8303 #endif
8304 item_type = TREE_TYPE (type);
8306 else if (is_subrange_type (type))
8307 mod_type_die = subrange_type_die (type, context_die);
8308 else if (is_base_type (type))
8309 mod_type_die = base_type_die (type);
8310 else
8312 gen_type_die (type, context_die);
8314 /* We have to get the type_main_variant here (and pass that to the
8315 `lookup_type_die' routine) because the ..._TYPE node we have
8316 might simply be a *copy* of some original type node (where the
8317 copy was created to help us keep track of typedef names) and
8318 that copy might have a different TYPE_UID from the original
8319 ..._TYPE node. */
8320 if (TREE_CODE (type) != VECTOR_TYPE)
8321 mod_type_die = lookup_type_die (type_main_variant (type));
8322 else
8323 /* Vectors have the debugging information in the type,
8324 not the main variant. */
8325 mod_type_die = lookup_type_die (type);
8326 if (mod_type_die == NULL)
8327 abort ();
8330 /* We want to equate the qualified type to the die below. */
8331 type = qualified_type;
8334 if (type)
8335 equate_type_number_to_die (type, mod_type_die);
8336 if (item_type)
8337 /* We must do this after the equate_type_number_to_die call, in case
8338 this is a recursive type. This ensures that the modified_type_die
8339 recursion will terminate even if the type is recursive. Recursive
8340 types are possible in Ada. */
8341 sub_die = modified_type_die (item_type,
8342 TYPE_READONLY (item_type),
8343 TYPE_VOLATILE (item_type),
8344 context_die);
8346 if (sub_die != NULL)
8347 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8349 return mod_type_die;
8352 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8353 an enumerated type. */
8355 static inline int
8356 type_is_enum (tree type)
8358 return TREE_CODE (type) == ENUMERAL_TYPE;
8361 /* Return the DBX register number described by a given RTL node. */
8363 static unsigned int
8364 dbx_reg_number (rtx rtl)
8366 unsigned regno = REGNO (rtl);
8368 if (regno >= FIRST_PSEUDO_REGISTER)
8369 abort ();
8371 return DBX_REGISTER_NUMBER (regno);
8374 /* Return a location descriptor that designates a machine register or
8375 zero if there is none. */
8377 static dw_loc_descr_ref
8378 reg_loc_descriptor (rtx rtl)
8380 unsigned reg;
8381 rtx regs;
8383 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8384 return 0;
8386 reg = dbx_reg_number (rtl);
8387 regs = targetm.dwarf_register_span (rtl);
8389 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1
8390 || regs)
8391 return multiple_reg_loc_descriptor (rtl, regs);
8392 else
8393 return one_reg_loc_descriptor (reg);
8396 /* Return a location descriptor that designates a machine register for
8397 a given hard register number. */
8399 static dw_loc_descr_ref
8400 one_reg_loc_descriptor (unsigned int regno)
8402 if (regno <= 31)
8403 return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8404 else
8405 return new_loc_descr (DW_OP_regx, regno, 0);
8408 /* Given an RTL of a register, return a location descriptor that
8409 designates a value that spans more than one register. */
8411 static dw_loc_descr_ref
8412 multiple_reg_loc_descriptor (rtx rtl, rtx regs)
8414 int nregs, size, i;
8415 unsigned reg;
8416 dw_loc_descr_ref loc_result = NULL;
8418 reg = dbx_reg_number (rtl);
8419 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8421 /* Simple, contiguous registers. */
8422 if (regs == NULL_RTX)
8424 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8426 loc_result = NULL;
8427 while (nregs--)
8429 dw_loc_descr_ref t;
8431 t = one_reg_loc_descriptor (reg);
8432 add_loc_descr (&loc_result, t);
8433 add_loc_descr (&loc_result, new_loc_descr (DW_OP_piece, size, 0));
8434 ++reg;
8436 return loc_result;
8439 /* Now onto stupid register sets in non contiguous locations. */
8441 if (GET_CODE (regs) != PARALLEL)
8442 abort ();
8444 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8445 loc_result = NULL;
8447 for (i = 0; i < XVECLEN (regs, 0); ++i)
8449 dw_loc_descr_ref t;
8451 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8452 add_loc_descr (&loc_result, t);
8453 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8454 add_loc_descr (&loc_result, new_loc_descr (DW_OP_piece, size, 0));
8456 return loc_result;
8459 /* Return a location descriptor that designates a constant. */
8461 static dw_loc_descr_ref
8462 int_loc_descriptor (HOST_WIDE_INT i)
8464 enum dwarf_location_atom op;
8466 /* Pick the smallest representation of a constant, rather than just
8467 defaulting to the LEB encoding. */
8468 if (i >= 0)
8470 if (i <= 31)
8471 op = DW_OP_lit0 + i;
8472 else if (i <= 0xff)
8473 op = DW_OP_const1u;
8474 else if (i <= 0xffff)
8475 op = DW_OP_const2u;
8476 else if (HOST_BITS_PER_WIDE_INT == 32
8477 || i <= 0xffffffff)
8478 op = DW_OP_const4u;
8479 else
8480 op = DW_OP_constu;
8482 else
8484 if (i >= -0x80)
8485 op = DW_OP_const1s;
8486 else if (i >= -0x8000)
8487 op = DW_OP_const2s;
8488 else if (HOST_BITS_PER_WIDE_INT == 32
8489 || i >= -0x80000000)
8490 op = DW_OP_const4s;
8491 else
8492 op = DW_OP_consts;
8495 return new_loc_descr (op, i, 0);
8498 /* Return a location descriptor that designates a base+offset location. */
8500 static dw_loc_descr_ref
8501 based_loc_descr (unsigned int reg, HOST_WIDE_INT offset, bool can_use_fbreg)
8503 dw_loc_descr_ref loc_result;
8504 /* For the "frame base", we use the frame pointer or stack pointer
8505 registers, since the RTL for local variables is relative to one of
8506 them. */
8507 unsigned fp_reg = DBX_REGISTER_NUMBER (frame_pointer_needed
8508 ? HARD_FRAME_POINTER_REGNUM
8509 : STACK_POINTER_REGNUM);
8511 if (reg == fp_reg && can_use_fbreg)
8512 loc_result = new_loc_descr (DW_OP_fbreg, offset, 0);
8513 else if (reg <= 31)
8514 loc_result = new_loc_descr (DW_OP_breg0 + reg, offset, 0);
8515 else
8516 loc_result = new_loc_descr (DW_OP_bregx, reg, offset);
8518 return loc_result;
8521 /* Return true if this RTL expression describes a base+offset calculation. */
8523 static inline int
8524 is_based_loc (rtx rtl)
8526 return (GET_CODE (rtl) == PLUS
8527 && ((REG_P (XEXP (rtl, 0))
8528 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8529 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
8532 /* The following routine converts the RTL for a variable or parameter
8533 (resident in memory) into an equivalent Dwarf representation of a
8534 mechanism for getting the address of that same variable onto the top of a
8535 hypothetical "address evaluation" stack.
8537 When creating memory location descriptors, we are effectively transforming
8538 the RTL for a memory-resident object into its Dwarf postfix expression
8539 equivalent. This routine recursively descends an RTL tree, turning
8540 it into Dwarf postfix code as it goes.
8542 MODE is the mode of the memory reference, needed to handle some
8543 autoincrement addressing modes.
8545 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the location
8546 list for RTL. We can't use it when we are emitting location list for
8547 virtual variable frame_base_decl (i.e. a location list for DW_AT_frame_base)
8548 which describes how frame base changes when !frame_pointer_needed.
8550 Return 0 if we can't represent the location. */
8552 static dw_loc_descr_ref
8553 mem_loc_descriptor (rtx rtl, enum machine_mode mode, bool can_use_fbreg)
8555 dw_loc_descr_ref mem_loc_result = NULL;
8556 enum dwarf_location_atom op;
8558 /* Note that for a dynamically sized array, the location we will generate a
8559 description of here will be the lowest numbered location which is
8560 actually within the array. That's *not* necessarily the same as the
8561 zeroth element of the array. */
8563 rtl = targetm.delegitimize_address (rtl);
8565 switch (GET_CODE (rtl))
8567 case POST_INC:
8568 case POST_DEC:
8569 case POST_MODIFY:
8570 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
8571 just fall into the SUBREG code. */
8573 /* ... fall through ... */
8575 case SUBREG:
8576 /* The case of a subreg may arise when we have a local (register)
8577 variable or a formal (register) parameter which doesn't quite fill
8578 up an entire register. For now, just assume that it is
8579 legitimate to make the Dwarf info refer to the whole register which
8580 contains the given subreg. */
8581 rtl = SUBREG_REG (rtl);
8583 /* ... fall through ... */
8585 case REG:
8586 /* Whenever a register number forms a part of the description of the
8587 method for calculating the (dynamic) address of a memory resident
8588 object, DWARF rules require the register number be referred to as
8589 a "base register". This distinction is not based in any way upon
8590 what category of register the hardware believes the given register
8591 belongs to. This is strictly DWARF terminology we're dealing with
8592 here. Note that in cases where the location of a memory-resident
8593 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8594 OP_CONST (0)) the actual DWARF location descriptor that we generate
8595 may just be OP_BASEREG (basereg). This may look deceptively like
8596 the object in question was allocated to a register (rather than in
8597 memory) so DWARF consumers need to be aware of the subtle
8598 distinction between OP_REG and OP_BASEREG. */
8599 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8600 mem_loc_result = based_loc_descr (dbx_reg_number (rtl), 0,
8601 can_use_fbreg);
8602 break;
8604 case MEM:
8605 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
8606 can_use_fbreg);
8607 if (mem_loc_result != 0)
8608 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
8609 break;
8611 case LO_SUM:
8612 rtl = XEXP (rtl, 1);
8614 /* ... fall through ... */
8616 case LABEL_REF:
8617 /* Some ports can transform a symbol ref into a label ref, because
8618 the symbol ref is too far away and has to be dumped into a constant
8619 pool. */
8620 case CONST:
8621 case SYMBOL_REF:
8622 /* Alternatively, the symbol in the constant pool might be referenced
8623 by a different symbol. */
8624 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
8626 bool marked;
8627 rtx tmp = get_pool_constant_mark (rtl, &marked);
8629 if (GET_CODE (tmp) == SYMBOL_REF)
8631 rtl = tmp;
8632 if (CONSTANT_POOL_ADDRESS_P (tmp))
8633 get_pool_constant_mark (tmp, &marked);
8634 else
8635 marked = true;
8638 /* If all references to this pool constant were optimized away,
8639 it was not output and thus we can't represent it.
8640 FIXME: might try to use DW_OP_const_value here, though
8641 DW_OP_piece complicates it. */
8642 if (!marked)
8643 return 0;
8646 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8647 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
8648 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8649 VARRAY_PUSH_RTX (used_rtx_varray, rtl);
8650 break;
8652 case PRE_MODIFY:
8653 /* Extract the PLUS expression nested inside and fall into
8654 PLUS code below. */
8655 rtl = XEXP (rtl, 1);
8656 goto plus;
8658 case PRE_INC:
8659 case PRE_DEC:
8660 /* Turn these into a PLUS expression and fall into the PLUS code
8661 below. */
8662 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8663 GEN_INT (GET_CODE (rtl) == PRE_INC
8664 ? GET_MODE_UNIT_SIZE (mode)
8665 : -GET_MODE_UNIT_SIZE (mode)));
8667 /* ... fall through ... */
8669 case PLUS:
8670 plus:
8671 if (is_based_loc (rtl))
8672 mem_loc_result = based_loc_descr (dbx_reg_number (XEXP (rtl, 0)),
8673 INTVAL (XEXP (rtl, 1)),
8674 can_use_fbreg);
8675 else
8677 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
8678 can_use_fbreg);
8679 if (mem_loc_result == 0)
8680 break;
8682 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8683 && INTVAL (XEXP (rtl, 1)) >= 0)
8684 add_loc_descr (&mem_loc_result,
8685 new_loc_descr (DW_OP_plus_uconst,
8686 INTVAL (XEXP (rtl, 1)), 0));
8687 else
8689 add_loc_descr (&mem_loc_result,
8690 mem_loc_descriptor (XEXP (rtl, 1), mode,
8691 can_use_fbreg));
8692 add_loc_descr (&mem_loc_result,
8693 new_loc_descr (DW_OP_plus, 0, 0));
8696 break;
8698 /* If a pseudo-reg is optimized away, it is possible for it to
8699 be replaced with a MEM containing a multiply or shift. */
8700 case MULT:
8701 op = DW_OP_mul;
8702 goto do_binop;
8704 case ASHIFT:
8705 op = DW_OP_shl;
8706 goto do_binop;
8708 case ASHIFTRT:
8709 op = DW_OP_shra;
8710 goto do_binop;
8712 case LSHIFTRT:
8713 op = DW_OP_shr;
8714 goto do_binop;
8716 do_binop:
8718 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
8719 can_use_fbreg);
8720 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
8721 can_use_fbreg);
8723 if (op0 == 0 || op1 == 0)
8724 break;
8726 mem_loc_result = op0;
8727 add_loc_descr (&mem_loc_result, op1);
8728 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
8729 break;
8732 case CONST_INT:
8733 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
8734 break;
8736 default:
8737 abort ();
8740 return mem_loc_result;
8743 /* Return a descriptor that describes the concatenation of two locations.
8744 This is typically a complex variable. */
8746 static dw_loc_descr_ref
8747 concat_loc_descriptor (rtx x0, rtx x1)
8749 dw_loc_descr_ref cc_loc_result = NULL;
8750 dw_loc_descr_ref x0_ref = loc_descriptor (x0, true);
8751 dw_loc_descr_ref x1_ref = loc_descriptor (x1, true);
8753 if (x0_ref == 0 || x1_ref == 0)
8754 return 0;
8756 cc_loc_result = x0_ref;
8757 add_loc_descr (&cc_loc_result,
8758 new_loc_descr (DW_OP_piece,
8759 GET_MODE_SIZE (GET_MODE (x0)), 0));
8761 add_loc_descr (&cc_loc_result, x1_ref);
8762 add_loc_descr (&cc_loc_result,
8763 new_loc_descr (DW_OP_piece,
8764 GET_MODE_SIZE (GET_MODE (x1)), 0));
8766 return cc_loc_result;
8769 /* Output a proper Dwarf location descriptor for a variable or parameter
8770 which is either allocated in a register or in a memory location. For a
8771 register, we just generate an OP_REG and the register number. For a
8772 memory location we provide a Dwarf postfix expression describing how to
8773 generate the (dynamic) address of the object onto the address stack.
8775 If we don't know how to describe it, return 0. */
8777 static dw_loc_descr_ref
8778 loc_descriptor (rtx rtl, bool can_use_fbreg)
8780 dw_loc_descr_ref loc_result = NULL;
8782 switch (GET_CODE (rtl))
8784 case SUBREG:
8785 /* The case of a subreg may arise when we have a local (register)
8786 variable or a formal (register) parameter which doesn't quite fill
8787 up an entire register. For now, just assume that it is
8788 legitimate to make the Dwarf info refer to the whole register which
8789 contains the given subreg. */
8790 rtl = SUBREG_REG (rtl);
8792 /* ... fall through ... */
8794 case REG:
8795 loc_result = reg_loc_descriptor (rtl);
8796 break;
8798 case MEM:
8799 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
8800 can_use_fbreg);
8801 break;
8803 case CONCAT:
8804 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
8805 break;
8807 case VAR_LOCATION:
8808 /* Single part. */
8809 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
8811 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), can_use_fbreg);
8812 break;
8815 rtl = XEXP (rtl, 1);
8816 /* FALLTHRU */
8818 case PARALLEL:
8820 rtvec par_elems = XVEC (rtl, 0);
8821 int num_elem = GET_NUM_ELEM (par_elems);
8822 enum machine_mode mode;
8823 int i;
8825 /* Create the first one, so we have something to add to. */
8826 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
8827 can_use_fbreg);
8828 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
8829 add_loc_descr (&loc_result,
8830 new_loc_descr (DW_OP_piece, GET_MODE_SIZE (mode), 0));
8831 for (i = 1; i < num_elem; i++)
8833 dw_loc_descr_ref temp;
8835 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
8836 can_use_fbreg);
8837 add_loc_descr (&loc_result, temp);
8838 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
8839 add_loc_descr (&loc_result,
8840 new_loc_descr (DW_OP_piece,
8841 GET_MODE_SIZE (mode), 0));
8844 break;
8846 default:
8847 abort ();
8850 return loc_result;
8853 /* Similar, but generate the descriptor from trees instead of rtl. This comes
8854 up particularly with variable length arrays. WANT_ADDRESS is 2 if this is
8855 a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
8856 top-level invocation, and we require the address of LOC; is 0 if we require
8857 the value of LOC. */
8859 static dw_loc_descr_ref
8860 loc_descriptor_from_tree_1 (tree loc, int want_address)
8862 dw_loc_descr_ref ret, ret1;
8863 int have_address = 0;
8864 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
8865 enum dwarf_location_atom op;
8867 /* ??? Most of the time we do not take proper care for sign/zero
8868 extending the values properly. Hopefully this won't be a real
8869 problem... */
8871 switch (TREE_CODE (loc))
8873 case ERROR_MARK:
8874 return 0;
8876 case PLACEHOLDER_EXPR:
8877 /* This case involves extracting fields from an object to determine the
8878 position of other fields. We don't try to encode this here. The
8879 only user of this is Ada, which encodes the needed information using
8880 the names of types. */
8881 return 0;
8883 case CALL_EXPR:
8884 return 0;
8886 case PREINCREMENT_EXPR:
8887 case PREDECREMENT_EXPR:
8888 case POSTINCREMENT_EXPR:
8889 case POSTDECREMENT_EXPR:
8890 /* There are no opcodes for these operations. */
8891 return 0;
8893 case ADDR_EXPR:
8894 /* If we already want an address, there's nothing we can do. */
8895 if (want_address)
8896 return 0;
8898 /* Otherwise, process the argument and look for the address. */
8899 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
8901 case VAR_DECL:
8902 if (DECL_THREAD_LOCAL (loc))
8904 rtx rtl;
8906 #ifndef ASM_OUTPUT_DWARF_DTPREL
8907 /* If this is not defined, we have no way to emit the data. */
8908 return 0;
8909 #endif
8911 /* The way DW_OP_GNU_push_tls_address is specified, we can only
8912 look up addresses of objects in the current module. */
8913 if (DECL_EXTERNAL (loc))
8914 return 0;
8916 rtl = rtl_for_decl_location (loc);
8917 if (rtl == NULL_RTX)
8918 return 0;
8920 if (!MEM_P (rtl))
8921 return 0;
8922 rtl = XEXP (rtl, 0);
8923 if (! CONSTANT_P (rtl))
8924 return 0;
8926 ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
8927 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8928 ret->dw_loc_oprnd1.v.val_addr = rtl;
8930 ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
8931 add_loc_descr (&ret, ret1);
8933 have_address = 1;
8934 break;
8936 /* FALLTHRU */
8938 case PARM_DECL:
8939 if (DECL_VALUE_EXPR (loc))
8940 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc), want_address);
8941 /* FALLTHRU */
8943 case RESULT_DECL:
8945 rtx rtl = rtl_for_decl_location (loc);
8947 if (rtl == NULL_RTX)
8948 return 0;
8949 else if (GET_CODE (rtl) == CONST_INT)
8951 HOST_WIDE_INT val = INTVAL (rtl);
8952 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
8953 val &= GET_MODE_MASK (DECL_MODE (loc));
8954 ret = int_loc_descriptor (val);
8956 else if (GET_CODE (rtl) == CONST_STRING)
8957 return 0;
8958 else if (CONSTANT_P (rtl))
8960 ret = new_loc_descr (DW_OP_addr, 0, 0);
8961 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8962 ret->dw_loc_oprnd1.v.val_addr = rtl;
8964 else
8966 enum machine_mode mode;
8968 /* Certain constructs can only be represented at top-level. */
8969 if (want_address == 2)
8970 return loc_descriptor (rtl, true);
8972 mode = GET_MODE (rtl);
8973 if (MEM_P (rtl))
8975 rtl = XEXP (rtl, 0);
8976 have_address = 1;
8978 ret = mem_loc_descriptor (rtl, mode, true);
8981 break;
8983 case INDIRECT_REF:
8984 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
8985 have_address = 1;
8986 break;
8988 case COMPOUND_EXPR:
8989 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
8991 case NOP_EXPR:
8992 case CONVERT_EXPR:
8993 case NON_LVALUE_EXPR:
8994 case VIEW_CONVERT_EXPR:
8995 case SAVE_EXPR:
8996 case MODIFY_EXPR:
8997 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
8999 case COMPONENT_REF:
9000 case BIT_FIELD_REF:
9001 case ARRAY_REF:
9002 case ARRAY_RANGE_REF:
9004 tree obj, offset;
9005 HOST_WIDE_INT bitsize, bitpos, bytepos;
9006 enum machine_mode mode;
9007 int volatilep;
9009 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9010 &unsignedp, &volatilep);
9012 if (obj == loc)
9013 return 0;
9015 ret = loc_descriptor_from_tree_1 (obj, 1);
9016 if (ret == 0
9017 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9018 return 0;
9020 if (offset != NULL_TREE)
9022 /* Variable offset. */
9023 add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9024 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9027 bytepos = bitpos / BITS_PER_UNIT;
9028 if (bytepos > 0)
9029 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9030 else if (bytepos < 0)
9032 add_loc_descr (&ret, int_loc_descriptor (bytepos));
9033 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9036 have_address = 1;
9037 break;
9040 case INTEGER_CST:
9041 if (host_integerp (loc, 0))
9042 ret = int_loc_descriptor (tree_low_cst (loc, 0));
9043 else
9044 return 0;
9045 break;
9047 case CONSTRUCTOR:
9049 /* Get an RTL for this, if something has been emitted. */
9050 rtx rtl = lookup_constant_def (loc);
9051 enum machine_mode mode;
9053 if (!rtl || !MEM_P (rtl))
9054 return 0;
9055 mode = GET_MODE (rtl);
9056 rtl = XEXP (rtl, 0);
9057 ret = mem_loc_descriptor (rtl, mode, true);
9058 have_address = 1;
9059 break;
9062 case TRUTH_AND_EXPR:
9063 case TRUTH_ANDIF_EXPR:
9064 case BIT_AND_EXPR:
9065 op = DW_OP_and;
9066 goto do_binop;
9068 case TRUTH_XOR_EXPR:
9069 case BIT_XOR_EXPR:
9070 op = DW_OP_xor;
9071 goto do_binop;
9073 case TRUTH_OR_EXPR:
9074 case TRUTH_ORIF_EXPR:
9075 case BIT_IOR_EXPR:
9076 op = DW_OP_or;
9077 goto do_binop;
9079 case FLOOR_DIV_EXPR:
9080 case CEIL_DIV_EXPR:
9081 case ROUND_DIV_EXPR:
9082 case TRUNC_DIV_EXPR:
9083 op = DW_OP_div;
9084 goto do_binop;
9086 case MINUS_EXPR:
9087 op = DW_OP_minus;
9088 goto do_binop;
9090 case FLOOR_MOD_EXPR:
9091 case CEIL_MOD_EXPR:
9092 case ROUND_MOD_EXPR:
9093 case TRUNC_MOD_EXPR:
9094 op = DW_OP_mod;
9095 goto do_binop;
9097 case MULT_EXPR:
9098 op = DW_OP_mul;
9099 goto do_binop;
9101 case LSHIFT_EXPR:
9102 op = DW_OP_shl;
9103 goto do_binop;
9105 case RSHIFT_EXPR:
9106 op = (unsignedp ? DW_OP_shr : DW_OP_shra);
9107 goto do_binop;
9109 case PLUS_EXPR:
9110 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9111 && host_integerp (TREE_OPERAND (loc, 1), 0))
9113 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9114 if (ret == 0)
9115 return 0;
9117 add_loc_descr (&ret,
9118 new_loc_descr (DW_OP_plus_uconst,
9119 tree_low_cst (TREE_OPERAND (loc, 1),
9121 0));
9122 break;
9125 op = DW_OP_plus;
9126 goto do_binop;
9128 case LE_EXPR:
9129 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9130 return 0;
9132 op = DW_OP_le;
9133 goto do_binop;
9135 case GE_EXPR:
9136 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9137 return 0;
9139 op = DW_OP_ge;
9140 goto do_binop;
9142 case LT_EXPR:
9143 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9144 return 0;
9146 op = DW_OP_lt;
9147 goto do_binop;
9149 case GT_EXPR:
9150 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9151 return 0;
9153 op = DW_OP_gt;
9154 goto do_binop;
9156 case EQ_EXPR:
9157 op = DW_OP_eq;
9158 goto do_binop;
9160 case NE_EXPR:
9161 op = DW_OP_ne;
9162 goto do_binop;
9164 do_binop:
9165 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9166 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9167 if (ret == 0 || ret1 == 0)
9168 return 0;
9170 add_loc_descr (&ret, ret1);
9171 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9172 break;
9174 case TRUTH_NOT_EXPR:
9175 case BIT_NOT_EXPR:
9176 op = DW_OP_not;
9177 goto do_unop;
9179 case ABS_EXPR:
9180 op = DW_OP_abs;
9181 goto do_unop;
9183 case NEGATE_EXPR:
9184 op = DW_OP_neg;
9185 goto do_unop;
9187 do_unop:
9188 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9189 if (ret == 0)
9190 return 0;
9192 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9193 break;
9195 case MIN_EXPR:
9196 case MAX_EXPR:
9198 const enum tree_code code =
9199 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9201 loc = build3 (COND_EXPR, TREE_TYPE (loc),
9202 build2 (code, integer_type_node,
9203 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9204 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9207 /* ... fall through ... */
9209 case COND_EXPR:
9211 dw_loc_descr_ref lhs
9212 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9213 dw_loc_descr_ref rhs
9214 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9215 dw_loc_descr_ref bra_node, jump_node, tmp;
9217 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9218 if (ret == 0 || lhs == 0 || rhs == 0)
9219 return 0;
9221 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9222 add_loc_descr (&ret, bra_node);
9224 add_loc_descr (&ret, rhs);
9225 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9226 add_loc_descr (&ret, jump_node);
9228 add_loc_descr (&ret, lhs);
9229 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9230 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9232 /* ??? Need a node to point the skip at. Use a nop. */
9233 tmp = new_loc_descr (DW_OP_nop, 0, 0);
9234 add_loc_descr (&ret, tmp);
9235 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9236 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9238 break;
9240 default:
9241 /* Leave front-end specific codes as simply unknown. This comes
9242 up, for instance, with the C STMT_EXPR. */
9243 if ((unsigned int) TREE_CODE (loc)
9244 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9245 return 0;
9247 #ifdef ENABLE_CHECKING
9248 /* Otherwise this is a generic code; we should just lists all of
9249 these explicitly. Aborting means we forgot one. */
9250 abort ();
9251 #else
9252 /* In a release build, we want to degrade gracefully: better to
9253 generate incomplete debugging information than to crash. */
9254 return NULL;
9255 #endif
9258 /* Show if we can't fill the request for an address. */
9259 if (want_address && !have_address)
9260 return 0;
9262 /* If we've got an address and don't want one, dereference. */
9263 if (!want_address && have_address)
9265 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9267 if (size > DWARF2_ADDR_SIZE || size == -1)
9268 return 0;
9269 else if (size == DWARF2_ADDR_SIZE)
9270 op = DW_OP_deref;
9271 else
9272 op = DW_OP_deref_size;
9274 add_loc_descr (&ret, new_loc_descr (op, size, 0));
9277 return ret;
9280 static inline dw_loc_descr_ref
9281 loc_descriptor_from_tree (tree loc)
9283 return loc_descriptor_from_tree_1 (loc, 2);
9286 /* Given a value, round it up to the lowest multiple of `boundary'
9287 which is not less than the value itself. */
9289 static inline HOST_WIDE_INT
9290 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9292 return (((value + boundary - 1) / boundary) * boundary);
9295 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9296 pointer to the declared type for the relevant field variable, or return
9297 `integer_type_node' if the given node turns out to be an
9298 ERROR_MARK node. */
9300 static inline tree
9301 field_type (tree decl)
9303 tree type;
9305 if (TREE_CODE (decl) == ERROR_MARK)
9306 return integer_type_node;
9308 type = DECL_BIT_FIELD_TYPE (decl);
9309 if (type == NULL_TREE)
9310 type = TREE_TYPE (decl);
9312 return type;
9315 /* Given a pointer to a tree node, return the alignment in bits for
9316 it, or else return BITS_PER_WORD if the node actually turns out to
9317 be an ERROR_MARK node. */
9319 static inline unsigned
9320 simple_type_align_in_bits (tree type)
9322 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9325 static inline unsigned
9326 simple_decl_align_in_bits (tree decl)
9328 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9331 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9332 lowest addressed byte of the "containing object" for the given FIELD_DECL,
9333 or return 0 if we are unable to determine what that offset is, either
9334 because the argument turns out to be a pointer to an ERROR_MARK node, or
9335 because the offset is actually variable. (We can't handle the latter case
9336 just yet). */
9338 static HOST_WIDE_INT
9339 field_byte_offset (tree decl)
9341 unsigned int type_align_in_bits;
9342 unsigned int decl_align_in_bits;
9343 unsigned HOST_WIDE_INT type_size_in_bits;
9344 HOST_WIDE_INT object_offset_in_bits;
9345 tree type;
9346 tree field_size_tree;
9347 HOST_WIDE_INT bitpos_int;
9348 HOST_WIDE_INT deepest_bitpos;
9349 unsigned HOST_WIDE_INT field_size_in_bits;
9351 if (TREE_CODE (decl) == ERROR_MARK)
9352 return 0;
9353 else if (TREE_CODE (decl) != FIELD_DECL)
9354 abort ();
9356 type = field_type (decl);
9357 field_size_tree = DECL_SIZE (decl);
9359 /* The size could be unspecified if there was an error, or for
9360 a flexible array member. */
9361 if (! field_size_tree)
9362 field_size_tree = bitsize_zero_node;
9364 /* We cannot yet cope with fields whose positions are variable, so
9365 for now, when we see such things, we simply return 0. Someday, we may
9366 be able to handle such cases, but it will be damn difficult. */
9367 if (! host_integerp (bit_position (decl), 0))
9368 return 0;
9370 bitpos_int = int_bit_position (decl);
9372 /* If we don't know the size of the field, pretend it's a full word. */
9373 if (host_integerp (field_size_tree, 1))
9374 field_size_in_bits = tree_low_cst (field_size_tree, 1);
9375 else
9376 field_size_in_bits = BITS_PER_WORD;
9378 type_size_in_bits = simple_type_size_in_bits (type);
9379 type_align_in_bits = simple_type_align_in_bits (type);
9380 decl_align_in_bits = simple_decl_align_in_bits (decl);
9382 /* The GCC front-end doesn't make any attempt to keep track of the starting
9383 bit offset (relative to the start of the containing structure type) of the
9384 hypothetical "containing object" for a bit-field. Thus, when computing
9385 the byte offset value for the start of the "containing object" of a
9386 bit-field, we must deduce this information on our own. This can be rather
9387 tricky to do in some cases. For example, handling the following structure
9388 type definition when compiling for an i386/i486 target (which only aligns
9389 long long's to 32-bit boundaries) can be very tricky:
9391 struct S { int field1; long long field2:31; };
9393 Fortunately, there is a simple rule-of-thumb which can be used in such
9394 cases. When compiling for an i386/i486, GCC will allocate 8 bytes for the
9395 structure shown above. It decides to do this based upon one simple rule
9396 for bit-field allocation. GCC allocates each "containing object" for each
9397 bit-field at the first (i.e. lowest addressed) legitimate alignment
9398 boundary (based upon the required minimum alignment for the declared type
9399 of the field) which it can possibly use, subject to the condition that
9400 there is still enough available space remaining in the containing object
9401 (when allocated at the selected point) to fully accommodate all of the
9402 bits of the bit-field itself.
9404 This simple rule makes it obvious why GCC allocates 8 bytes for each
9405 object of the structure type shown above. When looking for a place to
9406 allocate the "containing object" for `field2', the compiler simply tries
9407 to allocate a 64-bit "containing object" at each successive 32-bit
9408 boundary (starting at zero) until it finds a place to allocate that 64-
9409 bit field such that at least 31 contiguous (and previously unallocated)
9410 bits remain within that selected 64 bit field. (As it turns out, for the
9411 example above, the compiler finds it is OK to allocate the "containing
9412 object" 64-bit field at bit-offset zero within the structure type.)
9414 Here we attempt to work backwards from the limited set of facts we're
9415 given, and we try to deduce from those facts, where GCC must have believed
9416 that the containing object started (within the structure type). The value
9417 we deduce is then used (by the callers of this routine) to generate
9418 DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
9419 and, in the case of DW_AT_location, regular fields as well). */
9421 /* Figure out the bit-distance from the start of the structure to the
9422 "deepest" bit of the bit-field. */
9423 deepest_bitpos = bitpos_int + field_size_in_bits;
9425 /* This is the tricky part. Use some fancy footwork to deduce where the
9426 lowest addressed bit of the containing object must be. */
9427 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9429 /* Round up to type_align by default. This works best for bitfields. */
9430 object_offset_in_bits += type_align_in_bits - 1;
9431 object_offset_in_bits /= type_align_in_bits;
9432 object_offset_in_bits *= type_align_in_bits;
9434 if (object_offset_in_bits > bitpos_int)
9436 /* Sigh, the decl must be packed. */
9437 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9439 /* Round up to decl_align instead. */
9440 object_offset_in_bits += decl_align_in_bits - 1;
9441 object_offset_in_bits /= decl_align_in_bits;
9442 object_offset_in_bits *= decl_align_in_bits;
9445 return object_offset_in_bits / BITS_PER_UNIT;
9448 /* The following routines define various Dwarf attributes and any data
9449 associated with them. */
9451 /* Add a location description attribute value to a DIE.
9453 This emits location attributes suitable for whole variables and
9454 whole parameters. Note that the location attributes for struct fields are
9455 generated by the routine `data_member_location_attribute' below. */
9457 static inline void
9458 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9459 dw_loc_descr_ref descr)
9461 if (descr != 0)
9462 add_AT_loc (die, attr_kind, descr);
9465 /* Attach the specialized form of location attribute used for data members of
9466 struct and union types. In the special case of a FIELD_DECL node which
9467 represents a bit-field, the "offset" part of this special location
9468 descriptor must indicate the distance in bytes from the lowest-addressed
9469 byte of the containing struct or union type to the lowest-addressed byte of
9470 the "containing object" for the bit-field. (See the `field_byte_offset'
9471 function above).
9473 For any given bit-field, the "containing object" is a hypothetical object
9474 (of some integral or enum type) within which the given bit-field lives. The
9475 type of this hypothetical "containing object" is always the same as the
9476 declared type of the individual bit-field itself (for GCC anyway... the
9477 DWARF spec doesn't actually mandate this). Note that it is the size (in
9478 bytes) of the hypothetical "containing object" which will be given in the
9479 DW_AT_byte_size attribute for this bit-field. (See the
9480 `byte_size_attribute' function below.) It is also used when calculating the
9481 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
9482 function below.) */
9484 static void
9485 add_data_member_location_attribute (dw_die_ref die, tree decl)
9487 HOST_WIDE_INT offset;
9488 dw_loc_descr_ref loc_descr = 0;
9490 if (TREE_CODE (decl) == TREE_BINFO)
9492 /* We're working on the TAG_inheritance for a base class. */
9493 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
9495 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9496 aren't at a fixed offset from all (sub)objects of the same
9497 type. We need to extract the appropriate offset from our
9498 vtable. The following dwarf expression means
9500 BaseAddr = ObAddr + *((*ObAddr) - Offset)
9502 This is specific to the V3 ABI, of course. */
9504 dw_loc_descr_ref tmp;
9506 /* Make a copy of the object address. */
9507 tmp = new_loc_descr (DW_OP_dup, 0, 0);
9508 add_loc_descr (&loc_descr, tmp);
9510 /* Extract the vtable address. */
9511 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9512 add_loc_descr (&loc_descr, tmp);
9514 /* Calculate the address of the offset. */
9515 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9516 if (offset >= 0)
9517 abort ();
9519 tmp = int_loc_descriptor (-offset);
9520 add_loc_descr (&loc_descr, tmp);
9521 tmp = new_loc_descr (DW_OP_minus, 0, 0);
9522 add_loc_descr (&loc_descr, tmp);
9524 /* Extract the offset. */
9525 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9526 add_loc_descr (&loc_descr, tmp);
9528 /* Add it to the object address. */
9529 tmp = new_loc_descr (DW_OP_plus, 0, 0);
9530 add_loc_descr (&loc_descr, tmp);
9532 else
9533 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9535 else
9536 offset = field_byte_offset (decl);
9538 if (! loc_descr)
9540 enum dwarf_location_atom op;
9542 /* The DWARF2 standard says that we should assume that the structure
9543 address is already on the stack, so we can specify a structure field
9544 address by using DW_OP_plus_uconst. */
9546 #ifdef MIPS_DEBUGGING_INFO
9547 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9548 operator correctly. It works only if we leave the offset on the
9549 stack. */
9550 op = DW_OP_constu;
9551 #else
9552 op = DW_OP_plus_uconst;
9553 #endif
9555 loc_descr = new_loc_descr (op, offset, 0);
9558 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9561 /* Writes integer values to dw_vec_const array. */
9563 static void
9564 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9566 while (size != 0)
9568 *dest++ = val & 0xff;
9569 val >>= 8;
9570 --size;
9574 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
9576 static HOST_WIDE_INT
9577 extract_int (const unsigned char *src, unsigned int size)
9579 HOST_WIDE_INT val = 0;
9581 src += size;
9582 while (size != 0)
9584 val <<= 8;
9585 val |= *--src & 0xff;
9586 --size;
9588 return val;
9591 /* Writes floating point values to dw_vec_const array. */
9593 static void
9594 insert_float (rtx rtl, unsigned char *array)
9596 REAL_VALUE_TYPE rv;
9597 long val[4];
9598 int i;
9600 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9601 real_to_target (val, &rv, GET_MODE (rtl));
9603 /* real_to_target puts 32-bit pieces in each long. Pack them. */
9604 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9606 insert_int (val[i], 4, array);
9607 array += 4;
9611 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
9612 does not have a "location" either in memory or in a register. These
9613 things can arise in GNU C when a constant is passed as an actual parameter
9614 to an inlined function. They can also arise in C++ where declared
9615 constants do not necessarily get memory "homes". */
9617 static void
9618 add_const_value_attribute (dw_die_ref die, rtx rtl)
9620 switch (GET_CODE (rtl))
9622 case CONST_INT:
9624 HOST_WIDE_INT val = INTVAL (rtl);
9626 if (val < 0)
9627 add_AT_int (die, DW_AT_const_value, val);
9628 else
9629 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
9631 break;
9633 case CONST_DOUBLE:
9634 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9635 floating-point constant. A CONST_DOUBLE is used whenever the
9636 constant requires more than one word in order to be adequately
9637 represented. We output CONST_DOUBLEs as blocks. */
9639 enum machine_mode mode = GET_MODE (rtl);
9641 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
9643 unsigned int length = GET_MODE_SIZE (mode);
9644 unsigned char *array = ggc_alloc (length);
9646 insert_float (rtl, array);
9647 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
9649 else
9651 /* ??? We really should be using HOST_WIDE_INT throughout. */
9652 if (HOST_BITS_PER_LONG != HOST_BITS_PER_WIDE_INT)
9653 abort ();
9655 add_AT_long_long (die, DW_AT_const_value,
9656 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
9659 break;
9661 case CONST_VECTOR:
9663 enum machine_mode mode = GET_MODE (rtl);
9664 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9665 unsigned int length = CONST_VECTOR_NUNITS (rtl);
9666 unsigned char *array = ggc_alloc (length * elt_size);
9667 unsigned int i;
9668 unsigned char *p;
9670 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
9672 for (i = 0, p = array; i < length; i++, p += elt_size)
9674 rtx elt = CONST_VECTOR_ELT (rtl, i);
9675 HOST_WIDE_INT lo, hi;
9676 if (GET_CODE (elt) == CONST_INT)
9678 lo = INTVAL (elt);
9679 hi = -(lo < 0);
9681 else if (GET_CODE (elt) == CONST_DOUBLE)
9683 lo = CONST_DOUBLE_LOW (elt);
9684 hi = CONST_DOUBLE_HIGH (elt);
9686 else
9687 abort ();
9689 if (elt_size <= sizeof (HOST_WIDE_INT))
9690 insert_int (lo, elt_size, p);
9691 else if (elt_size == 2 * sizeof (HOST_WIDE_INT))
9693 unsigned char *p0 = p;
9694 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9696 if (WORDS_BIG_ENDIAN)
9698 p0 = p1;
9699 p1 = p;
9701 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9702 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9704 else
9705 abort ();
9708 else if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
9710 for (i = 0, p = array; i < length; i++, p += elt_size)
9712 rtx elt = CONST_VECTOR_ELT (rtl, i);
9713 insert_float (elt, p);
9716 else
9717 abort ();
9719 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
9721 break;
9723 case CONST_STRING:
9724 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
9725 break;
9727 case SYMBOL_REF:
9728 case LABEL_REF:
9729 case CONST:
9730 add_AT_addr (die, DW_AT_const_value, rtl);
9731 VARRAY_PUSH_RTX (used_rtx_varray, rtl);
9732 break;
9734 case PLUS:
9735 /* In cases where an inlined instance of an inline function is passed
9736 the address of an `auto' variable (which is local to the caller) we
9737 can get a situation where the DECL_RTL of the artificial local
9738 variable (for the inlining) which acts as a stand-in for the
9739 corresponding formal parameter (of the inline function) will look
9740 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
9741 exactly a compile-time constant expression, but it isn't the address
9742 of the (artificial) local variable either. Rather, it represents the
9743 *value* which the artificial local variable always has during its
9744 lifetime. We currently have no way to represent such quasi-constant
9745 values in Dwarf, so for now we just punt and generate nothing. */
9746 break;
9748 default:
9749 /* No other kinds of rtx should be possible here. */
9750 abort ();
9755 static rtx
9756 rtl_for_decl_location (tree decl)
9758 rtx rtl;
9760 /* Here we have to decide where we are going to say the parameter "lives"
9761 (as far as the debugger is concerned). We only have a couple of
9762 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
9764 DECL_RTL normally indicates where the parameter lives during most of the
9765 activation of the function. If optimization is enabled however, this
9766 could be either NULL or else a pseudo-reg. Both of those cases indicate
9767 that the parameter doesn't really live anywhere (as far as the code
9768 generation parts of GCC are concerned) during most of the function's
9769 activation. That will happen (for example) if the parameter is never
9770 referenced within the function.
9772 We could just generate a location descriptor here for all non-NULL
9773 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
9774 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
9775 where DECL_RTL is NULL or is a pseudo-reg.
9777 Note however that we can only get away with using DECL_INCOMING_RTL as
9778 a backup substitute for DECL_RTL in certain limited cases. In cases
9779 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
9780 we can be sure that the parameter was passed using the same type as it is
9781 declared to have within the function, and that its DECL_INCOMING_RTL
9782 points us to a place where a value of that type is passed.
9784 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
9785 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
9786 because in these cases DECL_INCOMING_RTL points us to a value of some
9787 type which is *different* from the type of the parameter itself. Thus,
9788 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
9789 such cases, the debugger would end up (for example) trying to fetch a
9790 `float' from a place which actually contains the first part of a
9791 `double'. That would lead to really incorrect and confusing
9792 output at debug-time.
9794 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
9795 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
9796 are a couple of exceptions however. On little-endian machines we can
9797 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
9798 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
9799 an integral type that is smaller than TREE_TYPE (decl). These cases arise
9800 when (on a little-endian machine) a non-prototyped function has a
9801 parameter declared to be of type `short' or `char'. In such cases,
9802 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
9803 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
9804 passed `int' value. If the debugger then uses that address to fetch
9805 a `short' or a `char' (on a little-endian machine) the result will be
9806 the correct data, so we allow for such exceptional cases below.
9808 Note that our goal here is to describe the place where the given formal
9809 parameter lives during most of the function's activation (i.e. between the
9810 end of the prologue and the start of the epilogue). We'll do that as best
9811 as we can. Note however that if the given formal parameter is modified
9812 sometime during the execution of the function, then a stack backtrace (at
9813 debug-time) will show the function as having been called with the *new*
9814 value rather than the value which was originally passed in. This happens
9815 rarely enough that it is not a major problem, but it *is* a problem, and
9816 I'd like to fix it.
9818 A future version of dwarf2out.c may generate two additional attributes for
9819 any given DW_TAG_formal_parameter DIE which will describe the "passed
9820 type" and the "passed location" for the given formal parameter in addition
9821 to the attributes we now generate to indicate the "declared type" and the
9822 "active location" for each parameter. This additional set of attributes
9823 could be used by debuggers for stack backtraces. Separately, note that
9824 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
9825 This happens (for example) for inlined-instances of inline function formal
9826 parameters which are never referenced. This really shouldn't be
9827 happening. All PARM_DECL nodes should get valid non-NULL
9828 DECL_INCOMING_RTL values. FIXME. */
9830 /* Use DECL_RTL as the "location" unless we find something better. */
9831 rtl = DECL_RTL_IF_SET (decl);
9833 /* When generating abstract instances, ignore everything except
9834 constants, symbols living in memory, and symbols living in
9835 fixed registers. */
9836 if (! reload_completed)
9838 if (rtl
9839 && (CONSTANT_P (rtl)
9840 || (MEM_P (rtl)
9841 && CONSTANT_P (XEXP (rtl, 0)))
9842 || (REG_P (rtl)
9843 && TREE_CODE (decl) == VAR_DECL
9844 && TREE_STATIC (decl))))
9846 rtl = targetm.delegitimize_address (rtl);
9847 return rtl;
9849 rtl = NULL_RTX;
9851 else if (TREE_CODE (decl) == PARM_DECL)
9853 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
9855 tree declared_type = type_main_variant (TREE_TYPE (decl));
9856 tree passed_type = type_main_variant (DECL_ARG_TYPE (decl));
9858 /* This decl represents a formal parameter which was optimized out.
9859 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
9860 all cases where (rtl == NULL_RTX) just below. */
9861 if (declared_type == passed_type)
9862 rtl = DECL_INCOMING_RTL (decl);
9863 else if (! BYTES_BIG_ENDIAN
9864 && TREE_CODE (declared_type) == INTEGER_TYPE
9865 && (GET_MODE_SIZE (TYPE_MODE (declared_type))
9866 <= GET_MODE_SIZE (TYPE_MODE (passed_type))))
9867 rtl = DECL_INCOMING_RTL (decl);
9870 /* If the parm was passed in registers, but lives on the stack, then
9871 make a big endian correction if the mode of the type of the
9872 parameter is not the same as the mode of the rtl. */
9873 /* ??? This is the same series of checks that are made in dbxout.c before
9874 we reach the big endian correction code there. It isn't clear if all
9875 of these checks are necessary here, but keeping them all is the safe
9876 thing to do. */
9877 else if (MEM_P (rtl)
9878 && XEXP (rtl, 0) != const0_rtx
9879 && ! CONSTANT_P (XEXP (rtl, 0))
9880 /* Not passed in memory. */
9881 && !MEM_P (DECL_INCOMING_RTL (decl))
9882 /* Not passed by invisible reference. */
9883 && (!REG_P (XEXP (rtl, 0))
9884 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
9885 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
9886 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
9887 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
9888 #endif
9890 /* Big endian correction check. */
9891 && BYTES_BIG_ENDIAN
9892 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
9893 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
9894 < UNITS_PER_WORD))
9896 int offset = (UNITS_PER_WORD
9897 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
9899 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
9900 plus_constant (XEXP (rtl, 0), offset));
9903 else if (TREE_CODE (decl) == VAR_DECL
9904 && rtl
9905 && MEM_P (rtl)
9906 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
9907 && BYTES_BIG_ENDIAN)
9909 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
9910 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
9912 /* If a variable is declared "register" yet is smaller than
9913 a register, then if we store the variable to memory, it
9914 looks like we're storing a register-sized value, when in
9915 fact we are not. We need to adjust the offset of the
9916 storage location to reflect the actual value's bytes,
9917 else gdb will not be able to display it. */
9918 if (rsize > dsize)
9919 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
9920 plus_constant (XEXP (rtl, 0), rsize-dsize));
9923 if (rtl != NULL_RTX)
9925 rtl = eliminate_regs (rtl, 0, NULL_RTX);
9926 #ifdef LEAF_REG_REMAP
9927 if (current_function_uses_only_leaf_regs)
9928 leaf_renumber_regs_insn (rtl);
9929 #endif
9932 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
9933 and will have been substituted directly into all expressions that use it.
9934 C does not have such a concept, but C++ and other languages do. */
9935 else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
9937 /* If a variable is initialized with a string constant without embedded
9938 zeros, build CONST_STRING. */
9939 if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
9940 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9942 tree arrtype = TREE_TYPE (decl);
9943 tree enttype = TREE_TYPE (arrtype);
9944 tree domain = TYPE_DOMAIN (arrtype);
9945 tree init = DECL_INITIAL (decl);
9946 enum machine_mode mode = TYPE_MODE (enttype);
9948 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
9949 && domain
9950 && integer_zerop (TYPE_MIN_VALUE (domain))
9951 && compare_tree_int (TYPE_MAX_VALUE (domain),
9952 TREE_STRING_LENGTH (init) - 1) == 0
9953 && ((size_t) TREE_STRING_LENGTH (init)
9954 == strlen (TREE_STRING_POINTER (init)) + 1))
9955 rtl = gen_rtx_CONST_STRING (VOIDmode, TREE_STRING_POINTER (init));
9957 /* If the initializer is something that we know will expand into an
9958 immediate RTL constant, expand it now. Expanding anything else
9959 tends to produce unresolved symbols; see debug/5770 and c++/6381. */
9960 else if (TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST
9961 || TREE_CODE (DECL_INITIAL (decl)) == REAL_CST)
9963 rtl = expand_expr (DECL_INITIAL (decl), NULL_RTX, VOIDmode,
9964 EXPAND_INITIALIZER);
9965 /* If expand_expr returns a MEM, it wasn't immediate. */
9966 if (rtl && MEM_P (rtl))
9967 abort ();
9971 if (rtl)
9972 rtl = targetm.delegitimize_address (rtl);
9974 /* If we don't look past the constant pool, we risk emitting a
9975 reference to a constant pool entry that isn't referenced from
9976 code, and thus is not emitted. */
9977 if (rtl)
9978 rtl = avoid_constant_pool_reference (rtl);
9980 return rtl;
9983 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
9984 data attribute for a variable or a parameter. We generate the
9985 DW_AT_const_value attribute only in those cases where the given variable
9986 or parameter does not have a true "location" either in memory or in a
9987 register. This can happen (for example) when a constant is passed as an
9988 actual argument in a call to an inline function. (It's possible that
9989 these things can crop up in other ways also.) Note that one type of
9990 constant value which can be passed into an inlined function is a constant
9991 pointer. This can happen for example if an actual argument in an inlined
9992 function call evaluates to a compile-time constant address. */
9994 static void
9995 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
9996 enum dwarf_attribute attr)
9998 rtx rtl;
9999 dw_loc_descr_ref descr;
10000 var_loc_list *loc_list;
10002 if (TREE_CODE (decl) == ERROR_MARK)
10003 return;
10004 else if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != PARM_DECL
10005 && TREE_CODE (decl) != RESULT_DECL)
10006 abort ();
10008 /* See if we possibly have multiple locations for this variable. */
10009 loc_list = lookup_decl_loc (decl);
10011 /* If it truly has multiple locations, the first and last node will
10012 differ. */
10013 if (loc_list && loc_list->first != loc_list->last)
10015 const char *secname;
10016 const char *endname;
10017 dw_loc_list_ref list;
10018 rtx varloc;
10019 struct var_loc_node *node;
10021 /* We need to figure out what section we should use as the base
10022 for the address ranges where a given location is valid.
10023 1. If this particular DECL has a section associated with it,
10024 use that.
10025 2. If this function has a section associated with it, use
10026 that.
10027 3. Otherwise, use the text section.
10028 XXX: If you split a variable across multiple sections, this
10029 won't notice. */
10031 if (DECL_SECTION_NAME (decl))
10033 tree sectree = DECL_SECTION_NAME (decl);
10034 secname = TREE_STRING_POINTER (sectree);
10036 else if (current_function_decl
10037 && DECL_SECTION_NAME (current_function_decl))
10039 tree sectree = DECL_SECTION_NAME (current_function_decl);
10040 secname = TREE_STRING_POINTER (sectree);
10042 else
10043 secname = text_section_label;
10045 /* Now that we know what section we are using for a base,
10046 actually construct the list of locations.
10047 The first location information is what is passed to the
10048 function that creates the location list, and the remaining
10049 locations just get added on to that list.
10050 Note that we only know the start address for a location
10051 (IE location changes), so to build the range, we use
10052 the range [current location start, next location start].
10053 This means we have to special case the last node, and generate
10054 a range of [last location start, end of function label]. */
10056 node = loc_list->first;
10057 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10058 list = new_loc_list (loc_descriptor (varloc, attr != DW_AT_frame_base),
10059 node->label, node->next->label, secname, 1);
10060 node = node->next;
10062 for (; node->next; node = node->next)
10063 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10065 /* The variable has a location between NODE->LABEL and
10066 NODE->NEXT->LABEL. */
10067 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10068 add_loc_descr_to_loc_list (&list,
10069 loc_descriptor (varloc,
10070 attr != DW_AT_frame_base),
10071 node->label, node->next->label, secname);
10074 /* If the variable has a location at the last label
10075 it keeps its location until the end of function. */
10076 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10078 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10080 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10081 if (!current_function_decl)
10082 endname = text_end_label;
10083 else
10085 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10086 current_function_funcdef_no);
10087 endname = ggc_strdup (label_id);
10089 add_loc_descr_to_loc_list (&list,
10090 loc_descriptor (varloc,
10091 attr != DW_AT_frame_base),
10092 node->label, endname, secname);
10095 /* Finally, add the location list to the DIE, and we are done. */
10096 add_AT_loc_list (die, attr, list);
10097 return;
10100 rtl = rtl_for_decl_location (decl);
10101 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10103 add_const_value_attribute (die, rtl);
10104 return;
10107 descr = loc_descriptor_from_tree (decl);
10108 if (descr)
10109 add_AT_location_description (die, attr, descr);
10112 /* If we don't have a copy of this variable in memory for some reason (such
10113 as a C++ member constant that doesn't have an out-of-line definition),
10114 we should tell the debugger about the constant value. */
10116 static void
10117 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10119 tree init = DECL_INITIAL (decl);
10120 tree type = TREE_TYPE (decl);
10122 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init
10123 && initializer_constant_valid_p (init, type) == null_pointer_node)
10124 /* OK */;
10125 else
10126 return;
10128 switch (TREE_CODE (type))
10130 case INTEGER_TYPE:
10131 if (host_integerp (init, 0))
10132 add_AT_unsigned (var_die, DW_AT_const_value,
10133 tree_low_cst (init, 0));
10134 else
10135 add_AT_long_long (var_die, DW_AT_const_value,
10136 TREE_INT_CST_HIGH (init),
10137 TREE_INT_CST_LOW (init));
10138 break;
10140 default:;
10144 /* Generate a DW_AT_name attribute given some string value to be included as
10145 the value of the attribute. */
10147 static void
10148 add_name_attribute (dw_die_ref die, const char *name_string)
10150 if (name_string != NULL && *name_string != 0)
10152 if (demangle_name_func)
10153 name_string = (*demangle_name_func) (name_string);
10155 add_AT_string (die, DW_AT_name, name_string);
10159 /* Generate a DW_AT_comp_dir attribute for DIE. */
10161 static void
10162 add_comp_dir_attribute (dw_die_ref die)
10164 const char *wd = get_src_pwd ();
10165 if (wd != NULL)
10166 add_AT_string (die, DW_AT_comp_dir, wd);
10169 /* Given a tree node describing an array bound (either lower or upper) output
10170 a representation for that bound. */
10172 static void
10173 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10175 switch (TREE_CODE (bound))
10177 case ERROR_MARK:
10178 return;
10180 /* All fixed-bounds are represented by INTEGER_CST nodes. */
10181 case INTEGER_CST:
10182 if (! host_integerp (bound, 0)
10183 || (bound_attr == DW_AT_lower_bound
10184 && (((is_c_family () || is_java ()) && integer_zerop (bound))
10185 || (is_fortran () && integer_onep (bound)))))
10186 /* Use the default. */
10188 else
10189 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
10190 break;
10192 case CONVERT_EXPR:
10193 case NOP_EXPR:
10194 case NON_LVALUE_EXPR:
10195 case VIEW_CONVERT_EXPR:
10196 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10197 break;
10199 case SAVE_EXPR:
10200 break;
10202 case VAR_DECL:
10203 case PARM_DECL:
10204 case RESULT_DECL:
10206 dw_die_ref decl_die = lookup_decl_die (bound);
10208 /* ??? Can this happen, or should the variable have been bound
10209 first? Probably it can, since I imagine that we try to create
10210 the types of parameters in the order in which they exist in
10211 the list, and won't have created a forward reference to a
10212 later parameter. */
10213 if (decl_die != NULL)
10214 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10215 break;
10218 default:
10220 /* Otherwise try to create a stack operation procedure to
10221 evaluate the value of the array bound. */
10223 dw_die_ref ctx, decl_die;
10224 dw_loc_descr_ref loc;
10226 loc = loc_descriptor_from_tree (bound);
10227 if (loc == NULL)
10228 break;
10230 if (current_function_decl == 0)
10231 ctx = comp_unit_die;
10232 else
10233 ctx = lookup_decl_die (current_function_decl);
10235 decl_die = new_die (DW_TAG_variable, ctx, bound);
10236 add_AT_flag (decl_die, DW_AT_artificial, 1);
10237 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10238 add_AT_loc (decl_die, DW_AT_location, loc);
10240 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10241 break;
10246 /* Note that the block of subscript information for an array type also
10247 includes information about the element type of type given array type. */
10249 static void
10250 add_subscript_info (dw_die_ref type_die, tree type)
10252 #ifndef MIPS_DEBUGGING_INFO
10253 unsigned dimension_number;
10254 #endif
10255 tree lower, upper;
10256 dw_die_ref subrange_die;
10258 /* The GNU compilers represent multidimensional array types as sequences of
10259 one dimensional array types whose element types are themselves array
10260 types. Here we squish that down, so that each multidimensional array
10261 type gets only one array_type DIE in the Dwarf debugging info. The draft
10262 Dwarf specification say that we are allowed to do this kind of
10263 compression in C (because there is no difference between an array or
10264 arrays and a multidimensional array in C) but for other source languages
10265 (e.g. Ada) we probably shouldn't do this. */
10267 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10268 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10269 We work around this by disabling this feature. See also
10270 gen_array_type_die. */
10271 #ifndef MIPS_DEBUGGING_INFO
10272 for (dimension_number = 0;
10273 TREE_CODE (type) == ARRAY_TYPE;
10274 type = TREE_TYPE (type), dimension_number++)
10275 #endif
10277 tree domain = TYPE_DOMAIN (type);
10279 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
10280 and (in GNU C only) variable bounds. Handle all three forms
10281 here. */
10282 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
10283 if (domain)
10285 /* We have an array type with specified bounds. */
10286 lower = TYPE_MIN_VALUE (domain);
10287 upper = TYPE_MAX_VALUE (domain);
10289 /* Define the index type. */
10290 if (TREE_TYPE (domain))
10292 /* ??? This is probably an Ada unnamed subrange type. Ignore the
10293 TREE_TYPE field. We can't emit debug info for this
10294 because it is an unnamed integral type. */
10295 if (TREE_CODE (domain) == INTEGER_TYPE
10296 && TYPE_NAME (domain) == NULL_TREE
10297 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10298 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
10300 else
10301 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10302 type_die);
10305 /* ??? If upper is NULL, the array has unspecified length,
10306 but it does have a lower bound. This happens with Fortran
10307 dimension arr(N:*)
10308 Since the debugger is definitely going to need to know N
10309 to produce useful results, go ahead and output the lower
10310 bound solo, and hope the debugger can cope. */
10312 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
10313 if (upper)
10314 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
10317 /* Otherwise we have an array type with an unspecified length. The
10318 DWARF-2 spec does not say how to handle this; let's just leave out the
10319 bounds. */
10323 static void
10324 add_byte_size_attribute (dw_die_ref die, tree tree_node)
10326 unsigned size;
10328 switch (TREE_CODE (tree_node))
10330 case ERROR_MARK:
10331 size = 0;
10332 break;
10333 case ENUMERAL_TYPE:
10334 case RECORD_TYPE:
10335 case UNION_TYPE:
10336 case QUAL_UNION_TYPE:
10337 size = int_size_in_bytes (tree_node);
10338 break;
10339 case FIELD_DECL:
10340 /* For a data member of a struct or union, the DW_AT_byte_size is
10341 generally given as the number of bytes normally allocated for an
10342 object of the *declared* type of the member itself. This is true
10343 even for bit-fields. */
10344 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10345 break;
10346 default:
10347 abort ();
10350 /* Note that `size' might be -1 when we get to this point. If it is, that
10351 indicates that the byte size of the entity in question is variable. We
10352 have no good way of expressing this fact in Dwarf at the present time,
10353 so just let the -1 pass on through. */
10354 add_AT_unsigned (die, DW_AT_byte_size, size);
10357 /* For a FIELD_DECL node which represents a bit-field, output an attribute
10358 which specifies the distance in bits from the highest order bit of the
10359 "containing object" for the bit-field to the highest order bit of the
10360 bit-field itself.
10362 For any given bit-field, the "containing object" is a hypothetical object
10363 (of some integral or enum type) within which the given bit-field lives. The
10364 type of this hypothetical "containing object" is always the same as the
10365 declared type of the individual bit-field itself. The determination of the
10366 exact location of the "containing object" for a bit-field is rather
10367 complicated. It's handled by the `field_byte_offset' function (above).
10369 Note that it is the size (in bytes) of the hypothetical "containing object"
10370 which will be given in the DW_AT_byte_size attribute for this bit-field.
10371 (See `byte_size_attribute' above). */
10373 static inline void
10374 add_bit_offset_attribute (dw_die_ref die, tree decl)
10376 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10377 tree type = DECL_BIT_FIELD_TYPE (decl);
10378 HOST_WIDE_INT bitpos_int;
10379 HOST_WIDE_INT highest_order_object_bit_offset;
10380 HOST_WIDE_INT highest_order_field_bit_offset;
10381 HOST_WIDE_INT unsigned bit_offset;
10383 /* Must be a field and a bit field. */
10384 if (!type
10385 || TREE_CODE (decl) != FIELD_DECL)
10386 abort ();
10388 /* We can't yet handle bit-fields whose offsets are variable, so if we
10389 encounter such things, just return without generating any attribute
10390 whatsoever. Likewise for variable or too large size. */
10391 if (! host_integerp (bit_position (decl), 0)
10392 || ! host_integerp (DECL_SIZE (decl), 1))
10393 return;
10395 bitpos_int = int_bit_position (decl);
10397 /* Note that the bit offset is always the distance (in bits) from the
10398 highest-order bit of the "containing object" to the highest-order bit of
10399 the bit-field itself. Since the "high-order end" of any object or field
10400 is different on big-endian and little-endian machines, the computation
10401 below must take account of these differences. */
10402 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10403 highest_order_field_bit_offset = bitpos_int;
10405 if (! BYTES_BIG_ENDIAN)
10407 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
10408 highest_order_object_bit_offset += simple_type_size_in_bits (type);
10411 bit_offset
10412 = (! BYTES_BIG_ENDIAN
10413 ? highest_order_object_bit_offset - highest_order_field_bit_offset
10414 : highest_order_field_bit_offset - highest_order_object_bit_offset);
10416 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10419 /* For a FIELD_DECL node which represents a bit field, output an attribute
10420 which specifies the length in bits of the given field. */
10422 static inline void
10423 add_bit_size_attribute (dw_die_ref die, tree decl)
10425 /* Must be a field and a bit field. */
10426 if (TREE_CODE (decl) != FIELD_DECL
10427 || ! DECL_BIT_FIELD_TYPE (decl))
10428 abort ();
10430 if (host_integerp (DECL_SIZE (decl), 1))
10431 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
10434 /* If the compiled language is ANSI C, then add a 'prototyped'
10435 attribute, if arg types are given for the parameters of a function. */
10437 static inline void
10438 add_prototyped_attribute (dw_die_ref die, tree func_type)
10440 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10441 && TYPE_ARG_TYPES (func_type) != NULL)
10442 add_AT_flag (die, DW_AT_prototyped, 1);
10445 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
10446 by looking in either the type declaration or object declaration
10447 equate table. */
10449 static inline void
10450 add_abstract_origin_attribute (dw_die_ref die, tree origin)
10452 dw_die_ref origin_die = NULL;
10454 if (TREE_CODE (origin) != FUNCTION_DECL)
10456 /* We may have gotten separated from the block for the inlined
10457 function, if we're in an exception handler or some such; make
10458 sure that the abstract function has been written out.
10460 Doing this for nested functions is wrong, however; functions are
10461 distinct units, and our context might not even be inline. */
10462 tree fn = origin;
10464 if (TYPE_P (fn))
10465 fn = TYPE_STUB_DECL (fn);
10467 fn = decl_function_context (fn);
10468 if (fn)
10469 dwarf2out_abstract_function (fn);
10472 if (DECL_P (origin))
10473 origin_die = lookup_decl_die (origin);
10474 else if (TYPE_P (origin))
10475 origin_die = lookup_type_die (origin);
10477 if (origin_die == NULL)
10478 abort ();
10480 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
10483 /* We do not currently support the pure_virtual attribute. */
10485 static inline void
10486 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
10488 if (DECL_VINDEX (func_decl))
10490 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10492 if (host_integerp (DECL_VINDEX (func_decl), 0))
10493 add_AT_loc (die, DW_AT_vtable_elem_location,
10494 new_loc_descr (DW_OP_constu,
10495 tree_low_cst (DECL_VINDEX (func_decl), 0),
10496 0));
10498 /* GNU extension: Record what type this method came from originally. */
10499 if (debug_info_level > DINFO_LEVEL_TERSE)
10500 add_AT_die_ref (die, DW_AT_containing_type,
10501 lookup_type_die (DECL_CONTEXT (func_decl)));
10505 /* Add source coordinate attributes for the given decl. */
10507 static void
10508 add_src_coords_attributes (dw_die_ref die, tree decl)
10510 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
10511 unsigned file_index = lookup_filename (s.file);
10513 add_AT_unsigned (die, DW_AT_decl_file, file_index);
10514 add_AT_unsigned (die, DW_AT_decl_line, s.line);
10517 /* Add a DW_AT_name attribute and source coordinate attribute for the
10518 given decl, but only if it actually has a name. */
10520 static void
10521 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
10523 tree decl_name;
10525 decl_name = DECL_NAME (decl);
10526 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
10528 add_name_attribute (die, dwarf2_name (decl, 0));
10529 if (! DECL_ARTIFICIAL (decl))
10530 add_src_coords_attributes (die, decl);
10532 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
10533 && TREE_PUBLIC (decl)
10534 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
10535 && !DECL_ABSTRACT (decl))
10536 add_AT_string (die, DW_AT_MIPS_linkage_name,
10537 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
10540 #ifdef VMS_DEBUGGING_INFO
10541 /* Get the function's name, as described by its RTL. This may be different
10542 from the DECL_NAME name used in the source file. */
10543 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
10545 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
10546 XEXP (DECL_RTL (decl), 0));
10547 VARRAY_PUSH_RTX (used_rtx_varray, XEXP (DECL_RTL (decl), 0));
10549 #endif
10552 /* Push a new declaration scope. */
10554 static void
10555 push_decl_scope (tree scope)
10557 VARRAY_PUSH_TREE (decl_scope_table, scope);
10560 /* Pop a declaration scope. */
10562 static inline void
10563 pop_decl_scope (void)
10565 if (VARRAY_ACTIVE_SIZE (decl_scope_table) <= 0)
10566 abort ();
10568 VARRAY_POP (decl_scope_table);
10571 /* Return the DIE for the scope that immediately contains this type.
10572 Non-named types get global scope. Named types nested in other
10573 types get their containing scope if it's open, or global scope
10574 otherwise. All other types (i.e. function-local named types) get
10575 the current active scope. */
10577 static dw_die_ref
10578 scope_die_for (tree t, dw_die_ref context_die)
10580 dw_die_ref scope_die = NULL;
10581 tree containing_scope;
10582 int i;
10584 /* Non-types always go in the current scope. */
10585 if (! TYPE_P (t))
10586 abort ();
10588 containing_scope = TYPE_CONTEXT (t);
10590 /* Use the containing namespace if it was passed in (for a declaration). */
10591 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
10593 if (context_die == lookup_decl_die (containing_scope))
10594 /* OK */;
10595 else
10596 containing_scope = NULL_TREE;
10599 /* Ignore function type "scopes" from the C frontend. They mean that
10600 a tagged type is local to a parmlist of a function declarator, but
10601 that isn't useful to DWARF. */
10602 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
10603 containing_scope = NULL_TREE;
10605 if (containing_scope == NULL_TREE)
10606 scope_die = comp_unit_die;
10607 else if (TYPE_P (containing_scope))
10609 /* For types, we can just look up the appropriate DIE. But
10610 first we check to see if we're in the middle of emitting it
10611 so we know where the new DIE should go. */
10612 for (i = VARRAY_ACTIVE_SIZE (decl_scope_table) - 1; i >= 0; --i)
10613 if (VARRAY_TREE (decl_scope_table, i) == containing_scope)
10614 break;
10616 if (i < 0)
10618 if (debug_info_level > DINFO_LEVEL_TERSE
10619 && !TREE_ASM_WRITTEN (containing_scope))
10620 abort ();
10622 /* If none of the current dies are suitable, we get file scope. */
10623 scope_die = comp_unit_die;
10625 else
10626 scope_die = lookup_type_die (containing_scope);
10628 else
10629 scope_die = context_die;
10631 return scope_die;
10634 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
10636 static inline int
10637 local_scope_p (dw_die_ref context_die)
10639 for (; context_die; context_die = context_die->die_parent)
10640 if (context_die->die_tag == DW_TAG_inlined_subroutine
10641 || context_die->die_tag == DW_TAG_subprogram)
10642 return 1;
10644 return 0;
10647 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
10648 whether or not to treat a DIE in this context as a declaration. */
10650 static inline int
10651 class_or_namespace_scope_p (dw_die_ref context_die)
10653 return (context_die
10654 && (context_die->die_tag == DW_TAG_structure_type
10655 || context_die->die_tag == DW_TAG_union_type
10656 || context_die->die_tag == DW_TAG_namespace));
10659 /* Many forms of DIEs require a "type description" attribute. This
10660 routine locates the proper "type descriptor" die for the type given
10661 by 'type', and adds a DW_AT_type attribute below the given die. */
10663 static void
10664 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
10665 int decl_volatile, dw_die_ref context_die)
10667 enum tree_code code = TREE_CODE (type);
10668 dw_die_ref type_die = NULL;
10670 /* ??? If this type is an unnamed subrange type of an integral or
10671 floating-point type, use the inner type. This is because we have no
10672 support for unnamed types in base_type_die. This can happen if this is
10673 an Ada subrange type. Correct solution is emit a subrange type die. */
10674 if ((code == INTEGER_TYPE || code == REAL_TYPE)
10675 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
10676 type = TREE_TYPE (type), code = TREE_CODE (type);
10678 if (code == ERROR_MARK
10679 /* Handle a special case. For functions whose return type is void, we
10680 generate *no* type attribute. (Note that no object may have type
10681 `void', so this only applies to function return types). */
10682 || code == VOID_TYPE)
10683 return;
10685 type_die = modified_type_die (type,
10686 decl_const || TYPE_READONLY (type),
10687 decl_volatile || TYPE_VOLATILE (type),
10688 context_die);
10690 if (type_die != NULL)
10691 add_AT_die_ref (object_die, DW_AT_type, type_die);
10694 /* Given a tree pointer to a struct, class, union, or enum type node, return
10695 a pointer to the (string) tag name for the given type, or zero if the type
10696 was declared without a tag. */
10698 static const char *
10699 type_tag (tree type)
10701 const char *name = 0;
10703 if (TYPE_NAME (type) != 0)
10705 tree t = 0;
10707 /* Find the IDENTIFIER_NODE for the type name. */
10708 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
10709 t = TYPE_NAME (type);
10711 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
10712 a TYPE_DECL node, regardless of whether or not a `typedef' was
10713 involved. */
10714 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10715 && ! DECL_IGNORED_P (TYPE_NAME (type)))
10716 t = DECL_NAME (TYPE_NAME (type));
10718 /* Now get the name as a string, or invent one. */
10719 if (t != 0)
10720 name = IDENTIFIER_POINTER (t);
10723 return (name == 0 || *name == '\0') ? 0 : name;
10726 /* Return the type associated with a data member, make a special check
10727 for bit field types. */
10729 static inline tree
10730 member_declared_type (tree member)
10732 return (DECL_BIT_FIELD_TYPE (member)
10733 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
10736 /* Get the decl's label, as described by its RTL. This may be different
10737 from the DECL_NAME name used in the source file. */
10739 #if 0
10740 static const char *
10741 decl_start_label (tree decl)
10743 rtx x;
10744 const char *fnname;
10746 x = DECL_RTL (decl);
10747 if (!MEM_P (x))
10748 abort ();
10750 x = XEXP (x, 0);
10751 if (GET_CODE (x) != SYMBOL_REF)
10752 abort ();
10754 fnname = XSTR (x, 0);
10755 return fnname;
10757 #endif
10759 /* These routines generate the internal representation of the DIE's for
10760 the compilation unit. Debugging information is collected by walking
10761 the declaration trees passed in from dwarf2out_decl(). */
10763 static void
10764 gen_array_type_die (tree type, dw_die_ref context_die)
10766 dw_die_ref scope_die = scope_die_for (type, context_die);
10767 dw_die_ref array_die;
10768 tree element_type;
10770 /* ??? The SGI dwarf reader fails for array of array of enum types unless
10771 the inner array type comes before the outer array type. Thus we must
10772 call gen_type_die before we call new_die. See below also. */
10773 #ifdef MIPS_DEBUGGING_INFO
10774 gen_type_die (TREE_TYPE (type), context_die);
10775 #endif
10777 array_die = new_die (DW_TAG_array_type, scope_die, type);
10778 add_name_attribute (array_die, type_tag (type));
10779 equate_type_number_to_die (type, array_die);
10781 if (TREE_CODE (type) == VECTOR_TYPE)
10783 /* The frontend feeds us a representation for the vector as a struct
10784 containing an array. Pull out the array type. */
10785 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
10786 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
10789 #if 0
10790 /* We default the array ordering. SDB will probably do
10791 the right things even if DW_AT_ordering is not present. It's not even
10792 an issue until we start to get into multidimensional arrays anyway. If
10793 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
10794 then we'll have to put the DW_AT_ordering attribute back in. (But if
10795 and when we find out that we need to put these in, we will only do so
10796 for multidimensional arrays. */
10797 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
10798 #endif
10800 #ifdef MIPS_DEBUGGING_INFO
10801 /* The SGI compilers handle arrays of unknown bound by setting
10802 AT_declaration and not emitting any subrange DIEs. */
10803 if (! TYPE_DOMAIN (type))
10804 add_AT_flag (array_die, DW_AT_declaration, 1);
10805 else
10806 #endif
10807 add_subscript_info (array_die, type);
10809 /* Add representation of the type of the elements of this array type. */
10810 element_type = TREE_TYPE (type);
10812 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10813 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10814 We work around this by disabling this feature. See also
10815 add_subscript_info. */
10816 #ifndef MIPS_DEBUGGING_INFO
10817 while (TREE_CODE (element_type) == ARRAY_TYPE)
10818 element_type = TREE_TYPE (element_type);
10820 gen_type_die (element_type, context_die);
10821 #endif
10823 add_type_attribute (array_die, element_type, 0, 0, context_die);
10826 static void
10827 gen_set_type_die (tree type, dw_die_ref context_die)
10829 dw_die_ref type_die
10830 = new_die (DW_TAG_set_type, scope_die_for (type, context_die), type);
10832 equate_type_number_to_die (type, type_die);
10833 add_type_attribute (type_die, TREE_TYPE (type), 0, 0, context_die);
10836 #if 0
10837 static void
10838 gen_entry_point_die (tree decl, dw_die_ref context_die)
10840 tree origin = decl_ultimate_origin (decl);
10841 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
10843 if (origin != NULL)
10844 add_abstract_origin_attribute (decl_die, origin);
10845 else
10847 add_name_and_src_coords_attributes (decl_die, decl);
10848 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
10849 0, 0, context_die);
10852 if (DECL_ABSTRACT (decl))
10853 equate_decl_number_to_die (decl, decl_die);
10854 else
10855 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
10857 #endif
10859 /* Walk through the list of incomplete types again, trying once more to
10860 emit full debugging info for them. */
10862 static void
10863 retry_incomplete_types (void)
10865 int i;
10867 for (i = VARRAY_ACTIVE_SIZE (incomplete_types) - 1; i >= 0; i--)
10868 gen_type_die (VARRAY_TREE (incomplete_types, i), comp_unit_die);
10871 /* Generate a DIE to represent an inlined instance of an enumeration type. */
10873 static void
10874 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
10876 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
10878 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10879 be incomplete and such types are not marked. */
10880 add_abstract_origin_attribute (type_die, type);
10883 /* Generate a DIE to represent an inlined instance of a structure type. */
10885 static void
10886 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
10888 dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
10890 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10891 be incomplete and such types are not marked. */
10892 add_abstract_origin_attribute (type_die, type);
10895 /* Generate a DIE to represent an inlined instance of a union type. */
10897 static void
10898 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
10900 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
10902 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10903 be incomplete and such types are not marked. */
10904 add_abstract_origin_attribute (type_die, type);
10907 /* Generate a DIE to represent an enumeration type. Note that these DIEs
10908 include all of the information about the enumeration values also. Each
10909 enumerated type name/value is listed as a child of the enumerated type
10910 DIE. */
10912 static dw_die_ref
10913 gen_enumeration_type_die (tree type, dw_die_ref context_die)
10915 dw_die_ref type_die = lookup_type_die (type);
10917 if (type_die == NULL)
10919 type_die = new_die (DW_TAG_enumeration_type,
10920 scope_die_for (type, context_die), type);
10921 equate_type_number_to_die (type, type_die);
10922 add_name_attribute (type_die, type_tag (type));
10924 else if (! TYPE_SIZE (type))
10925 return type_die;
10926 else
10927 remove_AT (type_die, DW_AT_declaration);
10929 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
10930 given enum type is incomplete, do not generate the DW_AT_byte_size
10931 attribute or the DW_AT_element_list attribute. */
10932 if (TYPE_SIZE (type))
10934 tree link;
10936 TREE_ASM_WRITTEN (type) = 1;
10937 add_byte_size_attribute (type_die, type);
10938 if (TYPE_STUB_DECL (type) != NULL_TREE)
10939 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
10941 /* If the first reference to this type was as the return type of an
10942 inline function, then it may not have a parent. Fix this now. */
10943 if (type_die->die_parent == NULL)
10944 add_child_die (scope_die_for (type, context_die), type_die);
10946 for (link = TYPE_VALUES (type);
10947 link != NULL; link = TREE_CHAIN (link))
10949 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
10950 tree value = TREE_VALUE (link);
10952 add_name_attribute (enum_die,
10953 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
10955 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
10956 /* DWARF2 does not provide a way of indicating whether or
10957 not enumeration constants are signed or unsigned. GDB
10958 always assumes the values are signed, so we output all
10959 values as if they were signed. That means that
10960 enumeration constants with very large unsigned values
10961 will appear to have negative values in the debugger. */
10962 add_AT_int (enum_die, DW_AT_const_value,
10963 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
10966 else
10967 add_AT_flag (type_die, DW_AT_declaration, 1);
10969 return type_die;
10972 /* Generate a DIE to represent either a real live formal parameter decl or to
10973 represent just the type of some formal parameter position in some function
10974 type.
10976 Note that this routine is a bit unusual because its argument may be a
10977 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
10978 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
10979 node. If it's the former then this function is being called to output a
10980 DIE to represent a formal parameter object (or some inlining thereof). If
10981 it's the latter, then this function is only being called to output a
10982 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
10983 argument type of some subprogram type. */
10985 static dw_die_ref
10986 gen_formal_parameter_die (tree node, dw_die_ref context_die)
10988 dw_die_ref parm_die
10989 = new_die (DW_TAG_formal_parameter, context_die, node);
10990 tree origin;
10992 switch (TREE_CODE_CLASS (TREE_CODE (node)))
10994 case 'd':
10995 origin = decl_ultimate_origin (node);
10996 if (origin != NULL)
10997 add_abstract_origin_attribute (parm_die, origin);
10998 else
11000 add_name_and_src_coords_attributes (parm_die, node);
11001 add_type_attribute (parm_die, TREE_TYPE (node),
11002 TREE_READONLY (node),
11003 TREE_THIS_VOLATILE (node),
11004 context_die);
11005 if (DECL_ARTIFICIAL (node))
11006 add_AT_flag (parm_die, DW_AT_artificial, 1);
11009 equate_decl_number_to_die (node, parm_die);
11010 if (! DECL_ABSTRACT (node))
11011 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
11013 break;
11015 case 't':
11016 /* We were called with some kind of a ..._TYPE node. */
11017 add_type_attribute (parm_die, node, 0, 0, context_die);
11018 break;
11020 default:
11021 abort ();
11024 return parm_die;
11027 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11028 at the end of an (ANSI prototyped) formal parameters list. */
11030 static void
11031 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11033 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11036 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11037 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11038 parameters as specified in some function type specification (except for
11039 those which appear as part of a function *definition*). */
11041 static void
11042 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11044 tree link;
11045 tree formal_type = NULL;
11046 tree first_parm_type;
11047 tree arg;
11049 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11051 arg = DECL_ARGUMENTS (function_or_method_type);
11052 function_or_method_type = TREE_TYPE (function_or_method_type);
11054 else
11055 arg = NULL_TREE;
11057 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11059 /* Make our first pass over the list of formal parameter types and output a
11060 DW_TAG_formal_parameter DIE for each one. */
11061 for (link = first_parm_type; link; )
11063 dw_die_ref parm_die;
11065 formal_type = TREE_VALUE (link);
11066 if (formal_type == void_type_node)
11067 break;
11069 /* Output a (nameless) DIE to represent the formal parameter itself. */
11070 parm_die = gen_formal_parameter_die (formal_type, context_die);
11071 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11072 && link == first_parm_type)
11073 || (arg && DECL_ARTIFICIAL (arg)))
11074 add_AT_flag (parm_die, DW_AT_artificial, 1);
11076 link = TREE_CHAIN (link);
11077 if (arg)
11078 arg = TREE_CHAIN (arg);
11081 /* If this function type has an ellipsis, add a
11082 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
11083 if (formal_type != void_type_node)
11084 gen_unspecified_parameters_die (function_or_method_type, context_die);
11086 /* Make our second (and final) pass over the list of formal parameter types
11087 and output DIEs to represent those types (as necessary). */
11088 for (link = TYPE_ARG_TYPES (function_or_method_type);
11089 link && TREE_VALUE (link);
11090 link = TREE_CHAIN (link))
11091 gen_type_die (TREE_VALUE (link), context_die);
11094 /* We want to generate the DIE for TYPE so that we can generate the
11095 die for MEMBER, which has been defined; we will need to refer back
11096 to the member declaration nested within TYPE. If we're trying to
11097 generate minimal debug info for TYPE, processing TYPE won't do the
11098 trick; we need to attach the member declaration by hand. */
11100 static void
11101 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11103 gen_type_die (type, context_die);
11105 /* If we're trying to avoid duplicate debug info, we may not have
11106 emitted the member decl for this function. Emit it now. */
11107 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11108 && ! lookup_decl_die (member))
11110 if (decl_ultimate_origin (member))
11111 abort ();
11113 push_decl_scope (type);
11114 if (TREE_CODE (member) == FUNCTION_DECL)
11115 gen_subprogram_die (member, lookup_type_die (type));
11116 else
11117 gen_variable_die (member, lookup_type_die (type));
11119 pop_decl_scope ();
11123 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11124 may later generate inlined and/or out-of-line instances of. */
11126 static void
11127 dwarf2out_abstract_function (tree decl)
11129 dw_die_ref old_die;
11130 tree save_fn;
11131 tree context;
11132 int was_abstract = DECL_ABSTRACT (decl);
11134 /* Make sure we have the actual abstract inline, not a clone. */
11135 decl = DECL_ORIGIN (decl);
11137 old_die = lookup_decl_die (decl);
11138 if (old_die && get_AT (old_die, DW_AT_inline))
11139 /* We've already generated the abstract instance. */
11140 return;
11142 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11143 we don't get confused by DECL_ABSTRACT. */
11144 if (debug_info_level > DINFO_LEVEL_TERSE)
11146 context = decl_class_context (decl);
11147 if (context)
11148 gen_type_die_for_member
11149 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11152 /* Pretend we've just finished compiling this function. */
11153 save_fn = current_function_decl;
11154 current_function_decl = decl;
11156 set_decl_abstract_flags (decl, 1);
11157 dwarf2out_decl (decl);
11158 if (! was_abstract)
11159 set_decl_abstract_flags (decl, 0);
11161 current_function_decl = save_fn;
11164 /* Generate a DIE to represent a declared function (either file-scope or
11165 block-local). */
11167 static void
11168 gen_subprogram_die (tree decl, dw_die_ref context_die)
11170 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11171 tree origin = decl_ultimate_origin (decl);
11172 dw_die_ref subr_die;
11173 rtx fp_reg;
11174 tree fn_arg_types;
11175 tree outer_scope;
11176 dw_die_ref old_die = lookup_decl_die (decl);
11177 int declaration = (current_function_decl != decl
11178 || class_or_namespace_scope_p (context_die));
11180 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11181 started to generate the abstract instance of an inline, decided to output
11182 its containing class, and proceeded to emit the declaration of the inline
11183 from the member list for the class. If so, DECLARATION takes priority;
11184 we'll get back to the abstract instance when done with the class. */
11186 /* The class-scope declaration DIE must be the primary DIE. */
11187 if (origin && declaration && class_or_namespace_scope_p (context_die))
11189 origin = NULL;
11190 if (old_die)
11191 abort ();
11194 if (origin != NULL)
11196 if (declaration && ! local_scope_p (context_die))
11197 abort ();
11199 /* Fixup die_parent for the abstract instance of a nested
11200 inline function. */
11201 if (old_die && old_die->die_parent == NULL)
11202 add_child_die (context_die, old_die);
11204 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11205 add_abstract_origin_attribute (subr_die, origin);
11207 else if (old_die)
11209 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11210 unsigned file_index = lookup_filename (s.file);
11212 if (!get_AT_flag (old_die, DW_AT_declaration)
11213 /* We can have a normal definition following an inline one in the
11214 case of redefinition of GNU C extern inlines.
11215 It seems reasonable to use AT_specification in this case. */
11216 && !get_AT (old_die, DW_AT_inline))
11218 /* ??? This can happen if there is a bug in the program, for
11219 instance, if it has duplicate function definitions. Ideally,
11220 we should detect this case and ignore it. For now, if we have
11221 already reported an error, any error at all, then assume that
11222 we got here because of an input error, not a dwarf2 bug. */
11223 if (errorcount)
11224 return;
11225 abort ();
11228 /* If the definition comes from the same place as the declaration,
11229 maybe use the old DIE. We always want the DIE for this function
11230 that has the *_pc attributes to be under comp_unit_die so the
11231 debugger can find it. We also need to do this for abstract
11232 instances of inlines, since the spec requires the out-of-line copy
11233 to have the same parent. For local class methods, this doesn't
11234 apply; we just use the old DIE. */
11235 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
11236 && (DECL_ARTIFICIAL (decl)
11237 || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
11238 && (get_AT_unsigned (old_die, DW_AT_decl_line)
11239 == (unsigned) s.line))))
11241 subr_die = old_die;
11243 /* Clear out the declaration attribute and the formal parameters.
11244 Do not remove all children, because it is possible that this
11245 declaration die was forced using force_decl_die(). In such
11246 cases die that forced declaration die (e.g. TAG_imported_module)
11247 is one of the children that we do not want to remove. */
11248 remove_AT (subr_die, DW_AT_declaration);
11249 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
11251 else
11253 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11254 add_AT_specification (subr_die, old_die);
11255 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11256 add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
11257 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11258 != (unsigned) s.line)
11259 add_AT_unsigned
11260 (subr_die, DW_AT_decl_line, s.line);
11263 else
11265 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11267 if (TREE_PUBLIC (decl))
11268 add_AT_flag (subr_die, DW_AT_external, 1);
11270 add_name_and_src_coords_attributes (subr_die, decl);
11271 if (debug_info_level > DINFO_LEVEL_TERSE)
11273 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11274 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11275 0, 0, context_die);
11278 add_pure_or_virtual_attribute (subr_die, decl);
11279 if (DECL_ARTIFICIAL (decl))
11280 add_AT_flag (subr_die, DW_AT_artificial, 1);
11282 if (TREE_PROTECTED (decl))
11283 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11284 else if (TREE_PRIVATE (decl))
11285 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
11288 if (declaration)
11290 if (!old_die || !get_AT (old_die, DW_AT_inline))
11292 add_AT_flag (subr_die, DW_AT_declaration, 1);
11294 /* The first time we see a member function, it is in the context of
11295 the class to which it belongs. We make sure of this by emitting
11296 the class first. The next time is the definition, which is
11297 handled above. The two may come from the same source text.
11299 Note that force_decl_die() forces function declaration die. It is
11300 later reused to represent definition. */
11301 equate_decl_number_to_die (decl, subr_die);
11304 else if (DECL_ABSTRACT (decl))
11306 if (DECL_DECLARED_INLINE_P (decl))
11308 if (cgraph_function_possibly_inlined_p (decl))
11309 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11310 else
11311 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
11313 else
11315 if (cgraph_function_possibly_inlined_p (decl))
11316 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11317 else
11318 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11321 equate_decl_number_to_die (decl, subr_die);
11323 else if (!DECL_EXTERNAL (decl))
11325 if (!old_die || !get_AT (old_die, DW_AT_inline))
11326 equate_decl_number_to_die (decl, subr_die);
11328 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11329 current_function_funcdef_no);
11330 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11331 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11332 current_function_funcdef_no);
11333 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11335 add_pubname (decl, subr_die);
11336 add_arange (decl, subr_die);
11338 #ifdef MIPS_DEBUGGING_INFO
11339 /* Add a reference to the FDE for this routine. */
11340 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11341 #endif
11343 /* Define the "frame base" location for this routine. We use the
11344 frame pointer or stack pointer registers, since the RTL for local
11345 variables is relative to one of them. */
11346 if (frame_base_decl && lookup_decl_loc (frame_base_decl) != NULL)
11348 add_location_or_const_value_attribute (subr_die, frame_base_decl,
11349 DW_AT_frame_base);
11351 else
11353 fp_reg
11354 = frame_pointer_needed ? hard_frame_pointer_rtx : stack_pointer_rtx;
11355 add_AT_loc (subr_die, DW_AT_frame_base, reg_loc_descriptor (fp_reg));
11358 if (cfun->static_chain_decl)
11359 add_AT_location_description (subr_die, DW_AT_static_link,
11360 loc_descriptor_from_tree (cfun->static_chain_decl));
11363 /* Now output descriptions of the arguments for this function. This gets
11364 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
11365 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
11366 `...' at the end of the formal parameter list. In order to find out if
11367 there was a trailing ellipsis or not, we must instead look at the type
11368 associated with the FUNCTION_DECL. This will be a node of type
11369 FUNCTION_TYPE. If the chain of type nodes hanging off of this
11370 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
11371 an ellipsis at the end. */
11373 /* In the case where we are describing a mere function declaration, all we
11374 need to do here (and all we *can* do here) is to describe the *types* of
11375 its formal parameters. */
11376 if (debug_info_level <= DINFO_LEVEL_TERSE)
11378 else if (declaration)
11379 gen_formal_types_die (decl, subr_die);
11380 else
11382 /* Generate DIEs to represent all known formal parameters. */
11383 tree arg_decls = DECL_ARGUMENTS (decl);
11384 tree parm;
11386 /* When generating DIEs, generate the unspecified_parameters DIE
11387 instead if we come across the arg "__builtin_va_alist" */
11388 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
11389 if (TREE_CODE (parm) == PARM_DECL)
11391 if (DECL_NAME (parm)
11392 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
11393 "__builtin_va_alist"))
11394 gen_unspecified_parameters_die (parm, subr_die);
11395 else
11396 gen_decl_die (parm, subr_die);
11399 /* Decide whether we need an unspecified_parameters DIE at the end.
11400 There are 2 more cases to do this for: 1) the ansi ... declaration -
11401 this is detectable when the end of the arg list is not a
11402 void_type_node 2) an unprototyped function declaration (not a
11403 definition). This just means that we have no info about the
11404 parameters at all. */
11405 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
11406 if (fn_arg_types != NULL)
11408 /* This is the prototyped case, check for.... */
11409 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
11410 gen_unspecified_parameters_die (decl, subr_die);
11412 else if (DECL_INITIAL (decl) == NULL_TREE)
11413 gen_unspecified_parameters_die (decl, subr_die);
11416 /* Output Dwarf info for all of the stuff within the body of the function
11417 (if it has one - it may be just a declaration). */
11418 outer_scope = DECL_INITIAL (decl);
11420 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
11421 a function. This BLOCK actually represents the outermost binding contour
11422 for the function, i.e. the contour in which the function's formal
11423 parameters and labels get declared. Curiously, it appears that the front
11424 end doesn't actually put the PARM_DECL nodes for the current function onto
11425 the BLOCK_VARS list for this outer scope, but are strung off of the
11426 DECL_ARGUMENTS list for the function instead.
11428 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
11429 the LABEL_DECL nodes for the function however, and we output DWARF info
11430 for those in decls_for_scope. Just within the `outer_scope' there will be
11431 a BLOCK node representing the function's outermost pair of curly braces,
11432 and any blocks used for the base and member initializers of a C++
11433 constructor function. */
11434 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
11436 /* Emit a DW_TAG_variable DIE for a named return value. */
11437 if (DECL_NAME (DECL_RESULT (decl)))
11438 gen_decl_die (DECL_RESULT (decl), subr_die);
11440 current_function_has_inlines = 0;
11441 decls_for_scope (outer_scope, subr_die, 0);
11443 #if 0 && defined (MIPS_DEBUGGING_INFO)
11444 if (current_function_has_inlines)
11446 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
11447 if (! comp_unit_has_inlines)
11449 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
11450 comp_unit_has_inlines = 1;
11453 #endif
11457 /* Generate a DIE to represent a declared data object. */
11459 static void
11460 gen_variable_die (tree decl, dw_die_ref context_die)
11462 tree origin = decl_ultimate_origin (decl);
11463 dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
11465 dw_die_ref old_die = lookup_decl_die (decl);
11466 int declaration = (DECL_EXTERNAL (decl)
11467 || class_or_namespace_scope_p (context_die));
11469 if (origin != NULL)
11470 add_abstract_origin_attribute (var_die, origin);
11472 /* Loop unrolling can create multiple blocks that refer to the same
11473 static variable, so we must test for the DW_AT_declaration flag.
11475 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
11476 copy decls and set the DECL_ABSTRACT flag on them instead of
11477 sharing them.
11479 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
11480 else if (old_die && TREE_STATIC (decl)
11481 && get_AT_flag (old_die, DW_AT_declaration) == 1)
11483 /* This is a definition of a C++ class level static. */
11484 add_AT_specification (var_die, old_die);
11485 if (DECL_NAME (decl))
11487 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11488 unsigned file_index = lookup_filename (s.file);
11490 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11491 add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
11493 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11494 != (unsigned) s.line)
11496 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
11499 else
11501 add_name_and_src_coords_attributes (var_die, decl);
11502 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
11503 TREE_THIS_VOLATILE (decl), context_die);
11505 if (TREE_PUBLIC (decl))
11506 add_AT_flag (var_die, DW_AT_external, 1);
11508 if (DECL_ARTIFICIAL (decl))
11509 add_AT_flag (var_die, DW_AT_artificial, 1);
11511 if (TREE_PROTECTED (decl))
11512 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
11513 else if (TREE_PRIVATE (decl))
11514 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
11517 if (declaration)
11518 add_AT_flag (var_die, DW_AT_declaration, 1);
11520 if (DECL_ABSTRACT (decl) || declaration)
11521 equate_decl_number_to_die (decl, var_die);
11523 if (! declaration && ! DECL_ABSTRACT (decl))
11525 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
11526 add_pubname (decl, var_die);
11528 else
11529 tree_add_const_value_attribute (var_die, decl);
11532 /* Generate a DIE to represent a label identifier. */
11534 static void
11535 gen_label_die (tree decl, dw_die_ref context_die)
11537 tree origin = decl_ultimate_origin (decl);
11538 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
11539 rtx insn;
11540 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11542 if (origin != NULL)
11543 add_abstract_origin_attribute (lbl_die, origin);
11544 else
11545 add_name_and_src_coords_attributes (lbl_die, decl);
11547 if (DECL_ABSTRACT (decl))
11548 equate_decl_number_to_die (decl, lbl_die);
11549 else
11551 insn = DECL_RTL_IF_SET (decl);
11553 /* Deleted labels are programmer specified labels which have been
11554 eliminated because of various optimizations. We still emit them
11555 here so that it is possible to put breakpoints on them. */
11556 if (insn
11557 && (LABEL_P (insn)
11558 || ((NOTE_P (insn)
11559 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
11561 /* When optimization is enabled (via -O) some parts of the compiler
11562 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
11563 represent source-level labels which were explicitly declared by
11564 the user. This really shouldn't be happening though, so catch
11565 it if it ever does happen. */
11566 if (INSN_DELETED_P (insn))
11567 abort ();
11569 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
11570 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
11575 /* Generate a DIE for a lexical block. */
11577 static void
11578 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
11580 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
11581 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11583 if (! BLOCK_ABSTRACT (stmt))
11585 if (BLOCK_FRAGMENT_CHAIN (stmt))
11587 tree chain;
11589 add_AT_range_list (stmt_die, DW_AT_ranges, add_ranges (stmt));
11591 chain = BLOCK_FRAGMENT_CHAIN (stmt);
11594 add_ranges (chain);
11595 chain = BLOCK_FRAGMENT_CHAIN (chain);
11597 while (chain);
11598 add_ranges (NULL);
11600 else
11602 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
11603 BLOCK_NUMBER (stmt));
11604 add_AT_lbl_id (stmt_die, DW_AT_low_pc, label);
11605 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
11606 BLOCK_NUMBER (stmt));
11607 add_AT_lbl_id (stmt_die, DW_AT_high_pc, label);
11611 decls_for_scope (stmt, stmt_die, depth);
11614 /* Generate a DIE for an inlined subprogram. */
11616 static void
11617 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
11619 tree decl = block_ultimate_origin (stmt);
11621 /* Emit info for the abstract instance first, if we haven't yet. We
11622 must emit this even if the block is abstract, otherwise when we
11623 emit the block below (or elsewhere), we may end up trying to emit
11624 a die whose origin die hasn't been emitted, and crashing. */
11625 dwarf2out_abstract_function (decl);
11627 if (! BLOCK_ABSTRACT (stmt))
11629 dw_die_ref subr_die
11630 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
11631 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11633 add_abstract_origin_attribute (subr_die, decl);
11634 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
11635 BLOCK_NUMBER (stmt));
11636 add_AT_lbl_id (subr_die, DW_AT_low_pc, label);
11637 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
11638 BLOCK_NUMBER (stmt));
11639 add_AT_lbl_id (subr_die, DW_AT_high_pc, label);
11640 decls_for_scope (stmt, subr_die, depth);
11641 current_function_has_inlines = 1;
11643 else
11644 /* We may get here if we're the outer block of function A that was
11645 inlined into function B that was inlined into function C. When
11646 generating debugging info for C, dwarf2out_abstract_function(B)
11647 would mark all inlined blocks as abstract, including this one.
11648 So, we wouldn't (and shouldn't) expect labels to be generated
11649 for this one. Instead, just emit debugging info for
11650 declarations within the block. This is particularly important
11651 in the case of initializers of arguments passed from B to us:
11652 if they're statement expressions containing declarations, we
11653 wouldn't generate dies for their abstract variables, and then,
11654 when generating dies for the real variables, we'd die (pun
11655 intended :-) */
11656 gen_lexical_block_die (stmt, context_die, depth);
11659 /* Generate a DIE for a field in a record, or structure. */
11661 static void
11662 gen_field_die (tree decl, dw_die_ref context_die)
11664 dw_die_ref decl_die;
11666 if (TREE_TYPE (decl) == error_mark_node)
11667 return;
11669 decl_die = new_die (DW_TAG_member, context_die, decl);
11670 add_name_and_src_coords_attributes (decl_die, decl);
11671 add_type_attribute (decl_die, member_declared_type (decl),
11672 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
11673 context_die);
11675 if (DECL_BIT_FIELD_TYPE (decl))
11677 add_byte_size_attribute (decl_die, decl);
11678 add_bit_size_attribute (decl_die, decl);
11679 add_bit_offset_attribute (decl_die, decl);
11682 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
11683 add_data_member_location_attribute (decl_die, decl);
11685 if (DECL_ARTIFICIAL (decl))
11686 add_AT_flag (decl_die, DW_AT_artificial, 1);
11688 if (TREE_PROTECTED (decl))
11689 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
11690 else if (TREE_PRIVATE (decl))
11691 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
11694 #if 0
11695 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
11696 Use modified_type_die instead.
11697 We keep this code here just in case these types of DIEs may be needed to
11698 represent certain things in other languages (e.g. Pascal) someday. */
11700 static void
11701 gen_pointer_type_die (tree type, dw_die_ref context_die)
11703 dw_die_ref ptr_die
11704 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
11706 equate_type_number_to_die (type, ptr_die);
11707 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
11708 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
11711 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
11712 Use modified_type_die instead.
11713 We keep this code here just in case these types of DIEs may be needed to
11714 represent certain things in other languages (e.g. Pascal) someday. */
11716 static void
11717 gen_reference_type_die (tree type, dw_die_ref context_die)
11719 dw_die_ref ref_die
11720 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
11722 equate_type_number_to_die (type, ref_die);
11723 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
11724 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
11726 #endif
11728 /* Generate a DIE for a pointer to a member type. */
11730 static void
11731 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
11733 dw_die_ref ptr_die
11734 = new_die (DW_TAG_ptr_to_member_type,
11735 scope_die_for (type, context_die), type);
11737 equate_type_number_to_die (type, ptr_die);
11738 add_AT_die_ref (ptr_die, DW_AT_containing_type,
11739 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
11740 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
11743 /* Generate the DIE for the compilation unit. */
11745 static dw_die_ref
11746 gen_compile_unit_die (const char *filename)
11748 dw_die_ref die;
11749 char producer[250];
11750 const char *language_string = lang_hooks.name;
11751 int language;
11753 die = new_die (DW_TAG_compile_unit, NULL, NULL);
11755 if (filename)
11757 add_name_attribute (die, filename);
11758 /* Don't add cwd for <built-in>. */
11759 if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
11760 add_comp_dir_attribute (die);
11763 sprintf (producer, "%s %s", language_string, version_string);
11765 #ifdef MIPS_DEBUGGING_INFO
11766 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
11767 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
11768 not appear in the producer string, the debugger reaches the conclusion
11769 that the object file is stripped and has no debugging information.
11770 To get the MIPS/SGI debugger to believe that there is debugging
11771 information in the object file, we add a -g to the producer string. */
11772 if (debug_info_level > DINFO_LEVEL_TERSE)
11773 strcat (producer, " -g");
11774 #endif
11776 add_AT_string (die, DW_AT_producer, producer);
11778 if (strcmp (language_string, "GNU C++") == 0)
11779 language = DW_LANG_C_plus_plus;
11780 else if (strcmp (language_string, "GNU Ada") == 0)
11781 language = DW_LANG_Ada95;
11782 else if (strcmp (language_string, "GNU F77") == 0)
11783 language = DW_LANG_Fortran77;
11784 else if (strcmp (language_string, "GNU F95") == 0)
11785 language = DW_LANG_Fortran95;
11786 else if (strcmp (language_string, "GNU Pascal") == 0)
11787 language = DW_LANG_Pascal83;
11788 else if (strcmp (language_string, "GNU Java") == 0)
11789 language = DW_LANG_Java;
11790 else
11791 language = DW_LANG_C89;
11793 add_AT_unsigned (die, DW_AT_language, language);
11794 return die;
11797 /* Generate a DIE for a string type. */
11799 static void
11800 gen_string_type_die (tree type, dw_die_ref context_die)
11802 dw_die_ref type_die
11803 = new_die (DW_TAG_string_type, scope_die_for (type, context_die), type);
11805 equate_type_number_to_die (type, type_die);
11807 /* ??? Fudge the string length attribute for now.
11808 TODO: add string length info. */
11809 #if 0
11810 string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
11811 bound_representation (upper_bound, 0, 'u');
11812 #endif
11815 /* Generate the DIE for a base class. */
11817 static void
11818 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
11820 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
11822 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
11823 add_data_member_location_attribute (die, binfo);
11825 if (BINFO_VIRTUAL_P (binfo))
11826 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11828 if (access == access_public_node)
11829 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
11830 else if (access == access_protected_node)
11831 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
11834 /* Generate a DIE for a class member. */
11836 static void
11837 gen_member_die (tree type, dw_die_ref context_die)
11839 tree member;
11840 tree binfo = TYPE_BINFO (type);
11841 dw_die_ref child;
11843 /* If this is not an incomplete type, output descriptions of each of its
11844 members. Note that as we output the DIEs necessary to represent the
11845 members of this record or union type, we will also be trying to output
11846 DIEs to represent the *types* of those members. However the `type'
11847 function (above) will specifically avoid generating type DIEs for member
11848 types *within* the list of member DIEs for this (containing) type except
11849 for those types (of members) which are explicitly marked as also being
11850 members of this (containing) type themselves. The g++ front- end can
11851 force any given type to be treated as a member of some other (containing)
11852 type by setting the TYPE_CONTEXT of the given (member) type to point to
11853 the TREE node representing the appropriate (containing) type. */
11855 /* First output info about the base classes. */
11856 if (binfo)
11858 VEC (tree) *accesses = BINFO_BASE_ACCESSES (binfo);
11859 int i;
11860 tree base;
11862 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
11863 gen_inheritance_die (base,
11864 (accesses ? VEC_index (tree, accesses, i)
11865 : access_public_node), context_die);
11868 /* Now output info about the data members and type members. */
11869 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
11871 /* If we thought we were generating minimal debug info for TYPE
11872 and then changed our minds, some of the member declarations
11873 may have already been defined. Don't define them again, but
11874 do put them in the right order. */
11876 child = lookup_decl_die (member);
11877 if (child)
11878 splice_child_die (context_die, child);
11879 else
11880 gen_decl_die (member, context_die);
11883 /* Now output info about the function members (if any). */
11884 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
11886 /* Don't include clones in the member list. */
11887 if (DECL_ABSTRACT_ORIGIN (member))
11888 continue;
11890 child = lookup_decl_die (member);
11891 if (child)
11892 splice_child_die (context_die, child);
11893 else
11894 gen_decl_die (member, context_die);
11898 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
11899 is set, we pretend that the type was never defined, so we only get the
11900 member DIEs needed by later specification DIEs. */
11902 static void
11903 gen_struct_or_union_type_die (tree type, dw_die_ref context_die)
11905 dw_die_ref type_die = lookup_type_die (type);
11906 dw_die_ref scope_die = 0;
11907 int nested = 0;
11908 int complete = (TYPE_SIZE (type)
11909 && (! TYPE_STUB_DECL (type)
11910 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
11911 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
11913 if (type_die && ! complete)
11914 return;
11916 if (TYPE_CONTEXT (type) != NULL_TREE
11917 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
11918 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
11919 nested = 1;
11921 scope_die = scope_die_for (type, context_die);
11923 if (! type_die || (nested && scope_die == comp_unit_die))
11924 /* First occurrence of type or toplevel definition of nested class. */
11926 dw_die_ref old_die = type_die;
11928 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
11929 ? DW_TAG_structure_type : DW_TAG_union_type,
11930 scope_die, type);
11931 equate_type_number_to_die (type, type_die);
11932 if (old_die)
11933 add_AT_specification (type_die, old_die);
11934 else
11935 add_name_attribute (type_die, type_tag (type));
11937 else
11938 remove_AT (type_die, DW_AT_declaration);
11940 /* If this type has been completed, then give it a byte_size attribute and
11941 then give a list of members. */
11942 if (complete && !ns_decl)
11944 /* Prevent infinite recursion in cases where the type of some member of
11945 this type is expressed in terms of this type itself. */
11946 TREE_ASM_WRITTEN (type) = 1;
11947 add_byte_size_attribute (type_die, type);
11948 if (TYPE_STUB_DECL (type) != NULL_TREE)
11949 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11951 /* If the first reference to this type was as the return type of an
11952 inline function, then it may not have a parent. Fix this now. */
11953 if (type_die->die_parent == NULL)
11954 add_child_die (scope_die, type_die);
11956 push_decl_scope (type);
11957 gen_member_die (type, type_die);
11958 pop_decl_scope ();
11960 /* GNU extension: Record what type our vtable lives in. */
11961 if (TYPE_VFIELD (type))
11963 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
11965 gen_type_die (vtype, context_die);
11966 add_AT_die_ref (type_die, DW_AT_containing_type,
11967 lookup_type_die (vtype));
11970 else
11972 add_AT_flag (type_die, DW_AT_declaration, 1);
11974 /* We don't need to do this for function-local types. */
11975 if (TYPE_STUB_DECL (type)
11976 && ! decl_function_context (TYPE_STUB_DECL (type)))
11977 VARRAY_PUSH_TREE (incomplete_types, type);
11981 /* Generate a DIE for a subroutine _type_. */
11983 static void
11984 gen_subroutine_type_die (tree type, dw_die_ref context_die)
11986 tree return_type = TREE_TYPE (type);
11987 dw_die_ref subr_die
11988 = new_die (DW_TAG_subroutine_type,
11989 scope_die_for (type, context_die), type);
11991 equate_type_number_to_die (type, subr_die);
11992 add_prototyped_attribute (subr_die, type);
11993 add_type_attribute (subr_die, return_type, 0, 0, context_die);
11994 gen_formal_types_die (type, subr_die);
11997 /* Generate a DIE for a type definition. */
11999 static void
12000 gen_typedef_die (tree decl, dw_die_ref context_die)
12002 dw_die_ref type_die;
12003 tree origin;
12005 if (TREE_ASM_WRITTEN (decl))
12006 return;
12008 TREE_ASM_WRITTEN (decl) = 1;
12009 type_die = new_die (DW_TAG_typedef, context_die, decl);
12010 origin = decl_ultimate_origin (decl);
12011 if (origin != NULL)
12012 add_abstract_origin_attribute (type_die, origin);
12013 else
12015 tree type;
12017 add_name_and_src_coords_attributes (type_die, decl);
12018 if (DECL_ORIGINAL_TYPE (decl))
12020 type = DECL_ORIGINAL_TYPE (decl);
12022 if (type == TREE_TYPE (decl))
12023 abort ();
12024 else
12025 equate_type_number_to_die (TREE_TYPE (decl), type_die);
12027 else
12028 type = TREE_TYPE (decl);
12030 add_type_attribute (type_die, type, TREE_READONLY (decl),
12031 TREE_THIS_VOLATILE (decl), context_die);
12034 if (DECL_ABSTRACT (decl))
12035 equate_decl_number_to_die (decl, type_die);
12038 /* Generate a type description DIE. */
12040 static void
12041 gen_type_die (tree type, dw_die_ref context_die)
12043 int need_pop;
12045 if (type == NULL_TREE || type == error_mark_node)
12046 return;
12048 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12049 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
12051 if (TREE_ASM_WRITTEN (type))
12052 return;
12054 /* Prevent broken recursion; we can't hand off to the same type. */
12055 if (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) == type)
12056 abort ();
12058 TREE_ASM_WRITTEN (type) = 1;
12059 gen_decl_die (TYPE_NAME (type), context_die);
12060 return;
12063 /* We are going to output a DIE to represent the unqualified version
12064 of this type (i.e. without any const or volatile qualifiers) so
12065 get the main variant (i.e. the unqualified version) of this type
12066 now. (Vectors are special because the debugging info is in the
12067 cloned type itself). */
12068 if (TREE_CODE (type) != VECTOR_TYPE)
12069 type = type_main_variant (type);
12071 if (TREE_ASM_WRITTEN (type))
12072 return;
12074 switch (TREE_CODE (type))
12076 case ERROR_MARK:
12077 break;
12079 case POINTER_TYPE:
12080 case REFERENCE_TYPE:
12081 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
12082 ensures that the gen_type_die recursion will terminate even if the
12083 type is recursive. Recursive types are possible in Ada. */
12084 /* ??? We could perhaps do this for all types before the switch
12085 statement. */
12086 TREE_ASM_WRITTEN (type) = 1;
12088 /* For these types, all that is required is that we output a DIE (or a
12089 set of DIEs) to represent the "basis" type. */
12090 gen_type_die (TREE_TYPE (type), context_die);
12091 break;
12093 case OFFSET_TYPE:
12094 /* This code is used for C++ pointer-to-data-member types.
12095 Output a description of the relevant class type. */
12096 gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
12098 /* Output a description of the type of the object pointed to. */
12099 gen_type_die (TREE_TYPE (type), context_die);
12101 /* Now output a DIE to represent this pointer-to-data-member type
12102 itself. */
12103 gen_ptr_to_mbr_type_die (type, context_die);
12104 break;
12106 case SET_TYPE:
12107 gen_type_die (TYPE_DOMAIN (type), context_die);
12108 gen_set_type_die (type, context_die);
12109 break;
12111 case FILE_TYPE:
12112 gen_type_die (TREE_TYPE (type), context_die);
12113 abort (); /* No way to represent these in Dwarf yet! */
12114 break;
12116 case FUNCTION_TYPE:
12117 /* Force out return type (in case it wasn't forced out already). */
12118 gen_type_die (TREE_TYPE (type), context_die);
12119 gen_subroutine_type_die (type, context_die);
12120 break;
12122 case METHOD_TYPE:
12123 /* Force out return type (in case it wasn't forced out already). */
12124 gen_type_die (TREE_TYPE (type), context_die);
12125 gen_subroutine_type_die (type, context_die);
12126 break;
12128 case ARRAY_TYPE:
12129 if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
12131 gen_type_die (TREE_TYPE (type), context_die);
12132 gen_string_type_die (type, context_die);
12134 else
12135 gen_array_type_die (type, context_die);
12136 break;
12138 case VECTOR_TYPE:
12139 gen_array_type_die (type, context_die);
12140 break;
12142 case ENUMERAL_TYPE:
12143 case RECORD_TYPE:
12144 case UNION_TYPE:
12145 case QUAL_UNION_TYPE:
12146 /* If this is a nested type whose containing class hasn't been written
12147 out yet, writing it out will cover this one, too. This does not apply
12148 to instantiations of member class templates; they need to be added to
12149 the containing class as they are generated. FIXME: This hurts the
12150 idea of combining type decls from multiple TUs, since we can't predict
12151 what set of template instantiations we'll get. */
12152 if (TYPE_CONTEXT (type)
12153 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12154 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
12156 gen_type_die (TYPE_CONTEXT (type), context_die);
12158 if (TREE_ASM_WRITTEN (type))
12159 return;
12161 /* If that failed, attach ourselves to the stub. */
12162 push_decl_scope (TYPE_CONTEXT (type));
12163 context_die = lookup_type_die (TYPE_CONTEXT (type));
12164 need_pop = 1;
12166 else
12168 declare_in_namespace (type, context_die);
12169 need_pop = 0;
12172 if (TREE_CODE (type) == ENUMERAL_TYPE)
12173 gen_enumeration_type_die (type, context_die);
12174 else
12175 gen_struct_or_union_type_die (type, context_die);
12177 if (need_pop)
12178 pop_decl_scope ();
12180 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
12181 it up if it is ever completed. gen_*_type_die will set it for us
12182 when appropriate. */
12183 return;
12185 case VOID_TYPE:
12186 case INTEGER_TYPE:
12187 case REAL_TYPE:
12188 case COMPLEX_TYPE:
12189 case BOOLEAN_TYPE:
12190 case CHAR_TYPE:
12191 /* No DIEs needed for fundamental types. */
12192 break;
12194 case LANG_TYPE:
12195 /* No Dwarf representation currently defined. */
12196 break;
12198 default:
12199 abort ();
12202 TREE_ASM_WRITTEN (type) = 1;
12205 /* Generate a DIE for a tagged type instantiation. */
12207 static void
12208 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
12210 if (type == NULL_TREE || type == error_mark_node)
12211 return;
12213 /* We are going to output a DIE to represent the unqualified version of
12214 this type (i.e. without any const or volatile qualifiers) so make sure
12215 that we have the main variant (i.e. the unqualified version) of this
12216 type now. */
12217 if (type != type_main_variant (type))
12218 abort ();
12220 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
12221 an instance of an unresolved type. */
12223 switch (TREE_CODE (type))
12225 case ERROR_MARK:
12226 break;
12228 case ENUMERAL_TYPE:
12229 gen_inlined_enumeration_type_die (type, context_die);
12230 break;
12232 case RECORD_TYPE:
12233 gen_inlined_structure_type_die (type, context_die);
12234 break;
12236 case UNION_TYPE:
12237 case QUAL_UNION_TYPE:
12238 gen_inlined_union_type_die (type, context_die);
12239 break;
12241 default:
12242 abort ();
12246 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12247 things which are local to the given block. */
12249 static void
12250 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
12252 int must_output_die = 0;
12253 tree origin;
12254 tree decl;
12255 enum tree_code origin_code;
12257 /* Ignore blocks never really used to make RTL. */
12258 if (stmt == NULL_TREE || !TREE_USED (stmt)
12259 || (!TREE_ASM_WRITTEN (stmt) && !BLOCK_ABSTRACT (stmt)))
12260 return;
12262 /* If the block is one fragment of a non-contiguous block, do not
12263 process the variables, since they will have been done by the
12264 origin block. Do process subblocks. */
12265 if (BLOCK_FRAGMENT_ORIGIN (stmt))
12267 tree sub;
12269 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
12270 gen_block_die (sub, context_die, depth + 1);
12272 return;
12275 /* Determine the "ultimate origin" of this block. This block may be an
12276 inlined instance of an inlined instance of inline function, so we have
12277 to trace all of the way back through the origin chain to find out what
12278 sort of node actually served as the original seed for the creation of
12279 the current block. */
12280 origin = block_ultimate_origin (stmt);
12281 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
12283 /* Determine if we need to output any Dwarf DIEs at all to represent this
12284 block. */
12285 if (origin_code == FUNCTION_DECL)
12286 /* The outer scopes for inlinings *must* always be represented. We
12287 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
12288 must_output_die = 1;
12289 else
12291 /* In the case where the current block represents an inlining of the
12292 "body block" of an inline function, we must *NOT* output any DIE for
12293 this block because we have already output a DIE to represent the whole
12294 inlined function scope and the "body block" of any function doesn't
12295 really represent a different scope according to ANSI C rules. So we
12296 check here to make sure that this block does not represent a "body
12297 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
12298 if (! is_body_block (origin ? origin : stmt))
12300 /* Determine if this block directly contains any "significant"
12301 local declarations which we will need to output DIEs for. */
12302 if (debug_info_level > DINFO_LEVEL_TERSE)
12303 /* We are not in terse mode so *any* local declaration counts
12304 as being a "significant" one. */
12305 must_output_die = (BLOCK_VARS (stmt) != NULL);
12306 else
12307 /* We are in terse mode, so only local (nested) function
12308 definitions count as "significant" local declarations. */
12309 for (decl = BLOCK_VARS (stmt);
12310 decl != NULL; decl = TREE_CHAIN (decl))
12311 if (TREE_CODE (decl) == FUNCTION_DECL
12312 && DECL_INITIAL (decl))
12314 must_output_die = 1;
12315 break;
12320 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
12321 DIE for any block which contains no significant local declarations at
12322 all. Rather, in such cases we just call `decls_for_scope' so that any
12323 needed Dwarf info for any sub-blocks will get properly generated. Note
12324 that in terse mode, our definition of what constitutes a "significant"
12325 local declaration gets restricted to include only inlined function
12326 instances and local (nested) function definitions. */
12327 if (must_output_die)
12329 if (origin_code == FUNCTION_DECL)
12330 gen_inlined_subroutine_die (stmt, context_die, depth);
12331 else
12332 gen_lexical_block_die (stmt, context_die, depth);
12334 else
12335 decls_for_scope (stmt, context_die, depth);
12338 /* Generate all of the decls declared within a given scope and (recursively)
12339 all of its sub-blocks. */
12341 static void
12342 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
12344 tree decl;
12345 tree subblocks;
12347 /* Ignore blocks never really used to make RTL. */
12348 if (stmt == NULL_TREE || ! TREE_USED (stmt))
12349 return;
12351 /* Output the DIEs to represent all of the data objects and typedefs
12352 declared directly within this block but not within any nested
12353 sub-blocks. Also, nested function and tag DIEs have been
12354 generated with a parent of NULL; fix that up now. */
12355 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
12357 dw_die_ref die;
12359 if (TREE_CODE (decl) == FUNCTION_DECL)
12360 die = lookup_decl_die (decl);
12361 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
12362 die = lookup_type_die (TREE_TYPE (decl));
12363 else
12364 die = NULL;
12366 if (die != NULL && die->die_parent == NULL)
12367 add_child_die (context_die, die);
12368 else
12369 gen_decl_die (decl, context_die);
12372 /* If we're at -g1, we're not interested in subblocks. */
12373 if (debug_info_level <= DINFO_LEVEL_TERSE)
12374 return;
12376 /* Output the DIEs to represent all sub-blocks (and the items declared
12377 therein) of this block. */
12378 for (subblocks = BLOCK_SUBBLOCKS (stmt);
12379 subblocks != NULL;
12380 subblocks = BLOCK_CHAIN (subblocks))
12381 gen_block_die (subblocks, context_die, depth + 1);
12384 /* Is this a typedef we can avoid emitting? */
12386 static inline int
12387 is_redundant_typedef (tree decl)
12389 if (TYPE_DECL_IS_STUB (decl))
12390 return 1;
12392 if (DECL_ARTIFICIAL (decl)
12393 && DECL_CONTEXT (decl)
12394 && is_tagged_type (DECL_CONTEXT (decl))
12395 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
12396 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
12397 /* Also ignore the artificial member typedef for the class name. */
12398 return 1;
12400 return 0;
12403 /* Returns the DIE for decl or aborts. */
12405 static dw_die_ref
12406 force_decl_die (tree decl)
12408 dw_die_ref decl_die;
12409 unsigned saved_external_flag;
12410 tree save_fn = NULL_TREE;
12411 decl_die = lookup_decl_die (decl);
12412 if (!decl_die)
12414 dw_die_ref context_die;
12415 tree decl_context = DECL_CONTEXT (decl);
12416 if (decl_context)
12418 /* Find die that represents this context. */
12419 if (TYPE_P (decl_context))
12420 context_die = force_type_die (decl_context);
12421 else
12422 context_die = force_decl_die (decl_context);
12424 else
12425 context_die = comp_unit_die;
12427 switch (TREE_CODE (decl))
12429 case FUNCTION_DECL:
12430 /* Clear current_function_decl, so that gen_subprogram_die thinks
12431 that this is a declaration. At this point, we just want to force
12432 declaration die. */
12433 save_fn = current_function_decl;
12434 current_function_decl = NULL_TREE;
12435 gen_subprogram_die (decl, context_die);
12436 current_function_decl = save_fn;
12437 break;
12439 case VAR_DECL:
12440 /* Set external flag to force declaration die. Restore it after
12441 gen_decl_die() call. */
12442 saved_external_flag = DECL_EXTERNAL (decl);
12443 DECL_EXTERNAL (decl) = 1;
12444 gen_decl_die (decl, context_die);
12445 DECL_EXTERNAL (decl) = saved_external_flag;
12446 break;
12448 case NAMESPACE_DECL:
12449 dwarf2out_decl (decl);
12450 break;
12452 default:
12453 abort ();
12456 /* See if we can find the die for this deci now.
12457 If not then abort. */
12458 if (!decl_die)
12459 decl_die = lookup_decl_die (decl);
12460 if (!decl_die)
12461 abort ();
12464 return decl_die;
12467 /* Returns the DIE for decl or aborts. */
12469 static dw_die_ref
12470 force_type_die (tree type)
12472 dw_die_ref type_die;
12474 type_die = lookup_type_die (type);
12475 if (!type_die)
12477 dw_die_ref context_die;
12478 if (TYPE_CONTEXT (type))
12479 if (TYPE_P (TYPE_CONTEXT (type)))
12480 context_die = force_type_die (TYPE_CONTEXT (type));
12481 else
12482 context_die = force_decl_die (TYPE_CONTEXT (type));
12483 else
12484 context_die = comp_unit_die;
12486 gen_type_die (type, context_die);
12487 type_die = lookup_type_die (type);
12488 if (!type_die)
12489 abort();
12491 return type_die;
12494 /* Force out any required namespaces to be able to output DECL,
12495 and return the new context_die for it, if it's changed. */
12497 static dw_die_ref
12498 setup_namespace_context (tree thing, dw_die_ref context_die)
12500 tree context = DECL_P (thing) ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing);
12501 if (context && TREE_CODE (context) == NAMESPACE_DECL)
12502 /* Force out the namespace. */
12503 context_die = force_decl_die (context);
12505 return context_die;
12508 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
12509 type) within its namespace, if appropriate.
12511 For compatibility with older debuggers, namespace DIEs only contain
12512 declarations; all definitions are emitted at CU scope. */
12514 static void
12515 declare_in_namespace (tree thing, dw_die_ref context_die)
12517 dw_die_ref ns_context;
12519 if (debug_info_level <= DINFO_LEVEL_TERSE)
12520 return;
12522 ns_context = setup_namespace_context (thing, context_die);
12524 if (ns_context != context_die)
12526 if (DECL_P (thing))
12527 gen_decl_die (thing, ns_context);
12528 else
12529 gen_type_die (thing, ns_context);
12533 /* Generate a DIE for a namespace or namespace alias. */
12535 static void
12536 gen_namespace_die (tree decl)
12538 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
12540 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
12541 they are an alias of. */
12542 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
12544 /* Output a real namespace. */
12545 dw_die_ref namespace_die
12546 = new_die (DW_TAG_namespace, context_die, decl);
12547 add_name_and_src_coords_attributes (namespace_die, decl);
12548 equate_decl_number_to_die (decl, namespace_die);
12550 else
12552 /* Output a namespace alias. */
12554 /* Force out the namespace we are an alias of, if necessary. */
12555 dw_die_ref origin_die
12556 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
12558 /* Now create the namespace alias DIE. */
12559 dw_die_ref namespace_die
12560 = new_die (DW_TAG_imported_declaration, context_die, decl);
12561 add_name_and_src_coords_attributes (namespace_die, decl);
12562 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
12563 equate_decl_number_to_die (decl, namespace_die);
12567 /* Generate Dwarf debug information for a decl described by DECL. */
12569 static void
12570 gen_decl_die (tree decl, dw_die_ref context_die)
12572 tree origin;
12574 if (DECL_P (decl) && DECL_IGNORED_P (decl))
12575 return;
12577 switch (TREE_CODE (decl))
12579 case ERROR_MARK:
12580 break;
12582 case CONST_DECL:
12583 /* The individual enumerators of an enum type get output when we output
12584 the Dwarf representation of the relevant enum type itself. */
12585 break;
12587 case FUNCTION_DECL:
12588 /* Don't output any DIEs to represent mere function declarations,
12589 unless they are class members or explicit block externs. */
12590 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
12591 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
12592 break;
12594 #if 0
12595 /* FIXME */
12596 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
12597 on local redeclarations of global functions. That seems broken. */
12598 if (current_function_decl != decl)
12599 /* This is only a declaration. */;
12600 #endif
12602 /* If we're emitting a clone, emit info for the abstract instance. */
12603 if (DECL_ORIGIN (decl) != decl)
12604 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
12606 /* If we're emitting an out-of-line copy of an inline function,
12607 emit info for the abstract instance and set up to refer to it. */
12608 else if (cgraph_function_possibly_inlined_p (decl)
12609 && ! DECL_ABSTRACT (decl)
12610 && ! class_or_namespace_scope_p (context_die)
12611 /* dwarf2out_abstract_function won't emit a die if this is just
12612 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
12613 that case, because that works only if we have a die. */
12614 && DECL_INITIAL (decl) != NULL_TREE)
12616 dwarf2out_abstract_function (decl);
12617 set_decl_origin_self (decl);
12620 /* Otherwise we're emitting the primary DIE for this decl. */
12621 else if (debug_info_level > DINFO_LEVEL_TERSE)
12623 /* Before we describe the FUNCTION_DECL itself, make sure that we
12624 have described its return type. */
12625 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
12627 /* And its virtual context. */
12628 if (DECL_VINDEX (decl) != NULL_TREE)
12629 gen_type_die (DECL_CONTEXT (decl), context_die);
12631 /* And its containing type. */
12632 origin = decl_class_context (decl);
12633 if (origin != NULL_TREE)
12634 gen_type_die_for_member (origin, decl, context_die);
12636 /* And its containing namespace. */
12637 declare_in_namespace (decl, context_die);
12640 /* Now output a DIE to represent the function itself. */
12641 gen_subprogram_die (decl, context_die);
12642 break;
12644 case TYPE_DECL:
12645 /* If we are in terse mode, don't generate any DIEs to represent any
12646 actual typedefs. */
12647 if (debug_info_level <= DINFO_LEVEL_TERSE)
12648 break;
12650 /* In the special case of a TYPE_DECL node representing the declaration
12651 of some type tag, if the given TYPE_DECL is marked as having been
12652 instantiated from some other (original) TYPE_DECL node (e.g. one which
12653 was generated within the original definition of an inline function) we
12654 have to generate a special (abbreviated) DW_TAG_structure_type,
12655 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
12656 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
12658 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
12659 break;
12662 if (is_redundant_typedef (decl))
12663 gen_type_die (TREE_TYPE (decl), context_die);
12664 else
12665 /* Output a DIE to represent the typedef itself. */
12666 gen_typedef_die (decl, context_die);
12667 break;
12669 case LABEL_DECL:
12670 if (debug_info_level >= DINFO_LEVEL_NORMAL)
12671 gen_label_die (decl, context_die);
12672 break;
12674 case VAR_DECL:
12675 case RESULT_DECL:
12676 /* If we are in terse mode, don't generate any DIEs to represent any
12677 variable declarations or definitions. */
12678 if (debug_info_level <= DINFO_LEVEL_TERSE)
12679 break;
12681 /* Output any DIEs that are needed to specify the type of this data
12682 object. */
12683 gen_type_die (TREE_TYPE (decl), context_die);
12685 /* And its containing type. */
12686 origin = decl_class_context (decl);
12687 if (origin != NULL_TREE)
12688 gen_type_die_for_member (origin, decl, context_die);
12690 /* And its containing namespace. */
12691 declare_in_namespace (decl, context_die);
12693 /* Now output the DIE to represent the data object itself. This gets
12694 complicated because of the possibility that the VAR_DECL really
12695 represents an inlined instance of a formal parameter for an inline
12696 function. */
12697 origin = decl_ultimate_origin (decl);
12698 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
12699 gen_formal_parameter_die (decl, context_die);
12700 else
12701 gen_variable_die (decl, context_die);
12702 break;
12704 case FIELD_DECL:
12705 /* Ignore the nameless fields that are used to skip bits but handle C++
12706 anonymous unions and structs. */
12707 if (DECL_NAME (decl) != NULL_TREE
12708 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
12709 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
12711 gen_type_die (member_declared_type (decl), context_die);
12712 gen_field_die (decl, context_die);
12714 break;
12716 case PARM_DECL:
12717 gen_type_die (TREE_TYPE (decl), context_die);
12718 gen_formal_parameter_die (decl, context_die);
12719 break;
12721 case NAMESPACE_DECL:
12722 gen_namespace_die (decl);
12723 break;
12725 default:
12726 if ((int)TREE_CODE (decl) > NUM_TREE_CODES)
12727 /* Probably some frontend-internal decl. Assume we don't care. */
12728 break;
12729 abort ();
12733 /* Add Ada "use" clause information for SGI Workshop debugger. */
12735 void
12736 dwarf2out_add_library_unit_info (const char *filename, const char *context_list)
12738 unsigned int file_index;
12740 if (filename != NULL)
12742 dw_die_ref unit_die = new_die (DW_TAG_module, comp_unit_die, NULL);
12743 tree context_list_decl
12744 = build_decl (LABEL_DECL, get_identifier (context_list),
12745 void_type_node);
12747 TREE_PUBLIC (context_list_decl) = TRUE;
12748 add_name_attribute (unit_die, context_list);
12749 file_index = lookup_filename (filename);
12750 add_AT_unsigned (unit_die, DW_AT_decl_file, file_index);
12751 add_pubname (context_list_decl, unit_die);
12755 /* Output debug information for global decl DECL. Called from toplev.c after
12756 compilation proper has finished. */
12758 static void
12759 dwarf2out_global_decl (tree decl)
12761 /* Output DWARF2 information for file-scope tentative data object
12762 declarations, file-scope (extern) function declarations (which had no
12763 corresponding body) and file-scope tagged type declarations and
12764 definitions which have not yet been forced out. */
12765 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
12766 dwarf2out_decl (decl);
12769 /* Output debug information for type decl DECL. Called from toplev.c
12770 and from language front ends (to record built-in types). */
12771 static void
12772 dwarf2out_type_decl (tree decl, int local)
12774 if (!local)
12775 dwarf2out_decl (decl);
12778 /* Output debug information for imported module or decl. */
12780 static void
12781 dwarf2out_imported_module_or_decl (tree decl, tree context)
12783 dw_die_ref imported_die, at_import_die;
12784 dw_die_ref scope_die;
12785 unsigned file_index;
12786 expanded_location xloc;
12788 if (debug_info_level <= DINFO_LEVEL_TERSE)
12789 return;
12791 if (!decl)
12792 abort ();
12794 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
12795 We need decl DIE for reference and scope die. First, get DIE for the decl
12796 itself. */
12798 /* Get the scope die for decl context. Use comp_unit_die for global module
12799 or decl. If die is not found for non globals, force new die. */
12800 if (!context)
12801 scope_die = comp_unit_die;
12802 else if (TYPE_P (context))
12803 scope_die = force_type_die (context);
12804 else
12805 scope_die = force_decl_die (context);
12807 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
12808 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
12809 at_import_die = force_type_die (TREE_TYPE (decl));
12810 else
12811 at_import_die = force_decl_die (decl);
12813 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
12814 if (TREE_CODE (decl) == NAMESPACE_DECL)
12815 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
12816 else
12817 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
12819 xloc = expand_location (input_location);
12820 file_index = lookup_filename (xloc.file);
12821 add_AT_unsigned (imported_die, DW_AT_decl_file, file_index);
12822 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
12823 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
12826 /* Write the debugging output for DECL. */
12828 void
12829 dwarf2out_decl (tree decl)
12831 dw_die_ref context_die = comp_unit_die;
12833 switch (TREE_CODE (decl))
12835 case ERROR_MARK:
12836 return;
12838 case FUNCTION_DECL:
12839 /* What we would really like to do here is to filter out all mere
12840 file-scope declarations of file-scope functions which are never
12841 referenced later within this translation unit (and keep all of ones
12842 that *are* referenced later on) but we aren't clairvoyant, so we have
12843 no idea which functions will be referenced in the future (i.e. later
12844 on within the current translation unit). So here we just ignore all
12845 file-scope function declarations which are not also definitions. If
12846 and when the debugger needs to know something about these functions,
12847 it will have to hunt around and find the DWARF information associated
12848 with the definition of the function.
12850 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
12851 nodes represent definitions and which ones represent mere
12852 declarations. We have to check DECL_INITIAL instead. That's because
12853 the C front-end supports some weird semantics for "extern inline"
12854 function definitions. These can get inlined within the current
12855 translation unit (an thus, we need to generate Dwarf info for their
12856 abstract instances so that the Dwarf info for the concrete inlined
12857 instances can have something to refer to) but the compiler never
12858 generates any out-of-lines instances of such things (despite the fact
12859 that they *are* definitions).
12861 The important point is that the C front-end marks these "extern
12862 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
12863 them anyway. Note that the C++ front-end also plays some similar games
12864 for inline function definitions appearing within include files which
12865 also contain `#pragma interface' pragmas. */
12866 if (DECL_INITIAL (decl) == NULL_TREE)
12867 return;
12869 /* If we're a nested function, initially use a parent of NULL; if we're
12870 a plain function, this will be fixed up in decls_for_scope. If
12871 we're a method, it will be ignored, since we already have a DIE. */
12872 if (decl_function_context (decl)
12873 /* But if we're in terse mode, we don't care about scope. */
12874 && debug_info_level > DINFO_LEVEL_TERSE)
12875 context_die = NULL;
12876 break;
12878 case VAR_DECL:
12879 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
12880 declaration and if the declaration was never even referenced from
12881 within this entire compilation unit. We suppress these DIEs in
12882 order to save space in the .debug section (by eliminating entries
12883 which are probably useless). Note that we must not suppress
12884 block-local extern declarations (whether used or not) because that
12885 would screw-up the debugger's name lookup mechanism and cause it to
12886 miss things which really ought to be in scope at a given point. */
12887 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
12888 return;
12890 /* If we are in terse mode, don't generate any DIEs to represent any
12891 variable declarations or definitions. */
12892 if (debug_info_level <= DINFO_LEVEL_TERSE)
12893 return;
12894 break;
12896 case NAMESPACE_DECL:
12897 if (debug_info_level <= DINFO_LEVEL_TERSE)
12898 return;
12899 if (lookup_decl_die (decl) != NULL)
12900 return;
12901 break;
12903 case TYPE_DECL:
12904 /* Don't emit stubs for types unless they are needed by other DIEs. */
12905 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
12906 return;
12908 /* Don't bother trying to generate any DIEs to represent any of the
12909 normal built-in types for the language we are compiling. */
12910 if (DECL_IS_BUILTIN (decl))
12912 /* OK, we need to generate one for `bool' so GDB knows what type
12913 comparisons have. */
12914 if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
12915 == DW_LANG_C_plus_plus)
12916 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
12917 && ! DECL_IGNORED_P (decl))
12918 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
12920 return;
12923 /* If we are in terse mode, don't generate any DIEs for types. */
12924 if (debug_info_level <= DINFO_LEVEL_TERSE)
12925 return;
12927 /* If we're a function-scope tag, initially use a parent of NULL;
12928 this will be fixed up in decls_for_scope. */
12929 if (decl_function_context (decl))
12930 context_die = NULL;
12932 break;
12934 default:
12935 return;
12938 gen_decl_die (decl, context_die);
12941 /* Output a marker (i.e. a label) for the beginning of the generated code for
12942 a lexical block. */
12944 static void
12945 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
12946 unsigned int blocknum)
12948 function_section (current_function_decl);
12949 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
12952 /* Output a marker (i.e. a label) for the end of the generated code for a
12953 lexical block. */
12955 static void
12956 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
12958 function_section (current_function_decl);
12959 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
12962 /* Returns nonzero if it is appropriate not to emit any debugging
12963 information for BLOCK, because it doesn't contain any instructions.
12965 Don't allow this for blocks with nested functions or local classes
12966 as we would end up with orphans, and in the presence of scheduling
12967 we may end up calling them anyway. */
12969 static bool
12970 dwarf2out_ignore_block (tree block)
12972 tree decl;
12974 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
12975 if (TREE_CODE (decl) == FUNCTION_DECL
12976 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
12977 return 0;
12979 return 1;
12982 /* Lookup FILE_NAME (in the list of filenames that we know about here in
12983 dwarf2out.c) and return its "index". The index of each (known) filename is
12984 just a unique number which is associated with only that one filename. We
12985 need such numbers for the sake of generating labels (in the .debug_sfnames
12986 section) and references to those files numbers (in the .debug_srcinfo
12987 and.debug_macinfo sections). If the filename given as an argument is not
12988 found in our current list, add it to the list and assign it the next
12989 available unique index number. In order to speed up searches, we remember
12990 the index of the filename was looked up last. This handles the majority of
12991 all searches. */
12993 static unsigned
12994 lookup_filename (const char *file_name)
12996 size_t i, n;
12997 char *save_file_name;
12999 /* Check to see if the file name that was searched on the previous
13000 call matches this file name. If so, return the index. */
13001 if (file_table_last_lookup_index != 0)
13003 const char *last
13004 = VARRAY_CHAR_PTR (file_table, file_table_last_lookup_index);
13005 if (strcmp (file_name, last) == 0)
13006 return file_table_last_lookup_index;
13009 /* Didn't match the previous lookup, search the table */
13010 n = VARRAY_ACTIVE_SIZE (file_table);
13011 for (i = 1; i < n; i++)
13012 if (strcmp (file_name, VARRAY_CHAR_PTR (file_table, i)) == 0)
13014 file_table_last_lookup_index = i;
13015 return i;
13018 /* Add the new entry to the end of the filename table. */
13019 file_table_last_lookup_index = n;
13020 save_file_name = (char *) ggc_strdup (file_name);
13021 VARRAY_PUSH_CHAR_PTR (file_table, save_file_name);
13022 VARRAY_PUSH_UINT (file_table_emitted, 0);
13024 return i;
13027 static int
13028 maybe_emit_file (int fileno)
13030 if (DWARF2_ASM_LINE_DEBUG_INFO && fileno > 0)
13032 if (!VARRAY_UINT (file_table_emitted, fileno))
13034 VARRAY_UINT (file_table_emitted, fileno) = ++emitcount;
13035 fprintf (asm_out_file, "\t.file %u ",
13036 VARRAY_UINT (file_table_emitted, fileno));
13037 output_quoted_string (asm_out_file,
13038 VARRAY_CHAR_PTR (file_table, fileno));
13039 fputc ('\n', asm_out_file);
13041 return VARRAY_UINT (file_table_emitted, fileno);
13043 else
13044 return fileno;
13047 static void
13048 init_file_table (void)
13050 /* Allocate the initial hunk of the file_table. */
13051 VARRAY_CHAR_PTR_INIT (file_table, 64, "file_table");
13052 VARRAY_UINT_INIT (file_table_emitted, 64, "file_table_emitted");
13054 /* Skip the first entry - file numbers begin at 1. */
13055 VARRAY_PUSH_CHAR_PTR (file_table, NULL);
13056 VARRAY_PUSH_UINT (file_table_emitted, 0);
13057 file_table_last_lookup_index = 0;
13060 /* Called by the final INSN scan whenever we see a var location. We
13061 use it to drop labels in the right places, and throw the location in
13062 our lookup table. */
13064 static void
13065 dwarf2out_var_location (rtx loc_note)
13067 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13068 struct var_loc_node *newloc;
13069 rtx prev_insn;
13070 static rtx last_insn;
13071 static const char *last_label;
13073 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13074 return;
13075 prev_insn = PREV_INSN (loc_note);
13077 newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13078 /* If the insn we processed last time is the previous insn
13079 and it is also a var location note, use the label we emitted
13080 last time. */
13081 if (last_insn != NULL_RTX
13082 && last_insn == prev_insn
13083 && NOTE_P (prev_insn)
13084 && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13086 newloc->label = last_label;
13088 else
13090 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13091 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13092 loclabel_num++;
13093 newloc->label = ggc_strdup (loclabel);
13095 newloc->var_loc_note = loc_note;
13096 newloc->next = NULL;
13098 last_insn = loc_note;
13099 last_label = newloc->label;
13101 add_var_loc_to_decl (NOTE_VAR_LOCATION_DECL (loc_note), newloc);
13104 /* We need to reset the locations at the beginning of each
13105 function. We can't do this in the end_function hook, because the
13106 declarations that use the locations won't have been outputted when
13107 that hook is called. */
13109 static void
13110 dwarf2out_begin_function (tree unused ATTRIBUTE_UNUSED)
13112 htab_empty (decl_loc_table);
13115 /* Output a label to mark the beginning of a source code line entry
13116 and record information relating to this source line, in
13117 'line_info_table' for later output of the .debug_line section. */
13119 static void
13120 dwarf2out_source_line (unsigned int line, const char *filename)
13122 if (debug_info_level >= DINFO_LEVEL_NORMAL
13123 && line != 0)
13125 function_section (current_function_decl);
13127 /* If requested, emit something human-readable. */
13128 if (flag_debug_asm)
13129 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13130 filename, line);
13132 if (DWARF2_ASM_LINE_DEBUG_INFO)
13134 unsigned file_num = lookup_filename (filename);
13136 file_num = maybe_emit_file (file_num);
13138 /* Emit the .loc directive understood by GNU as. */
13139 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
13141 /* Indicate that line number info exists. */
13142 line_info_table_in_use++;
13144 /* Indicate that multiple line number tables exist. */
13145 if (DECL_SECTION_NAME (current_function_decl))
13146 separate_line_info_table_in_use++;
13148 else if (DECL_SECTION_NAME (current_function_decl))
13150 dw_separate_line_info_ref line_info;
13151 targetm.asm_out.internal_label (asm_out_file, SEPARATE_LINE_CODE_LABEL,
13152 separate_line_info_table_in_use);
13154 /* Expand the line info table if necessary. */
13155 if (separate_line_info_table_in_use
13156 == separate_line_info_table_allocated)
13158 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13159 separate_line_info_table
13160 = ggc_realloc (separate_line_info_table,
13161 separate_line_info_table_allocated
13162 * sizeof (dw_separate_line_info_entry));
13163 memset (separate_line_info_table
13164 + separate_line_info_table_in_use,
13166 (LINE_INFO_TABLE_INCREMENT
13167 * sizeof (dw_separate_line_info_entry)));
13170 /* Add the new entry at the end of the line_info_table. */
13171 line_info
13172 = &separate_line_info_table[separate_line_info_table_in_use++];
13173 line_info->dw_file_num = lookup_filename (filename);
13174 line_info->dw_line_num = line;
13175 line_info->function = current_function_funcdef_no;
13177 else
13179 dw_line_info_ref line_info;
13181 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
13182 line_info_table_in_use);
13184 /* Expand the line info table if necessary. */
13185 if (line_info_table_in_use == line_info_table_allocated)
13187 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13188 line_info_table
13189 = ggc_realloc (line_info_table,
13190 (line_info_table_allocated
13191 * sizeof (dw_line_info_entry)));
13192 memset (line_info_table + line_info_table_in_use, 0,
13193 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
13196 /* Add the new entry at the end of the line_info_table. */
13197 line_info = &line_info_table[line_info_table_in_use++];
13198 line_info->dw_file_num = lookup_filename (filename);
13199 line_info->dw_line_num = line;
13204 /* Record the beginning of a new source file. */
13206 static void
13207 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
13209 if (flag_eliminate_dwarf2_dups)
13211 /* Record the beginning of the file for break_out_includes. */
13212 dw_die_ref bincl_die;
13214 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
13215 add_AT_string (bincl_die, DW_AT_name, filename);
13218 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13220 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13221 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
13222 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13223 lineno);
13224 maybe_emit_file (lookup_filename (filename));
13225 dw2_asm_output_data_uleb128 (lookup_filename (filename),
13226 "Filename we just started");
13230 /* Record the end of a source file. */
13232 static void
13233 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
13235 if (flag_eliminate_dwarf2_dups)
13236 /* Record the end of the file for break_out_includes. */
13237 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
13239 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13241 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13242 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13246 /* Called from debug_define in toplev.c. The `buffer' parameter contains
13247 the tail part of the directive line, i.e. the part which is past the
13248 initial whitespace, #, whitespace, directive-name, whitespace part. */
13250 static void
13251 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13252 const char *buffer ATTRIBUTE_UNUSED)
13254 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13256 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13257 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
13258 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13259 dw2_asm_output_nstring (buffer, -1, "The macro");
13263 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
13264 the tail part of the directive line, i.e. the part which is past the
13265 initial whitespace, #, whitespace, directive-name, whitespace part. */
13267 static void
13268 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13269 const char *buffer ATTRIBUTE_UNUSED)
13271 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13273 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13274 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
13275 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13276 dw2_asm_output_nstring (buffer, -1, "The macro");
13280 /* Set up for Dwarf output at the start of compilation. */
13282 static void
13283 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
13285 init_file_table ();
13287 /* Allocate the decl_die_table. */
13288 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13289 decl_die_table_eq, NULL);
13291 /* Allocate the decl_loc_table. */
13292 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13293 decl_loc_table_eq, NULL);
13295 /* Allocate the initial hunk of the decl_scope_table. */
13296 VARRAY_TREE_INIT (decl_scope_table, 256, "decl_scope_table");
13298 /* Allocate the initial hunk of the abbrev_die_table. */
13299 abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
13300 * sizeof (dw_die_ref));
13301 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
13302 /* Zero-th entry is allocated, but unused */
13303 abbrev_die_table_in_use = 1;
13305 /* Allocate the initial hunk of the line_info_table. */
13306 line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
13307 * sizeof (dw_line_info_entry));
13308 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
13310 /* Zero-th entry is allocated, but unused */
13311 line_info_table_in_use = 1;
13313 /* Generate the initial DIE for the .debug section. Note that the (string)
13314 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
13315 will (typically) be a relative pathname and that this pathname should be
13316 taken as being relative to the directory from which the compiler was
13317 invoked when the given (base) source file was compiled. We will fill
13318 in this value in dwarf2out_finish. */
13319 comp_unit_die = gen_compile_unit_die (NULL);
13321 VARRAY_TREE_INIT (incomplete_types, 64, "incomplete_types");
13323 VARRAY_RTX_INIT (used_rtx_varray, 32, "used_rtx_varray");
13325 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
13326 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
13327 DEBUG_ABBREV_SECTION_LABEL, 0);
13328 if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
13329 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
13330 else
13331 strcpy (text_section_label, stripattributes (TEXT_SECTION_NAME));
13333 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
13334 DEBUG_INFO_SECTION_LABEL, 0);
13335 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
13336 DEBUG_LINE_SECTION_LABEL, 0);
13337 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
13338 DEBUG_RANGES_SECTION_LABEL, 0);
13339 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13340 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
13341 named_section_flags (DEBUG_INFO_SECTION, SECTION_DEBUG);
13342 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
13343 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13344 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
13346 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13348 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13349 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
13350 DEBUG_MACINFO_SECTION_LABEL, 0);
13351 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
13354 if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
13356 text_section ();
13357 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
13361 /* A helper function for dwarf2out_finish called through
13362 ht_forall. Emit one queued .debug_str string. */
13364 static int
13365 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
13367 struct indirect_string_node *node = (struct indirect_string_node *) *h;
13369 if (node->form == DW_FORM_strp)
13371 named_section_flags (DEBUG_STR_SECTION, DEBUG_STR_SECTION_FLAGS);
13372 ASM_OUTPUT_LABEL (asm_out_file, node->label);
13373 assemble_string (node->str, strlen (node->str) + 1);
13376 return 1;
13381 /* Clear the marks for a die and its children.
13382 Be cool if the mark isn't set. */
13384 static void
13385 prune_unmark_dies (dw_die_ref die)
13387 dw_die_ref c;
13388 die->die_mark = 0;
13389 for (c = die->die_child; c; c = c->die_sib)
13390 prune_unmark_dies (c);
13394 /* Given DIE that we're marking as used, find any other dies
13395 it references as attributes and mark them as used. */
13397 static void
13398 prune_unused_types_walk_attribs (dw_die_ref die)
13400 dw_attr_ref a;
13402 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
13404 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
13406 /* A reference to another DIE.
13407 Make sure that it will get emitted. */
13408 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
13410 else if (a->dw_attr == DW_AT_decl_file)
13412 /* A reference to a file. Make sure the file name is emitted. */
13413 a->dw_attr_val.v.val_unsigned =
13414 maybe_emit_file (a->dw_attr_val.v.val_unsigned);
13420 /* Mark DIE as being used. If DOKIDS is true, then walk down
13421 to DIE's children. */
13423 static void
13424 prune_unused_types_mark (dw_die_ref die, int dokids)
13426 dw_die_ref c;
13428 if (die->die_mark == 0)
13430 /* We haven't done this node yet. Mark it as used. */
13431 die->die_mark = 1;
13433 /* We also have to mark its parents as used.
13434 (But we don't want to mark our parents' kids due to this.) */
13435 if (die->die_parent)
13436 prune_unused_types_mark (die->die_parent, 0);
13438 /* Mark any referenced nodes. */
13439 prune_unused_types_walk_attribs (die);
13441 /* If this node is a specification,
13442 also mark the definition, if it exists. */
13443 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
13444 prune_unused_types_mark (die->die_definition, 1);
13447 if (dokids && die->die_mark != 2)
13449 /* We need to walk the children, but haven't done so yet.
13450 Remember that we've walked the kids. */
13451 die->die_mark = 2;
13453 /* Walk them. */
13454 for (c = die->die_child; c; c = c->die_sib)
13456 /* If this is an array type, we need to make sure our
13457 kids get marked, even if they're types. */
13458 if (die->die_tag == DW_TAG_array_type)
13459 prune_unused_types_mark (c, 1);
13460 else
13461 prune_unused_types_walk (c);
13467 /* Walk the tree DIE and mark types that we actually use. */
13469 static void
13470 prune_unused_types_walk (dw_die_ref die)
13472 dw_die_ref c;
13474 /* Don't do anything if this node is already marked. */
13475 if (die->die_mark)
13476 return;
13478 switch (die->die_tag) {
13479 case DW_TAG_const_type:
13480 case DW_TAG_packed_type:
13481 case DW_TAG_pointer_type:
13482 case DW_TAG_reference_type:
13483 case DW_TAG_volatile_type:
13484 case DW_TAG_typedef:
13485 case DW_TAG_array_type:
13486 case DW_TAG_structure_type:
13487 case DW_TAG_union_type:
13488 case DW_TAG_class_type:
13489 case DW_TAG_friend:
13490 case DW_TAG_variant_part:
13491 case DW_TAG_enumeration_type:
13492 case DW_TAG_subroutine_type:
13493 case DW_TAG_string_type:
13494 case DW_TAG_set_type:
13495 case DW_TAG_subrange_type:
13496 case DW_TAG_ptr_to_member_type:
13497 case DW_TAG_file_type:
13498 /* It's a type node --- don't mark it. */
13499 return;
13501 default:
13502 /* Mark everything else. */
13503 break;
13506 die->die_mark = 1;
13508 /* Now, mark any dies referenced from here. */
13509 prune_unused_types_walk_attribs (die);
13511 /* Mark children. */
13512 for (c = die->die_child; c; c = c->die_sib)
13513 prune_unused_types_walk (c);
13517 /* Remove from the tree DIE any dies that aren't marked. */
13519 static void
13520 prune_unused_types_prune (dw_die_ref die)
13522 dw_die_ref c, p, n;
13523 if (!die->die_mark)
13524 abort();
13526 p = NULL;
13527 for (c = die->die_child; c; c = n)
13529 n = c->die_sib;
13530 if (c->die_mark)
13532 prune_unused_types_prune (c);
13533 p = c;
13535 else
13537 if (p)
13538 p->die_sib = n;
13539 else
13540 die->die_child = n;
13541 free_die (c);
13547 /* Remove dies representing declarations that we never use. */
13549 static void
13550 prune_unused_types (void)
13552 unsigned int i;
13553 limbo_die_node *node;
13555 /* Clear all the marks. */
13556 prune_unmark_dies (comp_unit_die);
13557 for (node = limbo_die_list; node; node = node->next)
13558 prune_unmark_dies (node->die);
13560 /* Set the mark on nodes that are actually used. */
13561 prune_unused_types_walk (comp_unit_die);
13562 for (node = limbo_die_list; node; node = node->next)
13563 prune_unused_types_walk (node->die);
13565 /* Also set the mark on nodes referenced from the
13566 pubname_table or arange_table. */
13567 for (i = 0; i < pubname_table_in_use; i++)
13568 prune_unused_types_mark (pubname_table[i].die, 1);
13569 for (i = 0; i < arange_table_in_use; i++)
13570 prune_unused_types_mark (arange_table[i], 1);
13572 /* Get rid of nodes that aren't marked. */
13573 prune_unused_types_prune (comp_unit_die);
13574 for (node = limbo_die_list; node; node = node->next)
13575 prune_unused_types_prune (node->die);
13577 /* Leave the marks clear. */
13578 prune_unmark_dies (comp_unit_die);
13579 for (node = limbo_die_list; node; node = node->next)
13580 prune_unmark_dies (node->die);
13583 /* Output stuff that dwarf requires at the end of every file,
13584 and generate the DWARF-2 debugging info. */
13586 static void
13587 dwarf2out_finish (const char *filename)
13589 limbo_die_node *node, *next_node;
13590 dw_die_ref die = 0;
13592 /* Add the name for the main input file now. We delayed this from
13593 dwarf2out_init to avoid complications with PCH. */
13594 add_name_attribute (comp_unit_die, filename);
13595 if (filename[0] != DIR_SEPARATOR)
13596 add_comp_dir_attribute (comp_unit_die);
13597 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
13599 size_t i;
13600 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
13601 if (VARRAY_CHAR_PTR (file_table, i)[0] != DIR_SEPARATOR
13602 /* Don't add cwd for <built-in>. */
13603 && VARRAY_CHAR_PTR (file_table, i)[0] != '<')
13605 add_comp_dir_attribute (comp_unit_die);
13606 break;
13610 /* Traverse the limbo die list, and add parent/child links. The only
13611 dies without parents that should be here are concrete instances of
13612 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
13613 For concrete instances, we can get the parent die from the abstract
13614 instance. */
13615 for (node = limbo_die_list; node; node = next_node)
13617 next_node = node->next;
13618 die = node->die;
13620 if (die->die_parent == NULL)
13622 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
13623 tree context;
13625 if (origin)
13626 add_child_die (origin->die_parent, die);
13627 else if (die == comp_unit_die)
13629 else if (errorcount > 0 || sorrycount > 0)
13630 /* It's OK to be confused by errors in the input. */
13631 add_child_die (comp_unit_die, die);
13632 else if (node->created_for
13633 && ((DECL_P (node->created_for)
13634 && (context = DECL_CONTEXT (node->created_for)))
13635 || (TYPE_P (node->created_for)
13636 && (context = TYPE_CONTEXT (node->created_for))))
13637 && TREE_CODE (context) == FUNCTION_DECL)
13639 /* In certain situations, the lexical block containing a
13640 nested function can be optimized away, which results
13641 in the nested function die being orphaned. Likewise
13642 with the return type of that nested function. Force
13643 this to be a child of the containing function. */
13644 origin = lookup_decl_die (context);
13645 if (! origin)
13646 abort ();
13647 add_child_die (origin, die);
13649 else
13650 abort ();
13654 limbo_die_list = NULL;
13656 /* Walk through the list of incomplete types again, trying once more to
13657 emit full debugging info for them. */
13658 retry_incomplete_types ();
13660 /* We need to reverse all the dies before break_out_includes, or
13661 we'll see the end of an include file before the beginning. */
13662 reverse_all_dies (comp_unit_die);
13664 if (flag_eliminate_unused_debug_types)
13665 prune_unused_types ();
13667 /* Generate separate CUs for each of the include files we've seen.
13668 They will go into limbo_die_list. */
13669 if (flag_eliminate_dwarf2_dups)
13670 break_out_includes (comp_unit_die);
13672 /* Traverse the DIE's and add add sibling attributes to those DIE's
13673 that have children. */
13674 add_sibling_attributes (comp_unit_die);
13675 for (node = limbo_die_list; node; node = node->next)
13676 add_sibling_attributes (node->die);
13678 /* Output a terminator label for the .text section. */
13679 text_section ();
13680 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
13682 /* Output the source line correspondence table. We must do this
13683 even if there is no line information. Otherwise, on an empty
13684 translation unit, we will generate a present, but empty,
13685 .debug_info section. IRIX 6.5 `nm' will then complain when
13686 examining the file. */
13687 if (! DWARF2_ASM_LINE_DEBUG_INFO)
13689 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13690 output_line_info ();
13693 /* Output location list section if necessary. */
13694 if (have_location_lists)
13696 /* Output the location lists info. */
13697 named_section_flags (DEBUG_LOC_SECTION, SECTION_DEBUG);
13698 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
13699 DEBUG_LOC_SECTION_LABEL, 0);
13700 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
13701 output_location_lists (die);
13702 have_location_lists = 0;
13705 /* We can only use the low/high_pc attributes if all of the code was
13706 in .text. */
13707 if (separate_line_info_table_in_use == 0)
13709 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
13710 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
13713 /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
13714 "base address". Use zero so that these addresses become absolute. */
13715 else if (have_location_lists || ranges_table_in_use)
13716 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
13718 if (debug_info_level >= DINFO_LEVEL_NORMAL)
13719 add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
13720 debug_line_section_label);
13722 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13723 add_AT_lbl_offset (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
13725 /* Output all of the compilation units. We put the main one last so that
13726 the offsets are available to output_pubnames. */
13727 for (node = limbo_die_list; node; node = node->next)
13728 output_comp_unit (node->die, 0);
13730 output_comp_unit (comp_unit_die, 0);
13732 /* Output the abbreviation table. */
13733 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13734 output_abbrev_section ();
13736 /* Output public names table if necessary. */
13737 if (pubname_table_in_use)
13739 named_section_flags (DEBUG_PUBNAMES_SECTION, SECTION_DEBUG);
13740 output_pubnames ();
13743 /* Output the address range information. We only put functions in the arange
13744 table, so don't write it out if we don't have any. */
13745 if (fde_table_in_use)
13747 named_section_flags (DEBUG_ARANGES_SECTION, SECTION_DEBUG);
13748 output_aranges ();
13751 /* Output ranges section if necessary. */
13752 if (ranges_table_in_use)
13754 named_section_flags (DEBUG_RANGES_SECTION, SECTION_DEBUG);
13755 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
13756 output_ranges ();
13759 /* Have to end the primary source file. */
13760 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13762 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13763 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13764 dw2_asm_output_data (1, 0, "End compilation unit");
13767 /* If we emitted any DW_FORM_strp form attribute, output the string
13768 table too. */
13769 if (debug_str_hash)
13770 htab_traverse (debug_str_hash, output_indirect_string, NULL);
13772 #else
13774 /* This should never be used, but its address is needed for comparisons. */
13775 const struct gcc_debug_hooks dwarf2_debug_hooks;
13777 #endif /* DWARF2_DEBUGGING_INFO */
13779 #include "gt-dwarf2out.h"