2005-05-19 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / dwarf2out.c
blob32711c7227cfee9e32266a743b3c0a2c68dfc9e5
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, 2005 Free Software Foundation, Inc.
4 Contributed by Gary Funck (gary@intrepid.com).
5 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6 Extensively modified by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
13 version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING. If not, write to the Free
22 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 02111-1307, USA. */
25 /* TODO: Emit .debug_line header even when there are no functions, since
26 the file numbers are used by .debug_info. Alternately, leave
27 out locations for types and decls.
28 Avoid talking about ctors and op= for PODs.
29 Factor out common prologue sequences into multiple CIEs. */
31 /* The first part of this file deals with the DWARF 2 frame unwind
32 information, which is also used by the GCC efficient exception handling
33 mechanism. The second part, controlled only by an #ifdef
34 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35 information. */
37 #include "config.h"
38 #include "system.h"
39 #include "coretypes.h"
40 #include "tm.h"
41 #include "tree.h"
42 #include "version.h"
43 #include "flags.h"
44 #include "real.h"
45 #include "rtl.h"
46 #include "hard-reg-set.h"
47 #include "regs.h"
48 #include "insn-config.h"
49 #include "reload.h"
50 #include "function.h"
51 #include "output.h"
52 #include "expr.h"
53 #include "libfuncs.h"
54 #include "except.h"
55 #include "dwarf2.h"
56 #include "dwarf2out.h"
57 #include "dwarf2asm.h"
58 #include "toplev.h"
59 #include "varray.h"
60 #include "ggc.h"
61 #include "md5.h"
62 #include "tm_p.h"
63 #include "diagnostic.h"
64 #include "debug.h"
65 #include "target.h"
66 #include "langhooks.h"
67 #include "hashtab.h"
68 #include "cgraph.h"
69 #include "input.h"
71 #ifdef DWARF2_DEBUGGING_INFO
72 static void dwarf2out_source_line (unsigned int, const char *);
73 #endif
75 /* DWARF2 Abbreviation Glossary:
76 CFA = Canonical Frame Address
77 a fixed address on the stack which identifies a call frame.
78 We define it to be the value of SP just before the call insn.
79 The CFA register and offset, which may change during the course
80 of the function, are used to calculate its value at runtime.
81 CFI = Call Frame Instruction
82 an instruction for the DWARF2 abstract machine
83 CIE = Common Information Entry
84 information describing information common to one or more FDEs
85 DIE = Debugging Information Entry
86 FDE = Frame Description Entry
87 information describing the stack call frame, in particular,
88 how to restore registers
90 DW_CFA_... = DWARF2 CFA call frame instruction
91 DW_TAG_... = DWARF2 DIE tag */
93 /* Decide whether we want to emit frame unwind information for the current
94 translation unit. */
96 int
97 dwarf2out_do_frame (void)
99 return (write_symbols == DWARF2_DEBUG
100 || write_symbols == VMS_AND_DWARF2_DEBUG
101 #ifdef DWARF2_FRAME_INFO
102 || DWARF2_FRAME_INFO
103 #endif
104 #ifdef DWARF2_UNWIND_INFO
105 || flag_unwind_tables
106 || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)
107 #endif
111 /* The size of the target's pointer type. */
112 #ifndef PTR_SIZE
113 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
114 #endif
116 /* Various versions of targetm.eh_frame_section. Note these must appear
117 outside the DWARF2_DEBUGGING_INFO || DWARF2_UNWIND_INFO macro guards. */
119 /* Version of targetm.eh_frame_section for systems with named sections. */
120 void
121 named_section_eh_frame_section (void)
123 #ifdef EH_FRAME_SECTION_NAME
124 int flags;
126 if (EH_TABLES_CAN_BE_READ_ONLY)
128 int fde_encoding;
129 int per_encoding;
130 int lsda_encoding;
132 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
133 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
134 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
135 flags = (! flag_pic
136 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
137 && (fde_encoding & 0x70) != DW_EH_PE_aligned
138 && (per_encoding & 0x70) != DW_EH_PE_absptr
139 && (per_encoding & 0x70) != DW_EH_PE_aligned
140 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
141 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
142 ? 0 : SECTION_WRITE;
144 else
145 flags = SECTION_WRITE;
146 named_section_flags (EH_FRAME_SECTION_NAME, flags);
147 #endif
150 /* Version of targetm.eh_frame_section for systems using collect2. */
151 void
152 collect2_eh_frame_section (void)
154 tree label = get_file_function_name ('F');
156 data_section ();
157 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
158 targetm.asm_out.globalize_label (asm_out_file, IDENTIFIER_POINTER (label));
159 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
162 /* Default version of targetm.eh_frame_section. */
163 void
164 default_eh_frame_section (void)
166 #ifdef EH_FRAME_SECTION_NAME
167 named_section_eh_frame_section ();
168 #else
169 collect2_eh_frame_section ();
170 #endif
173 DEF_VEC_P(rtx);
174 DEF_VEC_ALLOC_P(rtx,gc);
176 /* Array of RTXes referenced by the debugging information, which therefore
177 must be kept around forever. */
178 static GTY(()) VEC(rtx,gc) *used_rtx_array;
180 /* A pointer to the base of a list of incomplete types which might be
181 completed at some later time. incomplete_types_list needs to be a
182 VEC(tree,gc) because we want to tell the garbage collector about
183 it. */
184 static GTY(()) VEC(tree,gc) *incomplete_types;
186 /* A pointer to the base of a table of references to declaration
187 scopes. This table is a display which tracks the nesting
188 of declaration scopes at the current scope and containing
189 scopes. This table is used to find the proper place to
190 define type declaration DIE's. */
191 static GTY(()) VEC(tree,gc) *decl_scope_table;
193 /* How to start an assembler comment. */
194 #ifndef ASM_COMMENT_START
195 #define ASM_COMMENT_START ";#"
196 #endif
198 typedef struct dw_cfi_struct *dw_cfi_ref;
199 typedef struct dw_fde_struct *dw_fde_ref;
200 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
202 /* Call frames are described using a sequence of Call Frame
203 Information instructions. The register number, offset
204 and address fields are provided as possible operands;
205 their use is selected by the opcode field. */
207 enum dw_cfi_oprnd_type {
208 dw_cfi_oprnd_unused,
209 dw_cfi_oprnd_reg_num,
210 dw_cfi_oprnd_offset,
211 dw_cfi_oprnd_addr,
212 dw_cfi_oprnd_loc
215 typedef union dw_cfi_oprnd_struct GTY(())
217 unsigned long GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
218 HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
219 const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
220 struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
222 dw_cfi_oprnd;
224 typedef struct dw_cfi_struct GTY(())
226 dw_cfi_ref dw_cfi_next;
227 enum dwarf_call_frame_info dw_cfi_opc;
228 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
229 dw_cfi_oprnd1;
230 dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
231 dw_cfi_oprnd2;
233 dw_cfi_node;
235 /* This is how we define the location of the CFA. We use to handle it
236 as REG + OFFSET all the time, but now it can be more complex.
237 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
238 Instead of passing around REG and OFFSET, we pass a copy
239 of this structure. */
240 typedef struct cfa_loc GTY(())
242 unsigned long reg;
243 HOST_WIDE_INT offset;
244 HOST_WIDE_INT base_offset;
245 int indirect; /* 1 if CFA is accessed via a dereference. */
246 } dw_cfa_location;
248 /* All call frame descriptions (FDE's) in the GCC generated DWARF
249 refer to a single Common Information Entry (CIE), defined at
250 the beginning of the .debug_frame section. This use of a single
251 CIE obviates the need to keep track of multiple CIE's
252 in the DWARF generation routines below. */
254 typedef struct dw_fde_struct GTY(())
256 tree decl;
257 const char *dw_fde_begin;
258 const char *dw_fde_current_label;
259 const char *dw_fde_end;
260 const char *dw_fde_hot_section_label;
261 const char *dw_fde_hot_section_end_label;
262 const char *dw_fde_unlikely_section_label;
263 const char *dw_fde_unlikely_section_end_label;
264 bool dw_fde_switched_sections;
265 dw_cfi_ref dw_fde_cfi;
266 unsigned funcdef_number;
267 unsigned all_throwers_are_sibcalls : 1;
268 unsigned nothrow : 1;
269 unsigned uses_eh_lsda : 1;
271 dw_fde_node;
273 /* Maximum size (in bytes) of an artificially generated label. */
274 #define MAX_ARTIFICIAL_LABEL_BYTES 30
276 /* The size of addresses as they appear in the Dwarf 2 data.
277 Some architectures use word addresses to refer to code locations,
278 but Dwarf 2 info always uses byte addresses. On such machines,
279 Dwarf 2 addresses need to be larger than the architecture's
280 pointers. */
281 #ifndef DWARF2_ADDR_SIZE
282 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
283 #endif
285 /* The size in bytes of a DWARF field indicating an offset or length
286 relative to a debug info section, specified to be 4 bytes in the
287 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
288 as PTR_SIZE. */
290 #ifndef DWARF_OFFSET_SIZE
291 #define DWARF_OFFSET_SIZE 4
292 #endif
294 /* According to the (draft) DWARF 3 specification, the initial length
295 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
296 bytes are 0xffffffff, followed by the length stored in the next 8
297 bytes.
299 However, the SGI/MIPS ABI uses an initial length which is equal to
300 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
302 #ifndef DWARF_INITIAL_LENGTH_SIZE
303 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
304 #endif
306 #define DWARF_VERSION 2
308 /* Round SIZE up to the nearest BOUNDARY. */
309 #define DWARF_ROUND(SIZE,BOUNDARY) \
310 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
312 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
313 #ifndef DWARF_CIE_DATA_ALIGNMENT
314 #ifdef STACK_GROWS_DOWNWARD
315 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
316 #else
317 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
318 #endif
319 #endif
321 /* A pointer to the base of a table that contains frame description
322 information for each routine. */
323 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
325 /* Number of elements currently allocated for fde_table. */
326 static GTY(()) unsigned fde_table_allocated;
328 /* Number of elements in fde_table currently in use. */
329 static GTY(()) unsigned fde_table_in_use;
331 /* Size (in elements) of increments by which we may expand the
332 fde_table. */
333 #define FDE_TABLE_INCREMENT 256
335 /* A list of call frame insns for the CIE. */
336 static GTY(()) dw_cfi_ref cie_cfi_head;
338 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
339 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
340 attribute that accelerates the lookup of the FDE associated
341 with the subprogram. This variable holds the table index of the FDE
342 associated with the current function (body) definition. */
343 static unsigned current_funcdef_fde;
344 #endif
346 struct indirect_string_node GTY(())
348 const char *str;
349 unsigned int refcount;
350 unsigned int form;
351 char *label;
354 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
356 static GTY(()) int dw2_string_counter;
357 static GTY(()) unsigned long dwarf2out_cfi_label_num;
359 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
361 /* Forward declarations for functions defined in this file. */
363 static char *stripattributes (const char *);
364 static const char *dwarf_cfi_name (unsigned);
365 static dw_cfi_ref new_cfi (void);
366 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
367 static void add_fde_cfi (const char *, dw_cfi_ref);
368 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
369 static void lookup_cfa (dw_cfa_location *);
370 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
371 static void initial_return_save (rtx);
372 static HOST_WIDE_INT stack_adjust_offset (rtx);
373 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
374 static void output_call_frame_info (int);
375 static void dwarf2out_stack_adjust (rtx, bool);
376 static void flush_queued_reg_saves (void);
377 static bool clobbers_queued_reg_save (rtx);
378 static void dwarf2out_frame_debug_expr (rtx, const char *);
380 /* Support for complex CFA locations. */
381 static void output_cfa_loc (dw_cfi_ref);
382 static void get_cfa_from_loc_descr (dw_cfa_location *,
383 struct dw_loc_descr_struct *);
384 static struct dw_loc_descr_struct *build_cfa_loc
385 (dw_cfa_location *);
386 static void def_cfa_1 (const char *, dw_cfa_location *);
388 /* How to start an assembler comment. */
389 #ifndef ASM_COMMENT_START
390 #define ASM_COMMENT_START ";#"
391 #endif
393 /* Data and reference forms for relocatable data. */
394 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
395 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
397 #ifndef DEBUG_FRAME_SECTION
398 #define DEBUG_FRAME_SECTION ".debug_frame"
399 #endif
401 #ifndef FUNC_BEGIN_LABEL
402 #define FUNC_BEGIN_LABEL "LFB"
403 #endif
405 #ifndef FUNC_END_LABEL
406 #define FUNC_END_LABEL "LFE"
407 #endif
409 #ifndef FRAME_BEGIN_LABEL
410 #define FRAME_BEGIN_LABEL "Lframe"
411 #endif
412 #define CIE_AFTER_SIZE_LABEL "LSCIE"
413 #define CIE_END_LABEL "LECIE"
414 #define FDE_LABEL "LSFDE"
415 #define FDE_AFTER_SIZE_LABEL "LASFDE"
416 #define FDE_END_LABEL "LEFDE"
417 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
418 #define LINE_NUMBER_END_LABEL "LELT"
419 #define LN_PROLOG_AS_LABEL "LASLTP"
420 #define LN_PROLOG_END_LABEL "LELTP"
421 #define DIE_LABEL_PREFIX "DW"
423 /* The DWARF 2 CFA column which tracks the return address. Normally this
424 is the column for PC, or the first column after all of the hard
425 registers. */
426 #ifndef DWARF_FRAME_RETURN_COLUMN
427 #ifdef PC_REGNUM
428 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
429 #else
430 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
431 #endif
432 #endif
434 /* The mapping from gcc register number to DWARF 2 CFA column number. By
435 default, we just provide columns for all registers. */
436 #ifndef DWARF_FRAME_REGNUM
437 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
438 #endif
440 /* The offset from the incoming value of %sp to the top of the stack frame
441 for the current function. */
442 #ifndef INCOMING_FRAME_SP_OFFSET
443 #define INCOMING_FRAME_SP_OFFSET 0
444 #endif
446 /* Hook used by __throw. */
449 expand_builtin_dwarf_sp_column (void)
451 return GEN_INT (DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
454 /* Return a pointer to a copy of the section string name S with all
455 attributes stripped off, and an asterisk prepended (for assemble_name). */
457 static inline char *
458 stripattributes (const char *s)
460 char *stripped = xmalloc (strlen (s) + 2);
461 char *p = stripped;
463 *p++ = '*';
465 while (*s && *s != ',')
466 *p++ = *s++;
468 *p = '\0';
469 return stripped;
472 /* Generate code to initialize the register size table. */
474 void
475 expand_builtin_init_dwarf_reg_sizes (tree address)
477 int i;
478 enum machine_mode mode = TYPE_MODE (char_type_node);
479 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, 0);
480 rtx mem = gen_rtx_MEM (BLKmode, addr);
481 bool wrote_return_column = false;
483 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
484 if (DWARF_FRAME_REGNUM (i) < DWARF_FRAME_REGISTERS)
486 HOST_WIDE_INT offset = DWARF_FRAME_REGNUM (i) * GET_MODE_SIZE (mode);
487 enum machine_mode save_mode = reg_raw_mode[i];
488 HOST_WIDE_INT size;
490 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
491 save_mode = choose_hard_reg_mode (i, 1, true);
492 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
494 if (save_mode == VOIDmode)
495 continue;
496 wrote_return_column = true;
498 size = GET_MODE_SIZE (save_mode);
499 if (offset < 0)
500 continue;
502 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
505 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
506 gcc_assert (wrote_return_column);
507 i = DWARF_ALT_FRAME_RETURN_COLUMN;
508 wrote_return_column = false;
509 #else
510 i = DWARF_FRAME_RETURN_COLUMN;
511 #endif
513 if (! wrote_return_column)
515 enum machine_mode save_mode = Pmode;
516 HOST_WIDE_INT offset = i * GET_MODE_SIZE (mode);
517 HOST_WIDE_INT size = GET_MODE_SIZE (save_mode);
518 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
522 /* Convert a DWARF call frame info. operation to its string name */
524 static const char *
525 dwarf_cfi_name (unsigned int cfi_opc)
527 switch (cfi_opc)
529 case DW_CFA_advance_loc:
530 return "DW_CFA_advance_loc";
531 case DW_CFA_offset:
532 return "DW_CFA_offset";
533 case DW_CFA_restore:
534 return "DW_CFA_restore";
535 case DW_CFA_nop:
536 return "DW_CFA_nop";
537 case DW_CFA_set_loc:
538 return "DW_CFA_set_loc";
539 case DW_CFA_advance_loc1:
540 return "DW_CFA_advance_loc1";
541 case DW_CFA_advance_loc2:
542 return "DW_CFA_advance_loc2";
543 case DW_CFA_advance_loc4:
544 return "DW_CFA_advance_loc4";
545 case DW_CFA_offset_extended:
546 return "DW_CFA_offset_extended";
547 case DW_CFA_restore_extended:
548 return "DW_CFA_restore_extended";
549 case DW_CFA_undefined:
550 return "DW_CFA_undefined";
551 case DW_CFA_same_value:
552 return "DW_CFA_same_value";
553 case DW_CFA_register:
554 return "DW_CFA_register";
555 case DW_CFA_remember_state:
556 return "DW_CFA_remember_state";
557 case DW_CFA_restore_state:
558 return "DW_CFA_restore_state";
559 case DW_CFA_def_cfa:
560 return "DW_CFA_def_cfa";
561 case DW_CFA_def_cfa_register:
562 return "DW_CFA_def_cfa_register";
563 case DW_CFA_def_cfa_offset:
564 return "DW_CFA_def_cfa_offset";
566 /* DWARF 3 */
567 case DW_CFA_def_cfa_expression:
568 return "DW_CFA_def_cfa_expression";
569 case DW_CFA_expression:
570 return "DW_CFA_expression";
571 case DW_CFA_offset_extended_sf:
572 return "DW_CFA_offset_extended_sf";
573 case DW_CFA_def_cfa_sf:
574 return "DW_CFA_def_cfa_sf";
575 case DW_CFA_def_cfa_offset_sf:
576 return "DW_CFA_def_cfa_offset_sf";
578 /* SGI/MIPS specific */
579 case DW_CFA_MIPS_advance_loc8:
580 return "DW_CFA_MIPS_advance_loc8";
582 /* GNU extensions */
583 case DW_CFA_GNU_window_save:
584 return "DW_CFA_GNU_window_save";
585 case DW_CFA_GNU_args_size:
586 return "DW_CFA_GNU_args_size";
587 case DW_CFA_GNU_negative_offset_extended:
588 return "DW_CFA_GNU_negative_offset_extended";
590 default:
591 return "DW_CFA_<unknown>";
595 /* Return a pointer to a newly allocated Call Frame Instruction. */
597 static inline dw_cfi_ref
598 new_cfi (void)
600 dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
602 cfi->dw_cfi_next = NULL;
603 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
604 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
606 return cfi;
609 /* Add a Call Frame Instruction to list of instructions. */
611 static inline void
612 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
614 dw_cfi_ref *p;
616 /* Find the end of the chain. */
617 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
620 *p = cfi;
623 /* Generate a new label for the CFI info to refer to. */
625 char *
626 dwarf2out_cfi_label (void)
628 static char label[20];
630 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
631 ASM_OUTPUT_LABEL (asm_out_file, label);
632 return label;
635 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
636 or to the CIE if LABEL is NULL. */
638 static void
639 add_fde_cfi (const char *label, dw_cfi_ref cfi)
641 if (label)
643 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
645 if (*label == 0)
646 label = dwarf2out_cfi_label ();
648 if (fde->dw_fde_current_label == NULL
649 || strcmp (label, fde->dw_fde_current_label) != 0)
651 dw_cfi_ref xcfi;
653 fde->dw_fde_current_label = label = xstrdup (label);
655 /* Set the location counter to the new label. */
656 xcfi = new_cfi ();
657 xcfi->dw_cfi_opc = DW_CFA_advance_loc4;
658 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
659 add_cfi (&fde->dw_fde_cfi, xcfi);
662 add_cfi (&fde->dw_fde_cfi, cfi);
665 else
666 add_cfi (&cie_cfi_head, cfi);
669 /* Subroutine of lookup_cfa. */
671 static inline void
672 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
674 switch (cfi->dw_cfi_opc)
676 case DW_CFA_def_cfa_offset:
677 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
678 break;
679 case DW_CFA_def_cfa_register:
680 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
681 break;
682 case DW_CFA_def_cfa:
683 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
684 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
685 break;
686 case DW_CFA_def_cfa_expression:
687 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
688 break;
689 default:
690 break;
694 /* Find the previous value for the CFA. */
696 static void
697 lookup_cfa (dw_cfa_location *loc)
699 dw_cfi_ref cfi;
701 loc->reg = (unsigned long) -1;
702 loc->offset = 0;
703 loc->indirect = 0;
704 loc->base_offset = 0;
706 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
707 lookup_cfa_1 (cfi, loc);
709 if (fde_table_in_use)
711 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
712 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
713 lookup_cfa_1 (cfi, loc);
717 /* The current rule for calculating the DWARF2 canonical frame address. */
718 static dw_cfa_location cfa;
720 /* The register used for saving registers to the stack, and its offset
721 from the CFA. */
722 static dw_cfa_location cfa_store;
724 /* The running total of the size of arguments pushed onto the stack. */
725 static HOST_WIDE_INT args_size;
727 /* The last args_size we actually output. */
728 static HOST_WIDE_INT old_args_size;
730 /* Entry point to update the canonical frame address (CFA).
731 LABEL is passed to add_fde_cfi. The value of CFA is now to be
732 calculated from REG+OFFSET. */
734 void
735 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
737 dw_cfa_location loc;
738 loc.indirect = 0;
739 loc.base_offset = 0;
740 loc.reg = reg;
741 loc.offset = offset;
742 def_cfa_1 (label, &loc);
745 /* This routine does the actual work. The CFA is now calculated from
746 the dw_cfa_location structure. */
748 static void
749 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
751 dw_cfi_ref cfi;
752 dw_cfa_location old_cfa, loc;
754 cfa = *loc_p;
755 loc = *loc_p;
757 if (cfa_store.reg == loc.reg && loc.indirect == 0)
758 cfa_store.offset = loc.offset;
760 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
761 lookup_cfa (&old_cfa);
763 /* If nothing changed, no need to issue any call frame instructions. */
764 if (loc.reg == old_cfa.reg && loc.offset == old_cfa.offset
765 && loc.indirect == old_cfa.indirect
766 && (loc.indirect == 0 || loc.base_offset == old_cfa.base_offset))
767 return;
769 cfi = new_cfi ();
771 if (loc.reg == old_cfa.reg && !loc.indirect)
773 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction,
774 indicating the CFA register did not change but the offset
775 did. */
776 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
777 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
780 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
781 else if (loc.offset == old_cfa.offset && old_cfa.reg != (unsigned long) -1
782 && !loc.indirect)
784 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
785 indicating the CFA register has changed to <register> but the
786 offset has not changed. */
787 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
788 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
790 #endif
792 else if (loc.indirect == 0)
794 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
795 indicating the CFA register has changed to <register> with
796 the specified offset. */
797 cfi->dw_cfi_opc = DW_CFA_def_cfa;
798 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
799 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
801 else
803 /* Construct a DW_CFA_def_cfa_expression instruction to
804 calculate the CFA using a full location expression since no
805 register-offset pair is available. */
806 struct dw_loc_descr_struct *loc_list;
808 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
809 loc_list = build_cfa_loc (&loc);
810 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
813 add_fde_cfi (label, cfi);
816 /* Add the CFI for saving a register. REG is the CFA column number.
817 LABEL is passed to add_fde_cfi.
818 If SREG is -1, the register is saved at OFFSET from the CFA;
819 otherwise it is saved in SREG. */
821 static void
822 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
824 dw_cfi_ref cfi = new_cfi ();
826 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
828 if (sreg == INVALID_REGNUM)
830 if (reg & ~0x3f)
831 /* The register number won't fit in 6 bits, so we have to use
832 the long form. */
833 cfi->dw_cfi_opc = DW_CFA_offset_extended;
834 else
835 cfi->dw_cfi_opc = DW_CFA_offset;
837 #ifdef ENABLE_CHECKING
839 /* If we get an offset that is not a multiple of
840 DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
841 definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
842 description. */
843 HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
845 gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
847 #endif
848 offset /= DWARF_CIE_DATA_ALIGNMENT;
849 if (offset < 0)
850 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
852 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
854 else if (sreg == reg)
855 cfi->dw_cfi_opc = DW_CFA_same_value;
856 else
858 cfi->dw_cfi_opc = DW_CFA_register;
859 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
862 add_fde_cfi (label, cfi);
865 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
866 This CFI tells the unwinder that it needs to restore the window registers
867 from the previous frame's window save area.
869 ??? Perhaps we should note in the CIE where windows are saved (instead of
870 assuming 0(cfa)) and what registers are in the window. */
872 void
873 dwarf2out_window_save (const char *label)
875 dw_cfi_ref cfi = new_cfi ();
877 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
878 add_fde_cfi (label, cfi);
881 /* Add a CFI to update the running total of the size of arguments
882 pushed onto the stack. */
884 void
885 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
887 dw_cfi_ref cfi;
889 if (size == old_args_size)
890 return;
892 old_args_size = size;
894 cfi = new_cfi ();
895 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
896 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
897 add_fde_cfi (label, cfi);
900 /* Entry point for saving a register to the stack. REG is the GCC register
901 number. LABEL and OFFSET are passed to reg_save. */
903 void
904 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
906 reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
909 /* Entry point for saving the return address in the stack.
910 LABEL and OFFSET are passed to reg_save. */
912 void
913 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
915 reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
918 /* Entry point for saving the return address in a register.
919 LABEL and SREG are passed to reg_save. */
921 void
922 dwarf2out_return_reg (const char *label, unsigned int sreg)
924 reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
927 /* Record the initial position of the return address. RTL is
928 INCOMING_RETURN_ADDR_RTX. */
930 static void
931 initial_return_save (rtx rtl)
933 unsigned int reg = INVALID_REGNUM;
934 HOST_WIDE_INT offset = 0;
936 switch (GET_CODE (rtl))
938 case REG:
939 /* RA is in a register. */
940 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
941 break;
943 case MEM:
944 /* RA is on the stack. */
945 rtl = XEXP (rtl, 0);
946 switch (GET_CODE (rtl))
948 case REG:
949 gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
950 offset = 0;
951 break;
953 case PLUS:
954 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
955 offset = INTVAL (XEXP (rtl, 1));
956 break;
958 case MINUS:
959 gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
960 offset = -INTVAL (XEXP (rtl, 1));
961 break;
963 default:
964 gcc_unreachable ();
967 break;
969 case PLUS:
970 /* The return address is at some offset from any value we can
971 actually load. For instance, on the SPARC it is in %i7+8. Just
972 ignore the offset for now; it doesn't matter for unwinding frames. */
973 gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
974 initial_return_save (XEXP (rtl, 0));
975 return;
977 default:
978 gcc_unreachable ();
981 if (reg != DWARF_FRAME_RETURN_COLUMN)
982 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
985 /* Given a SET, calculate the amount of stack adjustment it
986 contains. */
988 static HOST_WIDE_INT
989 stack_adjust_offset (rtx pattern)
991 rtx src = SET_SRC (pattern);
992 rtx dest = SET_DEST (pattern);
993 HOST_WIDE_INT offset = 0;
994 enum rtx_code code;
996 if (dest == stack_pointer_rtx)
998 /* (set (reg sp) (plus (reg sp) (const_int))) */
999 code = GET_CODE (src);
1000 if (! (code == PLUS || code == MINUS)
1001 || XEXP (src, 0) != stack_pointer_rtx
1002 || GET_CODE (XEXP (src, 1)) != CONST_INT)
1003 return 0;
1005 offset = INTVAL (XEXP (src, 1));
1006 if (code == PLUS)
1007 offset = -offset;
1009 else if (MEM_P (dest))
1011 /* (set (mem (pre_dec (reg sp))) (foo)) */
1012 src = XEXP (dest, 0);
1013 code = GET_CODE (src);
1015 switch (code)
1017 case PRE_MODIFY:
1018 case POST_MODIFY:
1019 if (XEXP (src, 0) == stack_pointer_rtx)
1021 rtx val = XEXP (XEXP (src, 1), 1);
1022 /* We handle only adjustments by constant amount. */
1023 gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1024 && GET_CODE (val) == CONST_INT);
1025 offset = -INTVAL (val);
1026 break;
1028 return 0;
1030 case PRE_DEC:
1031 case POST_DEC:
1032 if (XEXP (src, 0) == stack_pointer_rtx)
1034 offset = GET_MODE_SIZE (GET_MODE (dest));
1035 break;
1037 return 0;
1039 case PRE_INC:
1040 case POST_INC:
1041 if (XEXP (src, 0) == stack_pointer_rtx)
1043 offset = -GET_MODE_SIZE (GET_MODE (dest));
1044 break;
1046 return 0;
1048 default:
1049 return 0;
1052 else
1053 return 0;
1055 return offset;
1058 /* Check INSN to see if it looks like a push or a stack adjustment, and
1059 make a note of it if it does. EH uses this information to find out how
1060 much extra space it needs to pop off the stack. */
1062 static void
1063 dwarf2out_stack_adjust (rtx insn, bool after_p)
1065 HOST_WIDE_INT offset;
1066 const char *label;
1067 int i;
1069 /* Don't handle epilogues at all. Certainly it would be wrong to do so
1070 with this function. Proper support would require all frame-related
1071 insns to be marked, and to be able to handle saving state around
1072 epilogues textually in the middle of the function. */
1073 if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1074 return;
1076 /* If only calls can throw, and we have a frame pointer,
1077 save up adjustments until we see the CALL_INSN. */
1078 if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1080 if (CALL_P (insn) && !after_p)
1082 /* Extract the size of the args from the CALL rtx itself. */
1083 insn = PATTERN (insn);
1084 if (GET_CODE (insn) == PARALLEL)
1085 insn = XVECEXP (insn, 0, 0);
1086 if (GET_CODE (insn) == SET)
1087 insn = SET_SRC (insn);
1088 gcc_assert (GET_CODE (insn) == CALL);
1089 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1091 return;
1094 if (CALL_P (insn) && !after_p)
1096 if (!flag_asynchronous_unwind_tables)
1097 dwarf2out_args_size ("", args_size);
1098 return;
1100 else if (BARRIER_P (insn))
1102 /* When we see a BARRIER, we know to reset args_size to 0. Usually
1103 the compiler will have already emitted a stack adjustment, but
1104 doesn't bother for calls to noreturn functions. */
1105 #ifdef STACK_GROWS_DOWNWARD
1106 offset = -args_size;
1107 #else
1108 offset = args_size;
1109 #endif
1111 else if (GET_CODE (PATTERN (insn)) == SET)
1112 offset = stack_adjust_offset (PATTERN (insn));
1113 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1114 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1116 /* There may be stack adjustments inside compound insns. Search
1117 for them. */
1118 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1119 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1120 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1122 else
1123 return;
1125 if (offset == 0)
1126 return;
1128 if (cfa.reg == STACK_POINTER_REGNUM)
1129 cfa.offset += offset;
1131 #ifndef STACK_GROWS_DOWNWARD
1132 offset = -offset;
1133 #endif
1135 args_size += offset;
1136 if (args_size < 0)
1137 args_size = 0;
1139 label = dwarf2out_cfi_label ();
1140 def_cfa_1 (label, &cfa);
1141 if (flag_asynchronous_unwind_tables)
1142 dwarf2out_args_size (label, args_size);
1145 #endif
1147 /* We delay emitting a register save until either (a) we reach the end
1148 of the prologue or (b) the register is clobbered. This clusters
1149 register saves so that there are fewer pc advances. */
1151 struct queued_reg_save GTY(())
1153 struct queued_reg_save *next;
1154 rtx reg;
1155 HOST_WIDE_INT cfa_offset;
1156 rtx saved_reg;
1159 static GTY(()) struct queued_reg_save *queued_reg_saves;
1161 /* The caller's ORIG_REG is saved in SAVED_IN_REG. */
1162 struct reg_saved_in_data GTY(()) {
1163 rtx orig_reg;
1164 rtx saved_in_reg;
1167 /* A list of registers saved in other registers.
1168 The list intentionally has a small maximum capacity of 4; if your
1169 port needs more than that, you might consider implementing a
1170 more efficient data structure. */
1171 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1172 static GTY(()) size_t num_regs_saved_in_regs;
1174 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1175 static const char *last_reg_save_label;
1177 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1178 SREG, or if SREG is NULL then it is saved at OFFSET to the CFA. */
1180 static void
1181 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1183 struct queued_reg_save *q;
1185 /* Duplicates waste space, but it's also necessary to remove them
1186 for correctness, since the queue gets output in reverse
1187 order. */
1188 for (q = queued_reg_saves; q != NULL; q = q->next)
1189 if (REGNO (q->reg) == REGNO (reg))
1190 break;
1192 if (q == NULL)
1194 q = ggc_alloc (sizeof (*q));
1195 q->next = queued_reg_saves;
1196 queued_reg_saves = q;
1199 q->reg = reg;
1200 q->cfa_offset = offset;
1201 q->saved_reg = sreg;
1203 last_reg_save_label = label;
1206 /* Output all the entries in QUEUED_REG_SAVES. */
1208 static void
1209 flush_queued_reg_saves (void)
1211 struct queued_reg_save *q;
1213 for (q = queued_reg_saves; q; q = q->next)
1215 size_t i;
1216 unsigned int reg, sreg;
1218 for (i = 0; i < num_regs_saved_in_regs; i++)
1219 if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1220 break;
1221 if (q->saved_reg && i == num_regs_saved_in_regs)
1223 gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1224 num_regs_saved_in_regs++;
1226 if (i != num_regs_saved_in_regs)
1228 regs_saved_in_regs[i].orig_reg = q->reg;
1229 regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1232 reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1233 if (q->saved_reg)
1234 sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1235 else
1236 sreg = INVALID_REGNUM;
1237 reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1240 queued_reg_saves = NULL;
1241 last_reg_save_label = NULL;
1244 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1245 location for? Or, does it clobber a register which we've previously
1246 said that some other register is saved in, and for which we now
1247 have a new location for? */
1249 static bool
1250 clobbers_queued_reg_save (rtx insn)
1252 struct queued_reg_save *q;
1254 for (q = queued_reg_saves; q; q = q->next)
1256 size_t i;
1257 if (modified_in_p (q->reg, insn))
1258 return true;
1259 for (i = 0; i < num_regs_saved_in_regs; i++)
1260 if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1261 && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1262 return true;
1265 return false;
1268 /* What register, if any, is currently saved in REG? */
1270 static rtx
1271 reg_saved_in (rtx reg)
1273 unsigned int regn = REGNO (reg);
1274 size_t i;
1275 struct queued_reg_save *q;
1277 for (q = queued_reg_saves; q; q = q->next)
1278 if (q->saved_reg && regn == REGNO (q->saved_reg))
1279 return q->reg;
1281 for (i = 0; i < num_regs_saved_in_regs; i++)
1282 if (regs_saved_in_regs[i].saved_in_reg
1283 && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1284 return regs_saved_in_regs[i].orig_reg;
1286 return NULL_RTX;
1290 /* A temporary register holding an integral value used in adjusting SP
1291 or setting up the store_reg. The "offset" field holds the integer
1292 value, not an offset. */
1293 static dw_cfa_location cfa_temp;
1295 /* Record call frame debugging information for an expression EXPR,
1296 which either sets SP or FP (adjusting how we calculate the frame
1297 address) or saves a register to the stack or another register.
1298 LABEL indicates the address of EXPR.
1300 This function encodes a state machine mapping rtxes to actions on
1301 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1302 users need not read the source code.
1304 The High-Level Picture
1306 Changes in the register we use to calculate the CFA: Currently we
1307 assume that if you copy the CFA register into another register, we
1308 should take the other one as the new CFA register; this seems to
1309 work pretty well. If it's wrong for some target, it's simple
1310 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1312 Changes in the register we use for saving registers to the stack:
1313 This is usually SP, but not always. Again, we deduce that if you
1314 copy SP into another register (and SP is not the CFA register),
1315 then the new register is the one we will be using for register
1316 saves. This also seems to work.
1318 Register saves: There's not much guesswork about this one; if
1319 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1320 register save, and the register used to calculate the destination
1321 had better be the one we think we're using for this purpose.
1322 It's also assumed that a copy from a call-saved register to another
1323 register is saving that register if RTX_FRAME_RELATED_P is set on
1324 that instruction. If the copy is from a call-saved register to
1325 the *same* register, that means that the register is now the same
1326 value as in the caller.
1328 Except: If the register being saved is the CFA register, and the
1329 offset is nonzero, we are saving the CFA, so we assume we have to
1330 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1331 the intent is to save the value of SP from the previous frame.
1333 In addition, if a register has previously been saved to a different
1334 register,
1336 Invariants / Summaries of Rules
1338 cfa current rule for calculating the CFA. It usually
1339 consists of a register and an offset.
1340 cfa_store register used by prologue code to save things to the stack
1341 cfa_store.offset is the offset from the value of
1342 cfa_store.reg to the actual CFA
1343 cfa_temp register holding an integral value. cfa_temp.offset
1344 stores the value, which will be used to adjust the
1345 stack pointer. cfa_temp is also used like cfa_store,
1346 to track stores to the stack via fp or a temp reg.
1348 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1349 with cfa.reg as the first operand changes the cfa.reg and its
1350 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1351 cfa_temp.offset.
1353 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1354 expression yielding a constant. This sets cfa_temp.reg
1355 and cfa_temp.offset.
1357 Rule 5: Create a new register cfa_store used to save items to the
1358 stack.
1360 Rules 10-14: Save a register to the stack. Define offset as the
1361 difference of the original location and cfa_store's
1362 location (or cfa_temp's location if cfa_temp is used).
1364 The Rules
1366 "{a,b}" indicates a choice of a xor b.
1367 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1369 Rule 1:
1370 (set <reg1> <reg2>:cfa.reg)
1371 effects: cfa.reg = <reg1>
1372 cfa.offset unchanged
1373 cfa_temp.reg = <reg1>
1374 cfa_temp.offset = cfa.offset
1376 Rule 2:
1377 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1378 {<const_int>,<reg>:cfa_temp.reg}))
1379 effects: cfa.reg = sp if fp used
1380 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1381 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1382 if cfa_store.reg==sp
1384 Rule 3:
1385 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1386 effects: cfa.reg = fp
1387 cfa_offset += +/- <const_int>
1389 Rule 4:
1390 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1391 constraints: <reg1> != fp
1392 <reg1> != sp
1393 effects: cfa.reg = <reg1>
1394 cfa_temp.reg = <reg1>
1395 cfa_temp.offset = cfa.offset
1397 Rule 5:
1398 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1399 constraints: <reg1> != fp
1400 <reg1> != sp
1401 effects: cfa_store.reg = <reg1>
1402 cfa_store.offset = cfa.offset - cfa_temp.offset
1404 Rule 6:
1405 (set <reg> <const_int>)
1406 effects: cfa_temp.reg = <reg>
1407 cfa_temp.offset = <const_int>
1409 Rule 7:
1410 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1411 effects: cfa_temp.reg = <reg1>
1412 cfa_temp.offset |= <const_int>
1414 Rule 8:
1415 (set <reg> (high <exp>))
1416 effects: none
1418 Rule 9:
1419 (set <reg> (lo_sum <exp> <const_int>))
1420 effects: cfa_temp.reg = <reg>
1421 cfa_temp.offset = <const_int>
1423 Rule 10:
1424 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1425 effects: cfa_store.offset -= <const_int>
1426 cfa.offset = cfa_store.offset if cfa.reg == sp
1427 cfa.reg = sp
1428 cfa.base_offset = -cfa_store.offset
1430 Rule 11:
1431 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1432 effects: cfa_store.offset += -/+ mode_size(mem)
1433 cfa.offset = cfa_store.offset if cfa.reg == sp
1434 cfa.reg = sp
1435 cfa.base_offset = -cfa_store.offset
1437 Rule 12:
1438 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1440 <reg2>)
1441 effects: cfa.reg = <reg1>
1442 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1444 Rule 13:
1445 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1446 effects: cfa.reg = <reg1>
1447 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1449 Rule 14:
1450 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1451 effects: cfa.reg = <reg1>
1452 cfa.base_offset = -cfa_temp.offset
1453 cfa_temp.offset -= mode_size(mem)
1455   Rule 15:
1456   (set <reg> {unspec, unspec_volatile})
1457   effects: target-dependent */
1459 static void
1460 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1462 rtx src, dest;
1463 HOST_WIDE_INT offset;
1465 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1466 the PARALLEL independently. The first element is always processed if
1467 it is a SET. This is for backward compatibility. Other elements
1468 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1469 flag is set in them. */
1470 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1472 int par_index;
1473 int limit = XVECLEN (expr, 0);
1475 for (par_index = 0; par_index < limit; par_index++)
1476 if (GET_CODE (XVECEXP (expr, 0, par_index)) == SET
1477 && (RTX_FRAME_RELATED_P (XVECEXP (expr, 0, par_index))
1478 || par_index == 0))
1479 dwarf2out_frame_debug_expr (XVECEXP (expr, 0, par_index), label);
1481 return;
1484 gcc_assert (GET_CODE (expr) == SET);
1486 src = SET_SRC (expr);
1487 dest = SET_DEST (expr);
1489 if (REG_P (src))
1491 rtx rsi = reg_saved_in (src);
1492 if (rsi)
1493 src = rsi;
1496 switch (GET_CODE (dest))
1498 case REG:
1499 switch (GET_CODE (src))
1501 /* Setting FP from SP. */
1502 case REG:
1503 if (cfa.reg == (unsigned) REGNO (src))
1505 /* Rule 1 */
1506 /* Update the CFA rule wrt SP or FP. Make sure src is
1507 relative to the current CFA register.
1509 We used to require that dest be either SP or FP, but the
1510 ARM copies SP to a temporary register, and from there to
1511 FP. So we just rely on the backends to only set
1512 RTX_FRAME_RELATED_P on appropriate insns. */
1513 cfa.reg = REGNO (dest);
1514 cfa_temp.reg = cfa.reg;
1515 cfa_temp.offset = cfa.offset;
1517 else
1519 /* Saving a register in a register. */
1520 gcc_assert (call_used_regs [REGNO (dest)]
1521 && (!fixed_regs [REGNO (dest)]
1522 /* For the SPARC and its register window. */
1523 || DWARF_FRAME_REGNUM (REGNO (src))
1524 == DWARF_FRAME_RETURN_COLUMN));
1525 queue_reg_save (label, src, dest, 0);
1527 break;
1529 case PLUS:
1530 case MINUS:
1531 case LO_SUM:
1532 if (dest == stack_pointer_rtx)
1534 /* Rule 2 */
1535 /* Adjusting SP. */
1536 switch (GET_CODE (XEXP (src, 1)))
1538 case CONST_INT:
1539 offset = INTVAL (XEXP (src, 1));
1540 break;
1541 case REG:
1542 gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1543 == cfa_temp.reg);
1544 offset = cfa_temp.offset;
1545 break;
1546 default:
1547 gcc_unreachable ();
1550 if (XEXP (src, 0) == hard_frame_pointer_rtx)
1552 /* Restoring SP from FP in the epilogue. */
1553 gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1554 cfa.reg = STACK_POINTER_REGNUM;
1556 else if (GET_CODE (src) == LO_SUM)
1557 /* Assume we've set the source reg of the LO_SUM from sp. */
1559 else
1560 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1562 if (GET_CODE (src) != MINUS)
1563 offset = -offset;
1564 if (cfa.reg == STACK_POINTER_REGNUM)
1565 cfa.offset += offset;
1566 if (cfa_store.reg == STACK_POINTER_REGNUM)
1567 cfa_store.offset += offset;
1569 else if (dest == hard_frame_pointer_rtx)
1571 /* Rule 3 */
1572 /* Either setting the FP from an offset of the SP,
1573 or adjusting the FP */
1574 gcc_assert (frame_pointer_needed);
1576 gcc_assert (REG_P (XEXP (src, 0))
1577 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1578 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1579 offset = INTVAL (XEXP (src, 1));
1580 if (GET_CODE (src) != MINUS)
1581 offset = -offset;
1582 cfa.offset += offset;
1583 cfa.reg = HARD_FRAME_POINTER_REGNUM;
1585 else
1587 gcc_assert (GET_CODE (src) != MINUS);
1589 /* Rule 4 */
1590 if (REG_P (XEXP (src, 0))
1591 && REGNO (XEXP (src, 0)) == cfa.reg
1592 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1594 /* Setting a temporary CFA register that will be copied
1595 into the FP later on. */
1596 offset = - INTVAL (XEXP (src, 1));
1597 cfa.offset += offset;
1598 cfa.reg = REGNO (dest);
1599 /* Or used to save regs to the stack. */
1600 cfa_temp.reg = cfa.reg;
1601 cfa_temp.offset = cfa.offset;
1604 /* Rule 5 */
1605 else if (REG_P (XEXP (src, 0))
1606 && REGNO (XEXP (src, 0)) == cfa_temp.reg
1607 && XEXP (src, 1) == stack_pointer_rtx)
1609 /* Setting a scratch register that we will use instead
1610 of SP for saving registers to the stack. */
1611 gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1612 cfa_store.reg = REGNO (dest);
1613 cfa_store.offset = cfa.offset - cfa_temp.offset;
1616 /* Rule 9 */
1617 else if (GET_CODE (src) == LO_SUM
1618 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1620 cfa_temp.reg = REGNO (dest);
1621 cfa_temp.offset = INTVAL (XEXP (src, 1));
1623 else
1624 gcc_unreachable ();
1626 break;
1628 /* Rule 6 */
1629 case CONST_INT:
1630 cfa_temp.reg = REGNO (dest);
1631 cfa_temp.offset = INTVAL (src);
1632 break;
1634 /* Rule 7 */
1635 case IOR:
1636 gcc_assert (REG_P (XEXP (src, 0))
1637 && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1638 && GET_CODE (XEXP (src, 1)) == CONST_INT);
1640 if ((unsigned) REGNO (dest) != cfa_temp.reg)
1641 cfa_temp.reg = REGNO (dest);
1642 cfa_temp.offset |= INTVAL (XEXP (src, 1));
1643 break;
1645 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1646 which will fill in all of the bits. */
1647 /* Rule 8 */
1648 case HIGH:
1649 break;
1651 /* Rule 15 */
1652 case UNSPEC:
1653 case UNSPEC_VOLATILE:
1654 gcc_assert (targetm.dwarf_handle_frame_unspec);
1655 targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1656 break;
1658 default:
1659 gcc_unreachable ();
1662 def_cfa_1 (label, &cfa);
1663 break;
1665 case MEM:
1666 gcc_assert (REG_P (src));
1668 /* Saving a register to the stack. Make sure dest is relative to the
1669 CFA register. */
1670 switch (GET_CODE (XEXP (dest, 0)))
1672 /* Rule 10 */
1673 /* With a push. */
1674 case PRE_MODIFY:
1675 /* We can't handle variable size modifications. */
1676 gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1677 == CONST_INT);
1678 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1680 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1681 && cfa_store.reg == STACK_POINTER_REGNUM);
1683 cfa_store.offset += offset;
1684 if (cfa.reg == STACK_POINTER_REGNUM)
1685 cfa.offset = cfa_store.offset;
1687 offset = -cfa_store.offset;
1688 break;
1690 /* Rule 11 */
1691 case PRE_INC:
1692 case PRE_DEC:
1693 offset = GET_MODE_SIZE (GET_MODE (dest));
1694 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1695 offset = -offset;
1697 gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1698 && cfa_store.reg == STACK_POINTER_REGNUM);
1700 cfa_store.offset += offset;
1701 if (cfa.reg == STACK_POINTER_REGNUM)
1702 cfa.offset = cfa_store.offset;
1704 offset = -cfa_store.offset;
1705 break;
1707 /* Rule 12 */
1708 /* With an offset. */
1709 case PLUS:
1710 case MINUS:
1711 case LO_SUM:
1713 int regno;
1715 gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT);
1716 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1717 if (GET_CODE (XEXP (dest, 0)) == MINUS)
1718 offset = -offset;
1720 regno = REGNO (XEXP (XEXP (dest, 0), 0));
1722 if (cfa_store.reg == (unsigned) regno)
1723 offset -= cfa_store.offset;
1724 else
1726 gcc_assert (cfa_temp.reg == (unsigned) regno);
1727 offset -= cfa_temp.offset;
1730 break;
1732 /* Rule 13 */
1733 /* Without an offset. */
1734 case REG:
1736 int regno = REGNO (XEXP (dest, 0));
1738 if (cfa_store.reg == (unsigned) regno)
1739 offset = -cfa_store.offset;
1740 else
1742 gcc_assert (cfa_temp.reg == (unsigned) regno);
1743 offset = -cfa_temp.offset;
1746 break;
1748 /* Rule 14 */
1749 case POST_INC:
1750 gcc_assert (cfa_temp.reg
1751 == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1752 offset = -cfa_temp.offset;
1753 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1754 break;
1756 default:
1757 gcc_unreachable ();
1760 if (REGNO (src) != STACK_POINTER_REGNUM
1761 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1762 && (unsigned) REGNO (src) == cfa.reg)
1764 /* We're storing the current CFA reg into the stack. */
1766 if (cfa.offset == 0)
1768 /* If the source register is exactly the CFA, assume
1769 we're saving SP like any other register; this happens
1770 on the ARM. */
1771 def_cfa_1 (label, &cfa);
1772 queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1773 break;
1775 else
1777 /* Otherwise, we'll need to look in the stack to
1778 calculate the CFA. */
1779 rtx x = XEXP (dest, 0);
1781 if (!REG_P (x))
1782 x = XEXP (x, 0);
1783 gcc_assert (REG_P (x));
1785 cfa.reg = REGNO (x);
1786 cfa.base_offset = offset;
1787 cfa.indirect = 1;
1788 def_cfa_1 (label, &cfa);
1789 break;
1793 def_cfa_1 (label, &cfa);
1794 queue_reg_save (label, src, NULL_RTX, offset);
1795 break;
1797 default:
1798 gcc_unreachable ();
1802 /* Record call frame debugging information for INSN, which either
1803 sets SP or FP (adjusting how we calculate the frame address) or saves a
1804 register to the stack. If INSN is NULL_RTX, initialize our state.
1806 If AFTER_P is false, we're being called before the insn is emitted,
1807 otherwise after. Call instructions get invoked twice. */
1809 void
1810 dwarf2out_frame_debug (rtx insn, bool after_p)
1812 const char *label;
1813 rtx src;
1815 if (insn == NULL_RTX)
1817 size_t i;
1819 /* Flush any queued register saves. */
1820 flush_queued_reg_saves ();
1822 /* Set up state for generating call frame debug info. */
1823 lookup_cfa (&cfa);
1824 gcc_assert (cfa.reg
1825 == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1827 cfa.reg = STACK_POINTER_REGNUM;
1828 cfa_store = cfa;
1829 cfa_temp.reg = -1;
1830 cfa_temp.offset = 0;
1832 for (i = 0; i < num_regs_saved_in_regs; i++)
1834 regs_saved_in_regs[i].orig_reg = NULL_RTX;
1835 regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1837 num_regs_saved_in_regs = 0;
1838 return;
1841 if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1842 flush_queued_reg_saves ();
1844 if (! RTX_FRAME_RELATED_P (insn))
1846 if (!ACCUMULATE_OUTGOING_ARGS)
1847 dwarf2out_stack_adjust (insn, after_p);
1848 return;
1851 label = dwarf2out_cfi_label ();
1852 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1853 if (src)
1854 insn = XEXP (src, 0);
1855 else
1856 insn = PATTERN (insn);
1858 dwarf2out_frame_debug_expr (insn, label);
1861 #endif
1863 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
1864 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1865 (enum dwarf_call_frame_info cfi);
1867 static enum dw_cfi_oprnd_type
1868 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1870 switch (cfi)
1872 case DW_CFA_nop:
1873 case DW_CFA_GNU_window_save:
1874 return dw_cfi_oprnd_unused;
1876 case DW_CFA_set_loc:
1877 case DW_CFA_advance_loc1:
1878 case DW_CFA_advance_loc2:
1879 case DW_CFA_advance_loc4:
1880 case DW_CFA_MIPS_advance_loc8:
1881 return dw_cfi_oprnd_addr;
1883 case DW_CFA_offset:
1884 case DW_CFA_offset_extended:
1885 case DW_CFA_def_cfa:
1886 case DW_CFA_offset_extended_sf:
1887 case DW_CFA_def_cfa_sf:
1888 case DW_CFA_restore_extended:
1889 case DW_CFA_undefined:
1890 case DW_CFA_same_value:
1891 case DW_CFA_def_cfa_register:
1892 case DW_CFA_register:
1893 return dw_cfi_oprnd_reg_num;
1895 case DW_CFA_def_cfa_offset:
1896 case DW_CFA_GNU_args_size:
1897 case DW_CFA_def_cfa_offset_sf:
1898 return dw_cfi_oprnd_offset;
1900 case DW_CFA_def_cfa_expression:
1901 case DW_CFA_expression:
1902 return dw_cfi_oprnd_loc;
1904 default:
1905 gcc_unreachable ();
1909 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
1910 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1911 (enum dwarf_call_frame_info cfi);
1913 static enum dw_cfi_oprnd_type
1914 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1916 switch (cfi)
1918 case DW_CFA_def_cfa:
1919 case DW_CFA_def_cfa_sf:
1920 case DW_CFA_offset:
1921 case DW_CFA_offset_extended_sf:
1922 case DW_CFA_offset_extended:
1923 return dw_cfi_oprnd_offset;
1925 case DW_CFA_register:
1926 return dw_cfi_oprnd_reg_num;
1928 default:
1929 return dw_cfi_oprnd_unused;
1933 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1935 /* Map register numbers held in the call frame info that gcc has
1936 collected using DWARF_FRAME_REGNUM to those that should be output in
1937 .debug_frame and .eh_frame. */
1938 #ifndef DWARF2_FRAME_REG_OUT
1939 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
1940 #endif
1942 /* Output a Call Frame Information opcode and its operand(s). */
1944 static void
1945 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
1947 unsigned long r;
1948 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
1949 dw2_asm_output_data (1, (cfi->dw_cfi_opc
1950 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
1951 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
1952 cfi->dw_cfi_oprnd1.dw_cfi_offset);
1953 else if (cfi->dw_cfi_opc == DW_CFA_offset)
1955 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1956 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
1957 "DW_CFA_offset, column 0x%lx", r);
1958 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1960 else if (cfi->dw_cfi_opc == DW_CFA_restore)
1962 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
1963 dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
1964 "DW_CFA_restore, column 0x%lx", r);
1966 else
1968 dw2_asm_output_data (1, cfi->dw_cfi_opc,
1969 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
1971 switch (cfi->dw_cfi_opc)
1973 case DW_CFA_set_loc:
1974 if (for_eh)
1975 dw2_asm_output_encoded_addr_rtx (
1976 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
1977 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
1978 NULL);
1979 else
1980 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
1981 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
1982 break;
1984 case DW_CFA_advance_loc1:
1985 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1986 fde->dw_fde_current_label, NULL);
1987 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1988 break;
1990 case DW_CFA_advance_loc2:
1991 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1992 fde->dw_fde_current_label, NULL);
1993 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1994 break;
1996 case DW_CFA_advance_loc4:
1997 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1998 fde->dw_fde_current_label, NULL);
1999 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2000 break;
2002 case DW_CFA_MIPS_advance_loc8:
2003 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2004 fde->dw_fde_current_label, NULL);
2005 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2006 break;
2008 case DW_CFA_offset_extended:
2009 case DW_CFA_def_cfa:
2010 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2011 dw2_asm_output_data_uleb128 (r, NULL);
2012 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2013 break;
2015 case DW_CFA_offset_extended_sf:
2016 case DW_CFA_def_cfa_sf:
2017 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2018 dw2_asm_output_data_uleb128 (r, NULL);
2019 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2020 break;
2022 case DW_CFA_restore_extended:
2023 case DW_CFA_undefined:
2024 case DW_CFA_same_value:
2025 case DW_CFA_def_cfa_register:
2026 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2027 dw2_asm_output_data_uleb128 (r, NULL);
2028 break;
2030 case DW_CFA_register:
2031 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2032 dw2_asm_output_data_uleb128 (r, NULL);
2033 r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2034 dw2_asm_output_data_uleb128 (r, NULL);
2035 break;
2037 case DW_CFA_def_cfa_offset:
2038 case DW_CFA_GNU_args_size:
2039 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2040 break;
2042 case DW_CFA_def_cfa_offset_sf:
2043 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2044 break;
2046 case DW_CFA_GNU_window_save:
2047 break;
2049 case DW_CFA_def_cfa_expression:
2050 case DW_CFA_expression:
2051 output_cfa_loc (cfi);
2052 break;
2054 case DW_CFA_GNU_negative_offset_extended:
2055 /* Obsoleted by DW_CFA_offset_extended_sf. */
2056 gcc_unreachable ();
2058 default:
2059 break;
2064 /* Output the call frame information used to record information
2065 that relates to calculating the frame pointer, and records the
2066 location of saved registers. */
2068 static void
2069 output_call_frame_info (int for_eh)
2071 unsigned int i;
2072 dw_fde_ref fde;
2073 dw_cfi_ref cfi;
2074 char l1[20], l2[20], section_start_label[20];
2075 bool any_lsda_needed = false;
2076 char augmentation[6];
2077 int augmentation_size;
2078 int fde_encoding = DW_EH_PE_absptr;
2079 int per_encoding = DW_EH_PE_absptr;
2080 int lsda_encoding = DW_EH_PE_absptr;
2081 int return_reg;
2083 /* Don't emit a CIE if there won't be any FDEs. */
2084 if (fde_table_in_use == 0)
2085 return;
2087 /* If we make FDEs linkonce, we may have to emit an empty label for
2088 an FDE that wouldn't otherwise be emitted. We want to avoid
2089 having an FDE kept around when the function it refers to is
2090 discarded. Example where this matters: a primary function
2091 template in C++ requires EH information, but an explicit
2092 specialization doesn't. */
2093 if (TARGET_USES_WEAK_UNWIND_INFO
2094 && ! flag_asynchronous_unwind_tables
2095 && for_eh)
2096 for (i = 0; i < fde_table_in_use; i++)
2097 if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2098 && !fde_table[i].uses_eh_lsda
2099 && ! DECL_WEAK (fde_table[i].decl))
2100 targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2101 for_eh, /* empty */ 1);
2103 /* If we don't have any functions we'll want to unwind out of, don't
2104 emit any EH unwind information. Note that if exceptions aren't
2105 enabled, we won't have collected nothrow information, and if we
2106 asked for asynchronous tables, we always want this info. */
2107 if (for_eh)
2109 bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2111 for (i = 0; i < fde_table_in_use; i++)
2112 if (fde_table[i].uses_eh_lsda)
2113 any_eh_needed = any_lsda_needed = true;
2114 else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2115 any_eh_needed = true;
2116 else if (! fde_table[i].nothrow
2117 && ! fde_table[i].all_throwers_are_sibcalls)
2118 any_eh_needed = true;
2120 if (! any_eh_needed)
2121 return;
2124 /* We're going to be generating comments, so turn on app. */
2125 if (flag_debug_asm)
2126 app_enable ();
2128 if (for_eh)
2129 targetm.asm_out.eh_frame_section ();
2130 else
2131 named_section_flags (DEBUG_FRAME_SECTION, SECTION_DEBUG);
2133 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2134 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2136 /* Output the CIE. */
2137 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2138 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2139 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2140 "Length of Common Information Entry");
2141 ASM_OUTPUT_LABEL (asm_out_file, l1);
2143 /* Now that the CIE pointer is PC-relative for EH,
2144 use 0 to identify the CIE. */
2145 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2146 (for_eh ? 0 : DW_CIE_ID),
2147 "CIE Identifier Tag");
2149 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2151 augmentation[0] = 0;
2152 augmentation_size = 0;
2153 if (for_eh)
2155 char *p;
2157 /* Augmentation:
2158 z Indicates that a uleb128 is present to size the
2159 augmentation section.
2160 L Indicates the encoding (and thus presence) of
2161 an LSDA pointer in the FDE augmentation.
2162 R Indicates a non-default pointer encoding for
2163 FDE code pointers.
2164 P Indicates the presence of an encoding + language
2165 personality routine in the CIE augmentation. */
2167 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2168 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2169 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2171 p = augmentation + 1;
2172 if (eh_personality_libfunc)
2174 *p++ = 'P';
2175 augmentation_size += 1 + size_of_encoded_value (per_encoding);
2177 if (any_lsda_needed)
2179 *p++ = 'L';
2180 augmentation_size += 1;
2182 if (fde_encoding != DW_EH_PE_absptr)
2184 *p++ = 'R';
2185 augmentation_size += 1;
2187 if (p > augmentation + 1)
2189 augmentation[0] = 'z';
2190 *p = '\0';
2193 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
2194 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2196 int offset = ( 4 /* Length */
2197 + 4 /* CIE Id */
2198 + 1 /* CIE version */
2199 + strlen (augmentation) + 1 /* Augmentation */
2200 + size_of_uleb128 (1) /* Code alignment */
2201 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2202 + 1 /* RA column */
2203 + 1 /* Augmentation size */
2204 + 1 /* Personality encoding */ );
2205 int pad = -offset & (PTR_SIZE - 1);
2207 augmentation_size += pad;
2209 /* Augmentations should be small, so there's scarce need to
2210 iterate for a solution. Die if we exceed one uleb128 byte. */
2211 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2215 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2216 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2217 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2218 "CIE Data Alignment Factor");
2220 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2221 if (DW_CIE_VERSION == 1)
2222 dw2_asm_output_data (1, return_reg, "CIE RA Column");
2223 else
2224 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2226 if (augmentation[0])
2228 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2229 if (eh_personality_libfunc)
2231 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2232 eh_data_format_name (per_encoding));
2233 dw2_asm_output_encoded_addr_rtx (per_encoding,
2234 eh_personality_libfunc, NULL);
2237 if (any_lsda_needed)
2238 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2239 eh_data_format_name (lsda_encoding));
2241 if (fde_encoding != DW_EH_PE_absptr)
2242 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2243 eh_data_format_name (fde_encoding));
2246 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2247 output_cfi (cfi, NULL, for_eh);
2249 /* Pad the CIE out to an address sized boundary. */
2250 ASM_OUTPUT_ALIGN (asm_out_file,
2251 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2252 ASM_OUTPUT_LABEL (asm_out_file, l2);
2254 /* Loop through all of the FDE's. */
2255 for (i = 0; i < fde_table_in_use; i++)
2257 fde = &fde_table[i];
2259 /* Don't emit EH unwind info for leaf functions that don't need it. */
2260 if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2261 && (fde->nothrow || fde->all_throwers_are_sibcalls)
2262 && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2263 && !fde->uses_eh_lsda)
2264 continue;
2266 targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2267 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2268 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2269 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2270 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2271 "FDE Length");
2272 ASM_OUTPUT_LABEL (asm_out_file, l1);
2274 if (for_eh)
2275 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2276 else
2277 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2278 "FDE CIE offset");
2280 if (for_eh)
2282 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2283 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2284 dw2_asm_output_encoded_addr_rtx (fde_encoding,
2285 sym_ref,
2286 "FDE initial location");
2287 if (fde->dw_fde_switched_sections)
2289 rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2290 fde->dw_fde_unlikely_section_label);
2291 rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2292 fde->dw_fde_hot_section_label);
2293 SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2294 SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2295 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3,
2296 "FDE initial location");
2297 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2298 fde->dw_fde_hot_section_end_label,
2299 fde->dw_fde_hot_section_label,
2300 "FDE address range");
2301 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2,
2302 "FDE initial location");
2303 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2304 fde->dw_fde_unlikely_section_end_label,
2305 fde->dw_fde_unlikely_section_label,
2306 "FDE address range");
2308 else
2309 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2310 fde->dw_fde_end, fde->dw_fde_begin,
2311 "FDE address range");
2313 else
2315 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2316 "FDE initial location");
2317 if (fde->dw_fde_switched_sections)
2319 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2320 fde->dw_fde_hot_section_label,
2321 "FDE initial location");
2322 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2323 fde->dw_fde_hot_section_end_label,
2324 fde->dw_fde_hot_section_label,
2325 "FDE address range");
2326 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2327 fde->dw_fde_unlikely_section_label,
2328 "FDE initial location");
2329 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2330 fde->dw_fde_unlikely_section_end_label,
2331 fde->dw_fde_unlikely_section_label,
2332 "FDE address range");
2334 else
2335 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2336 fde->dw_fde_end, fde->dw_fde_begin,
2337 "FDE address range");
2340 if (augmentation[0])
2342 if (any_lsda_needed)
2344 int size = size_of_encoded_value (lsda_encoding);
2346 if (lsda_encoding == DW_EH_PE_aligned)
2348 int offset = ( 4 /* Length */
2349 + 4 /* CIE offset */
2350 + 2 * size_of_encoded_value (fde_encoding)
2351 + 1 /* Augmentation size */ );
2352 int pad = -offset & (PTR_SIZE - 1);
2354 size += pad;
2355 gcc_assert (size_of_uleb128 (size) == 1);
2358 dw2_asm_output_data_uleb128 (size, "Augmentation size");
2360 if (fde->uses_eh_lsda)
2362 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2363 fde->funcdef_number);
2364 dw2_asm_output_encoded_addr_rtx (
2365 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2366 "Language Specific Data Area");
2368 else
2370 if (lsda_encoding == DW_EH_PE_aligned)
2371 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2372 dw2_asm_output_data
2373 (size_of_encoded_value (lsda_encoding), 0,
2374 "Language Specific Data Area (none)");
2377 else
2378 dw2_asm_output_data_uleb128 (0, "Augmentation size");
2381 /* Loop through the Call Frame Instructions associated with
2382 this FDE. */
2383 fde->dw_fde_current_label = fde->dw_fde_begin;
2384 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2385 output_cfi (cfi, fde, for_eh);
2387 /* Pad the FDE out to an address sized boundary. */
2388 ASM_OUTPUT_ALIGN (asm_out_file,
2389 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2390 ASM_OUTPUT_LABEL (asm_out_file, l2);
2393 if (for_eh && targetm.terminate_dw2_eh_frame_info)
2394 dw2_asm_output_data (4, 0, "End of Table");
2395 #ifdef MIPS_DEBUGGING_INFO
2396 /* Work around Irix 6 assembler bug whereby labels at the end of a section
2397 get a value of 0. Putting .align 0 after the label fixes it. */
2398 ASM_OUTPUT_ALIGN (asm_out_file, 0);
2399 #endif
2401 /* Turn off app to make assembly quicker. */
2402 if (flag_debug_asm)
2403 app_disable ();
2406 /* Output a marker (i.e. a label) for the beginning of a function, before
2407 the prologue. */
2409 void
2410 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2411 const char *file ATTRIBUTE_UNUSED)
2413 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2414 char * dup_label;
2415 dw_fde_ref fde;
2417 current_function_func_begin_label = NULL;
2419 #ifdef TARGET_UNWIND_INFO
2420 /* ??? current_function_func_begin_label is also used by except.c
2421 for call-site information. We must emit this label if it might
2422 be used. */
2423 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2424 && ! dwarf2out_do_frame ())
2425 return;
2426 #else
2427 if (! dwarf2out_do_frame ())
2428 return;
2429 #endif
2431 function_section (current_function_decl);
2432 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2433 current_function_funcdef_no);
2434 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2435 current_function_funcdef_no);
2436 dup_label = xstrdup (label);
2437 current_function_func_begin_label = dup_label;
2439 #ifdef TARGET_UNWIND_INFO
2440 /* We can elide the fde allocation if we're not emitting debug info. */
2441 if (! dwarf2out_do_frame ())
2442 return;
2443 #endif
2445 /* Expand the fde table if necessary. */
2446 if (fde_table_in_use == fde_table_allocated)
2448 fde_table_allocated += FDE_TABLE_INCREMENT;
2449 fde_table = ggc_realloc (fde_table,
2450 fde_table_allocated * sizeof (dw_fde_node));
2451 memset (fde_table + fde_table_in_use, 0,
2452 FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2455 /* Record the FDE associated with this function. */
2456 current_funcdef_fde = fde_table_in_use;
2458 /* Add the new FDE at the end of the fde_table. */
2459 fde = &fde_table[fde_table_in_use++];
2460 fde->decl = current_function_decl;
2461 fde->dw_fde_begin = dup_label;
2462 fde->dw_fde_current_label = NULL;
2463 fde->dw_fde_hot_section_label = NULL;
2464 fde->dw_fde_hot_section_end_label = NULL;
2465 fde->dw_fde_unlikely_section_label = NULL;
2466 fde->dw_fde_unlikely_section_end_label = NULL;
2467 fde->dw_fde_switched_sections = false;
2468 fde->dw_fde_end = NULL;
2469 fde->dw_fde_cfi = NULL;
2470 fde->funcdef_number = current_function_funcdef_no;
2471 fde->nothrow = TREE_NOTHROW (current_function_decl);
2472 fde->uses_eh_lsda = cfun->uses_eh_lsda;
2473 fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2475 args_size = old_args_size = 0;
2477 /* We only want to output line number information for the genuine dwarf2
2478 prologue case, not the eh frame case. */
2479 #ifdef DWARF2_DEBUGGING_INFO
2480 if (file)
2481 dwarf2out_source_line (line, file);
2482 #endif
2485 /* Output a marker (i.e. a label) for the absolute end of the generated code
2486 for a function definition. This gets called *after* the epilogue code has
2487 been generated. */
2489 void
2490 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2491 const char *file ATTRIBUTE_UNUSED)
2493 dw_fde_ref fde;
2494 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2496 /* Output a label to mark the endpoint of the code generated for this
2497 function. */
2498 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2499 current_function_funcdef_no);
2500 ASM_OUTPUT_LABEL (asm_out_file, label);
2501 fde = &fde_table[fde_table_in_use - 1];
2502 fde->dw_fde_end = xstrdup (label);
2505 void
2506 dwarf2out_frame_init (void)
2508 /* Allocate the initial hunk of the fde_table. */
2509 fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2510 fde_table_allocated = FDE_TABLE_INCREMENT;
2511 fde_table_in_use = 0;
2513 /* Generate the CFA instructions common to all FDE's. Do it now for the
2514 sake of lookup_cfa. */
2516 #ifdef DWARF2_UNWIND_INFO
2517 /* On entry, the Canonical Frame Address is at SP. */
2518 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2519 initial_return_save (INCOMING_RETURN_ADDR_RTX);
2520 #endif
2523 void
2524 dwarf2out_frame_finish (void)
2526 /* Output call frame information. */
2527 if (write_symbols == DWARF2_DEBUG
2528 || write_symbols == VMS_AND_DWARF2_DEBUG
2529 #ifdef DWARF2_FRAME_INFO
2530 || DWARF2_FRAME_INFO
2531 #endif
2533 output_call_frame_info (0);
2535 #ifndef TARGET_UNWIND_INFO
2536 /* Output another copy for the unwinder. */
2537 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2538 output_call_frame_info (1);
2539 #endif
2541 #endif
2543 /* And now, the subset of the debugging information support code necessary
2544 for emitting location expressions. */
2546 /* We need some way to distinguish DW_OP_addr with a direct symbol
2547 relocation from DW_OP_addr with a dtp-relative symbol relocation. */
2548 #define INTERNAL_DW_OP_tls_addr (0x100 + DW_OP_addr)
2551 typedef struct dw_val_struct *dw_val_ref;
2552 typedef struct die_struct *dw_die_ref;
2553 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2554 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2556 /* Each DIE may have a series of attribute/value pairs. Values
2557 can take on several forms. The forms that are used in this
2558 implementation are listed below. */
2560 enum dw_val_class
2562 dw_val_class_addr,
2563 dw_val_class_offset,
2564 dw_val_class_loc,
2565 dw_val_class_loc_list,
2566 dw_val_class_range_list,
2567 dw_val_class_const,
2568 dw_val_class_unsigned_const,
2569 dw_val_class_long_long,
2570 dw_val_class_vec,
2571 dw_val_class_flag,
2572 dw_val_class_die_ref,
2573 dw_val_class_fde_ref,
2574 dw_val_class_lbl_id,
2575 dw_val_class_lbl_offset,
2576 dw_val_class_str
2579 /* Describe a double word constant value. */
2580 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
2582 typedef struct dw_long_long_struct GTY(())
2584 unsigned long hi;
2585 unsigned long low;
2587 dw_long_long_const;
2589 /* Describe a floating point constant value, or a vector constant value. */
2591 typedef struct dw_vec_struct GTY(())
2593 unsigned char * GTY((length ("%h.length"))) array;
2594 unsigned length;
2595 unsigned elt_size;
2597 dw_vec_const;
2599 /* The dw_val_node describes an attribute's value, as it is
2600 represented internally. */
2602 typedef struct dw_val_struct GTY(())
2604 enum dw_val_class val_class;
2605 union dw_val_struct_union
2607 rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2608 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2609 dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2610 dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2611 HOST_WIDE_INT GTY ((default)) val_int;
2612 unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2613 dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2614 dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2615 struct dw_val_die_union
2617 dw_die_ref die;
2618 int external;
2619 } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2620 unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2621 struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2622 char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2623 unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2625 GTY ((desc ("%1.val_class"))) v;
2627 dw_val_node;
2629 /* Locations in memory are described using a sequence of stack machine
2630 operations. */
2632 typedef struct dw_loc_descr_struct GTY(())
2634 dw_loc_descr_ref dw_loc_next;
2635 enum dwarf_location_atom dw_loc_opc;
2636 dw_val_node dw_loc_oprnd1;
2637 dw_val_node dw_loc_oprnd2;
2638 int dw_loc_addr;
2640 dw_loc_descr_node;
2642 /* Location lists are ranges + location descriptions for that range,
2643 so you can track variables that are in different places over
2644 their entire life. */
2645 typedef struct dw_loc_list_struct GTY(())
2647 dw_loc_list_ref dw_loc_next;
2648 const char *begin; /* Label for begin address of range */
2649 const char *end; /* Label for end address of range */
2650 char *ll_symbol; /* Label for beginning of location list.
2651 Only on head of list */
2652 const char *section; /* Section this loclist is relative to */
2653 dw_loc_descr_ref expr;
2654 } dw_loc_list_node;
2656 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2658 static const char *dwarf_stack_op_name (unsigned);
2659 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2660 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2661 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2662 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2663 static unsigned long size_of_locs (dw_loc_descr_ref);
2664 static void output_loc_operands (dw_loc_descr_ref);
2665 static void output_loc_sequence (dw_loc_descr_ref);
2667 /* Convert a DWARF stack opcode into its string name. */
2669 static const char *
2670 dwarf_stack_op_name (unsigned int op)
2672 switch (op)
2674 case DW_OP_addr:
2675 case INTERNAL_DW_OP_tls_addr:
2676 return "DW_OP_addr";
2677 case DW_OP_deref:
2678 return "DW_OP_deref";
2679 case DW_OP_const1u:
2680 return "DW_OP_const1u";
2681 case DW_OP_const1s:
2682 return "DW_OP_const1s";
2683 case DW_OP_const2u:
2684 return "DW_OP_const2u";
2685 case DW_OP_const2s:
2686 return "DW_OP_const2s";
2687 case DW_OP_const4u:
2688 return "DW_OP_const4u";
2689 case DW_OP_const4s:
2690 return "DW_OP_const4s";
2691 case DW_OP_const8u:
2692 return "DW_OP_const8u";
2693 case DW_OP_const8s:
2694 return "DW_OP_const8s";
2695 case DW_OP_constu:
2696 return "DW_OP_constu";
2697 case DW_OP_consts:
2698 return "DW_OP_consts";
2699 case DW_OP_dup:
2700 return "DW_OP_dup";
2701 case DW_OP_drop:
2702 return "DW_OP_drop";
2703 case DW_OP_over:
2704 return "DW_OP_over";
2705 case DW_OP_pick:
2706 return "DW_OP_pick";
2707 case DW_OP_swap:
2708 return "DW_OP_swap";
2709 case DW_OP_rot:
2710 return "DW_OP_rot";
2711 case DW_OP_xderef:
2712 return "DW_OP_xderef";
2713 case DW_OP_abs:
2714 return "DW_OP_abs";
2715 case DW_OP_and:
2716 return "DW_OP_and";
2717 case DW_OP_div:
2718 return "DW_OP_div";
2719 case DW_OP_minus:
2720 return "DW_OP_minus";
2721 case DW_OP_mod:
2722 return "DW_OP_mod";
2723 case DW_OP_mul:
2724 return "DW_OP_mul";
2725 case DW_OP_neg:
2726 return "DW_OP_neg";
2727 case DW_OP_not:
2728 return "DW_OP_not";
2729 case DW_OP_or:
2730 return "DW_OP_or";
2731 case DW_OP_plus:
2732 return "DW_OP_plus";
2733 case DW_OP_plus_uconst:
2734 return "DW_OP_plus_uconst";
2735 case DW_OP_shl:
2736 return "DW_OP_shl";
2737 case DW_OP_shr:
2738 return "DW_OP_shr";
2739 case DW_OP_shra:
2740 return "DW_OP_shra";
2741 case DW_OP_xor:
2742 return "DW_OP_xor";
2743 case DW_OP_bra:
2744 return "DW_OP_bra";
2745 case DW_OP_eq:
2746 return "DW_OP_eq";
2747 case DW_OP_ge:
2748 return "DW_OP_ge";
2749 case DW_OP_gt:
2750 return "DW_OP_gt";
2751 case DW_OP_le:
2752 return "DW_OP_le";
2753 case DW_OP_lt:
2754 return "DW_OP_lt";
2755 case DW_OP_ne:
2756 return "DW_OP_ne";
2757 case DW_OP_skip:
2758 return "DW_OP_skip";
2759 case DW_OP_lit0:
2760 return "DW_OP_lit0";
2761 case DW_OP_lit1:
2762 return "DW_OP_lit1";
2763 case DW_OP_lit2:
2764 return "DW_OP_lit2";
2765 case DW_OP_lit3:
2766 return "DW_OP_lit3";
2767 case DW_OP_lit4:
2768 return "DW_OP_lit4";
2769 case DW_OP_lit5:
2770 return "DW_OP_lit5";
2771 case DW_OP_lit6:
2772 return "DW_OP_lit6";
2773 case DW_OP_lit7:
2774 return "DW_OP_lit7";
2775 case DW_OP_lit8:
2776 return "DW_OP_lit8";
2777 case DW_OP_lit9:
2778 return "DW_OP_lit9";
2779 case DW_OP_lit10:
2780 return "DW_OP_lit10";
2781 case DW_OP_lit11:
2782 return "DW_OP_lit11";
2783 case DW_OP_lit12:
2784 return "DW_OP_lit12";
2785 case DW_OP_lit13:
2786 return "DW_OP_lit13";
2787 case DW_OP_lit14:
2788 return "DW_OP_lit14";
2789 case DW_OP_lit15:
2790 return "DW_OP_lit15";
2791 case DW_OP_lit16:
2792 return "DW_OP_lit16";
2793 case DW_OP_lit17:
2794 return "DW_OP_lit17";
2795 case DW_OP_lit18:
2796 return "DW_OP_lit18";
2797 case DW_OP_lit19:
2798 return "DW_OP_lit19";
2799 case DW_OP_lit20:
2800 return "DW_OP_lit20";
2801 case DW_OP_lit21:
2802 return "DW_OP_lit21";
2803 case DW_OP_lit22:
2804 return "DW_OP_lit22";
2805 case DW_OP_lit23:
2806 return "DW_OP_lit23";
2807 case DW_OP_lit24:
2808 return "DW_OP_lit24";
2809 case DW_OP_lit25:
2810 return "DW_OP_lit25";
2811 case DW_OP_lit26:
2812 return "DW_OP_lit26";
2813 case DW_OP_lit27:
2814 return "DW_OP_lit27";
2815 case DW_OP_lit28:
2816 return "DW_OP_lit28";
2817 case DW_OP_lit29:
2818 return "DW_OP_lit29";
2819 case DW_OP_lit30:
2820 return "DW_OP_lit30";
2821 case DW_OP_lit31:
2822 return "DW_OP_lit31";
2823 case DW_OP_reg0:
2824 return "DW_OP_reg0";
2825 case DW_OP_reg1:
2826 return "DW_OP_reg1";
2827 case DW_OP_reg2:
2828 return "DW_OP_reg2";
2829 case DW_OP_reg3:
2830 return "DW_OP_reg3";
2831 case DW_OP_reg4:
2832 return "DW_OP_reg4";
2833 case DW_OP_reg5:
2834 return "DW_OP_reg5";
2835 case DW_OP_reg6:
2836 return "DW_OP_reg6";
2837 case DW_OP_reg7:
2838 return "DW_OP_reg7";
2839 case DW_OP_reg8:
2840 return "DW_OP_reg8";
2841 case DW_OP_reg9:
2842 return "DW_OP_reg9";
2843 case DW_OP_reg10:
2844 return "DW_OP_reg10";
2845 case DW_OP_reg11:
2846 return "DW_OP_reg11";
2847 case DW_OP_reg12:
2848 return "DW_OP_reg12";
2849 case DW_OP_reg13:
2850 return "DW_OP_reg13";
2851 case DW_OP_reg14:
2852 return "DW_OP_reg14";
2853 case DW_OP_reg15:
2854 return "DW_OP_reg15";
2855 case DW_OP_reg16:
2856 return "DW_OP_reg16";
2857 case DW_OP_reg17:
2858 return "DW_OP_reg17";
2859 case DW_OP_reg18:
2860 return "DW_OP_reg18";
2861 case DW_OP_reg19:
2862 return "DW_OP_reg19";
2863 case DW_OP_reg20:
2864 return "DW_OP_reg20";
2865 case DW_OP_reg21:
2866 return "DW_OP_reg21";
2867 case DW_OP_reg22:
2868 return "DW_OP_reg22";
2869 case DW_OP_reg23:
2870 return "DW_OP_reg23";
2871 case DW_OP_reg24:
2872 return "DW_OP_reg24";
2873 case DW_OP_reg25:
2874 return "DW_OP_reg25";
2875 case DW_OP_reg26:
2876 return "DW_OP_reg26";
2877 case DW_OP_reg27:
2878 return "DW_OP_reg27";
2879 case DW_OP_reg28:
2880 return "DW_OP_reg28";
2881 case DW_OP_reg29:
2882 return "DW_OP_reg29";
2883 case DW_OP_reg30:
2884 return "DW_OP_reg30";
2885 case DW_OP_reg31:
2886 return "DW_OP_reg31";
2887 case DW_OP_breg0:
2888 return "DW_OP_breg0";
2889 case DW_OP_breg1:
2890 return "DW_OP_breg1";
2891 case DW_OP_breg2:
2892 return "DW_OP_breg2";
2893 case DW_OP_breg3:
2894 return "DW_OP_breg3";
2895 case DW_OP_breg4:
2896 return "DW_OP_breg4";
2897 case DW_OP_breg5:
2898 return "DW_OP_breg5";
2899 case DW_OP_breg6:
2900 return "DW_OP_breg6";
2901 case DW_OP_breg7:
2902 return "DW_OP_breg7";
2903 case DW_OP_breg8:
2904 return "DW_OP_breg8";
2905 case DW_OP_breg9:
2906 return "DW_OP_breg9";
2907 case DW_OP_breg10:
2908 return "DW_OP_breg10";
2909 case DW_OP_breg11:
2910 return "DW_OP_breg11";
2911 case DW_OP_breg12:
2912 return "DW_OP_breg12";
2913 case DW_OP_breg13:
2914 return "DW_OP_breg13";
2915 case DW_OP_breg14:
2916 return "DW_OP_breg14";
2917 case DW_OP_breg15:
2918 return "DW_OP_breg15";
2919 case DW_OP_breg16:
2920 return "DW_OP_breg16";
2921 case DW_OP_breg17:
2922 return "DW_OP_breg17";
2923 case DW_OP_breg18:
2924 return "DW_OP_breg18";
2925 case DW_OP_breg19:
2926 return "DW_OP_breg19";
2927 case DW_OP_breg20:
2928 return "DW_OP_breg20";
2929 case DW_OP_breg21:
2930 return "DW_OP_breg21";
2931 case DW_OP_breg22:
2932 return "DW_OP_breg22";
2933 case DW_OP_breg23:
2934 return "DW_OP_breg23";
2935 case DW_OP_breg24:
2936 return "DW_OP_breg24";
2937 case DW_OP_breg25:
2938 return "DW_OP_breg25";
2939 case DW_OP_breg26:
2940 return "DW_OP_breg26";
2941 case DW_OP_breg27:
2942 return "DW_OP_breg27";
2943 case DW_OP_breg28:
2944 return "DW_OP_breg28";
2945 case DW_OP_breg29:
2946 return "DW_OP_breg29";
2947 case DW_OP_breg30:
2948 return "DW_OP_breg30";
2949 case DW_OP_breg31:
2950 return "DW_OP_breg31";
2951 case DW_OP_regx:
2952 return "DW_OP_regx";
2953 case DW_OP_fbreg:
2954 return "DW_OP_fbreg";
2955 case DW_OP_bregx:
2956 return "DW_OP_bregx";
2957 case DW_OP_piece:
2958 return "DW_OP_piece";
2959 case DW_OP_deref_size:
2960 return "DW_OP_deref_size";
2961 case DW_OP_xderef_size:
2962 return "DW_OP_xderef_size";
2963 case DW_OP_nop:
2964 return "DW_OP_nop";
2965 case DW_OP_push_object_address:
2966 return "DW_OP_push_object_address";
2967 case DW_OP_call2:
2968 return "DW_OP_call2";
2969 case DW_OP_call4:
2970 return "DW_OP_call4";
2971 case DW_OP_call_ref:
2972 return "DW_OP_call_ref";
2973 case DW_OP_GNU_push_tls_address:
2974 return "DW_OP_GNU_push_tls_address";
2975 default:
2976 return "OP_<unknown>";
2980 /* Return a pointer to a newly allocated location description. Location
2981 descriptions are simple expression terms that can be strung
2982 together to form more complicated location (address) descriptions. */
2984 static inline dw_loc_descr_ref
2985 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
2986 unsigned HOST_WIDE_INT oprnd2)
2988 dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
2990 descr->dw_loc_opc = op;
2991 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
2992 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
2993 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
2994 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
2996 return descr;
3000 /* Add a location description term to a location description expression. */
3002 static inline void
3003 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3005 dw_loc_descr_ref *d;
3007 /* Find the end of the chain. */
3008 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3011 *d = descr;
3014 /* Return the size of a location descriptor. */
3016 static unsigned long
3017 size_of_loc_descr (dw_loc_descr_ref loc)
3019 unsigned long size = 1;
3021 switch (loc->dw_loc_opc)
3023 case DW_OP_addr:
3024 case INTERNAL_DW_OP_tls_addr:
3025 size += DWARF2_ADDR_SIZE;
3026 break;
3027 case DW_OP_const1u:
3028 case DW_OP_const1s:
3029 size += 1;
3030 break;
3031 case DW_OP_const2u:
3032 case DW_OP_const2s:
3033 size += 2;
3034 break;
3035 case DW_OP_const4u:
3036 case DW_OP_const4s:
3037 size += 4;
3038 break;
3039 case DW_OP_const8u:
3040 case DW_OP_const8s:
3041 size += 8;
3042 break;
3043 case DW_OP_constu:
3044 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3045 break;
3046 case DW_OP_consts:
3047 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3048 break;
3049 case DW_OP_pick:
3050 size += 1;
3051 break;
3052 case DW_OP_plus_uconst:
3053 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3054 break;
3055 case DW_OP_skip:
3056 case DW_OP_bra:
3057 size += 2;
3058 break;
3059 case DW_OP_breg0:
3060 case DW_OP_breg1:
3061 case DW_OP_breg2:
3062 case DW_OP_breg3:
3063 case DW_OP_breg4:
3064 case DW_OP_breg5:
3065 case DW_OP_breg6:
3066 case DW_OP_breg7:
3067 case DW_OP_breg8:
3068 case DW_OP_breg9:
3069 case DW_OP_breg10:
3070 case DW_OP_breg11:
3071 case DW_OP_breg12:
3072 case DW_OP_breg13:
3073 case DW_OP_breg14:
3074 case DW_OP_breg15:
3075 case DW_OP_breg16:
3076 case DW_OP_breg17:
3077 case DW_OP_breg18:
3078 case DW_OP_breg19:
3079 case DW_OP_breg20:
3080 case DW_OP_breg21:
3081 case DW_OP_breg22:
3082 case DW_OP_breg23:
3083 case DW_OP_breg24:
3084 case DW_OP_breg25:
3085 case DW_OP_breg26:
3086 case DW_OP_breg27:
3087 case DW_OP_breg28:
3088 case DW_OP_breg29:
3089 case DW_OP_breg30:
3090 case DW_OP_breg31:
3091 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3092 break;
3093 case DW_OP_regx:
3094 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3095 break;
3096 case DW_OP_fbreg:
3097 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3098 break;
3099 case DW_OP_bregx:
3100 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3101 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3102 break;
3103 case DW_OP_piece:
3104 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3105 break;
3106 case DW_OP_deref_size:
3107 case DW_OP_xderef_size:
3108 size += 1;
3109 break;
3110 case DW_OP_call2:
3111 size += 2;
3112 break;
3113 case DW_OP_call4:
3114 size += 4;
3115 break;
3116 case DW_OP_call_ref:
3117 size += DWARF2_ADDR_SIZE;
3118 break;
3119 default:
3120 break;
3123 return size;
3126 /* Return the size of a series of location descriptors. */
3128 static unsigned long
3129 size_of_locs (dw_loc_descr_ref loc)
3131 unsigned long size;
3133 for (size = 0; loc != NULL; loc = loc->dw_loc_next)
3135 loc->dw_loc_addr = size;
3136 size += size_of_loc_descr (loc);
3139 return size;
3142 /* Output location description stack opcode's operands (if any). */
3144 static void
3145 output_loc_operands (dw_loc_descr_ref loc)
3147 dw_val_ref val1 = &loc->dw_loc_oprnd1;
3148 dw_val_ref val2 = &loc->dw_loc_oprnd2;
3150 switch (loc->dw_loc_opc)
3152 #ifdef DWARF2_DEBUGGING_INFO
3153 case DW_OP_addr:
3154 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3155 break;
3156 case DW_OP_const2u:
3157 case DW_OP_const2s:
3158 dw2_asm_output_data (2, val1->v.val_int, NULL);
3159 break;
3160 case DW_OP_const4u:
3161 case DW_OP_const4s:
3162 dw2_asm_output_data (4, val1->v.val_int, NULL);
3163 break;
3164 case DW_OP_const8u:
3165 case DW_OP_const8s:
3166 gcc_assert (HOST_BITS_PER_LONG >= 64);
3167 dw2_asm_output_data (8, val1->v.val_int, NULL);
3168 break;
3169 case DW_OP_skip:
3170 case DW_OP_bra:
3172 int offset;
3174 gcc_assert (val1->val_class == dw_val_class_loc);
3175 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3177 dw2_asm_output_data (2, offset, NULL);
3179 break;
3180 #else
3181 case DW_OP_addr:
3182 case DW_OP_const2u:
3183 case DW_OP_const2s:
3184 case DW_OP_const4u:
3185 case DW_OP_const4s:
3186 case DW_OP_const8u:
3187 case DW_OP_const8s:
3188 case DW_OP_skip:
3189 case DW_OP_bra:
3190 /* We currently don't make any attempt to make sure these are
3191 aligned properly like we do for the main unwind info, so
3192 don't support emitting things larger than a byte if we're
3193 only doing unwinding. */
3194 gcc_unreachable ();
3195 #endif
3196 case DW_OP_const1u:
3197 case DW_OP_const1s:
3198 dw2_asm_output_data (1, val1->v.val_int, NULL);
3199 break;
3200 case DW_OP_constu:
3201 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3202 break;
3203 case DW_OP_consts:
3204 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3205 break;
3206 case DW_OP_pick:
3207 dw2_asm_output_data (1, val1->v.val_int, NULL);
3208 break;
3209 case DW_OP_plus_uconst:
3210 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3211 break;
3212 case DW_OP_breg0:
3213 case DW_OP_breg1:
3214 case DW_OP_breg2:
3215 case DW_OP_breg3:
3216 case DW_OP_breg4:
3217 case DW_OP_breg5:
3218 case DW_OP_breg6:
3219 case DW_OP_breg7:
3220 case DW_OP_breg8:
3221 case DW_OP_breg9:
3222 case DW_OP_breg10:
3223 case DW_OP_breg11:
3224 case DW_OP_breg12:
3225 case DW_OP_breg13:
3226 case DW_OP_breg14:
3227 case DW_OP_breg15:
3228 case DW_OP_breg16:
3229 case DW_OP_breg17:
3230 case DW_OP_breg18:
3231 case DW_OP_breg19:
3232 case DW_OP_breg20:
3233 case DW_OP_breg21:
3234 case DW_OP_breg22:
3235 case DW_OP_breg23:
3236 case DW_OP_breg24:
3237 case DW_OP_breg25:
3238 case DW_OP_breg26:
3239 case DW_OP_breg27:
3240 case DW_OP_breg28:
3241 case DW_OP_breg29:
3242 case DW_OP_breg30:
3243 case DW_OP_breg31:
3244 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3245 break;
3246 case DW_OP_regx:
3247 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3248 break;
3249 case DW_OP_fbreg:
3250 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3251 break;
3252 case DW_OP_bregx:
3253 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3254 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3255 break;
3256 case DW_OP_piece:
3257 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3258 break;
3259 case DW_OP_deref_size:
3260 case DW_OP_xderef_size:
3261 dw2_asm_output_data (1, val1->v.val_int, NULL);
3262 break;
3264 case INTERNAL_DW_OP_tls_addr:
3265 #ifdef ASM_OUTPUT_DWARF_DTPREL
3266 ASM_OUTPUT_DWARF_DTPREL (asm_out_file, DWARF2_ADDR_SIZE,
3267 val1->v.val_addr);
3268 fputc ('\n', asm_out_file);
3269 #else
3270 gcc_unreachable ();
3271 #endif
3272 break;
3274 default:
3275 /* Other codes have no operands. */
3276 break;
3280 /* Output a sequence of location operations. */
3282 static void
3283 output_loc_sequence (dw_loc_descr_ref loc)
3285 for (; loc != NULL; loc = loc->dw_loc_next)
3287 /* Output the opcode. */
3288 dw2_asm_output_data (1, loc->dw_loc_opc,
3289 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3291 /* Output the operand(s) (if any). */
3292 output_loc_operands (loc);
3296 /* This routine will generate the correct assembly data for a location
3297 description based on a cfi entry with a complex address. */
3299 static void
3300 output_cfa_loc (dw_cfi_ref cfi)
3302 dw_loc_descr_ref loc;
3303 unsigned long size;
3305 /* Output the size of the block. */
3306 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3307 size = size_of_locs (loc);
3308 dw2_asm_output_data_uleb128 (size, NULL);
3310 /* Now output the operations themselves. */
3311 output_loc_sequence (loc);
3314 /* This function builds a dwarf location descriptor sequence from
3315 a dw_cfa_location. */
3317 static struct dw_loc_descr_struct *
3318 build_cfa_loc (dw_cfa_location *cfa)
3320 struct dw_loc_descr_struct *head, *tmp;
3322 gcc_assert (cfa->indirect);
3324 if (cfa->base_offset)
3326 if (cfa->reg <= 31)
3327 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3328 else
3329 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3331 else if (cfa->reg <= 31)
3332 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3333 else
3334 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3336 head->dw_loc_oprnd1.val_class = dw_val_class_const;
3337 tmp = new_loc_descr (DW_OP_deref, 0, 0);
3338 add_loc_descr (&head, tmp);
3339 if (cfa->offset != 0)
3341 tmp = new_loc_descr (DW_OP_plus_uconst, cfa->offset, 0);
3342 add_loc_descr (&head, tmp);
3345 return head;
3348 /* This function fills in aa dw_cfa_location structure from a dwarf location
3349 descriptor sequence. */
3351 static void
3352 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3354 struct dw_loc_descr_struct *ptr;
3355 cfa->offset = 0;
3356 cfa->base_offset = 0;
3357 cfa->indirect = 0;
3358 cfa->reg = -1;
3360 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3362 enum dwarf_location_atom op = ptr->dw_loc_opc;
3364 switch (op)
3366 case DW_OP_reg0:
3367 case DW_OP_reg1:
3368 case DW_OP_reg2:
3369 case DW_OP_reg3:
3370 case DW_OP_reg4:
3371 case DW_OP_reg5:
3372 case DW_OP_reg6:
3373 case DW_OP_reg7:
3374 case DW_OP_reg8:
3375 case DW_OP_reg9:
3376 case DW_OP_reg10:
3377 case DW_OP_reg11:
3378 case DW_OP_reg12:
3379 case DW_OP_reg13:
3380 case DW_OP_reg14:
3381 case DW_OP_reg15:
3382 case DW_OP_reg16:
3383 case DW_OP_reg17:
3384 case DW_OP_reg18:
3385 case DW_OP_reg19:
3386 case DW_OP_reg20:
3387 case DW_OP_reg21:
3388 case DW_OP_reg22:
3389 case DW_OP_reg23:
3390 case DW_OP_reg24:
3391 case DW_OP_reg25:
3392 case DW_OP_reg26:
3393 case DW_OP_reg27:
3394 case DW_OP_reg28:
3395 case DW_OP_reg29:
3396 case DW_OP_reg30:
3397 case DW_OP_reg31:
3398 cfa->reg = op - DW_OP_reg0;
3399 break;
3400 case DW_OP_regx:
3401 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3402 break;
3403 case DW_OP_breg0:
3404 case DW_OP_breg1:
3405 case DW_OP_breg2:
3406 case DW_OP_breg3:
3407 case DW_OP_breg4:
3408 case DW_OP_breg5:
3409 case DW_OP_breg6:
3410 case DW_OP_breg7:
3411 case DW_OP_breg8:
3412 case DW_OP_breg9:
3413 case DW_OP_breg10:
3414 case DW_OP_breg11:
3415 case DW_OP_breg12:
3416 case DW_OP_breg13:
3417 case DW_OP_breg14:
3418 case DW_OP_breg15:
3419 case DW_OP_breg16:
3420 case DW_OP_breg17:
3421 case DW_OP_breg18:
3422 case DW_OP_breg19:
3423 case DW_OP_breg20:
3424 case DW_OP_breg21:
3425 case DW_OP_breg22:
3426 case DW_OP_breg23:
3427 case DW_OP_breg24:
3428 case DW_OP_breg25:
3429 case DW_OP_breg26:
3430 case DW_OP_breg27:
3431 case DW_OP_breg28:
3432 case DW_OP_breg29:
3433 case DW_OP_breg30:
3434 case DW_OP_breg31:
3435 cfa->reg = op - DW_OP_breg0;
3436 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3437 break;
3438 case DW_OP_bregx:
3439 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3440 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3441 break;
3442 case DW_OP_deref:
3443 cfa->indirect = 1;
3444 break;
3445 case DW_OP_plus_uconst:
3446 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3447 break;
3448 default:
3449 internal_error ("DW_LOC_OP %s not implemented\n",
3450 dwarf_stack_op_name (ptr->dw_loc_opc));
3454 #endif /* .debug_frame support */
3456 /* And now, the support for symbolic debugging information. */
3457 #ifdef DWARF2_DEBUGGING_INFO
3459 /* .debug_str support. */
3460 static int output_indirect_string (void **, void *);
3462 static void dwarf2out_init (const char *);
3463 static void dwarf2out_finish (const char *);
3464 static void dwarf2out_define (unsigned int, const char *);
3465 static void dwarf2out_undef (unsigned int, const char *);
3466 static void dwarf2out_start_source_file (unsigned, const char *);
3467 static void dwarf2out_end_source_file (unsigned);
3468 static void dwarf2out_begin_block (unsigned, unsigned);
3469 static void dwarf2out_end_block (unsigned, unsigned);
3470 static bool dwarf2out_ignore_block (tree);
3471 static void dwarf2out_global_decl (tree);
3472 static void dwarf2out_type_decl (tree, int);
3473 static void dwarf2out_imported_module_or_decl (tree, tree);
3474 static void dwarf2out_abstract_function (tree);
3475 static void dwarf2out_var_location (rtx);
3476 static void dwarf2out_begin_function (tree);
3477 static void dwarf2out_switch_text_section (void);
3479 /* The debug hooks structure. */
3481 const struct gcc_debug_hooks dwarf2_debug_hooks =
3483 dwarf2out_init,
3484 dwarf2out_finish,
3485 dwarf2out_define,
3486 dwarf2out_undef,
3487 dwarf2out_start_source_file,
3488 dwarf2out_end_source_file,
3489 dwarf2out_begin_block,
3490 dwarf2out_end_block,
3491 dwarf2out_ignore_block,
3492 dwarf2out_source_line,
3493 dwarf2out_begin_prologue,
3494 debug_nothing_int_charstar, /* end_prologue */
3495 dwarf2out_end_epilogue,
3496 dwarf2out_begin_function,
3497 debug_nothing_int, /* end_function */
3498 dwarf2out_decl, /* function_decl */
3499 dwarf2out_global_decl,
3500 dwarf2out_type_decl, /* type_decl */
3501 dwarf2out_imported_module_or_decl,
3502 debug_nothing_tree, /* deferred_inline_function */
3503 /* The DWARF 2 backend tries to reduce debugging bloat by not
3504 emitting the abstract description of inline functions until
3505 something tries to reference them. */
3506 dwarf2out_abstract_function, /* outlining_inline_function */
3507 debug_nothing_rtx, /* label */
3508 debug_nothing_int, /* handle_pch */
3509 dwarf2out_var_location,
3510 dwarf2out_switch_text_section,
3511 1 /* start_end_main_source_file */
3513 #endif
3515 /* NOTE: In the comments in this file, many references are made to
3516 "Debugging Information Entries". This term is abbreviated as `DIE'
3517 throughout the remainder of this file. */
3519 /* An internal representation of the DWARF output is built, and then
3520 walked to generate the DWARF debugging info. The walk of the internal
3521 representation is done after the entire program has been compiled.
3522 The types below are used to describe the internal representation. */
3524 /* Various DIE's use offsets relative to the beginning of the
3525 .debug_info section to refer to each other. */
3527 typedef long int dw_offset;
3529 /* Define typedefs here to avoid circular dependencies. */
3531 typedef struct dw_attr_struct *dw_attr_ref;
3532 typedef struct dw_line_info_struct *dw_line_info_ref;
3533 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3534 typedef struct pubname_struct *pubname_ref;
3535 typedef struct dw_ranges_struct *dw_ranges_ref;
3537 /* Each entry in the line_info_table maintains the file and
3538 line number associated with the label generated for that
3539 entry. The label gives the PC value associated with
3540 the line number entry. */
3542 typedef struct dw_line_info_struct GTY(())
3544 unsigned long dw_file_num;
3545 unsigned long dw_line_num;
3547 dw_line_info_entry;
3549 /* Line information for functions in separate sections; each one gets its
3550 own sequence. */
3551 typedef struct dw_separate_line_info_struct GTY(())
3553 unsigned long dw_file_num;
3554 unsigned long dw_line_num;
3555 unsigned long function;
3557 dw_separate_line_info_entry;
3559 /* Each DIE attribute has a field specifying the attribute kind,
3560 a link to the next attribute in the chain, and an attribute value.
3561 Attributes are typically linked below the DIE they modify. */
3563 typedef struct dw_attr_struct GTY(())
3565 enum dwarf_attribute dw_attr;
3566 dw_attr_ref dw_attr_next;
3567 dw_val_node dw_attr_val;
3569 dw_attr_node;
3571 /* The Debugging Information Entry (DIE) structure */
3573 typedef struct die_struct GTY(())
3575 enum dwarf_tag die_tag;
3576 char *die_symbol;
3577 dw_attr_ref die_attr;
3578 dw_die_ref die_parent;
3579 dw_die_ref die_child;
3580 dw_die_ref die_sib;
3581 dw_die_ref die_definition; /* ref from a specification to its definition */
3582 dw_offset die_offset;
3583 unsigned long die_abbrev;
3584 int die_mark;
3585 unsigned int decl_id;
3587 die_node;
3589 /* The pubname structure */
3591 typedef struct pubname_struct GTY(())
3593 dw_die_ref die;
3594 char *name;
3596 pubname_entry;
3598 struct dw_ranges_struct GTY(())
3600 int block_num;
3603 /* The limbo die list structure. */
3604 typedef struct limbo_die_struct GTY(())
3606 dw_die_ref die;
3607 tree created_for;
3608 struct limbo_die_struct *next;
3610 limbo_die_node;
3612 /* How to start an assembler comment. */
3613 #ifndef ASM_COMMENT_START
3614 #define ASM_COMMENT_START ";#"
3615 #endif
3617 /* Define a macro which returns nonzero for a TYPE_DECL which was
3618 implicitly generated for a tagged type.
3620 Note that unlike the gcc front end (which generates a NULL named
3621 TYPE_DECL node for each complete tagged type, each array type, and
3622 each function type node created) the g++ front end generates a
3623 _named_ TYPE_DECL node for each tagged type node created.
3624 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3625 generate a DW_TAG_typedef DIE for them. */
3627 #define TYPE_DECL_IS_STUB(decl) \
3628 (DECL_NAME (decl) == NULL_TREE \
3629 || (DECL_ARTIFICIAL (decl) \
3630 && is_tagged_type (TREE_TYPE (decl)) \
3631 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3632 /* This is necessary for stub decls that \
3633 appear in nested inline functions. */ \
3634 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3635 && (decl_ultimate_origin (decl) \
3636 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3638 /* Information concerning the compilation unit's programming
3639 language, and compiler version. */
3641 /* Fixed size portion of the DWARF compilation unit header. */
3642 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3643 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3645 /* Fixed size portion of public names info. */
3646 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3648 /* Fixed size portion of the address range info. */
3649 #define DWARF_ARANGES_HEADER_SIZE \
3650 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3651 DWARF2_ADDR_SIZE * 2) \
3652 - DWARF_INITIAL_LENGTH_SIZE)
3654 /* Size of padding portion in the address range info. It must be
3655 aligned to twice the pointer size. */
3656 #define DWARF_ARANGES_PAD_SIZE \
3657 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3658 DWARF2_ADDR_SIZE * 2) \
3659 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3661 /* Use assembler line directives if available. */
3662 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3663 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3664 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3665 #else
3666 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3667 #endif
3668 #endif
3670 /* Minimum line offset in a special line info. opcode.
3671 This value was chosen to give a reasonable range of values. */
3672 #define DWARF_LINE_BASE -10
3674 /* First special line opcode - leave room for the standard opcodes. */
3675 #define DWARF_LINE_OPCODE_BASE 10
3677 /* Range of line offsets in a special line info. opcode. */
3678 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3680 /* Flag that indicates the initial value of the is_stmt_start flag.
3681 In the present implementation, we do not mark any lines as
3682 the beginning of a source statement, because that information
3683 is not made available by the GCC front-end. */
3684 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3686 #ifdef DWARF2_DEBUGGING_INFO
3687 /* This location is used by calc_die_sizes() to keep track
3688 the offset of each DIE within the .debug_info section. */
3689 static unsigned long next_die_offset;
3690 #endif
3692 /* Record the root of the DIE's built for the current compilation unit. */
3693 static GTY(()) dw_die_ref comp_unit_die;
3695 /* A list of DIEs with a NULL parent waiting to be relocated. */
3696 static GTY(()) limbo_die_node *limbo_die_list;
3698 /* Filenames referenced by this compilation unit. */
3699 static GTY(()) varray_type file_table;
3700 static GTY(()) varray_type file_table_emitted;
3701 static GTY(()) size_t file_table_last_lookup_index;
3703 /* A hash table of references to DIE's that describe declarations.
3704 The key is a DECL_UID() which is a unique number identifying each decl. */
3705 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3707 /* Node of the variable location list. */
3708 struct var_loc_node GTY ((chain_next ("%h.next")))
3710 rtx GTY (()) var_loc_note;
3711 const char * GTY (()) label;
3712 const char * GTY (()) section_label;
3713 struct var_loc_node * GTY (()) next;
3716 /* Variable location list. */
3717 struct var_loc_list_def GTY (())
3719 struct var_loc_node * GTY (()) first;
3721 /* Do not mark the last element of the chained list because
3722 it is marked through the chain. */
3723 struct var_loc_node * GTY ((skip ("%h"))) last;
3725 /* DECL_UID of the variable decl. */
3726 unsigned int decl_id;
3728 typedef struct var_loc_list_def var_loc_list;
3731 /* Table of decl location linked lists. */
3732 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3734 /* A pointer to the base of a list of references to DIE's that
3735 are uniquely identified by their tag, presence/absence of
3736 children DIE's, and list of attribute/value pairs. */
3737 static GTY((length ("abbrev_die_table_allocated")))
3738 dw_die_ref *abbrev_die_table;
3740 /* Number of elements currently allocated for abbrev_die_table. */
3741 static GTY(()) unsigned abbrev_die_table_allocated;
3743 /* Number of elements in type_die_table currently in use. */
3744 static GTY(()) unsigned abbrev_die_table_in_use;
3746 /* Size (in elements) of increments by which we may expand the
3747 abbrev_die_table. */
3748 #define ABBREV_DIE_TABLE_INCREMENT 256
3750 /* A pointer to the base of a table that contains line information
3751 for each source code line in .text in the compilation unit. */
3752 static GTY((length ("line_info_table_allocated")))
3753 dw_line_info_ref line_info_table;
3755 /* Number of elements currently allocated for line_info_table. */
3756 static GTY(()) unsigned line_info_table_allocated;
3758 /* Number of elements in line_info_table currently in use. */
3759 static GTY(()) unsigned line_info_table_in_use;
3761 /* A pointer to the base of a table that contains line information
3762 for each source code line outside of .text in the compilation unit. */
3763 static GTY ((length ("separate_line_info_table_allocated")))
3764 dw_separate_line_info_ref separate_line_info_table;
3766 /* Number of elements currently allocated for separate_line_info_table. */
3767 static GTY(()) unsigned separate_line_info_table_allocated;
3769 /* Number of elements in separate_line_info_table currently in use. */
3770 static GTY(()) unsigned separate_line_info_table_in_use;
3772 /* Size (in elements) of increments by which we may expand the
3773 line_info_table. */
3774 #define LINE_INFO_TABLE_INCREMENT 1024
3776 /* A pointer to the base of a table that contains a list of publicly
3777 accessible names. */
3778 static GTY ((length ("pubname_table_allocated"))) pubname_ref pubname_table;
3780 /* Number of elements currently allocated for pubname_table. */
3781 static GTY(()) unsigned pubname_table_allocated;
3783 /* Number of elements in pubname_table currently in use. */
3784 static GTY(()) unsigned pubname_table_in_use;
3786 /* Size (in elements) of increments by which we may expand the
3787 pubname_table. */
3788 #define PUBNAME_TABLE_INCREMENT 64
3790 /* Array of dies for which we should generate .debug_arange info. */
3791 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3793 /* Number of elements currently allocated for arange_table. */
3794 static GTY(()) unsigned arange_table_allocated;
3796 /* Number of elements in arange_table currently in use. */
3797 static GTY(()) unsigned arange_table_in_use;
3799 /* Size (in elements) of increments by which we may expand the
3800 arange_table. */
3801 #define ARANGE_TABLE_INCREMENT 64
3803 /* Array of dies for which we should generate .debug_ranges info. */
3804 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3806 /* Number of elements currently allocated for ranges_table. */
3807 static GTY(()) unsigned ranges_table_allocated;
3809 /* Number of elements in ranges_table currently in use. */
3810 static GTY(()) unsigned ranges_table_in_use;
3812 /* Size (in elements) of increments by which we may expand the
3813 ranges_table. */
3814 #define RANGES_TABLE_INCREMENT 64
3816 /* Whether we have location lists that need outputting */
3817 static GTY(()) unsigned have_location_lists;
3819 /* Unique label counter. */
3820 static GTY(()) unsigned int loclabel_num;
3822 #ifdef DWARF2_DEBUGGING_INFO
3823 /* Record whether the function being analyzed contains inlined functions. */
3824 static int current_function_has_inlines;
3825 #endif
3826 #if 0 && defined (MIPS_DEBUGGING_INFO)
3827 static int comp_unit_has_inlines;
3828 #endif
3830 /* Number of file tables emitted in maybe_emit_file(). */
3831 static GTY(()) int emitcount = 0;
3833 /* Number of internal labels generated by gen_internal_sym(). */
3834 static GTY(()) int label_num;
3836 #ifdef DWARF2_DEBUGGING_INFO
3838 /* Forward declarations for functions defined in this file. */
3840 static int is_pseudo_reg (rtx);
3841 static tree type_main_variant (tree);
3842 static int is_tagged_type (tree);
3843 static const char *dwarf_tag_name (unsigned);
3844 static const char *dwarf_attr_name (unsigned);
3845 static const char *dwarf_form_name (unsigned);
3846 static tree decl_ultimate_origin (tree);
3847 static tree block_ultimate_origin (tree);
3848 static tree decl_class_context (tree);
3849 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3850 static inline enum dw_val_class AT_class (dw_attr_ref);
3851 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3852 static inline unsigned AT_flag (dw_attr_ref);
3853 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3854 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3855 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3856 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3857 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
3858 unsigned long);
3859 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3860 unsigned int, unsigned char *);
3861 static hashval_t debug_str_do_hash (const void *);
3862 static int debug_str_eq (const void *, const void *);
3863 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3864 static inline const char *AT_string (dw_attr_ref);
3865 static int AT_string_form (dw_attr_ref);
3866 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3867 static void add_AT_specification (dw_die_ref, dw_die_ref);
3868 static inline dw_die_ref AT_ref (dw_attr_ref);
3869 static inline int AT_ref_external (dw_attr_ref);
3870 static inline void set_AT_ref_external (dw_attr_ref, int);
3871 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3872 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3873 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3874 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3875 dw_loc_list_ref);
3876 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3877 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3878 static inline rtx AT_addr (dw_attr_ref);
3879 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3880 static void add_AT_lbl_offset (dw_die_ref, enum dwarf_attribute, const char *);
3881 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3882 unsigned HOST_WIDE_INT);
3883 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3884 unsigned long);
3885 static inline const char *AT_lbl (dw_attr_ref);
3886 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3887 static const char *get_AT_low_pc (dw_die_ref);
3888 static const char *get_AT_hi_pc (dw_die_ref);
3889 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3890 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3891 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3892 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3893 static bool is_c_family (void);
3894 static bool is_cxx (void);
3895 static bool is_java (void);
3896 static bool is_fortran (void);
3897 static bool is_ada (void);
3898 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3899 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3900 static inline void free_die (dw_die_ref);
3901 static void remove_children (dw_die_ref);
3902 static void add_child_die (dw_die_ref, dw_die_ref);
3903 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3904 static dw_die_ref lookup_type_die (tree);
3905 static void equate_type_number_to_die (tree, dw_die_ref);
3906 static hashval_t decl_die_table_hash (const void *);
3907 static int decl_die_table_eq (const void *, const void *);
3908 static dw_die_ref lookup_decl_die (tree);
3909 static hashval_t decl_loc_table_hash (const void *);
3910 static int decl_loc_table_eq (const void *, const void *);
3911 static var_loc_list *lookup_decl_loc (tree);
3912 static void equate_decl_number_to_die (tree, dw_die_ref);
3913 static void add_var_loc_to_decl (tree, struct var_loc_node *);
3914 static void print_spaces (FILE *);
3915 static void print_die (dw_die_ref, FILE *);
3916 static void print_dwarf_line_table (FILE *);
3917 static void reverse_die_lists (dw_die_ref);
3918 static void reverse_all_dies (dw_die_ref);
3919 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3920 static dw_die_ref pop_compile_unit (dw_die_ref);
3921 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3922 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3923 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3924 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3925 static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
3926 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3927 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3928 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3929 static void compute_section_prefix (dw_die_ref);
3930 static int is_type_die (dw_die_ref);
3931 static int is_comdat_die (dw_die_ref);
3932 static int is_symbol_die (dw_die_ref);
3933 static void assign_symbol_names (dw_die_ref);
3934 static void break_out_includes (dw_die_ref);
3935 static hashval_t htab_cu_hash (const void *);
3936 static int htab_cu_eq (const void *, const void *);
3937 static void htab_cu_del (void *);
3938 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3939 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3940 static void add_sibling_attributes (dw_die_ref);
3941 static void build_abbrev_table (dw_die_ref);
3942 static void output_location_lists (dw_die_ref);
3943 static int constant_size (long unsigned);
3944 static unsigned long size_of_die (dw_die_ref);
3945 static void calc_die_sizes (dw_die_ref);
3946 static void mark_dies (dw_die_ref);
3947 static void unmark_dies (dw_die_ref);
3948 static void unmark_all_dies (dw_die_ref);
3949 static unsigned long size_of_pubnames (void);
3950 static unsigned long size_of_aranges (void);
3951 static enum dwarf_form value_format (dw_attr_ref);
3952 static void output_value_format (dw_attr_ref);
3953 static void output_abbrev_section (void);
3954 static void output_die_symbol (dw_die_ref);
3955 static void output_die (dw_die_ref);
3956 static void output_compilation_unit_header (void);
3957 static void output_comp_unit (dw_die_ref, int);
3958 static const char *dwarf2_name (tree, int);
3959 static void add_pubname (tree, dw_die_ref);
3960 static void output_pubnames (void);
3961 static void add_arange (tree, dw_die_ref);
3962 static void output_aranges (void);
3963 static unsigned int add_ranges (tree);
3964 static void output_ranges (void);
3965 static void output_line_info (void);
3966 static void output_file_names (void);
3967 static dw_die_ref base_type_die (tree);
3968 static tree root_type (tree);
3969 static int is_base_type (tree);
3970 static bool is_subrange_type (tree);
3971 static dw_die_ref subrange_type_die (tree, dw_die_ref);
3972 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3973 static int type_is_enum (tree);
3974 static unsigned int dbx_reg_number (rtx);
3975 static dw_loc_descr_ref reg_loc_descriptor (rtx);
3976 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
3977 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
3978 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3979 static dw_loc_descr_ref based_loc_descr (unsigned, HOST_WIDE_INT, bool);
3980 static int is_based_loc (rtx);
3981 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode, bool);
3982 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
3983 static dw_loc_descr_ref loc_descriptor (rtx, bool);
3984 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
3985 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
3986 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3987 static tree field_type (tree);
3988 static unsigned int simple_type_align_in_bits (tree);
3989 static unsigned int simple_decl_align_in_bits (tree);
3990 static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
3991 static HOST_WIDE_INT field_byte_offset (tree);
3992 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3993 dw_loc_descr_ref);
3994 static void add_data_member_location_attribute (dw_die_ref, tree);
3995 static void add_const_value_attribute (dw_die_ref, rtx);
3996 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3997 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
3998 static void insert_float (rtx, unsigned char *);
3999 static rtx rtl_for_decl_location (tree);
4000 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4001 enum dwarf_attribute);
4002 static void tree_add_const_value_attribute (dw_die_ref, tree);
4003 static void add_name_attribute (dw_die_ref, const char *);
4004 static void add_comp_dir_attribute (dw_die_ref);
4005 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4006 static void add_subscript_info (dw_die_ref, tree);
4007 static void add_byte_size_attribute (dw_die_ref, tree);
4008 static void add_bit_offset_attribute (dw_die_ref, tree);
4009 static void add_bit_size_attribute (dw_die_ref, tree);
4010 static void add_prototyped_attribute (dw_die_ref, tree);
4011 static void add_abstract_origin_attribute (dw_die_ref, tree);
4012 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4013 static void add_src_coords_attributes (dw_die_ref, tree);
4014 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4015 static void push_decl_scope (tree);
4016 static void pop_decl_scope (void);
4017 static dw_die_ref scope_die_for (tree, dw_die_ref);
4018 static inline int local_scope_p (dw_die_ref);
4019 static inline int class_or_namespace_scope_p (dw_die_ref);
4020 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4021 static void add_calling_convention_attribute (dw_die_ref, tree);
4022 static const char *type_tag (tree);
4023 static tree member_declared_type (tree);
4024 #if 0
4025 static const char *decl_start_label (tree);
4026 #endif
4027 static void gen_array_type_die (tree, dw_die_ref);
4028 #if 0
4029 static void gen_entry_point_die (tree, dw_die_ref);
4030 #endif
4031 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4032 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4033 static void gen_inlined_union_type_die (tree, dw_die_ref);
4034 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4035 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4036 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4037 static void gen_formal_types_die (tree, dw_die_ref);
4038 static void gen_subprogram_die (tree, dw_die_ref);
4039 static void gen_variable_die (tree, dw_die_ref);
4040 static void gen_label_die (tree, dw_die_ref);
4041 static void gen_lexical_block_die (tree, dw_die_ref, int);
4042 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4043 static void gen_field_die (tree, dw_die_ref);
4044 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4045 static dw_die_ref gen_compile_unit_die (const char *);
4046 static void gen_string_type_die (tree, dw_die_ref);
4047 static void gen_inheritance_die (tree, tree, dw_die_ref);
4048 static void gen_member_die (tree, dw_die_ref);
4049 static void gen_struct_or_union_type_die (tree, dw_die_ref);
4050 static void gen_subroutine_type_die (tree, dw_die_ref);
4051 static void gen_typedef_die (tree, dw_die_ref);
4052 static void gen_type_die (tree, dw_die_ref);
4053 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4054 static void gen_block_die (tree, dw_die_ref, int);
4055 static void decls_for_scope (tree, dw_die_ref, int);
4056 static int is_redundant_typedef (tree);
4057 static void gen_namespace_die (tree);
4058 static void gen_decl_die (tree, dw_die_ref);
4059 static dw_die_ref force_decl_die (tree);
4060 static dw_die_ref force_type_die (tree);
4061 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4062 static void declare_in_namespace (tree, dw_die_ref);
4063 static unsigned lookup_filename (const char *);
4064 static void init_file_table (void);
4065 static void retry_incomplete_types (void);
4066 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4067 static void splice_child_die (dw_die_ref, dw_die_ref);
4068 static int file_info_cmp (const void *, const void *);
4069 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4070 const char *, const char *, unsigned);
4071 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4072 const char *, const char *,
4073 const char *);
4074 static void output_loc_list (dw_loc_list_ref);
4075 static char *gen_internal_sym (const char *);
4077 static void prune_unmark_dies (dw_die_ref);
4078 static void prune_unused_types_mark (dw_die_ref, int);
4079 static void prune_unused_types_walk (dw_die_ref);
4080 static void prune_unused_types_walk_attribs (dw_die_ref);
4081 static void prune_unused_types_prune (dw_die_ref);
4082 static void prune_unused_types (void);
4083 static int maybe_emit_file (int);
4085 /* Section names used to hold DWARF debugging information. */
4086 #ifndef DEBUG_INFO_SECTION
4087 #define DEBUG_INFO_SECTION ".debug_info"
4088 #endif
4089 #ifndef DEBUG_ABBREV_SECTION
4090 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4091 #endif
4092 #ifndef DEBUG_ARANGES_SECTION
4093 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4094 #endif
4095 #ifndef DEBUG_MACINFO_SECTION
4096 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4097 #endif
4098 #ifndef DEBUG_LINE_SECTION
4099 #define DEBUG_LINE_SECTION ".debug_line"
4100 #endif
4101 #ifndef DEBUG_LOC_SECTION
4102 #define DEBUG_LOC_SECTION ".debug_loc"
4103 #endif
4104 #ifndef DEBUG_PUBNAMES_SECTION
4105 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
4106 #endif
4107 #ifndef DEBUG_STR_SECTION
4108 #define DEBUG_STR_SECTION ".debug_str"
4109 #endif
4110 #ifndef DEBUG_RANGES_SECTION
4111 #define DEBUG_RANGES_SECTION ".debug_ranges"
4112 #endif
4114 /* Standard ELF section names for compiled code and data. */
4115 #ifndef TEXT_SECTION_NAME
4116 #define TEXT_SECTION_NAME ".text"
4117 #endif
4119 /* Section flags for .debug_str section. */
4120 #define DEBUG_STR_SECTION_FLAGS \
4121 (HAVE_GAS_SHF_MERGE && flag_merge_constants \
4122 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4123 : SECTION_DEBUG)
4125 /* Labels we insert at beginning sections we can reference instead of
4126 the section names themselves. */
4128 #ifndef TEXT_SECTION_LABEL
4129 #define TEXT_SECTION_LABEL "Ltext"
4130 #endif
4131 #ifndef COLD_TEXT_SECTION_LABEL
4132 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4133 #endif
4134 #ifndef DEBUG_LINE_SECTION_LABEL
4135 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4136 #endif
4137 #ifndef DEBUG_INFO_SECTION_LABEL
4138 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4139 #endif
4140 #ifndef DEBUG_ABBREV_SECTION_LABEL
4141 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4142 #endif
4143 #ifndef DEBUG_LOC_SECTION_LABEL
4144 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4145 #endif
4146 #ifndef DEBUG_RANGES_SECTION_LABEL
4147 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4148 #endif
4149 #ifndef DEBUG_MACINFO_SECTION_LABEL
4150 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4151 #endif
4153 /* Definitions of defaults for formats and names of various special
4154 (artificial) labels which may be generated within this file (when the -g
4155 options is used and DWARF2_DEBUGGING_INFO is in effect.
4156 If necessary, these may be overridden from within the tm.h file, but
4157 typically, overriding these defaults is unnecessary. */
4159 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4160 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4161 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4162 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4163 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4164 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4165 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4166 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4167 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4168 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4170 #ifndef TEXT_END_LABEL
4171 #define TEXT_END_LABEL "Letext"
4172 #endif
4173 #ifndef COLD_END_LABEL
4174 #define COLD_END_LABEL "Letext_cold"
4175 #endif
4176 #ifndef BLOCK_BEGIN_LABEL
4177 #define BLOCK_BEGIN_LABEL "LBB"
4178 #endif
4179 #ifndef BLOCK_END_LABEL
4180 #define BLOCK_END_LABEL "LBE"
4181 #endif
4182 #ifndef LINE_CODE_LABEL
4183 #define LINE_CODE_LABEL "LM"
4184 #endif
4185 #ifndef SEPARATE_LINE_CODE_LABEL
4186 #define SEPARATE_LINE_CODE_LABEL "LSM"
4187 #endif
4189 /* We allow a language front-end to designate a function that is to be
4190 called to "demangle" any name before it is put into a DIE. */
4192 static const char *(*demangle_name_func) (const char *);
4194 void
4195 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4197 demangle_name_func = func;
4200 /* Test if rtl node points to a pseudo register. */
4202 static inline int
4203 is_pseudo_reg (rtx rtl)
4205 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4206 || (GET_CODE (rtl) == SUBREG
4207 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4210 /* Return a reference to a type, with its const and volatile qualifiers
4211 removed. */
4213 static inline tree
4214 type_main_variant (tree type)
4216 type = TYPE_MAIN_VARIANT (type);
4218 /* ??? There really should be only one main variant among any group of
4219 variants of a given type (and all of the MAIN_VARIANT values for all
4220 members of the group should point to that one type) but sometimes the C
4221 front-end messes this up for array types, so we work around that bug
4222 here. */
4223 if (TREE_CODE (type) == ARRAY_TYPE)
4224 while (type != TYPE_MAIN_VARIANT (type))
4225 type = TYPE_MAIN_VARIANT (type);
4227 return type;
4230 /* Return nonzero if the given type node represents a tagged type. */
4232 static inline int
4233 is_tagged_type (tree type)
4235 enum tree_code code = TREE_CODE (type);
4237 return (code == RECORD_TYPE || code == UNION_TYPE
4238 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4241 /* Convert a DIE tag into its string name. */
4243 static const char *
4244 dwarf_tag_name (unsigned int tag)
4246 switch (tag)
4248 case DW_TAG_padding:
4249 return "DW_TAG_padding";
4250 case DW_TAG_array_type:
4251 return "DW_TAG_array_type";
4252 case DW_TAG_class_type:
4253 return "DW_TAG_class_type";
4254 case DW_TAG_entry_point:
4255 return "DW_TAG_entry_point";
4256 case DW_TAG_enumeration_type:
4257 return "DW_TAG_enumeration_type";
4258 case DW_TAG_formal_parameter:
4259 return "DW_TAG_formal_parameter";
4260 case DW_TAG_imported_declaration:
4261 return "DW_TAG_imported_declaration";
4262 case DW_TAG_label:
4263 return "DW_TAG_label";
4264 case DW_TAG_lexical_block:
4265 return "DW_TAG_lexical_block";
4266 case DW_TAG_member:
4267 return "DW_TAG_member";
4268 case DW_TAG_pointer_type:
4269 return "DW_TAG_pointer_type";
4270 case DW_TAG_reference_type:
4271 return "DW_TAG_reference_type";
4272 case DW_TAG_compile_unit:
4273 return "DW_TAG_compile_unit";
4274 case DW_TAG_string_type:
4275 return "DW_TAG_string_type";
4276 case DW_TAG_structure_type:
4277 return "DW_TAG_structure_type";
4278 case DW_TAG_subroutine_type:
4279 return "DW_TAG_subroutine_type";
4280 case DW_TAG_typedef:
4281 return "DW_TAG_typedef";
4282 case DW_TAG_union_type:
4283 return "DW_TAG_union_type";
4284 case DW_TAG_unspecified_parameters:
4285 return "DW_TAG_unspecified_parameters";
4286 case DW_TAG_variant:
4287 return "DW_TAG_variant";
4288 case DW_TAG_common_block:
4289 return "DW_TAG_common_block";
4290 case DW_TAG_common_inclusion:
4291 return "DW_TAG_common_inclusion";
4292 case DW_TAG_inheritance:
4293 return "DW_TAG_inheritance";
4294 case DW_TAG_inlined_subroutine:
4295 return "DW_TAG_inlined_subroutine";
4296 case DW_TAG_module:
4297 return "DW_TAG_module";
4298 case DW_TAG_ptr_to_member_type:
4299 return "DW_TAG_ptr_to_member_type";
4300 case DW_TAG_set_type:
4301 return "DW_TAG_set_type";
4302 case DW_TAG_subrange_type:
4303 return "DW_TAG_subrange_type";
4304 case DW_TAG_with_stmt:
4305 return "DW_TAG_with_stmt";
4306 case DW_TAG_access_declaration:
4307 return "DW_TAG_access_declaration";
4308 case DW_TAG_base_type:
4309 return "DW_TAG_base_type";
4310 case DW_TAG_catch_block:
4311 return "DW_TAG_catch_block";
4312 case DW_TAG_const_type:
4313 return "DW_TAG_const_type";
4314 case DW_TAG_constant:
4315 return "DW_TAG_constant";
4316 case DW_TAG_enumerator:
4317 return "DW_TAG_enumerator";
4318 case DW_TAG_file_type:
4319 return "DW_TAG_file_type";
4320 case DW_TAG_friend:
4321 return "DW_TAG_friend";
4322 case DW_TAG_namelist:
4323 return "DW_TAG_namelist";
4324 case DW_TAG_namelist_item:
4325 return "DW_TAG_namelist_item";
4326 case DW_TAG_namespace:
4327 return "DW_TAG_namespace";
4328 case DW_TAG_packed_type:
4329 return "DW_TAG_packed_type";
4330 case DW_TAG_subprogram:
4331 return "DW_TAG_subprogram";
4332 case DW_TAG_template_type_param:
4333 return "DW_TAG_template_type_param";
4334 case DW_TAG_template_value_param:
4335 return "DW_TAG_template_value_param";
4336 case DW_TAG_thrown_type:
4337 return "DW_TAG_thrown_type";
4338 case DW_TAG_try_block:
4339 return "DW_TAG_try_block";
4340 case DW_TAG_variant_part:
4341 return "DW_TAG_variant_part";
4342 case DW_TAG_variable:
4343 return "DW_TAG_variable";
4344 case DW_TAG_volatile_type:
4345 return "DW_TAG_volatile_type";
4346 case DW_TAG_imported_module:
4347 return "DW_TAG_imported_module";
4348 case DW_TAG_MIPS_loop:
4349 return "DW_TAG_MIPS_loop";
4350 case DW_TAG_format_label:
4351 return "DW_TAG_format_label";
4352 case DW_TAG_function_template:
4353 return "DW_TAG_function_template";
4354 case DW_TAG_class_template:
4355 return "DW_TAG_class_template";
4356 case DW_TAG_GNU_BINCL:
4357 return "DW_TAG_GNU_BINCL";
4358 case DW_TAG_GNU_EINCL:
4359 return "DW_TAG_GNU_EINCL";
4360 default:
4361 return "DW_TAG_<unknown>";
4365 /* Convert a DWARF attribute code into its string name. */
4367 static const char *
4368 dwarf_attr_name (unsigned int attr)
4370 switch (attr)
4372 case DW_AT_sibling:
4373 return "DW_AT_sibling";
4374 case DW_AT_location:
4375 return "DW_AT_location";
4376 case DW_AT_name:
4377 return "DW_AT_name";
4378 case DW_AT_ordering:
4379 return "DW_AT_ordering";
4380 case DW_AT_subscr_data:
4381 return "DW_AT_subscr_data";
4382 case DW_AT_byte_size:
4383 return "DW_AT_byte_size";
4384 case DW_AT_bit_offset:
4385 return "DW_AT_bit_offset";
4386 case DW_AT_bit_size:
4387 return "DW_AT_bit_size";
4388 case DW_AT_element_list:
4389 return "DW_AT_element_list";
4390 case DW_AT_stmt_list:
4391 return "DW_AT_stmt_list";
4392 case DW_AT_low_pc:
4393 return "DW_AT_low_pc";
4394 case DW_AT_high_pc:
4395 return "DW_AT_high_pc";
4396 case DW_AT_language:
4397 return "DW_AT_language";
4398 case DW_AT_member:
4399 return "DW_AT_member";
4400 case DW_AT_discr:
4401 return "DW_AT_discr";
4402 case DW_AT_discr_value:
4403 return "DW_AT_discr_value";
4404 case DW_AT_visibility:
4405 return "DW_AT_visibility";
4406 case DW_AT_import:
4407 return "DW_AT_import";
4408 case DW_AT_string_length:
4409 return "DW_AT_string_length";
4410 case DW_AT_common_reference:
4411 return "DW_AT_common_reference";
4412 case DW_AT_comp_dir:
4413 return "DW_AT_comp_dir";
4414 case DW_AT_const_value:
4415 return "DW_AT_const_value";
4416 case DW_AT_containing_type:
4417 return "DW_AT_containing_type";
4418 case DW_AT_default_value:
4419 return "DW_AT_default_value";
4420 case DW_AT_inline:
4421 return "DW_AT_inline";
4422 case DW_AT_is_optional:
4423 return "DW_AT_is_optional";
4424 case DW_AT_lower_bound:
4425 return "DW_AT_lower_bound";
4426 case DW_AT_producer:
4427 return "DW_AT_producer";
4428 case DW_AT_prototyped:
4429 return "DW_AT_prototyped";
4430 case DW_AT_return_addr:
4431 return "DW_AT_return_addr";
4432 case DW_AT_start_scope:
4433 return "DW_AT_start_scope";
4434 case DW_AT_stride_size:
4435 return "DW_AT_stride_size";
4436 case DW_AT_upper_bound:
4437 return "DW_AT_upper_bound";
4438 case DW_AT_abstract_origin:
4439 return "DW_AT_abstract_origin";
4440 case DW_AT_accessibility:
4441 return "DW_AT_accessibility";
4442 case DW_AT_address_class:
4443 return "DW_AT_address_class";
4444 case DW_AT_artificial:
4445 return "DW_AT_artificial";
4446 case DW_AT_base_types:
4447 return "DW_AT_base_types";
4448 case DW_AT_calling_convention:
4449 return "DW_AT_calling_convention";
4450 case DW_AT_count:
4451 return "DW_AT_count";
4452 case DW_AT_data_member_location:
4453 return "DW_AT_data_member_location";
4454 case DW_AT_decl_column:
4455 return "DW_AT_decl_column";
4456 case DW_AT_decl_file:
4457 return "DW_AT_decl_file";
4458 case DW_AT_decl_line:
4459 return "DW_AT_decl_line";
4460 case DW_AT_declaration:
4461 return "DW_AT_declaration";
4462 case DW_AT_discr_list:
4463 return "DW_AT_discr_list";
4464 case DW_AT_encoding:
4465 return "DW_AT_encoding";
4466 case DW_AT_external:
4467 return "DW_AT_external";
4468 case DW_AT_frame_base:
4469 return "DW_AT_frame_base";
4470 case DW_AT_friend:
4471 return "DW_AT_friend";
4472 case DW_AT_identifier_case:
4473 return "DW_AT_identifier_case";
4474 case DW_AT_macro_info:
4475 return "DW_AT_macro_info";
4476 case DW_AT_namelist_items:
4477 return "DW_AT_namelist_items";
4478 case DW_AT_priority:
4479 return "DW_AT_priority";
4480 case DW_AT_segment:
4481 return "DW_AT_segment";
4482 case DW_AT_specification:
4483 return "DW_AT_specification";
4484 case DW_AT_static_link:
4485 return "DW_AT_static_link";
4486 case DW_AT_type:
4487 return "DW_AT_type";
4488 case DW_AT_use_location:
4489 return "DW_AT_use_location";
4490 case DW_AT_variable_parameter:
4491 return "DW_AT_variable_parameter";
4492 case DW_AT_virtuality:
4493 return "DW_AT_virtuality";
4494 case DW_AT_vtable_elem_location:
4495 return "DW_AT_vtable_elem_location";
4497 case DW_AT_allocated:
4498 return "DW_AT_allocated";
4499 case DW_AT_associated:
4500 return "DW_AT_associated";
4501 case DW_AT_data_location:
4502 return "DW_AT_data_location";
4503 case DW_AT_stride:
4504 return "DW_AT_stride";
4505 case DW_AT_entry_pc:
4506 return "DW_AT_entry_pc";
4507 case DW_AT_use_UTF8:
4508 return "DW_AT_use_UTF8";
4509 case DW_AT_extension:
4510 return "DW_AT_extension";
4511 case DW_AT_ranges:
4512 return "DW_AT_ranges";
4513 case DW_AT_trampoline:
4514 return "DW_AT_trampoline";
4515 case DW_AT_call_column:
4516 return "DW_AT_call_column";
4517 case DW_AT_call_file:
4518 return "DW_AT_call_file";
4519 case DW_AT_call_line:
4520 return "DW_AT_call_line";
4522 case DW_AT_MIPS_fde:
4523 return "DW_AT_MIPS_fde";
4524 case DW_AT_MIPS_loop_begin:
4525 return "DW_AT_MIPS_loop_begin";
4526 case DW_AT_MIPS_tail_loop_begin:
4527 return "DW_AT_MIPS_tail_loop_begin";
4528 case DW_AT_MIPS_epilog_begin:
4529 return "DW_AT_MIPS_epilog_begin";
4530 case DW_AT_MIPS_loop_unroll_factor:
4531 return "DW_AT_MIPS_loop_unroll_factor";
4532 case DW_AT_MIPS_software_pipeline_depth:
4533 return "DW_AT_MIPS_software_pipeline_depth";
4534 case DW_AT_MIPS_linkage_name:
4535 return "DW_AT_MIPS_linkage_name";
4536 case DW_AT_MIPS_stride:
4537 return "DW_AT_MIPS_stride";
4538 case DW_AT_MIPS_abstract_name:
4539 return "DW_AT_MIPS_abstract_name";
4540 case DW_AT_MIPS_clone_origin:
4541 return "DW_AT_MIPS_clone_origin";
4542 case DW_AT_MIPS_has_inlines:
4543 return "DW_AT_MIPS_has_inlines";
4545 case DW_AT_sf_names:
4546 return "DW_AT_sf_names";
4547 case DW_AT_src_info:
4548 return "DW_AT_src_info";
4549 case DW_AT_mac_info:
4550 return "DW_AT_mac_info";
4551 case DW_AT_src_coords:
4552 return "DW_AT_src_coords";
4553 case DW_AT_body_begin:
4554 return "DW_AT_body_begin";
4555 case DW_AT_body_end:
4556 return "DW_AT_body_end";
4557 case DW_AT_GNU_vector:
4558 return "DW_AT_GNU_vector";
4560 case DW_AT_VMS_rtnbeg_pd_address:
4561 return "DW_AT_VMS_rtnbeg_pd_address";
4563 default:
4564 return "DW_AT_<unknown>";
4568 /* Convert a DWARF value form code into its string name. */
4570 static const char *
4571 dwarf_form_name (unsigned int form)
4573 switch (form)
4575 case DW_FORM_addr:
4576 return "DW_FORM_addr";
4577 case DW_FORM_block2:
4578 return "DW_FORM_block2";
4579 case DW_FORM_block4:
4580 return "DW_FORM_block4";
4581 case DW_FORM_data2:
4582 return "DW_FORM_data2";
4583 case DW_FORM_data4:
4584 return "DW_FORM_data4";
4585 case DW_FORM_data8:
4586 return "DW_FORM_data8";
4587 case DW_FORM_string:
4588 return "DW_FORM_string";
4589 case DW_FORM_block:
4590 return "DW_FORM_block";
4591 case DW_FORM_block1:
4592 return "DW_FORM_block1";
4593 case DW_FORM_data1:
4594 return "DW_FORM_data1";
4595 case DW_FORM_flag:
4596 return "DW_FORM_flag";
4597 case DW_FORM_sdata:
4598 return "DW_FORM_sdata";
4599 case DW_FORM_strp:
4600 return "DW_FORM_strp";
4601 case DW_FORM_udata:
4602 return "DW_FORM_udata";
4603 case DW_FORM_ref_addr:
4604 return "DW_FORM_ref_addr";
4605 case DW_FORM_ref1:
4606 return "DW_FORM_ref1";
4607 case DW_FORM_ref2:
4608 return "DW_FORM_ref2";
4609 case DW_FORM_ref4:
4610 return "DW_FORM_ref4";
4611 case DW_FORM_ref8:
4612 return "DW_FORM_ref8";
4613 case DW_FORM_ref_udata:
4614 return "DW_FORM_ref_udata";
4615 case DW_FORM_indirect:
4616 return "DW_FORM_indirect";
4617 default:
4618 return "DW_FORM_<unknown>";
4622 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4623 instance of an inlined instance of a decl which is local to an inline
4624 function, so we have to trace all of the way back through the origin chain
4625 to find out what sort of node actually served as the original seed for the
4626 given block. */
4628 static tree
4629 decl_ultimate_origin (tree decl)
4631 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4632 nodes in the function to point to themselves; ignore that if
4633 we're trying to output the abstract instance of this function. */
4634 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4635 return NULL_TREE;
4637 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4638 most distant ancestor, this should never happen. */
4639 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4641 return DECL_ABSTRACT_ORIGIN (decl);
4644 /* Determine the "ultimate origin" of a block. The block may be an inlined
4645 instance of an inlined instance of a block which is local to an inline
4646 function, so we have to trace all of the way back through the origin chain
4647 to find out what sort of node actually served as the original seed for the
4648 given block. */
4650 static tree
4651 block_ultimate_origin (tree block)
4653 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4655 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4656 nodes in the function to point to themselves; ignore that if
4657 we're trying to output the abstract instance of this function. */
4658 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4659 return NULL_TREE;
4661 if (immediate_origin == NULL_TREE)
4662 return NULL_TREE;
4663 else
4665 tree ret_val;
4666 tree lookahead = immediate_origin;
4670 ret_val = lookahead;
4671 lookahead = (TREE_CODE (ret_val) == BLOCK
4672 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4674 while (lookahead != NULL && lookahead != ret_val);
4676 /* The block's abstract origin chain may not be the *ultimate* origin of
4677 the block. It could lead to a DECL that has an abstract origin set.
4678 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4679 will give us if it has one). Note that DECL's abstract origins are
4680 supposed to be the most distant ancestor (or so decl_ultimate_origin
4681 claims), so we don't need to loop following the DECL origins. */
4682 if (DECL_P (ret_val))
4683 return DECL_ORIGIN (ret_val);
4685 return ret_val;
4689 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4690 of a virtual function may refer to a base class, so we check the 'this'
4691 parameter. */
4693 static tree
4694 decl_class_context (tree decl)
4696 tree context = NULL_TREE;
4698 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4699 context = DECL_CONTEXT (decl);
4700 else
4701 context = TYPE_MAIN_VARIANT
4702 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4704 if (context && !TYPE_P (context))
4705 context = NULL_TREE;
4707 return context;
4710 /* Add an attribute/value pair to a DIE. We build the lists up in reverse
4711 addition order, and correct that in reverse_all_dies. */
4713 static inline void
4714 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4716 if (die != NULL && attr != NULL)
4718 attr->dw_attr_next = die->die_attr;
4719 die->die_attr = attr;
4723 static inline enum dw_val_class
4724 AT_class (dw_attr_ref a)
4726 return a->dw_attr_val.val_class;
4729 /* Add a flag value attribute to a DIE. */
4731 static inline void
4732 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4734 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4736 attr->dw_attr_next = NULL;
4737 attr->dw_attr = attr_kind;
4738 attr->dw_attr_val.val_class = dw_val_class_flag;
4739 attr->dw_attr_val.v.val_flag = flag;
4740 add_dwarf_attr (die, attr);
4743 static inline unsigned
4744 AT_flag (dw_attr_ref a)
4746 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4747 return a->dw_attr_val.v.val_flag;
4750 /* Add a signed integer attribute value to a DIE. */
4752 static inline void
4753 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4755 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4757 attr->dw_attr_next = NULL;
4758 attr->dw_attr = attr_kind;
4759 attr->dw_attr_val.val_class = dw_val_class_const;
4760 attr->dw_attr_val.v.val_int = int_val;
4761 add_dwarf_attr (die, attr);
4764 static inline HOST_WIDE_INT
4765 AT_int (dw_attr_ref a)
4767 gcc_assert (a && AT_class (a) == dw_val_class_const);
4768 return a->dw_attr_val.v.val_int;
4771 /* Add an unsigned integer attribute value to a DIE. */
4773 static inline void
4774 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4775 unsigned HOST_WIDE_INT unsigned_val)
4777 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4779 attr->dw_attr_next = NULL;
4780 attr->dw_attr = attr_kind;
4781 attr->dw_attr_val.val_class = dw_val_class_unsigned_const;
4782 attr->dw_attr_val.v.val_unsigned = unsigned_val;
4783 add_dwarf_attr (die, attr);
4786 static inline unsigned HOST_WIDE_INT
4787 AT_unsigned (dw_attr_ref a)
4789 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4790 return a->dw_attr_val.v.val_unsigned;
4793 /* Add an unsigned double integer attribute value to a DIE. */
4795 static inline void
4796 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4797 long unsigned int val_hi, long unsigned int val_low)
4799 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4801 attr->dw_attr_next = NULL;
4802 attr->dw_attr = attr_kind;
4803 attr->dw_attr_val.val_class = dw_val_class_long_long;
4804 attr->dw_attr_val.v.val_long_long.hi = val_hi;
4805 attr->dw_attr_val.v.val_long_long.low = val_low;
4806 add_dwarf_attr (die, attr);
4809 /* Add a floating point attribute value to a DIE and return it. */
4811 static inline void
4812 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4813 unsigned int length, unsigned int elt_size, unsigned char *array)
4815 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4817 attr->dw_attr_next = NULL;
4818 attr->dw_attr = attr_kind;
4819 attr->dw_attr_val.val_class = dw_val_class_vec;
4820 attr->dw_attr_val.v.val_vec.length = length;
4821 attr->dw_attr_val.v.val_vec.elt_size = elt_size;
4822 attr->dw_attr_val.v.val_vec.array = array;
4823 add_dwarf_attr (die, attr);
4826 /* Hash and equality functions for debug_str_hash. */
4828 static hashval_t
4829 debug_str_do_hash (const void *x)
4831 return htab_hash_string (((const struct indirect_string_node *)x)->str);
4834 static int
4835 debug_str_eq (const void *x1, const void *x2)
4837 return strcmp ((((const struct indirect_string_node *)x1)->str),
4838 (const char *)x2) == 0;
4841 /* Add a string attribute value to a DIE. */
4843 static inline void
4844 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4846 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4847 struct indirect_string_node *node;
4848 void **slot;
4850 if (! debug_str_hash)
4851 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4852 debug_str_eq, NULL);
4854 slot = htab_find_slot_with_hash (debug_str_hash, str,
4855 htab_hash_string (str), INSERT);
4856 if (*slot == NULL)
4857 *slot = ggc_alloc_cleared (sizeof (struct indirect_string_node));
4858 node = (struct indirect_string_node *) *slot;
4859 node->str = ggc_strdup (str);
4860 node->refcount++;
4862 attr->dw_attr_next = NULL;
4863 attr->dw_attr = attr_kind;
4864 attr->dw_attr_val.val_class = dw_val_class_str;
4865 attr->dw_attr_val.v.val_str = node;
4866 add_dwarf_attr (die, attr);
4869 static inline const char *
4870 AT_string (dw_attr_ref a)
4872 gcc_assert (a && AT_class (a) == dw_val_class_str);
4873 return a->dw_attr_val.v.val_str->str;
4876 /* Find out whether a string should be output inline in DIE
4877 or out-of-line in .debug_str section. */
4879 static int
4880 AT_string_form (dw_attr_ref a)
4882 struct indirect_string_node *node;
4883 unsigned int len;
4884 char label[32];
4886 gcc_assert (a && AT_class (a) == dw_val_class_str);
4888 node = a->dw_attr_val.v.val_str;
4889 if (node->form)
4890 return node->form;
4892 len = strlen (node->str) + 1;
4894 /* If the string is shorter or equal to the size of the reference, it is
4895 always better to put it inline. */
4896 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4897 return node->form = DW_FORM_string;
4899 /* If we cannot expect the linker to merge strings in .debug_str
4900 section, only put it into .debug_str if it is worth even in this
4901 single module. */
4902 if ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) == 0
4903 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
4904 return node->form = DW_FORM_string;
4906 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4907 ++dw2_string_counter;
4908 node->label = xstrdup (label);
4910 return node->form = DW_FORM_strp;
4913 /* Add a DIE reference attribute value to a DIE. */
4915 static inline void
4916 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4918 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4920 attr->dw_attr_next = NULL;
4921 attr->dw_attr = attr_kind;
4922 attr->dw_attr_val.val_class = dw_val_class_die_ref;
4923 attr->dw_attr_val.v.val_die_ref.die = targ_die;
4924 attr->dw_attr_val.v.val_die_ref.external = 0;
4925 add_dwarf_attr (die, attr);
4928 /* Add an AT_specification attribute to a DIE, and also make the back
4929 pointer from the specification to the definition. */
4931 static inline void
4932 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4934 add_AT_die_ref (die, DW_AT_specification, targ_die);
4935 gcc_assert (!targ_die->die_definition);
4936 targ_die->die_definition = die;
4939 static inline dw_die_ref
4940 AT_ref (dw_attr_ref a)
4942 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4943 return a->dw_attr_val.v.val_die_ref.die;
4946 static inline int
4947 AT_ref_external (dw_attr_ref a)
4949 if (a && AT_class (a) == dw_val_class_die_ref)
4950 return a->dw_attr_val.v.val_die_ref.external;
4952 return 0;
4955 static inline void
4956 set_AT_ref_external (dw_attr_ref a, int i)
4958 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4959 a->dw_attr_val.v.val_die_ref.external = i;
4962 /* Add an FDE reference attribute value to a DIE. */
4964 static inline void
4965 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4967 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4969 attr->dw_attr_next = NULL;
4970 attr->dw_attr = attr_kind;
4971 attr->dw_attr_val.val_class = dw_val_class_fde_ref;
4972 attr->dw_attr_val.v.val_fde_index = targ_fde;
4973 add_dwarf_attr (die, attr);
4976 /* Add a location description attribute value to a DIE. */
4978 static inline void
4979 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4981 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4983 attr->dw_attr_next = NULL;
4984 attr->dw_attr = attr_kind;
4985 attr->dw_attr_val.val_class = dw_val_class_loc;
4986 attr->dw_attr_val.v.val_loc = loc;
4987 add_dwarf_attr (die, attr);
4990 static inline dw_loc_descr_ref
4991 AT_loc (dw_attr_ref a)
4993 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4994 return a->dw_attr_val.v.val_loc;
4997 static inline void
4998 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5000 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5002 attr->dw_attr_next = NULL;
5003 attr->dw_attr = attr_kind;
5004 attr->dw_attr_val.val_class = dw_val_class_loc_list;
5005 attr->dw_attr_val.v.val_loc_list = loc_list;
5006 add_dwarf_attr (die, attr);
5007 have_location_lists = 1;
5010 static inline dw_loc_list_ref
5011 AT_loc_list (dw_attr_ref a)
5013 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5014 return a->dw_attr_val.v.val_loc_list;
5017 /* Add an address constant attribute value to a DIE. */
5019 static inline void
5020 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5022 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5024 attr->dw_attr_next = NULL;
5025 attr->dw_attr = attr_kind;
5026 attr->dw_attr_val.val_class = dw_val_class_addr;
5027 attr->dw_attr_val.v.val_addr = addr;
5028 add_dwarf_attr (die, attr);
5031 static inline rtx
5032 AT_addr (dw_attr_ref a)
5034 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5035 return a->dw_attr_val.v.val_addr;
5038 /* Add a label identifier attribute value to a DIE. */
5040 static inline void
5041 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5043 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5045 attr->dw_attr_next = NULL;
5046 attr->dw_attr = attr_kind;
5047 attr->dw_attr_val.val_class = dw_val_class_lbl_id;
5048 attr->dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5049 add_dwarf_attr (die, attr);
5052 /* Add a section offset attribute value to a DIE. */
5054 static inline void
5055 add_AT_lbl_offset (dw_die_ref die, enum dwarf_attribute attr_kind, const char *label)
5057 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5059 attr->dw_attr_next = NULL;
5060 attr->dw_attr = attr_kind;
5061 attr->dw_attr_val.val_class = dw_val_class_lbl_offset;
5062 attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
5063 add_dwarf_attr (die, attr);
5066 /* Add an offset attribute value to a DIE. */
5068 static inline void
5069 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5070 unsigned HOST_WIDE_INT offset)
5072 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5074 attr->dw_attr_next = NULL;
5075 attr->dw_attr = attr_kind;
5076 attr->dw_attr_val.val_class = dw_val_class_offset;
5077 attr->dw_attr_val.v.val_offset = offset;
5078 add_dwarf_attr (die, attr);
5081 /* Add an range_list attribute value to a DIE. */
5083 static void
5084 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5085 long unsigned int offset)
5087 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5089 attr->dw_attr_next = NULL;
5090 attr->dw_attr = attr_kind;
5091 attr->dw_attr_val.val_class = dw_val_class_range_list;
5092 attr->dw_attr_val.v.val_offset = offset;
5093 add_dwarf_attr (die, attr);
5096 static inline const char *
5097 AT_lbl (dw_attr_ref a)
5099 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5100 || AT_class (a) == dw_val_class_lbl_offset));
5101 return a->dw_attr_val.v.val_lbl_id;
5104 /* Get the attribute of type attr_kind. */
5106 static dw_attr_ref
5107 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5109 dw_attr_ref a;
5110 dw_die_ref spec = NULL;
5112 if (die != NULL)
5114 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5115 if (a->dw_attr == attr_kind)
5116 return a;
5117 else if (a->dw_attr == DW_AT_specification
5118 || a->dw_attr == DW_AT_abstract_origin)
5119 spec = AT_ref (a);
5121 if (spec)
5122 return get_AT (spec, attr_kind);
5125 return NULL;
5128 /* Return the "low pc" attribute value, typically associated with a subprogram
5129 DIE. Return null if the "low pc" attribute is either not present, or if it
5130 cannot be represented as an assembler label identifier. */
5132 static inline const char *
5133 get_AT_low_pc (dw_die_ref die)
5135 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5137 return a ? AT_lbl (a) : NULL;
5140 /* Return the "high pc" attribute value, typically associated with a subprogram
5141 DIE. Return null if the "high pc" attribute is either not present, or if it
5142 cannot be represented as an assembler label identifier. */
5144 static inline const char *
5145 get_AT_hi_pc (dw_die_ref die)
5147 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5149 return a ? AT_lbl (a) : NULL;
5152 /* Return the value of the string attribute designated by ATTR_KIND, or
5153 NULL if it is not present. */
5155 static inline const char *
5156 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5158 dw_attr_ref a = get_AT (die, attr_kind);
5160 return a ? AT_string (a) : NULL;
5163 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5164 if it is not present. */
5166 static inline int
5167 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5169 dw_attr_ref a = get_AT (die, attr_kind);
5171 return a ? AT_flag (a) : 0;
5174 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5175 if it is not present. */
5177 static inline unsigned
5178 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5180 dw_attr_ref a = get_AT (die, attr_kind);
5182 return a ? AT_unsigned (a) : 0;
5185 static inline dw_die_ref
5186 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5188 dw_attr_ref a = get_AT (die, attr_kind);
5190 return a ? AT_ref (a) : NULL;
5193 /* Return TRUE if the language is C or C++. */
5195 static inline bool
5196 is_c_family (void)
5198 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5200 return (lang == DW_LANG_C || lang == DW_LANG_C89
5201 || lang == DW_LANG_C_plus_plus);
5204 /* Return TRUE if the language is C++. */
5206 static inline bool
5207 is_cxx (void)
5209 return (get_AT_unsigned (comp_unit_die, DW_AT_language)
5210 == DW_LANG_C_plus_plus);
5213 /* Return TRUE if the language is Fortran. */
5215 static inline bool
5216 is_fortran (void)
5218 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5220 return (lang == DW_LANG_Fortran77
5221 || lang == DW_LANG_Fortran90
5222 || lang == DW_LANG_Fortran95);
5225 /* Return TRUE if the language is Java. */
5227 static inline bool
5228 is_java (void)
5230 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5232 return lang == DW_LANG_Java;
5235 /* Return TRUE if the language is Ada. */
5237 static inline bool
5238 is_ada (void)
5240 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5242 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5245 /* Free up the memory used by A. */
5247 static inline void free_AT (dw_attr_ref);
5248 static inline void
5249 free_AT (dw_attr_ref a)
5251 if (AT_class (a) == dw_val_class_str)
5252 if (a->dw_attr_val.v.val_str->refcount)
5253 a->dw_attr_val.v.val_str->refcount--;
5256 /* Remove the specified attribute if present. */
5258 static void
5259 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5261 dw_attr_ref *p;
5262 dw_attr_ref removed = NULL;
5264 if (die != NULL)
5266 for (p = &(die->die_attr); *p; p = &((*p)->dw_attr_next))
5267 if ((*p)->dw_attr == attr_kind)
5269 removed = *p;
5270 *p = (*p)->dw_attr_next;
5271 break;
5274 if (removed != 0)
5275 free_AT (removed);
5279 /* Remove child die whose die_tag is specified tag. */
5281 static void
5282 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5284 dw_die_ref current, prev, next;
5285 current = die->die_child;
5286 prev = NULL;
5287 while (current != NULL)
5289 if (current->die_tag == tag)
5291 next = current->die_sib;
5292 if (prev == NULL)
5293 die->die_child = next;
5294 else
5295 prev->die_sib = next;
5296 free_die (current);
5297 current = next;
5299 else
5301 prev = current;
5302 current = current->die_sib;
5307 /* Free up the memory used by DIE. */
5309 static inline void
5310 free_die (dw_die_ref die)
5312 remove_children (die);
5315 /* Discard the children of this DIE. */
5317 static void
5318 remove_children (dw_die_ref die)
5320 dw_die_ref child_die = die->die_child;
5322 die->die_child = NULL;
5324 while (child_die != NULL)
5326 dw_die_ref tmp_die = child_die;
5327 dw_attr_ref a;
5329 child_die = child_die->die_sib;
5331 for (a = tmp_die->die_attr; a != NULL;)
5333 dw_attr_ref tmp_a = a;
5335 a = a->dw_attr_next;
5336 free_AT (tmp_a);
5339 free_die (tmp_die);
5343 /* Add a child DIE below its parent. We build the lists up in reverse
5344 addition order, and correct that in reverse_all_dies. */
5346 static inline void
5347 add_child_die (dw_die_ref die, dw_die_ref child_die)
5349 if (die != NULL && child_die != NULL)
5351 gcc_assert (die != child_die);
5353 child_die->die_parent = die;
5354 child_die->die_sib = die->die_child;
5355 die->die_child = child_die;
5359 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5360 is the specification, to the front of PARENT's list of children. */
5362 static void
5363 splice_child_die (dw_die_ref parent, dw_die_ref child)
5365 dw_die_ref *p;
5367 /* We want the declaration DIE from inside the class, not the
5368 specification DIE at toplevel. */
5369 if (child->die_parent != parent)
5371 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5373 if (tmp)
5374 child = tmp;
5377 gcc_assert (child->die_parent == parent
5378 || (child->die_parent
5379 == get_AT_ref (parent, DW_AT_specification)));
5381 for (p = &(child->die_parent->die_child); *p; p = &((*p)->die_sib))
5382 if (*p == child)
5384 *p = child->die_sib;
5385 break;
5388 child->die_parent = parent;
5389 child->die_sib = parent->die_child;
5390 parent->die_child = child;
5393 /* Return a pointer to a newly created DIE node. */
5395 static inline dw_die_ref
5396 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5398 dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5400 die->die_tag = tag_value;
5402 if (parent_die != NULL)
5403 add_child_die (parent_die, die);
5404 else
5406 limbo_die_node *limbo_node;
5408 limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5409 limbo_node->die = die;
5410 limbo_node->created_for = t;
5411 limbo_node->next = limbo_die_list;
5412 limbo_die_list = limbo_node;
5415 return die;
5418 /* Return the DIE associated with the given type specifier. */
5420 static inline dw_die_ref
5421 lookup_type_die (tree type)
5423 return TYPE_SYMTAB_DIE (type);
5426 /* Equate a DIE to a given type specifier. */
5428 static inline void
5429 equate_type_number_to_die (tree type, dw_die_ref type_die)
5431 TYPE_SYMTAB_DIE (type) = type_die;
5434 /* Returns a hash value for X (which really is a die_struct). */
5436 static hashval_t
5437 decl_die_table_hash (const void *x)
5439 return (hashval_t) ((const dw_die_ref) x)->decl_id;
5442 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5444 static int
5445 decl_die_table_eq (const void *x, const void *y)
5447 return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5450 /* Return the DIE associated with a given declaration. */
5452 static inline dw_die_ref
5453 lookup_decl_die (tree decl)
5455 return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5458 /* Returns a hash value for X (which really is a var_loc_list). */
5460 static hashval_t
5461 decl_loc_table_hash (const void *x)
5463 return (hashval_t) ((const var_loc_list *) x)->decl_id;
5466 /* Return nonzero if decl_id of var_loc_list X is the same as
5467 UID of decl *Y. */
5469 static int
5470 decl_loc_table_eq (const void *x, const void *y)
5472 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5475 /* Return the var_loc list associated with a given declaration. */
5477 static inline var_loc_list *
5478 lookup_decl_loc (tree decl)
5480 return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5483 /* Equate a DIE to a particular declaration. */
5485 static void
5486 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5488 unsigned int decl_id = DECL_UID (decl);
5489 void **slot;
5491 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5492 *slot = decl_die;
5493 decl_die->decl_id = decl_id;
5496 /* Add a variable location node to the linked list for DECL. */
5498 static void
5499 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5501 unsigned int decl_id = DECL_UID (decl);
5502 var_loc_list *temp;
5503 void **slot;
5505 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5506 if (*slot == NULL)
5508 temp = ggc_alloc_cleared (sizeof (var_loc_list));
5509 temp->decl_id = decl_id;
5510 *slot = temp;
5512 else
5513 temp = *slot;
5515 if (temp->last)
5517 /* If the current location is the same as the end of the list,
5518 we have nothing to do. */
5519 if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5520 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5522 /* Add LOC to the end of list and update LAST. */
5523 temp->last->next = loc;
5524 temp->last = loc;
5527 /* Do not add empty location to the beginning of the list. */
5528 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5530 temp->first = loc;
5531 temp->last = loc;
5535 /* Keep track of the number of spaces used to indent the
5536 output of the debugging routines that print the structure of
5537 the DIE internal representation. */
5538 static int print_indent;
5540 /* Indent the line the number of spaces given by print_indent. */
5542 static inline void
5543 print_spaces (FILE *outfile)
5545 fprintf (outfile, "%*s", print_indent, "");
5548 /* Print the information associated with a given DIE, and its children.
5549 This routine is a debugging aid only. */
5551 static void
5552 print_die (dw_die_ref die, FILE *outfile)
5554 dw_attr_ref a;
5555 dw_die_ref c;
5557 print_spaces (outfile);
5558 fprintf (outfile, "DIE %4lu: %s\n",
5559 die->die_offset, dwarf_tag_name (die->die_tag));
5560 print_spaces (outfile);
5561 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5562 fprintf (outfile, " offset: %lu\n", die->die_offset);
5564 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5566 print_spaces (outfile);
5567 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5569 switch (AT_class (a))
5571 case dw_val_class_addr:
5572 fprintf (outfile, "address");
5573 break;
5574 case dw_val_class_offset:
5575 fprintf (outfile, "offset");
5576 break;
5577 case dw_val_class_loc:
5578 fprintf (outfile, "location descriptor");
5579 break;
5580 case dw_val_class_loc_list:
5581 fprintf (outfile, "location list -> label:%s",
5582 AT_loc_list (a)->ll_symbol);
5583 break;
5584 case dw_val_class_range_list:
5585 fprintf (outfile, "range list");
5586 break;
5587 case dw_val_class_const:
5588 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5589 break;
5590 case dw_val_class_unsigned_const:
5591 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5592 break;
5593 case dw_val_class_long_long:
5594 fprintf (outfile, "constant (%lu,%lu)",
5595 a->dw_attr_val.v.val_long_long.hi,
5596 a->dw_attr_val.v.val_long_long.low);
5597 break;
5598 case dw_val_class_vec:
5599 fprintf (outfile, "floating-point or vector constant");
5600 break;
5601 case dw_val_class_flag:
5602 fprintf (outfile, "%u", AT_flag (a));
5603 break;
5604 case dw_val_class_die_ref:
5605 if (AT_ref (a) != NULL)
5607 if (AT_ref (a)->die_symbol)
5608 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5609 else
5610 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5612 else
5613 fprintf (outfile, "die -> <null>");
5614 break;
5615 case dw_val_class_lbl_id:
5616 case dw_val_class_lbl_offset:
5617 fprintf (outfile, "label: %s", AT_lbl (a));
5618 break;
5619 case dw_val_class_str:
5620 if (AT_string (a) != NULL)
5621 fprintf (outfile, "\"%s\"", AT_string (a));
5622 else
5623 fprintf (outfile, "<null>");
5624 break;
5625 default:
5626 break;
5629 fprintf (outfile, "\n");
5632 if (die->die_child != NULL)
5634 print_indent += 4;
5635 for (c = die->die_child; c != NULL; c = c->die_sib)
5636 print_die (c, outfile);
5638 print_indent -= 4;
5640 if (print_indent == 0)
5641 fprintf (outfile, "\n");
5644 /* Print the contents of the source code line number correspondence table.
5645 This routine is a debugging aid only. */
5647 static void
5648 print_dwarf_line_table (FILE *outfile)
5650 unsigned i;
5651 dw_line_info_ref line_info;
5653 fprintf (outfile, "\n\nDWARF source line information\n");
5654 for (i = 1; i < line_info_table_in_use; i++)
5656 line_info = &line_info_table[i];
5657 fprintf (outfile, "%5d: ", i);
5658 fprintf (outfile, "%-20s",
5659 VARRAY_CHAR_PTR (file_table, line_info->dw_file_num));
5660 fprintf (outfile, "%6ld", line_info->dw_line_num);
5661 fprintf (outfile, "\n");
5664 fprintf (outfile, "\n\n");
5667 /* Print the information collected for a given DIE. */
5669 void
5670 debug_dwarf_die (dw_die_ref die)
5672 print_die (die, stderr);
5675 /* Print all DWARF information collected for the compilation unit.
5676 This routine is a debugging aid only. */
5678 void
5679 debug_dwarf (void)
5681 print_indent = 0;
5682 print_die (comp_unit_die, stderr);
5683 if (! DWARF2_ASM_LINE_DEBUG_INFO)
5684 print_dwarf_line_table (stderr);
5687 /* We build up the lists of children and attributes by pushing new ones
5688 onto the beginning of the list. Reverse the lists for DIE so that
5689 they are in order of addition. */
5691 static void
5692 reverse_die_lists (dw_die_ref die)
5694 dw_die_ref c, cp, cn;
5695 dw_attr_ref a, ap, an;
5697 for (a = die->die_attr, ap = 0; a; a = an)
5699 an = a->dw_attr_next;
5700 a->dw_attr_next = ap;
5701 ap = a;
5704 die->die_attr = ap;
5706 for (c = die->die_child, cp = 0; c; c = cn)
5708 cn = c->die_sib;
5709 c->die_sib = cp;
5710 cp = c;
5713 die->die_child = cp;
5716 /* reverse_die_lists only reverses the single die you pass it. Since we used to
5717 reverse all dies in add_sibling_attributes, which runs through all the dies,
5718 it would reverse all the dies. Now, however, since we don't call
5719 reverse_die_lists in add_sibling_attributes, we need a routine to
5720 recursively reverse all the dies. This is that routine. */
5722 static void
5723 reverse_all_dies (dw_die_ref die)
5725 dw_die_ref c;
5727 reverse_die_lists (die);
5729 for (c = die->die_child; c; c = c->die_sib)
5730 reverse_all_dies (c);
5733 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5734 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5735 DIE that marks the start of the DIEs for this include file. */
5737 static dw_die_ref
5738 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5740 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5741 dw_die_ref new_unit = gen_compile_unit_die (filename);
5743 new_unit->die_sib = old_unit;
5744 return new_unit;
5747 /* Close an include-file CU and reopen the enclosing one. */
5749 static dw_die_ref
5750 pop_compile_unit (dw_die_ref old_unit)
5752 dw_die_ref new_unit = old_unit->die_sib;
5754 old_unit->die_sib = NULL;
5755 return new_unit;
5758 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5759 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5761 /* Calculate the checksum of a location expression. */
5763 static inline void
5764 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5766 CHECKSUM (loc->dw_loc_opc);
5767 CHECKSUM (loc->dw_loc_oprnd1);
5768 CHECKSUM (loc->dw_loc_oprnd2);
5771 /* Calculate the checksum of an attribute. */
5773 static void
5774 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5776 dw_loc_descr_ref loc;
5777 rtx r;
5779 CHECKSUM (at->dw_attr);
5781 /* We don't care about differences in file numbering. */
5782 if (at->dw_attr == DW_AT_decl_file
5783 /* Or that this was compiled with a different compiler snapshot; if
5784 the output is the same, that's what matters. */
5785 || at->dw_attr == DW_AT_producer)
5786 return;
5788 switch (AT_class (at))
5790 case dw_val_class_const:
5791 CHECKSUM (at->dw_attr_val.v.val_int);
5792 break;
5793 case dw_val_class_unsigned_const:
5794 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5795 break;
5796 case dw_val_class_long_long:
5797 CHECKSUM (at->dw_attr_val.v.val_long_long);
5798 break;
5799 case dw_val_class_vec:
5800 CHECKSUM (at->dw_attr_val.v.val_vec);
5801 break;
5802 case dw_val_class_flag:
5803 CHECKSUM (at->dw_attr_val.v.val_flag);
5804 break;
5805 case dw_val_class_str:
5806 CHECKSUM_STRING (AT_string (at));
5807 break;
5809 case dw_val_class_addr:
5810 r = AT_addr (at);
5811 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5812 CHECKSUM_STRING (XSTR (r, 0));
5813 break;
5815 case dw_val_class_offset:
5816 CHECKSUM (at->dw_attr_val.v.val_offset);
5817 break;
5819 case dw_val_class_loc:
5820 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5821 loc_checksum (loc, ctx);
5822 break;
5824 case dw_val_class_die_ref:
5825 die_checksum (AT_ref (at), ctx, mark);
5826 break;
5828 case dw_val_class_fde_ref:
5829 case dw_val_class_lbl_id:
5830 case dw_val_class_lbl_offset:
5831 break;
5833 default:
5834 break;
5838 /* Calculate the checksum of a DIE. */
5840 static void
5841 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5843 dw_die_ref c;
5844 dw_attr_ref a;
5846 /* To avoid infinite recursion. */
5847 if (die->die_mark)
5849 CHECKSUM (die->die_mark);
5850 return;
5852 die->die_mark = ++(*mark);
5854 CHECKSUM (die->die_tag);
5856 for (a = die->die_attr; a; a = a->dw_attr_next)
5857 attr_checksum (a, ctx, mark);
5859 for (c = die->die_child; c; c = c->die_sib)
5860 die_checksum (c, ctx, mark);
5863 #undef CHECKSUM
5864 #undef CHECKSUM_STRING
5866 /* Do the location expressions look same? */
5867 static inline int
5868 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
5870 return loc1->dw_loc_opc == loc2->dw_loc_opc
5871 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
5872 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
5875 /* Do the values look the same? */
5876 static int
5877 same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
5879 dw_loc_descr_ref loc1, loc2;
5880 rtx r1, r2;
5882 if (v1->val_class != v2->val_class)
5883 return 0;
5885 switch (v1->val_class)
5887 case dw_val_class_const:
5888 return v1->v.val_int == v2->v.val_int;
5889 case dw_val_class_unsigned_const:
5890 return v1->v.val_unsigned == v2->v.val_unsigned;
5891 case dw_val_class_long_long:
5892 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
5893 && v1->v.val_long_long.low == v2->v.val_long_long.low;
5894 case dw_val_class_vec:
5895 if (v1->v.val_vec.length != v2->v.val_vec.length
5896 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
5897 return 0;
5898 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
5899 v1->v.val_vec.length * v1->v.val_vec.elt_size))
5900 return 0;
5901 return 1;
5902 case dw_val_class_flag:
5903 return v1->v.val_flag == v2->v.val_flag;
5904 case dw_val_class_str:
5905 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
5907 case dw_val_class_addr:
5908 r1 = v1->v.val_addr;
5909 r2 = v2->v.val_addr;
5910 if (GET_CODE (r1) != GET_CODE (r2))
5911 return 0;
5912 gcc_assert (GET_CODE (r1) == SYMBOL_REF);
5913 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
5915 case dw_val_class_offset:
5916 return v1->v.val_offset == v2->v.val_offset;
5918 case dw_val_class_loc:
5919 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
5920 loc1 && loc2;
5921 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
5922 if (!same_loc_p (loc1, loc2, mark))
5923 return 0;
5924 return !loc1 && !loc2;
5926 case dw_val_class_die_ref:
5927 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
5929 case dw_val_class_fde_ref:
5930 case dw_val_class_lbl_id:
5931 case dw_val_class_lbl_offset:
5932 return 1;
5934 default:
5935 return 1;
5939 /* Do the attributes look the same? */
5941 static int
5942 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
5944 if (at1->dw_attr != at2->dw_attr)
5945 return 0;
5947 /* We don't care about differences in file numbering. */
5948 if (at1->dw_attr == DW_AT_decl_file
5949 /* Or that this was compiled with a different compiler snapshot; if
5950 the output is the same, that's what matters. */
5951 || at1->dw_attr == DW_AT_producer)
5952 return 1;
5954 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
5957 /* Do the dies look the same? */
5959 static int
5960 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
5962 dw_die_ref c1, c2;
5963 dw_attr_ref a1, a2;
5965 /* To avoid infinite recursion. */
5966 if (die1->die_mark)
5967 return die1->die_mark == die2->die_mark;
5968 die1->die_mark = die2->die_mark = ++(*mark);
5970 if (die1->die_tag != die2->die_tag)
5971 return 0;
5973 for (a1 = die1->die_attr, a2 = die2->die_attr;
5974 a1 && a2;
5975 a1 = a1->dw_attr_next, a2 = a2->dw_attr_next)
5976 if (!same_attr_p (a1, a2, mark))
5977 return 0;
5978 if (a1 || a2)
5979 return 0;
5981 for (c1 = die1->die_child, c2 = die2->die_child;
5982 c1 && c2;
5983 c1 = c1->die_sib, c2 = c2->die_sib)
5984 if (!same_die_p (c1, c2, mark))
5985 return 0;
5986 if (c1 || c2)
5987 return 0;
5989 return 1;
5992 /* Do the dies look the same? Wrapper around same_die_p. */
5994 static int
5995 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
5997 int mark = 0;
5998 int ret = same_die_p (die1, die2, &mark);
6000 unmark_all_dies (die1);
6001 unmark_all_dies (die2);
6003 return ret;
6006 /* The prefix to attach to symbols on DIEs in the current comdat debug
6007 info section. */
6008 static char *comdat_symbol_id;
6010 /* The index of the current symbol within the current comdat CU. */
6011 static unsigned int comdat_symbol_number;
6013 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6014 children, and set comdat_symbol_id accordingly. */
6016 static void
6017 compute_section_prefix (dw_die_ref unit_die)
6019 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6020 const char *base = die_name ? lbasename (die_name) : "anonymous";
6021 char *name = alloca (strlen (base) + 64);
6022 char *p;
6023 int i, mark;
6024 unsigned char checksum[16];
6025 struct md5_ctx ctx;
6027 /* Compute the checksum of the DIE, then append part of it as hex digits to
6028 the name filename of the unit. */
6030 md5_init_ctx (&ctx);
6031 mark = 0;
6032 die_checksum (unit_die, &ctx, &mark);
6033 unmark_all_dies (unit_die);
6034 md5_finish_ctx (&ctx, checksum);
6036 sprintf (name, "%s.", base);
6037 clean_symbol_name (name);
6039 p = name + strlen (name);
6040 for (i = 0; i < 4; i++)
6042 sprintf (p, "%.2x", checksum[i]);
6043 p += 2;
6046 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6047 comdat_symbol_number = 0;
6050 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6052 static int
6053 is_type_die (dw_die_ref die)
6055 switch (die->die_tag)
6057 case DW_TAG_array_type:
6058 case DW_TAG_class_type:
6059 case DW_TAG_enumeration_type:
6060 case DW_TAG_pointer_type:
6061 case DW_TAG_reference_type:
6062 case DW_TAG_string_type:
6063 case DW_TAG_structure_type:
6064 case DW_TAG_subroutine_type:
6065 case DW_TAG_union_type:
6066 case DW_TAG_ptr_to_member_type:
6067 case DW_TAG_set_type:
6068 case DW_TAG_subrange_type:
6069 case DW_TAG_base_type:
6070 case DW_TAG_const_type:
6071 case DW_TAG_file_type:
6072 case DW_TAG_packed_type:
6073 case DW_TAG_volatile_type:
6074 case DW_TAG_typedef:
6075 return 1;
6076 default:
6077 return 0;
6081 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6082 Basically, we want to choose the bits that are likely to be shared between
6083 compilations (types) and leave out the bits that are specific to individual
6084 compilations (functions). */
6086 static int
6087 is_comdat_die (dw_die_ref c)
6089 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6090 we do for stabs. The advantage is a greater likelihood of sharing between
6091 objects that don't include headers in the same order (and therefore would
6092 put the base types in a different comdat). jason 8/28/00 */
6094 if (c->die_tag == DW_TAG_base_type)
6095 return 0;
6097 if (c->die_tag == DW_TAG_pointer_type
6098 || c->die_tag == DW_TAG_reference_type
6099 || c->die_tag == DW_TAG_const_type
6100 || c->die_tag == DW_TAG_volatile_type)
6102 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6104 return t ? is_comdat_die (t) : 0;
6107 return is_type_die (c);
6110 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6111 compilation unit. */
6113 static int
6114 is_symbol_die (dw_die_ref c)
6116 return (is_type_die (c)
6117 || (get_AT (c, DW_AT_declaration)
6118 && !get_AT (c, DW_AT_specification)));
6121 static char *
6122 gen_internal_sym (const char *prefix)
6124 char buf[256];
6126 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6127 return xstrdup (buf);
6130 /* Assign symbols to all worthy DIEs under DIE. */
6132 static void
6133 assign_symbol_names (dw_die_ref die)
6135 dw_die_ref c;
6137 if (is_symbol_die (die))
6139 if (comdat_symbol_id)
6141 char *p = alloca (strlen (comdat_symbol_id) + 64);
6143 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6144 comdat_symbol_id, comdat_symbol_number++);
6145 die->die_symbol = xstrdup (p);
6147 else
6148 die->die_symbol = gen_internal_sym ("LDIE");
6151 for (c = die->die_child; c != NULL; c = c->die_sib)
6152 assign_symbol_names (c);
6155 struct cu_hash_table_entry
6157 dw_die_ref cu;
6158 unsigned min_comdat_num, max_comdat_num;
6159 struct cu_hash_table_entry *next;
6162 /* Routines to manipulate hash table of CUs. */
6163 static hashval_t
6164 htab_cu_hash (const void *of)
6166 const struct cu_hash_table_entry *entry = of;
6168 return htab_hash_string (entry->cu->die_symbol);
6171 static int
6172 htab_cu_eq (const void *of1, const void *of2)
6174 const struct cu_hash_table_entry *entry1 = of1;
6175 const struct die_struct *entry2 = of2;
6177 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6180 static void
6181 htab_cu_del (void *what)
6183 struct cu_hash_table_entry *next, *entry = what;
6185 while (entry)
6187 next = entry->next;
6188 free (entry);
6189 entry = next;
6193 /* Check whether we have already seen this CU and set up SYM_NUM
6194 accordingly. */
6195 static int
6196 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6198 struct cu_hash_table_entry dummy;
6199 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6201 dummy.max_comdat_num = 0;
6203 slot = (struct cu_hash_table_entry **)
6204 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6205 INSERT);
6206 entry = *slot;
6208 for (; entry; last = entry, entry = entry->next)
6210 if (same_die_p_wrap (cu, entry->cu))
6211 break;
6214 if (entry)
6216 *sym_num = entry->min_comdat_num;
6217 return 1;
6220 entry = xcalloc (1, sizeof (struct cu_hash_table_entry));
6221 entry->cu = cu;
6222 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6223 entry->next = *slot;
6224 *slot = entry;
6226 return 0;
6229 /* Record SYM_NUM to record of CU in HTABLE. */
6230 static void
6231 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6233 struct cu_hash_table_entry **slot, *entry;
6235 slot = (struct cu_hash_table_entry **)
6236 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6237 NO_INSERT);
6238 entry = *slot;
6240 entry->max_comdat_num = sym_num;
6243 /* Traverse the DIE (which is always comp_unit_die), and set up
6244 additional compilation units for each of the include files we see
6245 bracketed by BINCL/EINCL. */
6247 static void
6248 break_out_includes (dw_die_ref die)
6250 dw_die_ref *ptr;
6251 dw_die_ref unit = NULL;
6252 limbo_die_node *node, **pnode;
6253 htab_t cu_hash_table;
6255 for (ptr = &(die->die_child); *ptr;)
6257 dw_die_ref c = *ptr;
6259 if (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6260 || (unit && is_comdat_die (c)))
6262 /* This DIE is for a secondary CU; remove it from the main one. */
6263 *ptr = c->die_sib;
6265 if (c->die_tag == DW_TAG_GNU_BINCL)
6267 unit = push_new_compile_unit (unit, c);
6268 free_die (c);
6270 else if (c->die_tag == DW_TAG_GNU_EINCL)
6272 unit = pop_compile_unit (unit);
6273 free_die (c);
6275 else
6276 add_child_die (unit, c);
6278 else
6280 /* Leave this DIE in the main CU. */
6281 ptr = &(c->die_sib);
6282 continue;
6286 #if 0
6287 /* We can only use this in debugging, since the frontend doesn't check
6288 to make sure that we leave every include file we enter. */
6289 gcc_assert (!unit);
6290 #endif
6292 assign_symbol_names (die);
6293 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6294 for (node = limbo_die_list, pnode = &limbo_die_list;
6295 node;
6296 node = node->next)
6298 int is_dupl;
6300 compute_section_prefix (node->die);
6301 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6302 &comdat_symbol_number);
6303 assign_symbol_names (node->die);
6304 if (is_dupl)
6305 *pnode = node->next;
6306 else
6308 pnode = &node->next;
6309 record_comdat_symbol_number (node->die, cu_hash_table,
6310 comdat_symbol_number);
6313 htab_delete (cu_hash_table);
6316 /* Traverse the DIE and add a sibling attribute if it may have the
6317 effect of speeding up access to siblings. To save some space,
6318 avoid generating sibling attributes for DIE's without children. */
6320 static void
6321 add_sibling_attributes (dw_die_ref die)
6323 dw_die_ref c;
6325 if (die->die_tag != DW_TAG_compile_unit
6326 && die->die_sib && die->die_child != NULL)
6327 /* Add the sibling link to the front of the attribute list. */
6328 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6330 for (c = die->die_child; c != NULL; c = c->die_sib)
6331 add_sibling_attributes (c);
6334 /* Output all location lists for the DIE and its children. */
6336 static void
6337 output_location_lists (dw_die_ref die)
6339 dw_die_ref c;
6340 dw_attr_ref d_attr;
6342 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6343 if (AT_class (d_attr) == dw_val_class_loc_list)
6344 output_loc_list (AT_loc_list (d_attr));
6346 for (c = die->die_child; c != NULL; c = c->die_sib)
6347 output_location_lists (c);
6351 /* The format of each DIE (and its attribute value pairs) is encoded in an
6352 abbreviation table. This routine builds the abbreviation table and assigns
6353 a unique abbreviation id for each abbreviation entry. The children of each
6354 die are visited recursively. */
6356 static void
6357 build_abbrev_table (dw_die_ref die)
6359 unsigned long abbrev_id;
6360 unsigned int n_alloc;
6361 dw_die_ref c;
6362 dw_attr_ref d_attr, a_attr;
6364 /* Scan the DIE references, and mark as external any that refer to
6365 DIEs from other CUs (i.e. those which are not marked). */
6366 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6367 if (AT_class (d_attr) == dw_val_class_die_ref
6368 && AT_ref (d_attr)->die_mark == 0)
6370 gcc_assert (AT_ref (d_attr)->die_symbol);
6372 set_AT_ref_external (d_attr, 1);
6375 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6377 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6379 if (abbrev->die_tag == die->die_tag)
6381 if ((abbrev->die_child != NULL) == (die->die_child != NULL))
6383 a_attr = abbrev->die_attr;
6384 d_attr = die->die_attr;
6386 while (a_attr != NULL && d_attr != NULL)
6388 if ((a_attr->dw_attr != d_attr->dw_attr)
6389 || (value_format (a_attr) != value_format (d_attr)))
6390 break;
6392 a_attr = a_attr->dw_attr_next;
6393 d_attr = d_attr->dw_attr_next;
6396 if (a_attr == NULL && d_attr == NULL)
6397 break;
6402 if (abbrev_id >= abbrev_die_table_in_use)
6404 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6406 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6407 abbrev_die_table = ggc_realloc (abbrev_die_table,
6408 sizeof (dw_die_ref) * n_alloc);
6410 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6411 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6412 abbrev_die_table_allocated = n_alloc;
6415 ++abbrev_die_table_in_use;
6416 abbrev_die_table[abbrev_id] = die;
6419 die->die_abbrev = abbrev_id;
6420 for (c = die->die_child; c != NULL; c = c->die_sib)
6421 build_abbrev_table (c);
6424 /* Return the power-of-two number of bytes necessary to represent VALUE. */
6426 static int
6427 constant_size (long unsigned int value)
6429 int log;
6431 if (value == 0)
6432 log = 0;
6433 else
6434 log = floor_log2 (value);
6436 log = log / 8;
6437 log = 1 << (floor_log2 (log) + 1);
6439 return log;
6442 /* Return the size of a DIE as it is represented in the
6443 .debug_info section. */
6445 static unsigned long
6446 size_of_die (dw_die_ref die)
6448 unsigned long size = 0;
6449 dw_attr_ref a;
6451 size += size_of_uleb128 (die->die_abbrev);
6452 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6454 switch (AT_class (a))
6456 case dw_val_class_addr:
6457 size += DWARF2_ADDR_SIZE;
6458 break;
6459 case dw_val_class_offset:
6460 size += DWARF_OFFSET_SIZE;
6461 break;
6462 case dw_val_class_loc:
6464 unsigned long lsize = size_of_locs (AT_loc (a));
6466 /* Block length. */
6467 size += constant_size (lsize);
6468 size += lsize;
6470 break;
6471 case dw_val_class_loc_list:
6472 size += DWARF_OFFSET_SIZE;
6473 break;
6474 case dw_val_class_range_list:
6475 size += DWARF_OFFSET_SIZE;
6476 break;
6477 case dw_val_class_const:
6478 size += size_of_sleb128 (AT_int (a));
6479 break;
6480 case dw_val_class_unsigned_const:
6481 size += constant_size (AT_unsigned (a));
6482 break;
6483 case dw_val_class_long_long:
6484 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6485 break;
6486 case dw_val_class_vec:
6487 size += 1 + (a->dw_attr_val.v.val_vec.length
6488 * a->dw_attr_val.v.val_vec.elt_size); /* block */
6489 break;
6490 case dw_val_class_flag:
6491 size += 1;
6492 break;
6493 case dw_val_class_die_ref:
6494 if (AT_ref_external (a))
6495 size += DWARF2_ADDR_SIZE;
6496 else
6497 size += DWARF_OFFSET_SIZE;
6498 break;
6499 case dw_val_class_fde_ref:
6500 size += DWARF_OFFSET_SIZE;
6501 break;
6502 case dw_val_class_lbl_id:
6503 size += DWARF2_ADDR_SIZE;
6504 break;
6505 case dw_val_class_lbl_offset:
6506 size += DWARF_OFFSET_SIZE;
6507 break;
6508 case dw_val_class_str:
6509 if (AT_string_form (a) == DW_FORM_strp)
6510 size += DWARF_OFFSET_SIZE;
6511 else
6512 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6513 break;
6514 default:
6515 gcc_unreachable ();
6519 return size;
6522 /* Size the debugging information associated with a given DIE. Visits the
6523 DIE's children recursively. Updates the global variable next_die_offset, on
6524 each time through. Uses the current value of next_die_offset to update the
6525 die_offset field in each DIE. */
6527 static void
6528 calc_die_sizes (dw_die_ref die)
6530 dw_die_ref c;
6532 die->die_offset = next_die_offset;
6533 next_die_offset += size_of_die (die);
6535 for (c = die->die_child; c != NULL; c = c->die_sib)
6536 calc_die_sizes (c);
6538 if (die->die_child != NULL)
6539 /* Count the null byte used to terminate sibling lists. */
6540 next_die_offset += 1;
6543 /* Set the marks for a die and its children. We do this so
6544 that we know whether or not a reference needs to use FORM_ref_addr; only
6545 DIEs in the same CU will be marked. We used to clear out the offset
6546 and use that as the flag, but ran into ordering problems. */
6548 static void
6549 mark_dies (dw_die_ref die)
6551 dw_die_ref c;
6553 gcc_assert (!die->die_mark);
6555 die->die_mark = 1;
6556 for (c = die->die_child; c; c = c->die_sib)
6557 mark_dies (c);
6560 /* Clear the marks for a die and its children. */
6562 static void
6563 unmark_dies (dw_die_ref die)
6565 dw_die_ref c;
6567 gcc_assert (die->die_mark);
6569 die->die_mark = 0;
6570 for (c = die->die_child; c; c = c->die_sib)
6571 unmark_dies (c);
6574 /* Clear the marks for a die, its children and referred dies. */
6576 static void
6577 unmark_all_dies (dw_die_ref die)
6579 dw_die_ref c;
6580 dw_attr_ref a;
6582 if (!die->die_mark)
6583 return;
6584 die->die_mark = 0;
6586 for (c = die->die_child; c; c = c->die_sib)
6587 unmark_all_dies (c);
6589 for (a = die->die_attr; a; a = a->dw_attr_next)
6590 if (AT_class (a) == dw_val_class_die_ref)
6591 unmark_all_dies (AT_ref (a));
6594 /* Return the size of the .debug_pubnames table generated for the
6595 compilation unit. */
6597 static unsigned long
6598 size_of_pubnames (void)
6600 unsigned long size;
6601 unsigned i;
6603 size = DWARF_PUBNAMES_HEADER_SIZE;
6604 for (i = 0; i < pubname_table_in_use; i++)
6606 pubname_ref p = &pubname_table[i];
6607 size += DWARF_OFFSET_SIZE + strlen (p->name) + 1;
6610 size += DWARF_OFFSET_SIZE;
6611 return size;
6614 /* Return the size of the information in the .debug_aranges section. */
6616 static unsigned long
6617 size_of_aranges (void)
6619 unsigned long size;
6621 size = DWARF_ARANGES_HEADER_SIZE;
6623 /* Count the address/length pair for this compilation unit. */
6624 size += 2 * DWARF2_ADDR_SIZE;
6625 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6627 /* Count the two zero words used to terminated the address range table. */
6628 size += 2 * DWARF2_ADDR_SIZE;
6629 return size;
6632 /* Select the encoding of an attribute value. */
6634 static enum dwarf_form
6635 value_format (dw_attr_ref a)
6637 switch (a->dw_attr_val.val_class)
6639 case dw_val_class_addr:
6640 return DW_FORM_addr;
6641 case dw_val_class_range_list:
6642 case dw_val_class_offset:
6643 switch (DWARF_OFFSET_SIZE)
6645 case 4:
6646 return DW_FORM_data4;
6647 case 8:
6648 return DW_FORM_data8;
6649 default:
6650 gcc_unreachable ();
6652 case dw_val_class_loc_list:
6653 /* FIXME: Could be DW_FORM_data8, with a > 32 bit size
6654 .debug_loc section */
6655 return DW_FORM_data4;
6656 case dw_val_class_loc:
6657 switch (constant_size (size_of_locs (AT_loc (a))))
6659 case 1:
6660 return DW_FORM_block1;
6661 case 2:
6662 return DW_FORM_block2;
6663 default:
6664 gcc_unreachable ();
6666 case dw_val_class_const:
6667 return DW_FORM_sdata;
6668 case dw_val_class_unsigned_const:
6669 switch (constant_size (AT_unsigned (a)))
6671 case 1:
6672 return DW_FORM_data1;
6673 case 2:
6674 return DW_FORM_data2;
6675 case 4:
6676 return DW_FORM_data4;
6677 case 8:
6678 return DW_FORM_data8;
6679 default:
6680 gcc_unreachable ();
6682 case dw_val_class_long_long:
6683 return DW_FORM_block1;
6684 case dw_val_class_vec:
6685 return DW_FORM_block1;
6686 case dw_val_class_flag:
6687 return DW_FORM_flag;
6688 case dw_val_class_die_ref:
6689 if (AT_ref_external (a))
6690 return DW_FORM_ref_addr;
6691 else
6692 return DW_FORM_ref;
6693 case dw_val_class_fde_ref:
6694 return DW_FORM_data;
6695 case dw_val_class_lbl_id:
6696 return DW_FORM_addr;
6697 case dw_val_class_lbl_offset:
6698 return DW_FORM_data;
6699 case dw_val_class_str:
6700 return AT_string_form (a);
6702 default:
6703 gcc_unreachable ();
6707 /* Output the encoding of an attribute value. */
6709 static void
6710 output_value_format (dw_attr_ref a)
6712 enum dwarf_form form = value_format (a);
6714 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6717 /* Output the .debug_abbrev section which defines the DIE abbreviation
6718 table. */
6720 static void
6721 output_abbrev_section (void)
6723 unsigned long abbrev_id;
6725 dw_attr_ref a_attr;
6727 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6729 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6731 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6732 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6733 dwarf_tag_name (abbrev->die_tag));
6735 if (abbrev->die_child != NULL)
6736 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6737 else
6738 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6740 for (a_attr = abbrev->die_attr; a_attr != NULL;
6741 a_attr = a_attr->dw_attr_next)
6743 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6744 dwarf_attr_name (a_attr->dw_attr));
6745 output_value_format (a_attr);
6748 dw2_asm_output_data (1, 0, NULL);
6749 dw2_asm_output_data (1, 0, NULL);
6752 /* Terminate the table. */
6753 dw2_asm_output_data (1, 0, NULL);
6756 /* Output a symbol we can use to refer to this DIE from another CU. */
6758 static inline void
6759 output_die_symbol (dw_die_ref die)
6761 char *sym = die->die_symbol;
6763 if (sym == 0)
6764 return;
6766 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6767 /* We make these global, not weak; if the target doesn't support
6768 .linkonce, it doesn't support combining the sections, so debugging
6769 will break. */
6770 targetm.asm_out.globalize_label (asm_out_file, sym);
6772 ASM_OUTPUT_LABEL (asm_out_file, sym);
6775 /* Return a new location list, given the begin and end range, and the
6776 expression. gensym tells us whether to generate a new internal symbol for
6777 this location list node, which is done for the head of the list only. */
6779 static inline dw_loc_list_ref
6780 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6781 const char *section, unsigned int gensym)
6783 dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
6785 retlist->begin = begin;
6786 retlist->end = end;
6787 retlist->expr = expr;
6788 retlist->section = section;
6789 if (gensym)
6790 retlist->ll_symbol = gen_internal_sym ("LLST");
6792 return retlist;
6795 /* Add a location description expression to a location list. */
6797 static inline void
6798 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6799 const char *begin, const char *end,
6800 const char *section)
6802 dw_loc_list_ref *d;
6804 /* Find the end of the chain. */
6805 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
6808 /* Add a new location list node to the list. */
6809 *d = new_loc_list (descr, begin, end, section, 0);
6812 static void
6813 dwarf2out_switch_text_section (void)
6815 dw_fde_ref fde;
6817 gcc_assert (cfun);
6819 fde = &fde_table[fde_table_in_use - 1];
6820 fde->dw_fde_switched_sections = true;
6821 fde->dw_fde_hot_section_label = cfun->hot_section_label;
6822 fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
6823 fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
6824 fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
6825 separate_line_info_table_in_use++;
6828 /* Output the location list given to us. */
6830 static void
6831 output_loc_list (dw_loc_list_ref list_head)
6833 dw_loc_list_ref curr = list_head;
6835 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
6837 /* Walk the location list, and output each range + expression. */
6838 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
6840 unsigned long size;
6841 if (separate_line_info_table_in_use == 0)
6843 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
6844 "Location list begin address (%s)",
6845 list_head->ll_symbol);
6846 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
6847 "Location list end address (%s)",
6848 list_head->ll_symbol);
6850 else
6852 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
6853 "Location list begin address (%s)",
6854 list_head->ll_symbol);
6855 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
6856 "Location list end address (%s)",
6857 list_head->ll_symbol);
6859 size = size_of_locs (curr->expr);
6861 /* Output the block length for this list of location operations. */
6862 gcc_assert (size <= 0xffff);
6863 dw2_asm_output_data (2, size, "%s", "Location expression size");
6865 output_loc_sequence (curr->expr);
6868 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6869 "Location list terminator begin (%s)",
6870 list_head->ll_symbol);
6871 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6872 "Location list terminator end (%s)",
6873 list_head->ll_symbol);
6876 /* Output the DIE and its attributes. Called recursively to generate
6877 the definitions of each child DIE. */
6879 static void
6880 output_die (dw_die_ref die)
6882 dw_attr_ref a;
6883 dw_die_ref c;
6884 unsigned long size;
6886 /* If someone in another CU might refer to us, set up a symbol for
6887 them to point to. */
6888 if (die->die_symbol)
6889 output_die_symbol (die);
6891 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
6892 die->die_offset, dwarf_tag_name (die->die_tag));
6894 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6896 const char *name = dwarf_attr_name (a->dw_attr);
6898 switch (AT_class (a))
6900 case dw_val_class_addr:
6901 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
6902 break;
6904 case dw_val_class_offset:
6905 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
6906 "%s", name);
6907 break;
6909 case dw_val_class_range_list:
6911 char *p = strchr (ranges_section_label, '\0');
6913 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
6914 a->dw_attr_val.v.val_offset);
6915 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
6916 "%s", name);
6917 *p = '\0';
6919 break;
6921 case dw_val_class_loc:
6922 size = size_of_locs (AT_loc (a));
6924 /* Output the block length for this list of location operations. */
6925 dw2_asm_output_data (constant_size (size), size, "%s", name);
6927 output_loc_sequence (AT_loc (a));
6928 break;
6930 case dw_val_class_const:
6931 /* ??? It would be slightly more efficient to use a scheme like is
6932 used for unsigned constants below, but gdb 4.x does not sign
6933 extend. Gdb 5.x does sign extend. */
6934 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
6935 break;
6937 case dw_val_class_unsigned_const:
6938 dw2_asm_output_data (constant_size (AT_unsigned (a)),
6939 AT_unsigned (a), "%s", name);
6940 break;
6942 case dw_val_class_long_long:
6944 unsigned HOST_WIDE_INT first, second;
6946 dw2_asm_output_data (1,
6947 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6948 "%s", name);
6950 if (WORDS_BIG_ENDIAN)
6952 first = a->dw_attr_val.v.val_long_long.hi;
6953 second = a->dw_attr_val.v.val_long_long.low;
6955 else
6957 first = a->dw_attr_val.v.val_long_long.low;
6958 second = a->dw_attr_val.v.val_long_long.hi;
6961 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6962 first, "long long constant");
6963 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6964 second, NULL);
6966 break;
6968 case dw_val_class_vec:
6970 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
6971 unsigned int len = a->dw_attr_val.v.val_vec.length;
6972 unsigned int i;
6973 unsigned char *p;
6975 dw2_asm_output_data (1, len * elt_size, "%s", name);
6976 if (elt_size > sizeof (HOST_WIDE_INT))
6978 elt_size /= 2;
6979 len *= 2;
6981 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
6982 i < len;
6983 i++, p += elt_size)
6984 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
6985 "fp or vector constant word %u", i);
6986 break;
6989 case dw_val_class_flag:
6990 dw2_asm_output_data (1, AT_flag (a), "%s", name);
6991 break;
6993 case dw_val_class_loc_list:
6995 char *sym = AT_loc_list (a)->ll_symbol;
6997 gcc_assert (sym);
6998 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, "%s", name);
7000 break;
7002 case dw_val_class_die_ref:
7003 if (AT_ref_external (a))
7005 char *sym = AT_ref (a)->die_symbol;
7007 gcc_assert (sym);
7008 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, "%s", name);
7010 else
7012 gcc_assert (AT_ref (a)->die_offset);
7013 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7014 "%s", name);
7016 break;
7018 case dw_val_class_fde_ref:
7020 char l1[20];
7022 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7023 a->dw_attr_val.v.val_fde_index * 2);
7024 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, "%s", name);
7026 break;
7028 case dw_val_class_lbl_id:
7029 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7030 break;
7032 case dw_val_class_lbl_offset:
7033 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a), "%s", name);
7034 break;
7036 case dw_val_class_str:
7037 if (AT_string_form (a) == DW_FORM_strp)
7038 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7039 a->dw_attr_val.v.val_str->label,
7040 "%s: \"%s\"", name, AT_string (a));
7041 else
7042 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7043 break;
7045 default:
7046 gcc_unreachable ();
7050 for (c = die->die_child; c != NULL; c = c->die_sib)
7051 output_die (c);
7053 /* Add null byte to terminate sibling list. */
7054 if (die->die_child != NULL)
7055 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7056 die->die_offset);
7059 /* Output the compilation unit that appears at the beginning of the
7060 .debug_info section, and precedes the DIE descriptions. */
7062 static void
7063 output_compilation_unit_header (void)
7065 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7066 dw2_asm_output_data (4, 0xffffffff,
7067 "Initial length escape value indicating 64-bit DWARF extension");
7068 dw2_asm_output_data (DWARF_OFFSET_SIZE,
7069 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7070 "Length of Compilation Unit Info");
7071 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7072 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7073 "Offset Into Abbrev. Section");
7074 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7077 /* Output the compilation unit DIE and its children. */
7079 static void
7080 output_comp_unit (dw_die_ref die, int output_if_empty)
7082 const char *secname;
7083 char *oldsym, *tmp;
7085 /* Unless we are outputting main CU, we may throw away empty ones. */
7086 if (!output_if_empty && die->die_child == NULL)
7087 return;
7089 /* Even if there are no children of this DIE, we must output the information
7090 about the compilation unit. Otherwise, on an empty translation unit, we
7091 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
7092 will then complain when examining the file. First mark all the DIEs in
7093 this CU so we know which get local refs. */
7094 mark_dies (die);
7096 build_abbrev_table (die);
7098 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
7099 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7100 calc_die_sizes (die);
7102 oldsym = die->die_symbol;
7103 if (oldsym)
7105 tmp = alloca (strlen (oldsym) + 24);
7107 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7108 secname = tmp;
7109 die->die_symbol = NULL;
7111 else
7112 secname = (const char *) DEBUG_INFO_SECTION;
7114 /* Output debugging information. */
7115 named_section_flags (secname, SECTION_DEBUG);
7116 output_compilation_unit_header ();
7117 output_die (die);
7119 /* Leave the marks on the main CU, so we can check them in
7120 output_pubnames. */
7121 if (oldsym)
7123 unmark_dies (die);
7124 die->die_symbol = oldsym;
7128 /* The DWARF2 pubname for a nested thingy looks like "A::f". The
7129 output of lang_hooks.decl_printable_name for C++ looks like
7130 "A::f(int)". Let's drop the argument list, and maybe the scope. */
7132 static const char *
7133 dwarf2_name (tree decl, int scope)
7135 return lang_hooks.decl_printable_name (decl, scope ? 1 : 0);
7138 /* Add a new entry to .debug_pubnames if appropriate. */
7140 static void
7141 add_pubname (tree decl, dw_die_ref die)
7143 pubname_ref p;
7145 if (! TREE_PUBLIC (decl))
7146 return;
7148 if (pubname_table_in_use == pubname_table_allocated)
7150 pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
7151 pubname_table
7152 = ggc_realloc (pubname_table,
7153 (pubname_table_allocated * sizeof (pubname_entry)));
7154 memset (pubname_table + pubname_table_in_use, 0,
7155 PUBNAME_TABLE_INCREMENT * sizeof (pubname_entry));
7158 p = &pubname_table[pubname_table_in_use++];
7159 p->die = die;
7160 p->name = xstrdup (dwarf2_name (decl, 1));
7163 /* Output the public names table used to speed up access to externally
7164 visible names. For now, only generate entries for externally
7165 visible procedures. */
7167 static void
7168 output_pubnames (void)
7170 unsigned i;
7171 unsigned long pubnames_length = size_of_pubnames ();
7173 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7174 dw2_asm_output_data (4, 0xffffffff,
7175 "Initial length escape value indicating 64-bit DWARF extension");
7176 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7177 "Length of Public Names Info");
7178 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7179 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7180 "Offset of Compilation Unit Info");
7181 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7182 "Compilation Unit Length");
7184 for (i = 0; i < pubname_table_in_use; i++)
7186 pubname_ref pub = &pubname_table[i];
7188 /* We shouldn't see pubnames for DIEs outside of the main CU. */
7189 gcc_assert (pub->die->die_mark);
7191 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7192 "DIE offset");
7194 dw2_asm_output_nstring (pub->name, -1, "external name");
7197 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7200 /* Add a new entry to .debug_aranges if appropriate. */
7202 static void
7203 add_arange (tree decl, dw_die_ref die)
7205 if (! DECL_SECTION_NAME (decl))
7206 return;
7208 if (arange_table_in_use == arange_table_allocated)
7210 arange_table_allocated += ARANGE_TABLE_INCREMENT;
7211 arange_table = ggc_realloc (arange_table,
7212 (arange_table_allocated
7213 * sizeof (dw_die_ref)));
7214 memset (arange_table + arange_table_in_use, 0,
7215 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7218 arange_table[arange_table_in_use++] = die;
7221 /* Output the information that goes into the .debug_aranges table.
7222 Namely, define the beginning and ending address range of the
7223 text section generated for this compilation unit. */
7225 static void
7226 output_aranges (void)
7228 unsigned i;
7229 unsigned long aranges_length = size_of_aranges ();
7231 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7232 dw2_asm_output_data (4, 0xffffffff,
7233 "Initial length escape value indicating 64-bit DWARF extension");
7234 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7235 "Length of Address Ranges Info");
7236 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7237 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7238 "Offset of Compilation Unit Info");
7239 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7240 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7242 /* We need to align to twice the pointer size here. */
7243 if (DWARF_ARANGES_PAD_SIZE)
7245 /* Pad using a 2 byte words so that padding is correct for any
7246 pointer size. */
7247 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7248 2 * DWARF2_ADDR_SIZE);
7249 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7250 dw2_asm_output_data (2, 0, NULL);
7253 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7254 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7255 text_section_label, "Length");
7256 if (flag_reorder_blocks_and_partition)
7258 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7259 "Address");
7260 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7261 cold_text_section_label, "Length");
7264 for (i = 0; i < arange_table_in_use; i++)
7266 dw_die_ref die = arange_table[i];
7268 /* We shouldn't see aranges for DIEs outside of the main CU. */
7269 gcc_assert (die->die_mark);
7271 if (die->die_tag == DW_TAG_subprogram)
7273 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7274 "Address");
7275 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7276 get_AT_low_pc (die), "Length");
7278 else
7280 /* A static variable; extract the symbol from DW_AT_location.
7281 Note that this code isn't currently hit, as we only emit
7282 aranges for functions (jason 9/23/99). */
7283 dw_attr_ref a = get_AT (die, DW_AT_location);
7284 dw_loc_descr_ref loc;
7286 gcc_assert (a && AT_class (a) == dw_val_class_loc);
7288 loc = AT_loc (a);
7289 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7291 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7292 loc->dw_loc_oprnd1.v.val_addr, "Address");
7293 dw2_asm_output_data (DWARF2_ADDR_SIZE,
7294 get_AT_unsigned (die, DW_AT_byte_size),
7295 "Length");
7299 /* Output the terminator words. */
7300 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7301 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7304 /* Add a new entry to .debug_ranges. Return the offset at which it
7305 was placed. */
7307 static unsigned int
7308 add_ranges (tree block)
7310 unsigned int in_use = ranges_table_in_use;
7312 if (in_use == ranges_table_allocated)
7314 ranges_table_allocated += RANGES_TABLE_INCREMENT;
7315 ranges_table
7316 = ggc_realloc (ranges_table, (ranges_table_allocated
7317 * sizeof (struct dw_ranges_struct)));
7318 memset (ranges_table + ranges_table_in_use, 0,
7319 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7322 ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7323 ranges_table_in_use = in_use + 1;
7325 return in_use * 2 * DWARF2_ADDR_SIZE;
7328 static void
7329 output_ranges (void)
7331 unsigned i;
7332 static const char *const start_fmt = "Offset 0x%x";
7333 const char *fmt = start_fmt;
7335 for (i = 0; i < ranges_table_in_use; i++)
7337 int block_num = ranges_table[i].block_num;
7339 if (block_num)
7341 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7342 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7344 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7345 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7347 /* If all code is in the text section, then the compilation
7348 unit base address defaults to DW_AT_low_pc, which is the
7349 base of the text section. */
7350 if (separate_line_info_table_in_use == 0)
7352 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7353 text_section_label,
7354 fmt, i * 2 * DWARF2_ADDR_SIZE);
7355 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7356 text_section_label, NULL);
7359 /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7360 compilation unit base address to zero, which allows us to
7361 use absolute addresses, and not worry about whether the
7362 target supports cross-section arithmetic. */
7363 else
7365 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7366 fmt, i * 2 * DWARF2_ADDR_SIZE);
7367 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7370 fmt = NULL;
7372 else
7374 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7375 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7376 fmt = start_fmt;
7381 /* Data structure containing information about input files. */
7382 struct file_info
7384 char *path; /* Complete file name. */
7385 char *fname; /* File name part. */
7386 int length; /* Length of entire string. */
7387 int file_idx; /* Index in input file table. */
7388 int dir_idx; /* Index in directory table. */
7391 /* Data structure containing information about directories with source
7392 files. */
7393 struct dir_info
7395 char *path; /* Path including directory name. */
7396 int length; /* Path length. */
7397 int prefix; /* Index of directory entry which is a prefix. */
7398 int count; /* Number of files in this directory. */
7399 int dir_idx; /* Index of directory used as base. */
7400 int used; /* Used in the end? */
7403 /* Callback function for file_info comparison. We sort by looking at
7404 the directories in the path. */
7406 static int
7407 file_info_cmp (const void *p1, const void *p2)
7409 const struct file_info *s1 = p1;
7410 const struct file_info *s2 = p2;
7411 unsigned char *cp1;
7412 unsigned char *cp2;
7414 /* Take care of file names without directories. We need to make sure that
7415 we return consistent values to qsort since some will get confused if
7416 we return the same value when identical operands are passed in opposite
7417 orders. So if neither has a directory, return 0 and otherwise return
7418 1 or -1 depending on which one has the directory. */
7419 if ((s1->path == s1->fname || s2->path == s2->fname))
7420 return (s2->path == s2->fname) - (s1->path == s1->fname);
7422 cp1 = (unsigned char *) s1->path;
7423 cp2 = (unsigned char *) s2->path;
7425 while (1)
7427 ++cp1;
7428 ++cp2;
7429 /* Reached the end of the first path? If so, handle like above. */
7430 if ((cp1 == (unsigned char *) s1->fname)
7431 || (cp2 == (unsigned char *) s2->fname))
7432 return ((cp2 == (unsigned char *) s2->fname)
7433 - (cp1 == (unsigned char *) s1->fname));
7435 /* Character of current path component the same? */
7436 else if (*cp1 != *cp2)
7437 return *cp1 - *cp2;
7441 /* Output the directory table and the file name table. We try to minimize
7442 the total amount of memory needed. A heuristic is used to avoid large
7443 slowdowns with many input files. */
7445 static void
7446 output_file_names (void)
7448 struct file_info *files;
7449 struct dir_info *dirs;
7450 int *saved;
7451 int *savehere;
7452 int *backmap;
7453 size_t ndirs;
7454 int idx_offset;
7455 size_t i;
7456 int idx;
7458 /* Handle the case where file_table is empty. */
7459 if (VARRAY_ACTIVE_SIZE (file_table) <= 1)
7461 dw2_asm_output_data (1, 0, "End directory table");
7462 dw2_asm_output_data (1, 0, "End file name table");
7463 return;
7466 /* Allocate the various arrays we need. */
7467 files = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct file_info));
7468 dirs = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct dir_info));
7470 /* Sort the file names. */
7471 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7473 char *f;
7475 /* Skip all leading "./". */
7476 f = VARRAY_CHAR_PTR (file_table, i);
7477 while (f[0] == '.' && f[1] == '/')
7478 f += 2;
7480 /* Create a new array entry. */
7481 files[i].path = f;
7482 files[i].length = strlen (f);
7483 files[i].file_idx = i;
7485 /* Search for the file name part. */
7486 f = strrchr (f, '/');
7487 files[i].fname = f == NULL ? files[i].path : f + 1;
7490 qsort (files + 1, VARRAY_ACTIVE_SIZE (file_table) - 1,
7491 sizeof (files[0]), file_info_cmp);
7493 /* Find all the different directories used. */
7494 dirs[0].path = files[1].path;
7495 dirs[0].length = files[1].fname - files[1].path;
7496 dirs[0].prefix = -1;
7497 dirs[0].count = 1;
7498 dirs[0].dir_idx = 0;
7499 dirs[0].used = 0;
7500 files[1].dir_idx = 0;
7501 ndirs = 1;
7503 for (i = 2; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7504 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7505 && memcmp (dirs[ndirs - 1].path, files[i].path,
7506 dirs[ndirs - 1].length) == 0)
7508 /* Same directory as last entry. */
7509 files[i].dir_idx = ndirs - 1;
7510 ++dirs[ndirs - 1].count;
7512 else
7514 size_t j;
7516 /* This is a new directory. */
7517 dirs[ndirs].path = files[i].path;
7518 dirs[ndirs].length = files[i].fname - files[i].path;
7519 dirs[ndirs].count = 1;
7520 dirs[ndirs].dir_idx = ndirs;
7521 dirs[ndirs].used = 0;
7522 files[i].dir_idx = ndirs;
7524 /* Search for a prefix. */
7525 dirs[ndirs].prefix = -1;
7526 for (j = 0; j < ndirs; j++)
7527 if (dirs[j].length < dirs[ndirs].length
7528 && dirs[j].length > 1
7529 && (dirs[ndirs].prefix == -1
7530 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7531 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7532 dirs[ndirs].prefix = j;
7534 ++ndirs;
7537 /* Now to the actual work. We have to find a subset of the directories which
7538 allow expressing the file name using references to the directory table
7539 with the least amount of characters. We do not do an exhaustive search
7540 where we would have to check out every combination of every single
7541 possible prefix. Instead we use a heuristic which provides nearly optimal
7542 results in most cases and never is much off. */
7543 saved = alloca (ndirs * sizeof (int));
7544 savehere = alloca (ndirs * sizeof (int));
7546 memset (saved, '\0', ndirs * sizeof (saved[0]));
7547 for (i = 0; i < ndirs; i++)
7549 size_t j;
7550 int total;
7552 /* We can always save some space for the current directory. But this
7553 does not mean it will be enough to justify adding the directory. */
7554 savehere[i] = dirs[i].length;
7555 total = (savehere[i] - saved[i]) * dirs[i].count;
7557 for (j = i + 1; j < ndirs; j++)
7559 savehere[j] = 0;
7560 if (saved[j] < dirs[i].length)
7562 /* Determine whether the dirs[i] path is a prefix of the
7563 dirs[j] path. */
7564 int k;
7566 k = dirs[j].prefix;
7567 while (k != -1 && k != (int) i)
7568 k = dirs[k].prefix;
7570 if (k == (int) i)
7572 /* Yes it is. We can possibly safe some memory but
7573 writing the filenames in dirs[j] relative to
7574 dirs[i]. */
7575 savehere[j] = dirs[i].length;
7576 total += (savehere[j] - saved[j]) * dirs[j].count;
7581 /* Check whether we can safe enough to justify adding the dirs[i]
7582 directory. */
7583 if (total > dirs[i].length + 1)
7585 /* It's worthwhile adding. */
7586 for (j = i; j < ndirs; j++)
7587 if (savehere[j] > 0)
7589 /* Remember how much we saved for this directory so far. */
7590 saved[j] = savehere[j];
7592 /* Remember the prefix directory. */
7593 dirs[j].dir_idx = i;
7598 /* We have to emit them in the order they appear in the file_table array
7599 since the index is used in the debug info generation. To do this
7600 efficiently we generate a back-mapping of the indices first. */
7601 backmap = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (int));
7602 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7604 backmap[files[i].file_idx] = i;
7606 /* Mark this directory as used. */
7607 dirs[dirs[files[i].dir_idx].dir_idx].used = 1;
7610 /* That was it. We are ready to emit the information. First emit the
7611 directory name table. We have to make sure the first actually emitted
7612 directory name has index one; zero is reserved for the current working
7613 directory. Make sure we do not confuse these indices with the one for the
7614 constructed table (even though most of the time they are identical). */
7615 idx = 1;
7616 idx_offset = dirs[0].length > 0 ? 1 : 0;
7617 for (i = 1 - idx_offset; i < ndirs; i++)
7618 if (dirs[i].used != 0)
7620 dirs[i].used = idx++;
7621 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7622 "Directory Entry: 0x%x", dirs[i].used);
7625 dw2_asm_output_data (1, 0, "End directory table");
7627 /* Correct the index for the current working directory entry if it
7628 exists. */
7629 if (idx_offset == 0)
7630 dirs[0].used = 0;
7632 /* Now write all the file names. */
7633 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7635 int file_idx = backmap[i];
7636 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7638 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7639 "File Entry: 0x%lx", (unsigned long) i);
7641 /* Include directory index. */
7642 dw2_asm_output_data_uleb128 (dirs[dir_idx].used, NULL);
7644 /* Modification time. */
7645 dw2_asm_output_data_uleb128 (0, NULL);
7647 /* File length in bytes. */
7648 dw2_asm_output_data_uleb128 (0, NULL);
7651 dw2_asm_output_data (1, 0, "End file name table");
7655 /* Output the source line number correspondence information. This
7656 information goes into the .debug_line section. */
7658 static void
7659 output_line_info (void)
7661 char l1[20], l2[20], p1[20], p2[20];
7662 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7663 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7664 unsigned opc;
7665 unsigned n_op_args;
7666 unsigned long lt_index;
7667 unsigned long current_line;
7668 long line_offset;
7669 long line_delta;
7670 unsigned long current_file;
7671 unsigned long function;
7673 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7674 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7675 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7676 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7678 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7679 dw2_asm_output_data (4, 0xffffffff,
7680 "Initial length escape value indicating 64-bit DWARF extension");
7681 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7682 "Length of Source Line Info");
7683 ASM_OUTPUT_LABEL (asm_out_file, l1);
7685 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7686 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7687 ASM_OUTPUT_LABEL (asm_out_file, p1);
7689 /* Define the architecture-dependent minimum instruction length (in
7690 bytes). In this implementation of DWARF, this field is used for
7691 information purposes only. Since GCC generates assembly language,
7692 we have no a priori knowledge of how many instruction bytes are
7693 generated for each source line, and therefore can use only the
7694 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7695 commands. Accordingly, we fix this as `1', which is "correct
7696 enough" for all architectures, and don't let the target override. */
7697 dw2_asm_output_data (1, 1,
7698 "Minimum Instruction Length");
7700 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7701 "Default is_stmt_start flag");
7702 dw2_asm_output_data (1, DWARF_LINE_BASE,
7703 "Line Base Value (Special Opcodes)");
7704 dw2_asm_output_data (1, DWARF_LINE_RANGE,
7705 "Line Range Value (Special Opcodes)");
7706 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7707 "Special Opcode Base");
7709 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7711 switch (opc)
7713 case DW_LNS_advance_pc:
7714 case DW_LNS_advance_line:
7715 case DW_LNS_set_file:
7716 case DW_LNS_set_column:
7717 case DW_LNS_fixed_advance_pc:
7718 n_op_args = 1;
7719 break;
7720 default:
7721 n_op_args = 0;
7722 break;
7725 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
7726 opc, n_op_args);
7729 /* Write out the information about the files we use. */
7730 output_file_names ();
7731 ASM_OUTPUT_LABEL (asm_out_file, p2);
7733 /* We used to set the address register to the first location in the text
7734 section here, but that didn't accomplish anything since we already
7735 have a line note for the opening brace of the first function. */
7737 /* Generate the line number to PC correspondence table, encoded as
7738 a series of state machine operations. */
7739 current_file = 1;
7740 current_line = 1;
7742 if (cfun
7743 && (last_text_section == in_unlikely_executed_text
7744 || (last_text_section == in_named
7745 && last_text_section_name == cfun->unlikely_text_section_name)))
7746 strcpy (prev_line_label, cfun->cold_section_label);
7747 else
7748 strcpy (prev_line_label, text_section_label);
7749 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
7751 dw_line_info_ref line_info = &line_info_table[lt_index];
7753 #if 0
7754 /* Disable this optimization for now; GDB wants to see two line notes
7755 at the beginning of a function so it can find the end of the
7756 prologue. */
7758 /* Don't emit anything for redundant notes. Just updating the
7759 address doesn't accomplish anything, because we already assume
7760 that anything after the last address is this line. */
7761 if (line_info->dw_line_num == current_line
7762 && line_info->dw_file_num == current_file)
7763 continue;
7764 #endif
7766 /* Emit debug info for the address of the current line.
7768 Unfortunately, we have little choice here currently, and must always
7769 use the most general form. GCC does not know the address delta
7770 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
7771 attributes which will give an upper bound on the address range. We
7772 could perhaps use length attributes to determine when it is safe to
7773 use DW_LNS_fixed_advance_pc. */
7775 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
7776 if (0)
7778 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
7779 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7780 "DW_LNS_fixed_advance_pc");
7781 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7783 else
7785 /* This can handle any delta. This takes
7786 4+DWARF2_ADDR_SIZE bytes. */
7787 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7788 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7789 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7790 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7793 strcpy (prev_line_label, line_label);
7795 /* Emit debug info for the source file of the current line, if
7796 different from the previous line. */
7797 if (line_info->dw_file_num != current_file)
7799 current_file = line_info->dw_file_num;
7800 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7801 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7802 VARRAY_CHAR_PTR (file_table,
7803 current_file));
7806 /* Emit debug info for the current line number, choosing the encoding
7807 that uses the least amount of space. */
7808 if (line_info->dw_line_num != current_line)
7810 line_offset = line_info->dw_line_num - current_line;
7811 line_delta = line_offset - DWARF_LINE_BASE;
7812 current_line = line_info->dw_line_num;
7813 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7814 /* This can handle deltas from -10 to 234, using the current
7815 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
7816 takes 1 byte. */
7817 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7818 "line %lu", current_line);
7819 else
7821 /* This can handle any delta. This takes at least 4 bytes,
7822 depending on the value being encoded. */
7823 dw2_asm_output_data (1, DW_LNS_advance_line,
7824 "advance to line %lu", current_line);
7825 dw2_asm_output_data_sleb128 (line_offset, NULL);
7826 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7829 else
7830 /* We still need to start a new row, so output a copy insn. */
7831 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7834 /* Emit debug info for the address of the end of the function. */
7835 if (0)
7837 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7838 "DW_LNS_fixed_advance_pc");
7839 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
7841 else
7843 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7844 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7845 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7846 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
7849 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7850 dw2_asm_output_data_uleb128 (1, NULL);
7851 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7853 function = 0;
7854 current_file = 1;
7855 current_line = 1;
7856 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
7858 dw_separate_line_info_ref line_info
7859 = &separate_line_info_table[lt_index];
7861 #if 0
7862 /* Don't emit anything for redundant notes. */
7863 if (line_info->dw_line_num == current_line
7864 && line_info->dw_file_num == current_file
7865 && line_info->function == function)
7866 goto cont;
7867 #endif
7869 /* Emit debug info for the address of the current line. If this is
7870 a new function, or the first line of a function, then we need
7871 to handle it differently. */
7872 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
7873 lt_index);
7874 if (function != line_info->function)
7876 function = line_info->function;
7878 /* Set the address register to the first line in the function. */
7879 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7880 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7881 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7882 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7884 else
7886 /* ??? See the DW_LNS_advance_pc comment above. */
7887 if (0)
7889 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7890 "DW_LNS_fixed_advance_pc");
7891 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7893 else
7895 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7896 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7897 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7898 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7902 strcpy (prev_line_label, line_label);
7904 /* Emit debug info for the source file of the current line, if
7905 different from the previous line. */
7906 if (line_info->dw_file_num != current_file)
7908 current_file = line_info->dw_file_num;
7909 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7910 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7911 VARRAY_CHAR_PTR (file_table,
7912 current_file));
7915 /* Emit debug info for the current line number, choosing the encoding
7916 that uses the least amount of space. */
7917 if (line_info->dw_line_num != current_line)
7919 line_offset = line_info->dw_line_num - current_line;
7920 line_delta = line_offset - DWARF_LINE_BASE;
7921 current_line = line_info->dw_line_num;
7922 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7923 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7924 "line %lu", current_line);
7925 else
7927 dw2_asm_output_data (1, DW_LNS_advance_line,
7928 "advance to line %lu", current_line);
7929 dw2_asm_output_data_sleb128 (line_offset, NULL);
7930 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7933 else
7934 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7936 #if 0
7937 cont:
7938 #endif
7940 lt_index++;
7942 /* If we're done with a function, end its sequence. */
7943 if (lt_index == separate_line_info_table_in_use
7944 || separate_line_info_table[lt_index].function != function)
7946 current_file = 1;
7947 current_line = 1;
7949 /* Emit debug info for the address of the end of the function. */
7950 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
7951 if (0)
7953 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7954 "DW_LNS_fixed_advance_pc");
7955 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7957 else
7959 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7960 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7961 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7962 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7965 /* Output the marker for the end of this sequence. */
7966 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7967 dw2_asm_output_data_uleb128 (1, NULL);
7968 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7972 /* Output the marker for the end of the line number info. */
7973 ASM_OUTPUT_LABEL (asm_out_file, l2);
7976 /* Given a pointer to a tree node for some base type, return a pointer to
7977 a DIE that describes the given type.
7979 This routine must only be called for GCC type nodes that correspond to
7980 Dwarf base (fundamental) types. */
7982 static dw_die_ref
7983 base_type_die (tree type)
7985 dw_die_ref base_type_result;
7986 const char *type_name;
7987 enum dwarf_type encoding;
7988 tree name = TYPE_NAME (type);
7990 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
7991 return 0;
7993 if (name)
7995 if (TREE_CODE (name) == TYPE_DECL)
7996 name = DECL_NAME (name);
7998 type_name = IDENTIFIER_POINTER (name);
8000 else
8001 type_name = "__unknown__";
8003 switch (TREE_CODE (type))
8005 case INTEGER_TYPE:
8006 /* Carefully distinguish the C character types, without messing
8007 up if the language is not C. Note that we check only for the names
8008 that contain spaces; other names might occur by coincidence in other
8009 languages. */
8010 if (! (TYPE_PRECISION (type) == CHAR_TYPE_SIZE
8011 && (type == char_type_node
8012 || ! strcmp (type_name, "signed char")
8013 || ! strcmp (type_name, "unsigned char"))))
8015 if (TYPE_UNSIGNED (type))
8016 encoding = DW_ATE_unsigned;
8017 else
8018 encoding = DW_ATE_signed;
8019 break;
8021 /* else fall through. */
8023 case CHAR_TYPE:
8024 /* GNU Pascal/Ada CHAR type. Not used in C. */
8025 if (TYPE_UNSIGNED (type))
8026 encoding = DW_ATE_unsigned_char;
8027 else
8028 encoding = DW_ATE_signed_char;
8029 break;
8031 case REAL_TYPE:
8032 encoding = DW_ATE_float;
8033 break;
8035 /* Dwarf2 doesn't know anything about complex ints, so use
8036 a user defined type for it. */
8037 case COMPLEX_TYPE:
8038 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8039 encoding = DW_ATE_complex_float;
8040 else
8041 encoding = DW_ATE_lo_user;
8042 break;
8044 case BOOLEAN_TYPE:
8045 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
8046 encoding = DW_ATE_boolean;
8047 break;
8049 default:
8050 /* No other TREE_CODEs are Dwarf fundamental types. */
8051 gcc_unreachable ();
8054 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8055 if (demangle_name_func)
8056 type_name = (*demangle_name_func) (type_name);
8058 add_AT_string (base_type_result, DW_AT_name, type_name);
8059 add_AT_unsigned (base_type_result, DW_AT_byte_size,
8060 int_size_in_bytes (type));
8061 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8063 return base_type_result;
8066 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
8067 the Dwarf "root" type for the given input type. The Dwarf "root" type of
8068 a given type is generally the same as the given type, except that if the
8069 given type is a pointer or reference type, then the root type of the given
8070 type is the root type of the "basis" type for the pointer or reference
8071 type. (This definition of the "root" type is recursive.) Also, the root
8072 type of a `const' qualified type or a `volatile' qualified type is the
8073 root type of the given type without the qualifiers. */
8075 static tree
8076 root_type (tree type)
8078 if (TREE_CODE (type) == ERROR_MARK)
8079 return error_mark_node;
8081 switch (TREE_CODE (type))
8083 case ERROR_MARK:
8084 return error_mark_node;
8086 case POINTER_TYPE:
8087 case REFERENCE_TYPE:
8088 return type_main_variant (root_type (TREE_TYPE (type)));
8090 default:
8091 return type_main_variant (type);
8095 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8096 given input type is a Dwarf "fundamental" type. Otherwise return null. */
8098 static inline int
8099 is_base_type (tree type)
8101 switch (TREE_CODE (type))
8103 case ERROR_MARK:
8104 case VOID_TYPE:
8105 case INTEGER_TYPE:
8106 case REAL_TYPE:
8107 case COMPLEX_TYPE:
8108 case BOOLEAN_TYPE:
8109 case CHAR_TYPE:
8110 return 1;
8112 case ARRAY_TYPE:
8113 case RECORD_TYPE:
8114 case UNION_TYPE:
8115 case QUAL_UNION_TYPE:
8116 case ENUMERAL_TYPE:
8117 case FUNCTION_TYPE:
8118 case METHOD_TYPE:
8119 case POINTER_TYPE:
8120 case REFERENCE_TYPE:
8121 case OFFSET_TYPE:
8122 case LANG_TYPE:
8123 case VECTOR_TYPE:
8124 return 0;
8126 default:
8127 gcc_unreachable ();
8130 return 0;
8133 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8134 node, return the size in bits for the type if it is a constant, or else
8135 return the alignment for the type if the type's size is not constant, or
8136 else return BITS_PER_WORD if the type actually turns out to be an
8137 ERROR_MARK node. */
8139 static inline unsigned HOST_WIDE_INT
8140 simple_type_size_in_bits (tree type)
8142 if (TREE_CODE (type) == ERROR_MARK)
8143 return BITS_PER_WORD;
8144 else if (TYPE_SIZE (type) == NULL_TREE)
8145 return 0;
8146 else if (host_integerp (TYPE_SIZE (type), 1))
8147 return tree_low_cst (TYPE_SIZE (type), 1);
8148 else
8149 return TYPE_ALIGN (type);
8152 /* Return true if the debug information for the given type should be
8153 emitted as a subrange type. */
8155 static inline bool
8156 is_subrange_type (tree type)
8158 tree subtype = TREE_TYPE (type);
8160 /* Subrange types are identified by the fact that they are integer
8161 types, and that they have a subtype which is either an integer type
8162 or an enumeral type. */
8164 if (TREE_CODE (type) != INTEGER_TYPE
8165 || subtype == NULL_TREE)
8166 return false;
8168 if (TREE_CODE (subtype) != INTEGER_TYPE
8169 && TREE_CODE (subtype) != ENUMERAL_TYPE)
8170 return false;
8172 if (TREE_CODE (type) == TREE_CODE (subtype)
8173 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8174 && TYPE_MIN_VALUE (type) != NULL
8175 && TYPE_MIN_VALUE (subtype) != NULL
8176 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8177 && TYPE_MAX_VALUE (type) != NULL
8178 && TYPE_MAX_VALUE (subtype) != NULL
8179 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8181 /* The type and its subtype have the same representation. If in
8182 addition the two types also have the same name, then the given
8183 type is not a subrange type, but rather a plain base type. */
8184 /* FIXME: brobecker/2004-03-22:
8185 Sizetype INTEGER_CSTs nodes are canonicalized. It should
8186 therefore be sufficient to check the TYPE_SIZE node pointers
8187 rather than checking the actual size. Unfortunately, we have
8188 found some cases, such as in the Ada "integer" type, where
8189 this is not the case. Until this problem is solved, we need to
8190 keep checking the actual size. */
8191 tree type_name = TYPE_NAME (type);
8192 tree subtype_name = TYPE_NAME (subtype);
8194 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8195 type_name = DECL_NAME (type_name);
8197 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8198 subtype_name = DECL_NAME (subtype_name);
8200 if (type_name == subtype_name)
8201 return false;
8204 return true;
8207 /* Given a pointer to a tree node for a subrange type, return a pointer
8208 to a DIE that describes the given type. */
8210 static dw_die_ref
8211 subrange_type_die (tree type, dw_die_ref context_die)
8213 dw_die_ref subtype_die;
8214 dw_die_ref subrange_die;
8215 tree name = TYPE_NAME (type);
8216 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8217 tree subtype = TREE_TYPE (type);
8219 if (context_die == NULL)
8220 context_die = comp_unit_die;
8222 if (TREE_CODE (subtype) == ENUMERAL_TYPE)
8223 subtype_die = gen_enumeration_type_die (subtype, context_die);
8224 else
8225 subtype_die = base_type_die (subtype);
8227 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8229 if (name != NULL)
8231 if (TREE_CODE (name) == TYPE_DECL)
8232 name = DECL_NAME (name);
8233 add_name_attribute (subrange_die, IDENTIFIER_POINTER (name));
8236 if (int_size_in_bytes (subtype) != size_in_bytes)
8238 /* The size of the subrange type and its base type do not match,
8239 so we need to generate a size attribute for the subrange type. */
8240 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8243 if (TYPE_MIN_VALUE (type) != NULL)
8244 add_bound_info (subrange_die, DW_AT_lower_bound,
8245 TYPE_MIN_VALUE (type));
8246 if (TYPE_MAX_VALUE (type) != NULL)
8247 add_bound_info (subrange_die, DW_AT_upper_bound,
8248 TYPE_MAX_VALUE (type));
8249 add_AT_die_ref (subrange_die, DW_AT_type, subtype_die);
8251 return subrange_die;
8254 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8255 entry that chains various modifiers in front of the given type. */
8257 static dw_die_ref
8258 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8259 dw_die_ref context_die)
8261 enum tree_code code = TREE_CODE (type);
8262 dw_die_ref mod_type_die = NULL;
8263 dw_die_ref sub_die = NULL;
8264 tree item_type = NULL;
8266 if (code != ERROR_MARK)
8268 tree qualified_type;
8270 /* See if we already have the appropriately qualified variant of
8271 this type. */
8272 qualified_type
8273 = get_qualified_type (type,
8274 ((is_const_type ? TYPE_QUAL_CONST : 0)
8275 | (is_volatile_type
8276 ? TYPE_QUAL_VOLATILE : 0)));
8278 /* If we do, then we can just use its DIE, if it exists. */
8279 if (qualified_type)
8281 mod_type_die = lookup_type_die (qualified_type);
8282 if (mod_type_die)
8283 return mod_type_die;
8286 /* Handle C typedef types. */
8287 if (qualified_type && TYPE_NAME (qualified_type)
8288 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL
8289 && DECL_ORIGINAL_TYPE (TYPE_NAME (qualified_type)))
8291 tree type_name = TYPE_NAME (qualified_type);
8292 tree dtype = TREE_TYPE (type_name);
8294 if (qualified_type == dtype)
8296 /* For a named type, use the typedef. */
8297 gen_type_die (qualified_type, context_die);
8298 mod_type_die = lookup_type_die (qualified_type);
8300 else if (is_const_type < TYPE_READONLY (dtype)
8301 || is_volatile_type < TYPE_VOLATILE (dtype))
8302 /* cv-unqualified version of named type. Just use the unnamed
8303 type to which it refers. */
8304 mod_type_die
8305 = modified_type_die (DECL_ORIGINAL_TYPE (type_name),
8306 is_const_type, is_volatile_type,
8307 context_die);
8309 /* Else cv-qualified version of named type; fall through. */
8312 if (mod_type_die)
8313 /* OK. */
8315 else if (is_const_type)
8317 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8318 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8320 else if (is_volatile_type)
8322 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8323 sub_die = modified_type_die (type, 0, 0, context_die);
8325 else if (code == POINTER_TYPE)
8327 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8328 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8329 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8330 #if 0
8331 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8332 #endif
8333 item_type = TREE_TYPE (type);
8335 else if (code == REFERENCE_TYPE)
8337 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8338 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8339 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8340 #if 0
8341 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8342 #endif
8343 item_type = TREE_TYPE (type);
8345 else if (is_subrange_type (type))
8346 mod_type_die = subrange_type_die (type, context_die);
8347 else if (is_base_type (type))
8348 mod_type_die = base_type_die (type);
8349 else
8351 gen_type_die (type, context_die);
8353 /* We have to get the type_main_variant here (and pass that to the
8354 `lookup_type_die' routine) because the ..._TYPE node we have
8355 might simply be a *copy* of some original type node (where the
8356 copy was created to help us keep track of typedef names) and
8357 that copy might have a different TYPE_UID from the original
8358 ..._TYPE node. */
8359 if (TREE_CODE (type) != VECTOR_TYPE)
8360 mod_type_die = lookup_type_die (type_main_variant (type));
8361 else
8362 /* Vectors have the debugging information in the type,
8363 not the main variant. */
8364 mod_type_die = lookup_type_die (type);
8365 gcc_assert (mod_type_die);
8368 /* We want to equate the qualified type to the die below. */
8369 type = qualified_type;
8372 if (type)
8373 equate_type_number_to_die (type, mod_type_die);
8374 if (item_type)
8375 /* We must do this after the equate_type_number_to_die call, in case
8376 this is a recursive type. This ensures that the modified_type_die
8377 recursion will terminate even if the type is recursive. Recursive
8378 types are possible in Ada. */
8379 sub_die = modified_type_die (item_type,
8380 TYPE_READONLY (item_type),
8381 TYPE_VOLATILE (item_type),
8382 context_die);
8384 if (sub_die != NULL)
8385 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8387 return mod_type_die;
8390 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8391 an enumerated type. */
8393 static inline int
8394 type_is_enum (tree type)
8396 return TREE_CODE (type) == ENUMERAL_TYPE;
8399 /* Return the DBX register number described by a given RTL node. */
8401 static unsigned int
8402 dbx_reg_number (rtx rtl)
8404 unsigned regno = REGNO (rtl);
8406 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8408 return DBX_REGISTER_NUMBER (regno);
8411 /* Return a location descriptor that designates a machine register or
8412 zero if there is none. */
8414 static dw_loc_descr_ref
8415 reg_loc_descriptor (rtx rtl)
8417 unsigned reg;
8418 rtx regs;
8420 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8421 return 0;
8423 reg = dbx_reg_number (rtl);
8424 regs = targetm.dwarf_register_span (rtl);
8426 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1
8427 || regs)
8428 return multiple_reg_loc_descriptor (rtl, regs);
8429 else
8430 return one_reg_loc_descriptor (reg);
8433 /* Return a location descriptor that designates a machine register for
8434 a given hard register number. */
8436 static dw_loc_descr_ref
8437 one_reg_loc_descriptor (unsigned int regno)
8439 if (regno <= 31)
8440 return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8441 else
8442 return new_loc_descr (DW_OP_regx, regno, 0);
8445 /* Given an RTL of a register, return a location descriptor that
8446 designates a value that spans more than one register. */
8448 static dw_loc_descr_ref
8449 multiple_reg_loc_descriptor (rtx rtl, rtx regs)
8451 int nregs, size, i;
8452 unsigned reg;
8453 dw_loc_descr_ref loc_result = NULL;
8455 reg = dbx_reg_number (rtl);
8456 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8458 /* Simple, contiguous registers. */
8459 if (regs == NULL_RTX)
8461 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8463 loc_result = NULL;
8464 while (nregs--)
8466 dw_loc_descr_ref t;
8468 t = one_reg_loc_descriptor (reg);
8469 add_loc_descr (&loc_result, t);
8470 add_loc_descr (&loc_result, new_loc_descr (DW_OP_piece, size, 0));
8471 ++reg;
8473 return loc_result;
8476 /* Now onto stupid register sets in non contiguous locations. */
8478 gcc_assert (GET_CODE (regs) == PARALLEL);
8480 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8481 loc_result = NULL;
8483 for (i = 0; i < XVECLEN (regs, 0); ++i)
8485 dw_loc_descr_ref t;
8487 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8488 add_loc_descr (&loc_result, t);
8489 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8490 add_loc_descr (&loc_result, new_loc_descr (DW_OP_piece, size, 0));
8492 return loc_result;
8495 /* Return a location descriptor that designates a constant. */
8497 static dw_loc_descr_ref
8498 int_loc_descriptor (HOST_WIDE_INT i)
8500 enum dwarf_location_atom op;
8502 /* Pick the smallest representation of a constant, rather than just
8503 defaulting to the LEB encoding. */
8504 if (i >= 0)
8506 if (i <= 31)
8507 op = DW_OP_lit0 + i;
8508 else if (i <= 0xff)
8509 op = DW_OP_const1u;
8510 else if (i <= 0xffff)
8511 op = DW_OP_const2u;
8512 else if (HOST_BITS_PER_WIDE_INT == 32
8513 || i <= 0xffffffff)
8514 op = DW_OP_const4u;
8515 else
8516 op = DW_OP_constu;
8518 else
8520 if (i >= -0x80)
8521 op = DW_OP_const1s;
8522 else if (i >= -0x8000)
8523 op = DW_OP_const2s;
8524 else if (HOST_BITS_PER_WIDE_INT == 32
8525 || i >= -0x80000000)
8526 op = DW_OP_const4s;
8527 else
8528 op = DW_OP_consts;
8531 return new_loc_descr (op, i, 0);
8534 /* Return a location descriptor that designates a base+offset location. */
8536 static dw_loc_descr_ref
8537 based_loc_descr (unsigned int reg, HOST_WIDE_INT offset, bool can_use_fbreg)
8539 dw_loc_descr_ref loc_result;
8540 /* For the "frame base", we use the frame pointer or stack pointer
8541 registers, since the RTL for local variables is relative to one of
8542 them. */
8543 unsigned fp_reg = DBX_REGISTER_NUMBER (frame_pointer_needed
8544 ? HARD_FRAME_POINTER_REGNUM
8545 : STACK_POINTER_REGNUM);
8547 if (reg == fp_reg && can_use_fbreg)
8548 loc_result = new_loc_descr (DW_OP_fbreg, offset, 0);
8549 else if (reg <= 31)
8550 loc_result = new_loc_descr (DW_OP_breg0 + reg, offset, 0);
8551 else
8552 loc_result = new_loc_descr (DW_OP_bregx, reg, offset);
8554 return loc_result;
8557 /* Return true if this RTL expression describes a base+offset calculation. */
8559 static inline int
8560 is_based_loc (rtx rtl)
8562 return (GET_CODE (rtl) == PLUS
8563 && ((REG_P (XEXP (rtl, 0))
8564 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8565 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
8568 /* The following routine converts the RTL for a variable or parameter
8569 (resident in memory) into an equivalent Dwarf representation of a
8570 mechanism for getting the address of that same variable onto the top of a
8571 hypothetical "address evaluation" stack.
8573 When creating memory location descriptors, we are effectively transforming
8574 the RTL for a memory-resident object into its Dwarf postfix expression
8575 equivalent. This routine recursively descends an RTL tree, turning
8576 it into Dwarf postfix code as it goes.
8578 MODE is the mode of the memory reference, needed to handle some
8579 autoincrement addressing modes.
8581 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the location
8582 list for RTL. We can't use it when we are emitting location list for
8583 virtual variable frame_base_decl (i.e. a location list for DW_AT_frame_base)
8584 which describes how frame base changes when !frame_pointer_needed.
8586 Return 0 if we can't represent the location. */
8588 static dw_loc_descr_ref
8589 mem_loc_descriptor (rtx rtl, enum machine_mode mode, bool can_use_fbreg)
8591 dw_loc_descr_ref mem_loc_result = NULL;
8592 enum dwarf_location_atom op;
8594 /* Note that for a dynamically sized array, the location we will generate a
8595 description of here will be the lowest numbered location which is
8596 actually within the array. That's *not* necessarily the same as the
8597 zeroth element of the array. */
8599 rtl = targetm.delegitimize_address (rtl);
8601 switch (GET_CODE (rtl))
8603 case POST_INC:
8604 case POST_DEC:
8605 case POST_MODIFY:
8606 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
8607 just fall into the SUBREG code. */
8609 /* ... fall through ... */
8611 case SUBREG:
8612 /* The case of a subreg may arise when we have a local (register)
8613 variable or a formal (register) parameter which doesn't quite fill
8614 up an entire register. For now, just assume that it is
8615 legitimate to make the Dwarf info refer to the whole register which
8616 contains the given subreg. */
8617 rtl = SUBREG_REG (rtl);
8619 /* ... fall through ... */
8621 case REG:
8622 /* Whenever a register number forms a part of the description of the
8623 method for calculating the (dynamic) address of a memory resident
8624 object, DWARF rules require the register number be referred to as
8625 a "base register". This distinction is not based in any way upon
8626 what category of register the hardware believes the given register
8627 belongs to. This is strictly DWARF terminology we're dealing with
8628 here. Note that in cases where the location of a memory-resident
8629 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8630 OP_CONST (0)) the actual DWARF location descriptor that we generate
8631 may just be OP_BASEREG (basereg). This may look deceptively like
8632 the object in question was allocated to a register (rather than in
8633 memory) so DWARF consumers need to be aware of the subtle
8634 distinction between OP_REG and OP_BASEREG. */
8635 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8636 mem_loc_result = based_loc_descr (dbx_reg_number (rtl), 0,
8637 can_use_fbreg);
8638 break;
8640 case MEM:
8641 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
8642 can_use_fbreg);
8643 if (mem_loc_result != 0)
8644 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
8645 break;
8647 case LO_SUM:
8648 rtl = XEXP (rtl, 1);
8650 /* ... fall through ... */
8652 case LABEL_REF:
8653 /* Some ports can transform a symbol ref into a label ref, because
8654 the symbol ref is too far away and has to be dumped into a constant
8655 pool. */
8656 case CONST:
8657 case SYMBOL_REF:
8658 /* Alternatively, the symbol in the constant pool might be referenced
8659 by a different symbol. */
8660 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
8662 bool marked;
8663 rtx tmp = get_pool_constant_mark (rtl, &marked);
8665 if (GET_CODE (tmp) == SYMBOL_REF)
8667 rtl = tmp;
8668 if (CONSTANT_POOL_ADDRESS_P (tmp))
8669 get_pool_constant_mark (tmp, &marked);
8670 else
8671 marked = true;
8674 /* If all references to this pool constant were optimized away,
8675 it was not output and thus we can't represent it.
8676 FIXME: might try to use DW_OP_const_value here, though
8677 DW_OP_piece complicates it. */
8678 if (!marked)
8679 return 0;
8682 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8683 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
8684 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8685 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
8686 break;
8688 case PRE_MODIFY:
8689 /* Extract the PLUS expression nested inside and fall into
8690 PLUS code below. */
8691 rtl = XEXP (rtl, 1);
8692 goto plus;
8694 case PRE_INC:
8695 case PRE_DEC:
8696 /* Turn these into a PLUS expression and fall into the PLUS code
8697 below. */
8698 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8699 GEN_INT (GET_CODE (rtl) == PRE_INC
8700 ? GET_MODE_UNIT_SIZE (mode)
8701 : -GET_MODE_UNIT_SIZE (mode)));
8703 /* ... fall through ... */
8705 case PLUS:
8706 plus:
8707 if (is_based_loc (rtl))
8708 mem_loc_result = based_loc_descr (dbx_reg_number (XEXP (rtl, 0)),
8709 INTVAL (XEXP (rtl, 1)),
8710 can_use_fbreg);
8711 else
8713 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
8714 can_use_fbreg);
8715 if (mem_loc_result == 0)
8716 break;
8718 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8719 && INTVAL (XEXP (rtl, 1)) >= 0)
8720 add_loc_descr (&mem_loc_result,
8721 new_loc_descr (DW_OP_plus_uconst,
8722 INTVAL (XEXP (rtl, 1)), 0));
8723 else
8725 add_loc_descr (&mem_loc_result,
8726 mem_loc_descriptor (XEXP (rtl, 1), mode,
8727 can_use_fbreg));
8728 add_loc_descr (&mem_loc_result,
8729 new_loc_descr (DW_OP_plus, 0, 0));
8732 break;
8734 /* If a pseudo-reg is optimized away, it is possible for it to
8735 be replaced with a MEM containing a multiply or shift. */
8736 case MULT:
8737 op = DW_OP_mul;
8738 goto do_binop;
8740 case ASHIFT:
8741 op = DW_OP_shl;
8742 goto do_binop;
8744 case ASHIFTRT:
8745 op = DW_OP_shra;
8746 goto do_binop;
8748 case LSHIFTRT:
8749 op = DW_OP_shr;
8750 goto do_binop;
8752 do_binop:
8754 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
8755 can_use_fbreg);
8756 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
8757 can_use_fbreg);
8759 if (op0 == 0 || op1 == 0)
8760 break;
8762 mem_loc_result = op0;
8763 add_loc_descr (&mem_loc_result, op1);
8764 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
8765 break;
8768 case CONST_INT:
8769 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
8770 break;
8772 default:
8773 gcc_unreachable ();
8776 return mem_loc_result;
8779 /* Return a descriptor that describes the concatenation of two locations.
8780 This is typically a complex variable. */
8782 static dw_loc_descr_ref
8783 concat_loc_descriptor (rtx x0, rtx x1)
8785 dw_loc_descr_ref cc_loc_result = NULL;
8786 dw_loc_descr_ref x0_ref = loc_descriptor (x0, false);
8787 dw_loc_descr_ref x1_ref = loc_descriptor (x1, false);
8789 if (x0_ref == 0 || x1_ref == 0)
8790 return 0;
8792 cc_loc_result = x0_ref;
8793 add_loc_descr (&cc_loc_result,
8794 new_loc_descr (DW_OP_piece,
8795 GET_MODE_SIZE (GET_MODE (x0)), 0));
8797 add_loc_descr (&cc_loc_result, x1_ref);
8798 add_loc_descr (&cc_loc_result,
8799 new_loc_descr (DW_OP_piece,
8800 GET_MODE_SIZE (GET_MODE (x1)), 0));
8802 return cc_loc_result;
8805 /* Output a proper Dwarf location descriptor for a variable or parameter
8806 which is either allocated in a register or in a memory location. For a
8807 register, we just generate an OP_REG and the register number. For a
8808 memory location we provide a Dwarf postfix expression describing how to
8809 generate the (dynamic) address of the object onto the address stack.
8811 If we don't know how to describe it, return 0. */
8813 static dw_loc_descr_ref
8814 loc_descriptor (rtx rtl, bool can_use_fbreg)
8816 dw_loc_descr_ref loc_result = NULL;
8818 switch (GET_CODE (rtl))
8820 case SUBREG:
8821 /* The case of a subreg may arise when we have a local (register)
8822 variable or a formal (register) parameter which doesn't quite fill
8823 up an entire register. For now, just assume that it is
8824 legitimate to make the Dwarf info refer to the whole register which
8825 contains the given subreg. */
8826 rtl = SUBREG_REG (rtl);
8828 /* ... fall through ... */
8830 case REG:
8831 loc_result = reg_loc_descriptor (rtl);
8832 break;
8834 case MEM:
8835 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
8836 can_use_fbreg);
8837 break;
8839 case CONCAT:
8840 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
8841 break;
8843 case VAR_LOCATION:
8844 /* Single part. */
8845 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
8847 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), can_use_fbreg);
8848 break;
8851 rtl = XEXP (rtl, 1);
8852 /* FALLTHRU */
8854 case PARALLEL:
8856 rtvec par_elems = XVEC (rtl, 0);
8857 int num_elem = GET_NUM_ELEM (par_elems);
8858 enum machine_mode mode;
8859 int i;
8861 /* Create the first one, so we have something to add to. */
8862 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
8863 can_use_fbreg);
8864 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
8865 add_loc_descr (&loc_result,
8866 new_loc_descr (DW_OP_piece, GET_MODE_SIZE (mode), 0));
8867 for (i = 1; i < num_elem; i++)
8869 dw_loc_descr_ref temp;
8871 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
8872 can_use_fbreg);
8873 add_loc_descr (&loc_result, temp);
8874 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
8875 add_loc_descr (&loc_result,
8876 new_loc_descr (DW_OP_piece,
8877 GET_MODE_SIZE (mode), 0));
8880 break;
8882 default:
8883 gcc_unreachable ();
8886 return loc_result;
8889 /* Similar, but generate the descriptor from trees instead of rtl. This comes
8890 up particularly with variable length arrays. WANT_ADDRESS is 2 if this is
8891 a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
8892 top-level invocation, and we require the address of LOC; is 0 if we require
8893 the value of LOC. */
8895 static dw_loc_descr_ref
8896 loc_descriptor_from_tree_1 (tree loc, int want_address)
8898 dw_loc_descr_ref ret, ret1;
8899 int have_address = 0;
8900 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
8901 enum dwarf_location_atom op;
8903 /* ??? Most of the time we do not take proper care for sign/zero
8904 extending the values properly. Hopefully this won't be a real
8905 problem... */
8907 switch (TREE_CODE (loc))
8909 case ERROR_MARK:
8910 return 0;
8912 case PLACEHOLDER_EXPR:
8913 /* This case involves extracting fields from an object to determine the
8914 position of other fields. We don't try to encode this here. The
8915 only user of this is Ada, which encodes the needed information using
8916 the names of types. */
8917 return 0;
8919 case CALL_EXPR:
8920 return 0;
8922 case PREINCREMENT_EXPR:
8923 case PREDECREMENT_EXPR:
8924 case POSTINCREMENT_EXPR:
8925 case POSTDECREMENT_EXPR:
8926 /* There are no opcodes for these operations. */
8927 return 0;
8929 case ADDR_EXPR:
8930 /* If we already want an address, there's nothing we can do. */
8931 if (want_address)
8932 return 0;
8934 /* Otherwise, process the argument and look for the address. */
8935 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
8937 case VAR_DECL:
8938 if (DECL_THREAD_LOCAL (loc))
8940 rtx rtl;
8942 #ifndef ASM_OUTPUT_DWARF_DTPREL
8943 /* If this is not defined, we have no way to emit the data. */
8944 return 0;
8945 #endif
8947 /* The way DW_OP_GNU_push_tls_address is specified, we can only
8948 look up addresses of objects in the current module. */
8949 if (DECL_EXTERNAL (loc))
8950 return 0;
8952 rtl = rtl_for_decl_location (loc);
8953 if (rtl == NULL_RTX)
8954 return 0;
8956 if (!MEM_P (rtl))
8957 return 0;
8958 rtl = XEXP (rtl, 0);
8959 if (! CONSTANT_P (rtl))
8960 return 0;
8962 ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
8963 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8964 ret->dw_loc_oprnd1.v.val_addr = rtl;
8966 ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
8967 add_loc_descr (&ret, ret1);
8969 have_address = 1;
8970 break;
8972 /* FALLTHRU */
8974 case PARM_DECL:
8975 if (DECL_VALUE_EXPR (loc))
8976 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc), want_address);
8977 /* FALLTHRU */
8979 case RESULT_DECL:
8981 rtx rtl = rtl_for_decl_location (loc);
8983 if (rtl == NULL_RTX)
8984 return 0;
8985 else if (GET_CODE (rtl) == CONST_INT)
8987 HOST_WIDE_INT val = INTVAL (rtl);
8988 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
8989 val &= GET_MODE_MASK (DECL_MODE (loc));
8990 ret = int_loc_descriptor (val);
8992 else if (GET_CODE (rtl) == CONST_STRING)
8993 return 0;
8994 else if (CONSTANT_P (rtl))
8996 ret = new_loc_descr (DW_OP_addr, 0, 0);
8997 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8998 ret->dw_loc_oprnd1.v.val_addr = rtl;
9000 else
9002 enum machine_mode mode;
9004 /* Certain constructs can only be represented at top-level. */
9005 if (want_address == 2)
9006 return loc_descriptor (rtl, false);
9008 mode = GET_MODE (rtl);
9009 if (MEM_P (rtl))
9011 rtl = XEXP (rtl, 0);
9012 have_address = 1;
9014 ret = mem_loc_descriptor (rtl, mode, false);
9017 break;
9019 case INDIRECT_REF:
9020 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9021 have_address = 1;
9022 break;
9024 case COMPOUND_EXPR:
9025 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9027 case NOP_EXPR:
9028 case CONVERT_EXPR:
9029 case NON_LVALUE_EXPR:
9030 case VIEW_CONVERT_EXPR:
9031 case SAVE_EXPR:
9032 case MODIFY_EXPR:
9033 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
9035 case COMPONENT_REF:
9036 case BIT_FIELD_REF:
9037 case ARRAY_REF:
9038 case ARRAY_RANGE_REF:
9040 tree obj, offset;
9041 HOST_WIDE_INT bitsize, bitpos, bytepos;
9042 enum machine_mode mode;
9043 int volatilep;
9045 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9046 &unsignedp, &volatilep, false);
9048 if (obj == loc)
9049 return 0;
9051 ret = loc_descriptor_from_tree_1 (obj, 1);
9052 if (ret == 0
9053 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9054 return 0;
9056 if (offset != NULL_TREE)
9058 /* Variable offset. */
9059 add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9060 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9063 bytepos = bitpos / BITS_PER_UNIT;
9064 if (bytepos > 0)
9065 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9066 else if (bytepos < 0)
9068 add_loc_descr (&ret, int_loc_descriptor (bytepos));
9069 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9072 have_address = 1;
9073 break;
9076 case INTEGER_CST:
9077 if (host_integerp (loc, 0))
9078 ret = int_loc_descriptor (tree_low_cst (loc, 0));
9079 else
9080 return 0;
9081 break;
9083 case CONSTRUCTOR:
9085 /* Get an RTL for this, if something has been emitted. */
9086 rtx rtl = lookup_constant_def (loc);
9087 enum machine_mode mode;
9089 if (!rtl || !MEM_P (rtl))
9090 return 0;
9091 mode = GET_MODE (rtl);
9092 rtl = XEXP (rtl, 0);
9093 ret = mem_loc_descriptor (rtl, mode, false);
9094 have_address = 1;
9095 break;
9098 case TRUTH_AND_EXPR:
9099 case TRUTH_ANDIF_EXPR:
9100 case BIT_AND_EXPR:
9101 op = DW_OP_and;
9102 goto do_binop;
9104 case TRUTH_XOR_EXPR:
9105 case BIT_XOR_EXPR:
9106 op = DW_OP_xor;
9107 goto do_binop;
9109 case TRUTH_OR_EXPR:
9110 case TRUTH_ORIF_EXPR:
9111 case BIT_IOR_EXPR:
9112 op = DW_OP_or;
9113 goto do_binop;
9115 case FLOOR_DIV_EXPR:
9116 case CEIL_DIV_EXPR:
9117 case ROUND_DIV_EXPR:
9118 case TRUNC_DIV_EXPR:
9119 op = DW_OP_div;
9120 goto do_binop;
9122 case MINUS_EXPR:
9123 op = DW_OP_minus;
9124 goto do_binop;
9126 case FLOOR_MOD_EXPR:
9127 case CEIL_MOD_EXPR:
9128 case ROUND_MOD_EXPR:
9129 case TRUNC_MOD_EXPR:
9130 op = DW_OP_mod;
9131 goto do_binop;
9133 case MULT_EXPR:
9134 op = DW_OP_mul;
9135 goto do_binop;
9137 case LSHIFT_EXPR:
9138 op = DW_OP_shl;
9139 goto do_binop;
9141 case RSHIFT_EXPR:
9142 op = (unsignedp ? DW_OP_shr : DW_OP_shra);
9143 goto do_binop;
9145 case PLUS_EXPR:
9146 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9147 && host_integerp (TREE_OPERAND (loc, 1), 0))
9149 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9150 if (ret == 0)
9151 return 0;
9153 add_loc_descr (&ret,
9154 new_loc_descr (DW_OP_plus_uconst,
9155 tree_low_cst (TREE_OPERAND (loc, 1),
9157 0));
9158 break;
9161 op = DW_OP_plus;
9162 goto do_binop;
9164 case LE_EXPR:
9165 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9166 return 0;
9168 op = DW_OP_le;
9169 goto do_binop;
9171 case GE_EXPR:
9172 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9173 return 0;
9175 op = DW_OP_ge;
9176 goto do_binop;
9178 case LT_EXPR:
9179 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9180 return 0;
9182 op = DW_OP_lt;
9183 goto do_binop;
9185 case GT_EXPR:
9186 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9187 return 0;
9189 op = DW_OP_gt;
9190 goto do_binop;
9192 case EQ_EXPR:
9193 op = DW_OP_eq;
9194 goto do_binop;
9196 case NE_EXPR:
9197 op = DW_OP_ne;
9198 goto do_binop;
9200 do_binop:
9201 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9202 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9203 if (ret == 0 || ret1 == 0)
9204 return 0;
9206 add_loc_descr (&ret, ret1);
9207 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9208 break;
9210 case TRUTH_NOT_EXPR:
9211 case BIT_NOT_EXPR:
9212 op = DW_OP_not;
9213 goto do_unop;
9215 case ABS_EXPR:
9216 op = DW_OP_abs;
9217 goto do_unop;
9219 case NEGATE_EXPR:
9220 op = DW_OP_neg;
9221 goto do_unop;
9223 do_unop:
9224 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9225 if (ret == 0)
9226 return 0;
9228 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9229 break;
9231 case MIN_EXPR:
9232 case MAX_EXPR:
9234 const enum tree_code code =
9235 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9237 loc = build3 (COND_EXPR, TREE_TYPE (loc),
9238 build2 (code, integer_type_node,
9239 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9240 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9243 /* ... fall through ... */
9245 case COND_EXPR:
9247 dw_loc_descr_ref lhs
9248 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9249 dw_loc_descr_ref rhs
9250 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9251 dw_loc_descr_ref bra_node, jump_node, tmp;
9253 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9254 if (ret == 0 || lhs == 0 || rhs == 0)
9255 return 0;
9257 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9258 add_loc_descr (&ret, bra_node);
9260 add_loc_descr (&ret, rhs);
9261 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9262 add_loc_descr (&ret, jump_node);
9264 add_loc_descr (&ret, lhs);
9265 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9266 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9268 /* ??? Need a node to point the skip at. Use a nop. */
9269 tmp = new_loc_descr (DW_OP_nop, 0, 0);
9270 add_loc_descr (&ret, tmp);
9271 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9272 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9274 break;
9276 case FIX_TRUNC_EXPR:
9277 case FIX_CEIL_EXPR:
9278 case FIX_FLOOR_EXPR:
9279 case FIX_ROUND_EXPR:
9280 return 0;
9282 default:
9283 /* Leave front-end specific codes as simply unknown. This comes
9284 up, for instance, with the C STMT_EXPR. */
9285 if ((unsigned int) TREE_CODE (loc)
9286 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9287 return 0;
9289 #ifdef ENABLE_CHECKING
9290 /* Otherwise this is a generic code; we should just lists all of
9291 these explicitly. We forgot one. */
9292 gcc_unreachable ();
9293 #else
9294 /* In a release build, we want to degrade gracefully: better to
9295 generate incomplete debugging information than to crash. */
9296 return NULL;
9297 #endif
9300 /* Show if we can't fill the request for an address. */
9301 if (want_address && !have_address)
9302 return 0;
9304 /* If we've got an address and don't want one, dereference. */
9305 if (!want_address && have_address)
9307 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9309 if (size > DWARF2_ADDR_SIZE || size == -1)
9310 return 0;
9311 else if (size == DWARF2_ADDR_SIZE)
9312 op = DW_OP_deref;
9313 else
9314 op = DW_OP_deref_size;
9316 add_loc_descr (&ret, new_loc_descr (op, size, 0));
9319 return ret;
9322 static inline dw_loc_descr_ref
9323 loc_descriptor_from_tree (tree loc)
9325 return loc_descriptor_from_tree_1 (loc, 2);
9328 /* Given a value, round it up to the lowest multiple of `boundary'
9329 which is not less than the value itself. */
9331 static inline HOST_WIDE_INT
9332 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9334 return (((value + boundary - 1) / boundary) * boundary);
9337 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9338 pointer to the declared type for the relevant field variable, or return
9339 `integer_type_node' if the given node turns out to be an
9340 ERROR_MARK node. */
9342 static inline tree
9343 field_type (tree decl)
9345 tree type;
9347 if (TREE_CODE (decl) == ERROR_MARK)
9348 return integer_type_node;
9350 type = DECL_BIT_FIELD_TYPE (decl);
9351 if (type == NULL_TREE)
9352 type = TREE_TYPE (decl);
9354 return type;
9357 /* Given a pointer to a tree node, return the alignment in bits for
9358 it, or else return BITS_PER_WORD if the node actually turns out to
9359 be an ERROR_MARK node. */
9361 static inline unsigned
9362 simple_type_align_in_bits (tree type)
9364 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9367 static inline unsigned
9368 simple_decl_align_in_bits (tree decl)
9370 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9373 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9374 lowest addressed byte of the "containing object" for the given FIELD_DECL,
9375 or return 0 if we are unable to determine what that offset is, either
9376 because the argument turns out to be a pointer to an ERROR_MARK node, or
9377 because the offset is actually variable. (We can't handle the latter case
9378 just yet). */
9380 static HOST_WIDE_INT
9381 field_byte_offset (tree decl)
9383 unsigned int type_align_in_bits;
9384 unsigned int decl_align_in_bits;
9385 unsigned HOST_WIDE_INT type_size_in_bits;
9386 HOST_WIDE_INT object_offset_in_bits;
9387 tree type;
9388 tree field_size_tree;
9389 HOST_WIDE_INT bitpos_int;
9390 HOST_WIDE_INT deepest_bitpos;
9391 unsigned HOST_WIDE_INT field_size_in_bits;
9393 if (TREE_CODE (decl) == ERROR_MARK)
9394 return 0;
9396 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9398 type = field_type (decl);
9399 field_size_tree = DECL_SIZE (decl);
9401 /* The size could be unspecified if there was an error, or for
9402 a flexible array member. */
9403 if (! field_size_tree)
9404 field_size_tree = bitsize_zero_node;
9406 /* We cannot yet cope with fields whose positions are variable, so
9407 for now, when we see such things, we simply return 0. Someday, we may
9408 be able to handle such cases, but it will be damn difficult. */
9409 if (! host_integerp (bit_position (decl), 0))
9410 return 0;
9412 bitpos_int = int_bit_position (decl);
9414 /* If we don't know the size of the field, pretend it's a full word. */
9415 if (host_integerp (field_size_tree, 1))
9416 field_size_in_bits = tree_low_cst (field_size_tree, 1);
9417 else
9418 field_size_in_bits = BITS_PER_WORD;
9420 type_size_in_bits = simple_type_size_in_bits (type);
9421 type_align_in_bits = simple_type_align_in_bits (type);
9422 decl_align_in_bits = simple_decl_align_in_bits (decl);
9424 /* The GCC front-end doesn't make any attempt to keep track of the starting
9425 bit offset (relative to the start of the containing structure type) of the
9426 hypothetical "containing object" for a bit-field. Thus, when computing
9427 the byte offset value for the start of the "containing object" of a
9428 bit-field, we must deduce this information on our own. This can be rather
9429 tricky to do in some cases. For example, handling the following structure
9430 type definition when compiling for an i386/i486 target (which only aligns
9431 long long's to 32-bit boundaries) can be very tricky:
9433 struct S { int field1; long long field2:31; };
9435 Fortunately, there is a simple rule-of-thumb which can be used in such
9436 cases. When compiling for an i386/i486, GCC will allocate 8 bytes for the
9437 structure shown above. It decides to do this based upon one simple rule
9438 for bit-field allocation. GCC allocates each "containing object" for each
9439 bit-field at the first (i.e. lowest addressed) legitimate alignment
9440 boundary (based upon the required minimum alignment for the declared type
9441 of the field) which it can possibly use, subject to the condition that
9442 there is still enough available space remaining in the containing object
9443 (when allocated at the selected point) to fully accommodate all of the
9444 bits of the bit-field itself.
9446 This simple rule makes it obvious why GCC allocates 8 bytes for each
9447 object of the structure type shown above. When looking for a place to
9448 allocate the "containing object" for `field2', the compiler simply tries
9449 to allocate a 64-bit "containing object" at each successive 32-bit
9450 boundary (starting at zero) until it finds a place to allocate that 64-
9451 bit field such that at least 31 contiguous (and previously unallocated)
9452 bits remain within that selected 64 bit field. (As it turns out, for the
9453 example above, the compiler finds it is OK to allocate the "containing
9454 object" 64-bit field at bit-offset zero within the structure type.)
9456 Here we attempt to work backwards from the limited set of facts we're
9457 given, and we try to deduce from those facts, where GCC must have believed
9458 that the containing object started (within the structure type). The value
9459 we deduce is then used (by the callers of this routine) to generate
9460 DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
9461 and, in the case of DW_AT_location, regular fields as well). */
9463 /* Figure out the bit-distance from the start of the structure to the
9464 "deepest" bit of the bit-field. */
9465 deepest_bitpos = bitpos_int + field_size_in_bits;
9467 /* This is the tricky part. Use some fancy footwork to deduce where the
9468 lowest addressed bit of the containing object must be. */
9469 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9471 /* Round up to type_align by default. This works best for bitfields. */
9472 object_offset_in_bits += type_align_in_bits - 1;
9473 object_offset_in_bits /= type_align_in_bits;
9474 object_offset_in_bits *= type_align_in_bits;
9476 if (object_offset_in_bits > bitpos_int)
9478 /* Sigh, the decl must be packed. */
9479 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9481 /* Round up to decl_align instead. */
9482 object_offset_in_bits += decl_align_in_bits - 1;
9483 object_offset_in_bits /= decl_align_in_bits;
9484 object_offset_in_bits *= decl_align_in_bits;
9487 return object_offset_in_bits / BITS_PER_UNIT;
9490 /* The following routines define various Dwarf attributes and any data
9491 associated with them. */
9493 /* Add a location description attribute value to a DIE.
9495 This emits location attributes suitable for whole variables and
9496 whole parameters. Note that the location attributes for struct fields are
9497 generated by the routine `data_member_location_attribute' below. */
9499 static inline void
9500 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9501 dw_loc_descr_ref descr)
9503 if (descr != 0)
9504 add_AT_loc (die, attr_kind, descr);
9507 /* Attach the specialized form of location attribute used for data members of
9508 struct and union types. In the special case of a FIELD_DECL node which
9509 represents a bit-field, the "offset" part of this special location
9510 descriptor must indicate the distance in bytes from the lowest-addressed
9511 byte of the containing struct or union type to the lowest-addressed byte of
9512 the "containing object" for the bit-field. (See the `field_byte_offset'
9513 function above).
9515 For any given bit-field, the "containing object" is a hypothetical object
9516 (of some integral or enum type) within which the given bit-field lives. The
9517 type of this hypothetical "containing object" is always the same as the
9518 declared type of the individual bit-field itself (for GCC anyway... the
9519 DWARF spec doesn't actually mandate this). Note that it is the size (in
9520 bytes) of the hypothetical "containing object" which will be given in the
9521 DW_AT_byte_size attribute for this bit-field. (See the
9522 `byte_size_attribute' function below.) It is also used when calculating the
9523 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
9524 function below.) */
9526 static void
9527 add_data_member_location_attribute (dw_die_ref die, tree decl)
9529 HOST_WIDE_INT offset;
9530 dw_loc_descr_ref loc_descr = 0;
9532 if (TREE_CODE (decl) == TREE_BINFO)
9534 /* We're working on the TAG_inheritance for a base class. */
9535 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
9537 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9538 aren't at a fixed offset from all (sub)objects of the same
9539 type. We need to extract the appropriate offset from our
9540 vtable. The following dwarf expression means
9542 BaseAddr = ObAddr + *((*ObAddr) - Offset)
9544 This is specific to the V3 ABI, of course. */
9546 dw_loc_descr_ref tmp;
9548 /* Make a copy of the object address. */
9549 tmp = new_loc_descr (DW_OP_dup, 0, 0);
9550 add_loc_descr (&loc_descr, tmp);
9552 /* Extract the vtable address. */
9553 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9554 add_loc_descr (&loc_descr, tmp);
9556 /* Calculate the address of the offset. */
9557 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9558 gcc_assert (offset < 0);
9560 tmp = int_loc_descriptor (-offset);
9561 add_loc_descr (&loc_descr, tmp);
9562 tmp = new_loc_descr (DW_OP_minus, 0, 0);
9563 add_loc_descr (&loc_descr, tmp);
9565 /* Extract the offset. */
9566 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9567 add_loc_descr (&loc_descr, tmp);
9569 /* Add it to the object address. */
9570 tmp = new_loc_descr (DW_OP_plus, 0, 0);
9571 add_loc_descr (&loc_descr, tmp);
9573 else
9574 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9576 else
9577 offset = field_byte_offset (decl);
9579 if (! loc_descr)
9581 enum dwarf_location_atom op;
9583 /* The DWARF2 standard says that we should assume that the structure
9584 address is already on the stack, so we can specify a structure field
9585 address by using DW_OP_plus_uconst. */
9587 #ifdef MIPS_DEBUGGING_INFO
9588 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9589 operator correctly. It works only if we leave the offset on the
9590 stack. */
9591 op = DW_OP_constu;
9592 #else
9593 op = DW_OP_plus_uconst;
9594 #endif
9596 loc_descr = new_loc_descr (op, offset, 0);
9599 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9602 /* Writes integer values to dw_vec_const array. */
9604 static void
9605 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9607 while (size != 0)
9609 *dest++ = val & 0xff;
9610 val >>= 8;
9611 --size;
9615 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
9617 static HOST_WIDE_INT
9618 extract_int (const unsigned char *src, unsigned int size)
9620 HOST_WIDE_INT val = 0;
9622 src += size;
9623 while (size != 0)
9625 val <<= 8;
9626 val |= *--src & 0xff;
9627 --size;
9629 return val;
9632 /* Writes floating point values to dw_vec_const array. */
9634 static void
9635 insert_float (rtx rtl, unsigned char *array)
9637 REAL_VALUE_TYPE rv;
9638 long val[4];
9639 int i;
9641 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9642 real_to_target (val, &rv, GET_MODE (rtl));
9644 /* real_to_target puts 32-bit pieces in each long. Pack them. */
9645 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9647 insert_int (val[i], 4, array);
9648 array += 4;
9652 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
9653 does not have a "location" either in memory or in a register. These
9654 things can arise in GNU C when a constant is passed as an actual parameter
9655 to an inlined function. They can also arise in C++ where declared
9656 constants do not necessarily get memory "homes". */
9658 static void
9659 add_const_value_attribute (dw_die_ref die, rtx rtl)
9661 switch (GET_CODE (rtl))
9663 case CONST_INT:
9665 HOST_WIDE_INT val = INTVAL (rtl);
9667 if (val < 0)
9668 add_AT_int (die, DW_AT_const_value, val);
9669 else
9670 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
9672 break;
9674 case CONST_DOUBLE:
9675 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9676 floating-point constant. A CONST_DOUBLE is used whenever the
9677 constant requires more than one word in order to be adequately
9678 represented. We output CONST_DOUBLEs as blocks. */
9680 enum machine_mode mode = GET_MODE (rtl);
9682 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
9684 unsigned int length = GET_MODE_SIZE (mode);
9685 unsigned char *array = ggc_alloc (length);
9687 insert_float (rtl, array);
9688 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
9690 else
9692 /* ??? We really should be using HOST_WIDE_INT throughout. */
9693 gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
9695 add_AT_long_long (die, DW_AT_const_value,
9696 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
9699 break;
9701 case CONST_VECTOR:
9703 enum machine_mode mode = GET_MODE (rtl);
9704 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9705 unsigned int length = CONST_VECTOR_NUNITS (rtl);
9706 unsigned char *array = ggc_alloc (length * elt_size);
9707 unsigned int i;
9708 unsigned char *p;
9710 switch (GET_MODE_CLASS (mode))
9712 case MODE_VECTOR_INT:
9713 for (i = 0, p = array; i < length; i++, p += elt_size)
9715 rtx elt = CONST_VECTOR_ELT (rtl, i);
9716 HOST_WIDE_INT lo, hi;
9718 switch (GET_CODE (elt))
9720 case CONST_INT:
9721 lo = INTVAL (elt);
9722 hi = -(lo < 0);
9723 break;
9725 case CONST_DOUBLE:
9726 lo = CONST_DOUBLE_LOW (elt);
9727 hi = CONST_DOUBLE_HIGH (elt);
9728 break;
9730 default:
9731 gcc_unreachable ();
9734 if (elt_size <= sizeof (HOST_WIDE_INT))
9735 insert_int (lo, elt_size, p);
9736 else
9738 unsigned char *p0 = p;
9739 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9741 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
9742 if (WORDS_BIG_ENDIAN)
9744 p0 = p1;
9745 p1 = p;
9747 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9748 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9751 break;
9753 case MODE_VECTOR_FLOAT:
9754 for (i = 0, p = array; i < length; i++, p += elt_size)
9756 rtx elt = CONST_VECTOR_ELT (rtl, i);
9757 insert_float (elt, p);
9759 break;
9761 default:
9762 gcc_unreachable ();
9765 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
9767 break;
9769 case CONST_STRING:
9770 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
9771 break;
9773 case SYMBOL_REF:
9774 case LABEL_REF:
9775 case CONST:
9776 add_AT_addr (die, DW_AT_const_value, rtl);
9777 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9778 break;
9780 case PLUS:
9781 /* In cases where an inlined instance of an inline function is passed
9782 the address of an `auto' variable (which is local to the caller) we
9783 can get a situation where the DECL_RTL of the artificial local
9784 variable (for the inlining) which acts as a stand-in for the
9785 corresponding formal parameter (of the inline function) will look
9786 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
9787 exactly a compile-time constant expression, but it isn't the address
9788 of the (artificial) local variable either. Rather, it represents the
9789 *value* which the artificial local variable always has during its
9790 lifetime. We currently have no way to represent such quasi-constant
9791 values in Dwarf, so for now we just punt and generate nothing. */
9792 break;
9794 default:
9795 /* No other kinds of rtx should be possible here. */
9796 gcc_unreachable ();
9801 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
9802 for use in a later add_const_value_attribute call. */
9804 static rtx
9805 rtl_for_decl_init (tree init, tree type)
9807 rtx rtl = NULL_RTX;
9809 /* If a variable is initialized with a string constant without embedded
9810 zeros, build CONST_STRING. */
9811 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
9813 tree enttype = TREE_TYPE (type);
9814 tree domain = TYPE_DOMAIN (type);
9815 enum machine_mode mode = TYPE_MODE (enttype);
9817 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
9818 && domain
9819 && integer_zerop (TYPE_MIN_VALUE (domain))
9820 && compare_tree_int (TYPE_MAX_VALUE (domain),
9821 TREE_STRING_LENGTH (init) - 1) == 0
9822 && ((size_t) TREE_STRING_LENGTH (init)
9823 == strlen (TREE_STRING_POINTER (init)) + 1))
9824 rtl = gen_rtx_CONST_STRING (VOIDmode,
9825 ggc_strdup (TREE_STRING_POINTER (init)));
9827 /* If the initializer is something that we know will expand into an
9828 immediate RTL constant, expand it now. Expanding anything else
9829 tends to produce unresolved symbols; see debug/5770 and c++/6381. */
9830 /* Aggregate, vector, and complex types may contain constructors that may
9831 result in code being generated when expand_expr is called, so we can't
9832 handle them here. Integer and float are useful and safe types to handle
9833 here. */
9834 else if ((INTEGRAL_TYPE_P (type) || SCALAR_FLOAT_TYPE_P (type))
9835 && initializer_constant_valid_p (init, type) == null_pointer_node)
9837 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
9839 /* If expand_expr returns a MEM, it wasn't immediate. */
9840 gcc_assert (!rtl || !MEM_P (rtl));
9843 return rtl;
9846 /* Generate RTL for the variable DECL to represent its location. */
9848 static rtx
9849 rtl_for_decl_location (tree decl)
9851 rtx rtl;
9853 /* Here we have to decide where we are going to say the parameter "lives"
9854 (as far as the debugger is concerned). We only have a couple of
9855 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
9857 DECL_RTL normally indicates where the parameter lives during most of the
9858 activation of the function. If optimization is enabled however, this
9859 could be either NULL or else a pseudo-reg. Both of those cases indicate
9860 that the parameter doesn't really live anywhere (as far as the code
9861 generation parts of GCC are concerned) during most of the function's
9862 activation. That will happen (for example) if the parameter is never
9863 referenced within the function.
9865 We could just generate a location descriptor here for all non-NULL
9866 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
9867 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
9868 where DECL_RTL is NULL or is a pseudo-reg.
9870 Note however that we can only get away with using DECL_INCOMING_RTL as
9871 a backup substitute for DECL_RTL in certain limited cases. In cases
9872 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
9873 we can be sure that the parameter was passed using the same type as it is
9874 declared to have within the function, and that its DECL_INCOMING_RTL
9875 points us to a place where a value of that type is passed.
9877 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
9878 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
9879 because in these cases DECL_INCOMING_RTL points us to a value of some
9880 type which is *different* from the type of the parameter itself. Thus,
9881 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
9882 such cases, the debugger would end up (for example) trying to fetch a
9883 `float' from a place which actually contains the first part of a
9884 `double'. That would lead to really incorrect and confusing
9885 output at debug-time.
9887 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
9888 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
9889 are a couple of exceptions however. On little-endian machines we can
9890 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
9891 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
9892 an integral type that is smaller than TREE_TYPE (decl). These cases arise
9893 when (on a little-endian machine) a non-prototyped function has a
9894 parameter declared to be of type `short' or `char'. In such cases,
9895 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
9896 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
9897 passed `int' value. If the debugger then uses that address to fetch
9898 a `short' or a `char' (on a little-endian machine) the result will be
9899 the correct data, so we allow for such exceptional cases below.
9901 Note that our goal here is to describe the place where the given formal
9902 parameter lives during most of the function's activation (i.e. between the
9903 end of the prologue and the start of the epilogue). We'll do that as best
9904 as we can. Note however that if the given formal parameter is modified
9905 sometime during the execution of the function, then a stack backtrace (at
9906 debug-time) will show the function as having been called with the *new*
9907 value rather than the value which was originally passed in. This happens
9908 rarely enough that it is not a major problem, but it *is* a problem, and
9909 I'd like to fix it.
9911 A future version of dwarf2out.c may generate two additional attributes for
9912 any given DW_TAG_formal_parameter DIE which will describe the "passed
9913 type" and the "passed location" for the given formal parameter in addition
9914 to the attributes we now generate to indicate the "declared type" and the
9915 "active location" for each parameter. This additional set of attributes
9916 could be used by debuggers for stack backtraces. Separately, note that
9917 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
9918 This happens (for example) for inlined-instances of inline function formal
9919 parameters which are never referenced. This really shouldn't be
9920 happening. All PARM_DECL nodes should get valid non-NULL
9921 DECL_INCOMING_RTL values. FIXME. */
9923 /* Use DECL_RTL as the "location" unless we find something better. */
9924 rtl = DECL_RTL_IF_SET (decl);
9926 /* When generating abstract instances, ignore everything except
9927 constants, symbols living in memory, and symbols living in
9928 fixed registers. */
9929 if (! reload_completed)
9931 if (rtl
9932 && (CONSTANT_P (rtl)
9933 || (MEM_P (rtl)
9934 && CONSTANT_P (XEXP (rtl, 0)))
9935 || (REG_P (rtl)
9936 && TREE_CODE (decl) == VAR_DECL
9937 && TREE_STATIC (decl))))
9939 rtl = targetm.delegitimize_address (rtl);
9940 return rtl;
9942 rtl = NULL_RTX;
9944 else if (TREE_CODE (decl) == PARM_DECL)
9946 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
9948 tree declared_type = TREE_TYPE (decl);
9949 tree passed_type = DECL_ARG_TYPE (decl);
9950 enum machine_mode dmode = TYPE_MODE (declared_type);
9951 enum machine_mode pmode = TYPE_MODE (passed_type);
9953 /* This decl represents a formal parameter which was optimized out.
9954 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
9955 all cases where (rtl == NULL_RTX) just below. */
9956 if (dmode == pmode)
9957 rtl = DECL_INCOMING_RTL (decl);
9958 else if (SCALAR_INT_MODE_P (dmode)
9959 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
9960 && DECL_INCOMING_RTL (decl))
9962 rtx inc = DECL_INCOMING_RTL (decl);
9963 if (REG_P (inc))
9964 rtl = inc;
9965 else if (MEM_P (inc))
9967 if (BYTES_BIG_ENDIAN)
9968 rtl = adjust_address_nv (inc, dmode,
9969 GET_MODE_SIZE (pmode)
9970 - GET_MODE_SIZE (dmode));
9971 else
9972 rtl = inc;
9977 /* If the parm was passed in registers, but lives on the stack, then
9978 make a big endian correction if the mode of the type of the
9979 parameter is not the same as the mode of the rtl. */
9980 /* ??? This is the same series of checks that are made in dbxout.c before
9981 we reach the big endian correction code there. It isn't clear if all
9982 of these checks are necessary here, but keeping them all is the safe
9983 thing to do. */
9984 else if (MEM_P (rtl)
9985 && XEXP (rtl, 0) != const0_rtx
9986 && ! CONSTANT_P (XEXP (rtl, 0))
9987 /* Not passed in memory. */
9988 && !MEM_P (DECL_INCOMING_RTL (decl))
9989 /* Not passed by invisible reference. */
9990 && (!REG_P (XEXP (rtl, 0))
9991 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
9992 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
9993 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
9994 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
9995 #endif
9997 /* Big endian correction check. */
9998 && BYTES_BIG_ENDIAN
9999 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10000 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10001 < UNITS_PER_WORD))
10003 int offset = (UNITS_PER_WORD
10004 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10006 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10007 plus_constant (XEXP (rtl, 0), offset));
10010 else if (TREE_CODE (decl) == VAR_DECL
10011 && rtl
10012 && MEM_P (rtl)
10013 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10014 && BYTES_BIG_ENDIAN)
10016 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10017 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10019 /* If a variable is declared "register" yet is smaller than
10020 a register, then if we store the variable to memory, it
10021 looks like we're storing a register-sized value, when in
10022 fact we are not. We need to adjust the offset of the
10023 storage location to reflect the actual value's bytes,
10024 else gdb will not be able to display it. */
10025 if (rsize > dsize)
10026 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10027 plus_constant (XEXP (rtl, 0), rsize-dsize));
10030 if (rtl != NULL_RTX)
10032 rtl = eliminate_regs (rtl, 0, NULL_RTX);
10033 #ifdef LEAF_REG_REMAP
10034 if (current_function_uses_only_leaf_regs)
10035 leaf_renumber_regs_insn (rtl);
10036 #endif
10039 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10040 and will have been substituted directly into all expressions that use it.
10041 C does not have such a concept, but C++ and other languages do. */
10042 else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10043 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10045 if (rtl)
10046 rtl = targetm.delegitimize_address (rtl);
10048 /* If we don't look past the constant pool, we risk emitting a
10049 reference to a constant pool entry that isn't referenced from
10050 code, and thus is not emitted. */
10051 if (rtl)
10052 rtl = avoid_constant_pool_reference (rtl);
10054 return rtl;
10057 /* Return true if DECL's containing function has a frame base attribute.
10058 Return false otherwise. */
10060 static bool
10061 containing_function_has_frame_base (tree decl)
10063 tree declcontext = decl_function_context (decl);
10064 dw_die_ref context;
10065 dw_attr_ref attr;
10067 if (!declcontext)
10068 return false;
10070 context = lookup_decl_die (declcontext);
10071 if (!context)
10072 return false;
10074 for (attr = context->die_attr; attr; attr = attr->dw_attr_next)
10075 if (attr->dw_attr == DW_AT_frame_base)
10076 return true;
10077 return false;
10080 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10081 data attribute for a variable or a parameter. We generate the
10082 DW_AT_const_value attribute only in those cases where the given variable
10083 or parameter does not have a true "location" either in memory or in a
10084 register. This can happen (for example) when a constant is passed as an
10085 actual argument in a call to an inline function. (It's possible that
10086 these things can crop up in other ways also.) Note that one type of
10087 constant value which can be passed into an inlined function is a constant
10088 pointer. This can happen for example if an actual argument in an inlined
10089 function call evaluates to a compile-time constant address. */
10091 static void
10092 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10093 enum dwarf_attribute attr)
10095 rtx rtl;
10096 dw_loc_descr_ref descr;
10097 var_loc_list *loc_list;
10098 bool can_use_fb;
10099 struct var_loc_node *node;
10100 if (TREE_CODE (decl) == ERROR_MARK)
10101 return;
10103 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10104 || TREE_CODE (decl) == RESULT_DECL);
10106 can_use_fb = containing_function_has_frame_base (decl);
10108 /* See if we possibly have multiple locations for this variable. */
10109 loc_list = lookup_decl_loc (decl);
10111 /* If it truly has multiple locations, the first and last node will
10112 differ. */
10113 if (loc_list && loc_list->first != loc_list->last)
10115 const char *secname;
10116 const char *endname;
10117 dw_loc_list_ref list;
10118 rtx varloc;
10120 /* We need to figure out what section we should use as the base
10121 for the address ranges where a given location is valid.
10122 1. If this particular DECL has a section associated with it,
10123 use that.
10124 2. If this function has a section associated with it, use
10125 that.
10126 3. Otherwise, use the text section.
10127 XXX: If you split a variable across multiple sections, this
10128 won't notice. */
10130 if (DECL_SECTION_NAME (decl))
10132 tree sectree = DECL_SECTION_NAME (decl);
10133 secname = TREE_STRING_POINTER (sectree);
10135 else if (current_function_decl
10136 && DECL_SECTION_NAME (current_function_decl))
10138 tree sectree = DECL_SECTION_NAME (current_function_decl);
10139 secname = TREE_STRING_POINTER (sectree);
10141 else if (cfun
10142 && (last_text_section == in_unlikely_executed_text
10143 || (last_text_section == in_named
10144 && last_text_section_name ==
10145 cfun->unlikely_text_section_name)))
10146 secname = cfun->cold_section_label;
10147 else
10148 secname = text_section_label;
10150 /* Now that we know what section we are using for a base,
10151 actually construct the list of locations.
10152 The first location information is what is passed to the
10153 function that creates the location list, and the remaining
10154 locations just get added on to that list.
10155 Note that we only know the start address for a location
10156 (IE location changes), so to build the range, we use
10157 the range [current location start, next location start].
10158 This means we have to special case the last node, and generate
10159 a range of [last location start, end of function label]. */
10161 node = loc_list->first;
10162 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10163 list = new_loc_list (loc_descriptor (varloc, can_use_fb),
10164 node->label, node->next->label, secname, 1);
10165 node = node->next;
10167 for (; node->next; node = node->next)
10168 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10170 /* The variable has a location between NODE->LABEL and
10171 NODE->NEXT->LABEL. */
10172 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10173 add_loc_descr_to_loc_list (&list,
10174 loc_descriptor (varloc,
10175 can_use_fb),
10176 node->label, node->next->label, secname);
10179 /* If the variable has a location at the last label
10180 it keeps its location until the end of function. */
10181 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10183 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10185 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10186 if (!current_function_decl)
10187 endname = text_end_label;
10188 else
10190 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10191 current_function_funcdef_no);
10192 endname = ggc_strdup (label_id);
10194 add_loc_descr_to_loc_list (&list,
10195 loc_descriptor (varloc,
10196 can_use_fb),
10197 node->label, endname, secname);
10200 /* Finally, add the location list to the DIE, and we are done. */
10201 add_AT_loc_list (die, attr, list);
10202 return;
10205 /* Try to get some constant RTL for this decl, and use that as the value of
10206 the location. */
10208 rtl = rtl_for_decl_location (decl);
10209 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10211 add_const_value_attribute (die, rtl);
10212 return;
10215 /* We couldn't get any rtl, and we had no >1 element location list, so try
10216 directly generating the location description from the tree. */
10217 descr = loc_descriptor_from_tree (decl);
10218 if (descr)
10220 add_AT_location_description (die, attr, descr);
10221 return;
10224 /* Lastly, if we have tried to generate the location otherwise, and it
10225 didn't work out (we wouldn't be here if we did), and we have a one entry
10226 location list, try generating a location from that. */
10227 if (loc_list && loc_list->first)
10229 node = loc_list->first;
10230 descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note),
10231 can_use_fb);
10232 if (descr)
10233 add_AT_location_description (die, attr, descr);
10237 /* If we don't have a copy of this variable in memory for some reason (such
10238 as a C++ member constant that doesn't have an out-of-line definition),
10239 we should tell the debugger about the constant value. */
10241 static void
10242 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10244 tree init = DECL_INITIAL (decl);
10245 tree type = TREE_TYPE (decl);
10246 rtx rtl;
10248 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10249 /* OK */;
10250 else
10251 return;
10253 rtl = rtl_for_decl_init (init, type);
10254 if (rtl)
10255 add_const_value_attribute (var_die, rtl);
10258 /* Generate a DW_AT_name attribute given some string value to be included as
10259 the value of the attribute. */
10261 static void
10262 add_name_attribute (dw_die_ref die, const char *name_string)
10264 if (name_string != NULL && *name_string != 0)
10266 if (demangle_name_func)
10267 name_string = (*demangle_name_func) (name_string);
10269 add_AT_string (die, DW_AT_name, name_string);
10273 /* Generate a DW_AT_comp_dir attribute for DIE. */
10275 static void
10276 add_comp_dir_attribute (dw_die_ref die)
10278 const char *wd = get_src_pwd ();
10279 if (wd != NULL)
10280 add_AT_string (die, DW_AT_comp_dir, wd);
10283 /* Given a tree node describing an array bound (either lower or upper) output
10284 a representation for that bound. */
10286 static void
10287 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10289 switch (TREE_CODE (bound))
10291 case ERROR_MARK:
10292 return;
10294 /* All fixed-bounds are represented by INTEGER_CST nodes. */
10295 case INTEGER_CST:
10296 if (! host_integerp (bound, 0)
10297 || (bound_attr == DW_AT_lower_bound
10298 && (((is_c_family () || is_java ()) && integer_zerop (bound))
10299 || (is_fortran () && integer_onep (bound)))))
10300 /* Use the default. */
10302 else
10303 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
10304 break;
10306 case CONVERT_EXPR:
10307 case NOP_EXPR:
10308 case NON_LVALUE_EXPR:
10309 case VIEW_CONVERT_EXPR:
10310 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10311 break;
10313 case SAVE_EXPR:
10314 break;
10316 case VAR_DECL:
10317 case PARM_DECL:
10318 case RESULT_DECL:
10320 dw_die_ref decl_die = lookup_decl_die (bound);
10322 /* ??? Can this happen, or should the variable have been bound
10323 first? Probably it can, since I imagine that we try to create
10324 the types of parameters in the order in which they exist in
10325 the list, and won't have created a forward reference to a
10326 later parameter. */
10327 if (decl_die != NULL)
10328 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10329 break;
10332 default:
10334 /* Otherwise try to create a stack operation procedure to
10335 evaluate the value of the array bound. */
10337 dw_die_ref ctx, decl_die;
10338 dw_loc_descr_ref loc;
10340 loc = loc_descriptor_from_tree (bound);
10341 if (loc == NULL)
10342 break;
10344 if (current_function_decl == 0)
10345 ctx = comp_unit_die;
10346 else
10347 ctx = lookup_decl_die (current_function_decl);
10349 decl_die = new_die (DW_TAG_variable, ctx, bound);
10350 add_AT_flag (decl_die, DW_AT_artificial, 1);
10351 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10352 add_AT_loc (decl_die, DW_AT_location, loc);
10354 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10355 break;
10360 /* Note that the block of subscript information for an array type also
10361 includes information about the element type of type given array type. */
10363 static void
10364 add_subscript_info (dw_die_ref type_die, tree type)
10366 #ifndef MIPS_DEBUGGING_INFO
10367 unsigned dimension_number;
10368 #endif
10369 tree lower, upper;
10370 dw_die_ref subrange_die;
10372 /* The GNU compilers represent multidimensional array types as sequences of
10373 one dimensional array types whose element types are themselves array
10374 types. Here we squish that down, so that each multidimensional array
10375 type gets only one array_type DIE in the Dwarf debugging info. The draft
10376 Dwarf specification say that we are allowed to do this kind of
10377 compression in C (because there is no difference between an array or
10378 arrays and a multidimensional array in C) but for other source languages
10379 (e.g. Ada) we probably shouldn't do this. */
10381 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10382 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10383 We work around this by disabling this feature. See also
10384 gen_array_type_die. */
10385 #ifndef MIPS_DEBUGGING_INFO
10386 for (dimension_number = 0;
10387 TREE_CODE (type) == ARRAY_TYPE;
10388 type = TREE_TYPE (type), dimension_number++)
10389 #endif
10391 tree domain = TYPE_DOMAIN (type);
10393 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
10394 and (in GNU C only) variable bounds. Handle all three forms
10395 here. */
10396 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
10397 if (domain)
10399 /* We have an array type with specified bounds. */
10400 lower = TYPE_MIN_VALUE (domain);
10401 upper = TYPE_MAX_VALUE (domain);
10403 /* Define the index type. */
10404 if (TREE_TYPE (domain))
10406 /* ??? This is probably an Ada unnamed subrange type. Ignore the
10407 TREE_TYPE field. We can't emit debug info for this
10408 because it is an unnamed integral type. */
10409 if (TREE_CODE (domain) == INTEGER_TYPE
10410 && TYPE_NAME (domain) == NULL_TREE
10411 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10412 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
10414 else
10415 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10416 type_die);
10419 /* ??? If upper is NULL, the array has unspecified length,
10420 but it does have a lower bound. This happens with Fortran
10421 dimension arr(N:*)
10422 Since the debugger is definitely going to need to know N
10423 to produce useful results, go ahead and output the lower
10424 bound solo, and hope the debugger can cope. */
10426 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
10427 if (upper)
10428 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
10431 /* Otherwise we have an array type with an unspecified length. The
10432 DWARF-2 spec does not say how to handle this; let's just leave out the
10433 bounds. */
10437 static void
10438 add_byte_size_attribute (dw_die_ref die, tree tree_node)
10440 unsigned size;
10442 switch (TREE_CODE (tree_node))
10444 case ERROR_MARK:
10445 size = 0;
10446 break;
10447 case ENUMERAL_TYPE:
10448 case RECORD_TYPE:
10449 case UNION_TYPE:
10450 case QUAL_UNION_TYPE:
10451 size = int_size_in_bytes (tree_node);
10452 break;
10453 case FIELD_DECL:
10454 /* For a data member of a struct or union, the DW_AT_byte_size is
10455 generally given as the number of bytes normally allocated for an
10456 object of the *declared* type of the member itself. This is true
10457 even for bit-fields. */
10458 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10459 break;
10460 default:
10461 gcc_unreachable ();
10464 /* Note that `size' might be -1 when we get to this point. If it is, that
10465 indicates that the byte size of the entity in question is variable. We
10466 have no good way of expressing this fact in Dwarf at the present time,
10467 so just let the -1 pass on through. */
10468 add_AT_unsigned (die, DW_AT_byte_size, size);
10471 /* For a FIELD_DECL node which represents a bit-field, output an attribute
10472 which specifies the distance in bits from the highest order bit of the
10473 "containing object" for the bit-field to the highest order bit of the
10474 bit-field itself.
10476 For any given bit-field, the "containing object" is a hypothetical object
10477 (of some integral or enum type) within which the given bit-field lives. The
10478 type of this hypothetical "containing object" is always the same as the
10479 declared type of the individual bit-field itself. The determination of the
10480 exact location of the "containing object" for a bit-field is rather
10481 complicated. It's handled by the `field_byte_offset' function (above).
10483 Note that it is the size (in bytes) of the hypothetical "containing object"
10484 which will be given in the DW_AT_byte_size attribute for this bit-field.
10485 (See `byte_size_attribute' above). */
10487 static inline void
10488 add_bit_offset_attribute (dw_die_ref die, tree decl)
10490 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10491 tree type = DECL_BIT_FIELD_TYPE (decl);
10492 HOST_WIDE_INT bitpos_int;
10493 HOST_WIDE_INT highest_order_object_bit_offset;
10494 HOST_WIDE_INT highest_order_field_bit_offset;
10495 HOST_WIDE_INT unsigned bit_offset;
10497 /* Must be a field and a bit field. */
10498 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
10500 /* We can't yet handle bit-fields whose offsets are variable, so if we
10501 encounter such things, just return without generating any attribute
10502 whatsoever. Likewise for variable or too large size. */
10503 if (! host_integerp (bit_position (decl), 0)
10504 || ! host_integerp (DECL_SIZE (decl), 1))
10505 return;
10507 bitpos_int = int_bit_position (decl);
10509 /* Note that the bit offset is always the distance (in bits) from the
10510 highest-order bit of the "containing object" to the highest-order bit of
10511 the bit-field itself. Since the "high-order end" of any object or field
10512 is different on big-endian and little-endian machines, the computation
10513 below must take account of these differences. */
10514 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10515 highest_order_field_bit_offset = bitpos_int;
10517 if (! BYTES_BIG_ENDIAN)
10519 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
10520 highest_order_object_bit_offset += simple_type_size_in_bits (type);
10523 bit_offset
10524 = (! BYTES_BIG_ENDIAN
10525 ? highest_order_object_bit_offset - highest_order_field_bit_offset
10526 : highest_order_field_bit_offset - highest_order_object_bit_offset);
10528 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10531 /* For a FIELD_DECL node which represents a bit field, output an attribute
10532 which specifies the length in bits of the given field. */
10534 static inline void
10535 add_bit_size_attribute (dw_die_ref die, tree decl)
10537 /* Must be a field and a bit field. */
10538 gcc_assert (TREE_CODE (decl) == FIELD_DECL
10539 && DECL_BIT_FIELD_TYPE (decl));
10541 if (host_integerp (DECL_SIZE (decl), 1))
10542 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
10545 /* If the compiled language is ANSI C, then add a 'prototyped'
10546 attribute, if arg types are given for the parameters of a function. */
10548 static inline void
10549 add_prototyped_attribute (dw_die_ref die, tree func_type)
10551 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10552 && TYPE_ARG_TYPES (func_type) != NULL)
10553 add_AT_flag (die, DW_AT_prototyped, 1);
10556 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
10557 by looking in either the type declaration or object declaration
10558 equate table. */
10560 static inline void
10561 add_abstract_origin_attribute (dw_die_ref die, tree origin)
10563 dw_die_ref origin_die = NULL;
10565 if (TREE_CODE (origin) != FUNCTION_DECL)
10567 /* We may have gotten separated from the block for the inlined
10568 function, if we're in an exception handler or some such; make
10569 sure that the abstract function has been written out.
10571 Doing this for nested functions is wrong, however; functions are
10572 distinct units, and our context might not even be inline. */
10573 tree fn = origin;
10575 if (TYPE_P (fn))
10576 fn = TYPE_STUB_DECL (fn);
10578 fn = decl_function_context (fn);
10579 if (fn)
10580 dwarf2out_abstract_function (fn);
10583 if (DECL_P (origin))
10584 origin_die = lookup_decl_die (origin);
10585 else if (TYPE_P (origin))
10586 origin_die = lookup_type_die (origin);
10588 /* XXX: Functions that are never lowered don't always have correct block
10589 trees (in the case of java, they simply have no block tree, in some other
10590 languages). For these functions, there is nothing we can really do to
10591 output correct debug info for inlined functions in all cases. Rather
10592 than die, we'll just produce deficient debug info now, in that we will
10593 have variables without a proper abstract origin. In the future, when all
10594 functions are lowered, we should re-add a gcc_assert (origin_die)
10595 here. */
10597 if (origin_die)
10598 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
10601 /* We do not currently support the pure_virtual attribute. */
10603 static inline void
10604 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
10606 if (DECL_VINDEX (func_decl))
10608 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10610 if (host_integerp (DECL_VINDEX (func_decl), 0))
10611 add_AT_loc (die, DW_AT_vtable_elem_location,
10612 new_loc_descr (DW_OP_constu,
10613 tree_low_cst (DECL_VINDEX (func_decl), 0),
10614 0));
10616 /* GNU extension: Record what type this method came from originally. */
10617 if (debug_info_level > DINFO_LEVEL_TERSE)
10618 add_AT_die_ref (die, DW_AT_containing_type,
10619 lookup_type_die (DECL_CONTEXT (func_decl)));
10623 /* Add source coordinate attributes for the given decl. */
10625 static void
10626 add_src_coords_attributes (dw_die_ref die, tree decl)
10628 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
10629 unsigned file_index = lookup_filename (s.file);
10631 add_AT_unsigned (die, DW_AT_decl_file, file_index);
10632 add_AT_unsigned (die, DW_AT_decl_line, s.line);
10635 /* Add a DW_AT_name attribute and source coordinate attribute for the
10636 given decl, but only if it actually has a name. */
10638 static void
10639 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
10641 tree decl_name;
10643 decl_name = DECL_NAME (decl);
10644 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
10646 add_name_attribute (die, dwarf2_name (decl, 0));
10647 if (! DECL_ARTIFICIAL (decl))
10648 add_src_coords_attributes (die, decl);
10650 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
10651 && TREE_PUBLIC (decl)
10652 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
10653 && !DECL_ABSTRACT (decl))
10654 add_AT_string (die, DW_AT_MIPS_linkage_name,
10655 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
10658 #ifdef VMS_DEBUGGING_INFO
10659 /* Get the function's name, as described by its RTL. This may be different
10660 from the DECL_NAME name used in the source file. */
10661 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
10663 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
10664 XEXP (DECL_RTL (decl), 0));
10665 VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
10667 #endif
10670 /* Push a new declaration scope. */
10672 static void
10673 push_decl_scope (tree scope)
10675 VEC_safe_push (tree, gc, decl_scope_table, scope);
10678 /* Pop a declaration scope. */
10680 static inline void
10681 pop_decl_scope (void)
10683 VEC_pop (tree, decl_scope_table);
10686 /* Return the DIE for the scope that immediately contains this type.
10687 Non-named types get global scope. Named types nested in other
10688 types get their containing scope if it's open, or global scope
10689 otherwise. All other types (i.e. function-local named types) get
10690 the current active scope. */
10692 static dw_die_ref
10693 scope_die_for (tree t, dw_die_ref context_die)
10695 dw_die_ref scope_die = NULL;
10696 tree containing_scope;
10697 int i;
10699 /* Non-types always go in the current scope. */
10700 gcc_assert (TYPE_P (t));
10702 containing_scope = TYPE_CONTEXT (t);
10704 /* Use the containing namespace if it was passed in (for a declaration). */
10705 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
10707 if (context_die == lookup_decl_die (containing_scope))
10708 /* OK */;
10709 else
10710 containing_scope = NULL_TREE;
10713 /* Ignore function type "scopes" from the C frontend. They mean that
10714 a tagged type is local to a parmlist of a function declarator, but
10715 that isn't useful to DWARF. */
10716 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
10717 containing_scope = NULL_TREE;
10719 if (containing_scope == NULL_TREE)
10720 scope_die = comp_unit_die;
10721 else if (TYPE_P (containing_scope))
10723 /* For types, we can just look up the appropriate DIE. But
10724 first we check to see if we're in the middle of emitting it
10725 so we know where the new DIE should go. */
10726 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
10727 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
10728 break;
10730 if (i < 0)
10732 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
10733 || TREE_ASM_WRITTEN (containing_scope));
10735 /* If none of the current dies are suitable, we get file scope. */
10736 scope_die = comp_unit_die;
10738 else
10739 scope_die = lookup_type_die (containing_scope);
10741 else
10742 scope_die = context_die;
10744 return scope_die;
10747 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
10749 static inline int
10750 local_scope_p (dw_die_ref context_die)
10752 for (; context_die; context_die = context_die->die_parent)
10753 if (context_die->die_tag == DW_TAG_inlined_subroutine
10754 || context_die->die_tag == DW_TAG_subprogram)
10755 return 1;
10757 return 0;
10760 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
10761 whether or not to treat a DIE in this context as a declaration. */
10763 static inline int
10764 class_or_namespace_scope_p (dw_die_ref context_die)
10766 return (context_die
10767 && (context_die->die_tag == DW_TAG_structure_type
10768 || context_die->die_tag == DW_TAG_union_type
10769 || context_die->die_tag == DW_TAG_namespace));
10772 /* Many forms of DIEs require a "type description" attribute. This
10773 routine locates the proper "type descriptor" die for the type given
10774 by 'type', and adds a DW_AT_type attribute below the given die. */
10776 static void
10777 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
10778 int decl_volatile, dw_die_ref context_die)
10780 enum tree_code code = TREE_CODE (type);
10781 dw_die_ref type_die = NULL;
10783 /* ??? If this type is an unnamed subrange type of an integral or
10784 floating-point type, use the inner type. This is because we have no
10785 support for unnamed types in base_type_die. This can happen if this is
10786 an Ada subrange type. Correct solution is emit a subrange type die. */
10787 if ((code == INTEGER_TYPE || code == REAL_TYPE)
10788 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
10789 type = TREE_TYPE (type), code = TREE_CODE (type);
10791 if (code == ERROR_MARK
10792 /* Handle a special case. For functions whose return type is void, we
10793 generate *no* type attribute. (Note that no object may have type
10794 `void', so this only applies to function return types). */
10795 || code == VOID_TYPE)
10796 return;
10798 type_die = modified_type_die (type,
10799 decl_const || TYPE_READONLY (type),
10800 decl_volatile || TYPE_VOLATILE (type),
10801 context_die);
10803 if (type_die != NULL)
10804 add_AT_die_ref (object_die, DW_AT_type, type_die);
10807 /* Given an object die, add the calling convention attribute for the
10808 function call type. */
10809 static void
10810 add_calling_convention_attribute (dw_die_ref subr_die, tree type)
10812 enum dwarf_calling_convention value = DW_CC_normal;
10814 value = targetm.dwarf_calling_convention (type);
10816 /* Only add the attribute if the backend requests it, and
10817 is not DW_CC_normal. */
10818 if (value && (value != DW_CC_normal))
10819 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
10822 /* Given a tree pointer to a struct, class, union, or enum type node, return
10823 a pointer to the (string) tag name for the given type, or zero if the type
10824 was declared without a tag. */
10826 static const char *
10827 type_tag (tree type)
10829 const char *name = 0;
10831 if (TYPE_NAME (type) != 0)
10833 tree t = 0;
10835 /* Find the IDENTIFIER_NODE for the type name. */
10836 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
10837 t = TYPE_NAME (type);
10839 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
10840 a TYPE_DECL node, regardless of whether or not a `typedef' was
10841 involved. */
10842 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10843 && ! DECL_IGNORED_P (TYPE_NAME (type)))
10844 t = DECL_NAME (TYPE_NAME (type));
10846 /* Now get the name as a string, or invent one. */
10847 if (t != 0)
10848 name = IDENTIFIER_POINTER (t);
10851 return (name == 0 || *name == '\0') ? 0 : name;
10854 /* Return the type associated with a data member, make a special check
10855 for bit field types. */
10857 static inline tree
10858 member_declared_type (tree member)
10860 return (DECL_BIT_FIELD_TYPE (member)
10861 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
10864 /* Get the decl's label, as described by its RTL. This may be different
10865 from the DECL_NAME name used in the source file. */
10867 #if 0
10868 static const char *
10869 decl_start_label (tree decl)
10871 rtx x;
10872 const char *fnname;
10874 x = DECL_RTL (decl);
10875 gcc_assert (MEM_P (x));
10877 x = XEXP (x, 0);
10878 gcc_assert (GET_CODE (x) == SYMBOL_REF);
10880 fnname = XSTR (x, 0);
10881 return fnname;
10883 #endif
10885 /* These routines generate the internal representation of the DIE's for
10886 the compilation unit. Debugging information is collected by walking
10887 the declaration trees passed in from dwarf2out_decl(). */
10889 static void
10890 gen_array_type_die (tree type, dw_die_ref context_die)
10892 dw_die_ref scope_die = scope_die_for (type, context_die);
10893 dw_die_ref array_die;
10894 tree element_type;
10896 /* ??? The SGI dwarf reader fails for array of array of enum types unless
10897 the inner array type comes before the outer array type. Thus we must
10898 call gen_type_die before we call new_die. See below also. */
10899 #ifdef MIPS_DEBUGGING_INFO
10900 gen_type_die (TREE_TYPE (type), context_die);
10901 #endif
10903 array_die = new_die (DW_TAG_array_type, scope_die, type);
10904 add_name_attribute (array_die, type_tag (type));
10905 equate_type_number_to_die (type, array_die);
10907 if (TREE_CODE (type) == VECTOR_TYPE)
10909 /* The frontend feeds us a representation for the vector as a struct
10910 containing an array. Pull out the array type. */
10911 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
10912 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
10915 #if 0
10916 /* We default the array ordering. SDB will probably do
10917 the right things even if DW_AT_ordering is not present. It's not even
10918 an issue until we start to get into multidimensional arrays anyway. If
10919 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
10920 then we'll have to put the DW_AT_ordering attribute back in. (But if
10921 and when we find out that we need to put these in, we will only do so
10922 for multidimensional arrays. */
10923 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
10924 #endif
10926 #ifdef MIPS_DEBUGGING_INFO
10927 /* The SGI compilers handle arrays of unknown bound by setting
10928 AT_declaration and not emitting any subrange DIEs. */
10929 if (! TYPE_DOMAIN (type))
10930 add_AT_flag (array_die, DW_AT_declaration, 1);
10931 else
10932 #endif
10933 add_subscript_info (array_die, type);
10935 /* Add representation of the type of the elements of this array type. */
10936 element_type = TREE_TYPE (type);
10938 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10939 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10940 We work around this by disabling this feature. See also
10941 add_subscript_info. */
10942 #ifndef MIPS_DEBUGGING_INFO
10943 while (TREE_CODE (element_type) == ARRAY_TYPE)
10944 element_type = TREE_TYPE (element_type);
10946 gen_type_die (element_type, context_die);
10947 #endif
10949 add_type_attribute (array_die, element_type, 0, 0, context_die);
10952 #if 0
10953 static void
10954 gen_entry_point_die (tree decl, dw_die_ref context_die)
10956 tree origin = decl_ultimate_origin (decl);
10957 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
10959 if (origin != NULL)
10960 add_abstract_origin_attribute (decl_die, origin);
10961 else
10963 add_name_and_src_coords_attributes (decl_die, decl);
10964 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
10965 0, 0, context_die);
10968 if (DECL_ABSTRACT (decl))
10969 equate_decl_number_to_die (decl, decl_die);
10970 else
10971 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
10973 #endif
10975 /* Walk through the list of incomplete types again, trying once more to
10976 emit full debugging info for them. */
10978 static void
10979 retry_incomplete_types (void)
10981 int i;
10983 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
10984 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
10987 /* Generate a DIE to represent an inlined instance of an enumeration type. */
10989 static void
10990 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
10992 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
10994 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10995 be incomplete and such types are not marked. */
10996 add_abstract_origin_attribute (type_die, type);
10999 /* Generate a DIE to represent an inlined instance of a structure type. */
11001 static void
11002 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
11004 dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
11006 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11007 be incomplete and such types are not marked. */
11008 add_abstract_origin_attribute (type_die, type);
11011 /* Generate a DIE to represent an inlined instance of a union type. */
11013 static void
11014 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
11016 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
11018 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11019 be incomplete and such types are not marked. */
11020 add_abstract_origin_attribute (type_die, type);
11023 /* Generate a DIE to represent an enumeration type. Note that these DIEs
11024 include all of the information about the enumeration values also. Each
11025 enumerated type name/value is listed as a child of the enumerated type
11026 DIE. */
11028 static dw_die_ref
11029 gen_enumeration_type_die (tree type, dw_die_ref context_die)
11031 dw_die_ref type_die = lookup_type_die (type);
11033 if (type_die == NULL)
11035 type_die = new_die (DW_TAG_enumeration_type,
11036 scope_die_for (type, context_die), type);
11037 equate_type_number_to_die (type, type_die);
11038 add_name_attribute (type_die, type_tag (type));
11040 else if (! TYPE_SIZE (type))
11041 return type_die;
11042 else
11043 remove_AT (type_die, DW_AT_declaration);
11045 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
11046 given enum type is incomplete, do not generate the DW_AT_byte_size
11047 attribute or the DW_AT_element_list attribute. */
11048 if (TYPE_SIZE (type))
11050 tree link;
11052 TREE_ASM_WRITTEN (type) = 1;
11053 add_byte_size_attribute (type_die, type);
11054 if (TYPE_STUB_DECL (type) != NULL_TREE)
11055 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11057 /* If the first reference to this type was as the return type of an
11058 inline function, then it may not have a parent. Fix this now. */
11059 if (type_die->die_parent == NULL)
11060 add_child_die (scope_die_for (type, context_die), type_die);
11062 for (link = TYPE_VALUES (type);
11063 link != NULL; link = TREE_CHAIN (link))
11065 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
11066 tree value = TREE_VALUE (link);
11068 add_name_attribute (enum_die,
11069 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
11071 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
11072 /* DWARF2 does not provide a way of indicating whether or
11073 not enumeration constants are signed or unsigned. GDB
11074 always assumes the values are signed, so we output all
11075 values as if they were signed. That means that
11076 enumeration constants with very large unsigned values
11077 will appear to have negative values in the debugger. */
11078 add_AT_int (enum_die, DW_AT_const_value,
11079 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
11082 else
11083 add_AT_flag (type_die, DW_AT_declaration, 1);
11085 return type_die;
11088 /* Generate a DIE to represent either a real live formal parameter decl or to
11089 represent just the type of some formal parameter position in some function
11090 type.
11092 Note that this routine is a bit unusual because its argument may be a
11093 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
11094 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
11095 node. If it's the former then this function is being called to output a
11096 DIE to represent a formal parameter object (or some inlining thereof). If
11097 it's the latter, then this function is only being called to output a
11098 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
11099 argument type of some subprogram type. */
11101 static dw_die_ref
11102 gen_formal_parameter_die (tree node, dw_die_ref context_die)
11104 dw_die_ref parm_die
11105 = new_die (DW_TAG_formal_parameter, context_die, node);
11106 tree origin;
11108 switch (TREE_CODE_CLASS (TREE_CODE (node)))
11110 case tcc_declaration:
11111 origin = decl_ultimate_origin (node);
11112 if (origin != NULL)
11113 add_abstract_origin_attribute (parm_die, origin);
11114 else
11116 add_name_and_src_coords_attributes (parm_die, node);
11117 add_type_attribute (parm_die, TREE_TYPE (node),
11118 TREE_READONLY (node),
11119 TREE_THIS_VOLATILE (node),
11120 context_die);
11121 if (DECL_ARTIFICIAL (node))
11122 add_AT_flag (parm_die, DW_AT_artificial, 1);
11125 equate_decl_number_to_die (node, parm_die);
11126 if (! DECL_ABSTRACT (node))
11127 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
11129 break;
11131 case tcc_type:
11132 /* We were called with some kind of a ..._TYPE node. */
11133 add_type_attribute (parm_die, node, 0, 0, context_die);
11134 break;
11136 default:
11137 gcc_unreachable ();
11140 return parm_die;
11143 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11144 at the end of an (ANSI prototyped) formal parameters list. */
11146 static void
11147 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11149 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11152 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11153 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11154 parameters as specified in some function type specification (except for
11155 those which appear as part of a function *definition*). */
11157 static void
11158 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11160 tree link;
11161 tree formal_type = NULL;
11162 tree first_parm_type;
11163 tree arg;
11165 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11167 arg = DECL_ARGUMENTS (function_or_method_type);
11168 function_or_method_type = TREE_TYPE (function_or_method_type);
11170 else
11171 arg = NULL_TREE;
11173 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11175 /* Make our first pass over the list of formal parameter types and output a
11176 DW_TAG_formal_parameter DIE for each one. */
11177 for (link = first_parm_type; link; )
11179 dw_die_ref parm_die;
11181 formal_type = TREE_VALUE (link);
11182 if (formal_type == void_type_node)
11183 break;
11185 /* Output a (nameless) DIE to represent the formal parameter itself. */
11186 parm_die = gen_formal_parameter_die (formal_type, context_die);
11187 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11188 && link == first_parm_type)
11189 || (arg && DECL_ARTIFICIAL (arg)))
11190 add_AT_flag (parm_die, DW_AT_artificial, 1);
11192 link = TREE_CHAIN (link);
11193 if (arg)
11194 arg = TREE_CHAIN (arg);
11197 /* If this function type has an ellipsis, add a
11198 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
11199 if (formal_type != void_type_node)
11200 gen_unspecified_parameters_die (function_or_method_type, context_die);
11202 /* Make our second (and final) pass over the list of formal parameter types
11203 and output DIEs to represent those types (as necessary). */
11204 for (link = TYPE_ARG_TYPES (function_or_method_type);
11205 link && TREE_VALUE (link);
11206 link = TREE_CHAIN (link))
11207 gen_type_die (TREE_VALUE (link), context_die);
11210 /* We want to generate the DIE for TYPE so that we can generate the
11211 die for MEMBER, which has been defined; we will need to refer back
11212 to the member declaration nested within TYPE. If we're trying to
11213 generate minimal debug info for TYPE, processing TYPE won't do the
11214 trick; we need to attach the member declaration by hand. */
11216 static void
11217 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11219 gen_type_die (type, context_die);
11221 /* If we're trying to avoid duplicate debug info, we may not have
11222 emitted the member decl for this function. Emit it now. */
11223 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11224 && ! lookup_decl_die (member))
11226 dw_die_ref type_die;
11227 gcc_assert (!decl_ultimate_origin (member));
11229 push_decl_scope (type);
11230 type_die = lookup_type_die (type);
11231 if (TREE_CODE (member) == FUNCTION_DECL)
11232 gen_subprogram_die (member, type_die);
11233 else if (TREE_CODE (member) == FIELD_DECL)
11235 /* Ignore the nameless fields that are used to skip bits but handle
11236 C++ anonymous unions and structs. */
11237 if (DECL_NAME (member) != NULL_TREE
11238 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11239 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11241 gen_type_die (member_declared_type (member), type_die);
11242 gen_field_die (member, type_die);
11245 else
11246 gen_variable_die (member, type_die);
11248 pop_decl_scope ();
11252 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11253 may later generate inlined and/or out-of-line instances of. */
11255 static void
11256 dwarf2out_abstract_function (tree decl)
11258 dw_die_ref old_die;
11259 tree save_fn;
11260 tree context;
11261 int was_abstract = DECL_ABSTRACT (decl);
11263 /* Make sure we have the actual abstract inline, not a clone. */
11264 decl = DECL_ORIGIN (decl);
11266 old_die = lookup_decl_die (decl);
11267 if (old_die && get_AT (old_die, DW_AT_inline))
11268 /* We've already generated the abstract instance. */
11269 return;
11271 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11272 we don't get confused by DECL_ABSTRACT. */
11273 if (debug_info_level > DINFO_LEVEL_TERSE)
11275 context = decl_class_context (decl);
11276 if (context)
11277 gen_type_die_for_member
11278 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11281 /* Pretend we've just finished compiling this function. */
11282 save_fn = current_function_decl;
11283 current_function_decl = decl;
11285 set_decl_abstract_flags (decl, 1);
11286 dwarf2out_decl (decl);
11287 if (! was_abstract)
11288 set_decl_abstract_flags (decl, 0);
11290 current_function_decl = save_fn;
11293 /* Generate a DIE to represent a declared function (either file-scope or
11294 block-local). */
11296 static void
11297 gen_subprogram_die (tree decl, dw_die_ref context_die)
11299 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11300 tree origin = decl_ultimate_origin (decl);
11301 dw_die_ref subr_die;
11302 rtx fp_reg;
11303 tree fn_arg_types;
11304 tree outer_scope;
11305 dw_die_ref old_die = lookup_decl_die (decl);
11306 int declaration = (current_function_decl != decl
11307 || class_or_namespace_scope_p (context_die));
11309 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11310 started to generate the abstract instance of an inline, decided to output
11311 its containing class, and proceeded to emit the declaration of the inline
11312 from the member list for the class. If so, DECLARATION takes priority;
11313 we'll get back to the abstract instance when done with the class. */
11315 /* The class-scope declaration DIE must be the primary DIE. */
11316 if (origin && declaration && class_or_namespace_scope_p (context_die))
11318 origin = NULL;
11319 gcc_assert (!old_die);
11322 if (origin != NULL)
11324 gcc_assert (!declaration || local_scope_p (context_die));
11326 /* Fixup die_parent for the abstract instance of a nested
11327 inline function. */
11328 if (old_die && old_die->die_parent == NULL)
11329 add_child_die (context_die, old_die);
11331 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11332 add_abstract_origin_attribute (subr_die, origin);
11334 else if (old_die)
11336 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11337 unsigned file_index = lookup_filename (s.file);
11339 if (!get_AT_flag (old_die, DW_AT_declaration)
11340 /* We can have a normal definition following an inline one in the
11341 case of redefinition of GNU C extern inlines.
11342 It seems reasonable to use AT_specification in this case. */
11343 && !get_AT (old_die, DW_AT_inline))
11345 /* Detect and ignore this case, where we are trying to output
11346 something we have already output. */
11347 return;
11350 /* If the definition comes from the same place as the declaration,
11351 maybe use the old DIE. We always want the DIE for this function
11352 that has the *_pc attributes to be under comp_unit_die so the
11353 debugger can find it. We also need to do this for abstract
11354 instances of inlines, since the spec requires the out-of-line copy
11355 to have the same parent. For local class methods, this doesn't
11356 apply; we just use the old DIE. */
11357 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
11358 && (DECL_ARTIFICIAL (decl)
11359 || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
11360 && (get_AT_unsigned (old_die, DW_AT_decl_line)
11361 == (unsigned) s.line))))
11363 subr_die = old_die;
11365 /* Clear out the declaration attribute and the formal parameters.
11366 Do not remove all children, because it is possible that this
11367 declaration die was forced using force_decl_die(). In such
11368 cases die that forced declaration die (e.g. TAG_imported_module)
11369 is one of the children that we do not want to remove. */
11370 remove_AT (subr_die, DW_AT_declaration);
11371 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
11373 else
11375 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11376 add_AT_specification (subr_die, old_die);
11377 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11378 add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
11379 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11380 != (unsigned) s.line)
11381 add_AT_unsigned
11382 (subr_die, DW_AT_decl_line, s.line);
11385 else
11387 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11389 if (TREE_PUBLIC (decl))
11390 add_AT_flag (subr_die, DW_AT_external, 1);
11392 add_name_and_src_coords_attributes (subr_die, decl);
11393 if (debug_info_level > DINFO_LEVEL_TERSE)
11395 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11396 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11397 0, 0, context_die);
11400 add_pure_or_virtual_attribute (subr_die, decl);
11401 if (DECL_ARTIFICIAL (decl))
11402 add_AT_flag (subr_die, DW_AT_artificial, 1);
11404 if (TREE_PROTECTED (decl))
11405 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11406 else if (TREE_PRIVATE (decl))
11407 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
11410 if (declaration)
11412 if (!old_die || !get_AT (old_die, DW_AT_inline))
11414 add_AT_flag (subr_die, DW_AT_declaration, 1);
11416 /* The first time we see a member function, it is in the context of
11417 the class to which it belongs. We make sure of this by emitting
11418 the class first. The next time is the definition, which is
11419 handled above. The two may come from the same source text.
11421 Note that force_decl_die() forces function declaration die. It is
11422 later reused to represent definition. */
11423 equate_decl_number_to_die (decl, subr_die);
11426 else if (DECL_ABSTRACT (decl))
11428 if (DECL_DECLARED_INLINE_P (decl))
11430 if (cgraph_function_possibly_inlined_p (decl))
11431 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11432 else
11433 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
11435 else
11437 if (cgraph_function_possibly_inlined_p (decl))
11438 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11439 else
11440 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11443 equate_decl_number_to_die (decl, subr_die);
11445 else if (!DECL_EXTERNAL (decl))
11447 if (!old_die || !get_AT (old_die, DW_AT_inline))
11448 equate_decl_number_to_die (decl, subr_die);
11450 if (!flag_reorder_blocks_and_partition)
11452 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11453 current_function_funcdef_no);
11454 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11455 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11456 current_function_funcdef_no);
11457 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11459 add_pubname (decl, subr_die);
11460 add_arange (decl, subr_die);
11462 else
11463 { /* Do nothing for now; maybe need to duplicate die, one for
11464 hot section and ond for cold section, then use the hot/cold
11465 section begin/end labels to generate the aranges... */
11467 add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
11468 add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
11469 add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
11470 add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
11472 add_pubname (decl, subr_die);
11473 add_arange (decl, subr_die);
11474 add_arange (decl, subr_die);
11478 #ifdef MIPS_DEBUGGING_INFO
11479 /* Add a reference to the FDE for this routine. */
11480 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11481 #endif
11483 /* Define the "frame base" location for this routine. We use the
11484 frame pointer or stack pointer registers, since the RTL for local
11485 variables is relative to one of them. */
11486 if (frame_base_decl && lookup_decl_loc (frame_base_decl) != NULL)
11488 add_location_or_const_value_attribute (subr_die, frame_base_decl,
11489 DW_AT_frame_base);
11491 else
11493 fp_reg
11494 = frame_pointer_needed ? hard_frame_pointer_rtx : stack_pointer_rtx;
11495 add_AT_loc (subr_die, DW_AT_frame_base, reg_loc_descriptor (fp_reg));
11498 if (cfun->static_chain_decl)
11499 add_AT_location_description (subr_die, DW_AT_static_link,
11500 loc_descriptor_from_tree (cfun->static_chain_decl));
11503 /* Now output descriptions of the arguments for this function. This gets
11504 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
11505 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
11506 `...' at the end of the formal parameter list. In order to find out if
11507 there was a trailing ellipsis or not, we must instead look at the type
11508 associated with the FUNCTION_DECL. This will be a node of type
11509 FUNCTION_TYPE. If the chain of type nodes hanging off of this
11510 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
11511 an ellipsis at the end. */
11513 /* In the case where we are describing a mere function declaration, all we
11514 need to do here (and all we *can* do here) is to describe the *types* of
11515 its formal parameters. */
11516 if (debug_info_level <= DINFO_LEVEL_TERSE)
11518 else if (declaration)
11519 gen_formal_types_die (decl, subr_die);
11520 else
11522 /* Generate DIEs to represent all known formal parameters. */
11523 tree arg_decls = DECL_ARGUMENTS (decl);
11524 tree parm;
11526 /* When generating DIEs, generate the unspecified_parameters DIE
11527 instead if we come across the arg "__builtin_va_alist" */
11528 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
11529 if (TREE_CODE (parm) == PARM_DECL)
11531 if (DECL_NAME (parm)
11532 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
11533 "__builtin_va_alist"))
11534 gen_unspecified_parameters_die (parm, subr_die);
11535 else
11536 gen_decl_die (parm, subr_die);
11539 /* Decide whether we need an unspecified_parameters DIE at the end.
11540 There are 2 more cases to do this for: 1) the ansi ... declaration -
11541 this is detectable when the end of the arg list is not a
11542 void_type_node 2) an unprototyped function declaration (not a
11543 definition). This just means that we have no info about the
11544 parameters at all. */
11545 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
11546 if (fn_arg_types != NULL)
11548 /* This is the prototyped case, check for.... */
11549 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
11550 gen_unspecified_parameters_die (decl, subr_die);
11552 else if (DECL_INITIAL (decl) == NULL_TREE)
11553 gen_unspecified_parameters_die (decl, subr_die);
11556 /* Output Dwarf info for all of the stuff within the body of the function
11557 (if it has one - it may be just a declaration). */
11558 outer_scope = DECL_INITIAL (decl);
11560 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
11561 a function. This BLOCK actually represents the outermost binding contour
11562 for the function, i.e. the contour in which the function's formal
11563 parameters and labels get declared. Curiously, it appears that the front
11564 end doesn't actually put the PARM_DECL nodes for the current function onto
11565 the BLOCK_VARS list for this outer scope, but are strung off of the
11566 DECL_ARGUMENTS list for the function instead.
11568 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
11569 the LABEL_DECL nodes for the function however, and we output DWARF info
11570 for those in decls_for_scope. Just within the `outer_scope' there will be
11571 a BLOCK node representing the function's outermost pair of curly braces,
11572 and any blocks used for the base and member initializers of a C++
11573 constructor function. */
11574 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
11576 /* Emit a DW_TAG_variable DIE for a named return value. */
11577 if (DECL_NAME (DECL_RESULT (decl)))
11578 gen_decl_die (DECL_RESULT (decl), subr_die);
11580 current_function_has_inlines = 0;
11581 decls_for_scope (outer_scope, subr_die, 0);
11583 #if 0 && defined (MIPS_DEBUGGING_INFO)
11584 if (current_function_has_inlines)
11586 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
11587 if (! comp_unit_has_inlines)
11589 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
11590 comp_unit_has_inlines = 1;
11593 #endif
11595 /* Add the calling convention attribute if requested. */
11596 add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
11600 /* Generate a DIE to represent a declared data object. */
11602 static void
11603 gen_variable_die (tree decl, dw_die_ref context_die)
11605 tree origin = decl_ultimate_origin (decl);
11606 dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
11608 dw_die_ref old_die = lookup_decl_die (decl);
11609 int declaration = (DECL_EXTERNAL (decl)
11610 || class_or_namespace_scope_p (context_die));
11612 if (origin != NULL)
11613 add_abstract_origin_attribute (var_die, origin);
11615 /* Loop unrolling can create multiple blocks that refer to the same
11616 static variable, so we must test for the DW_AT_declaration flag.
11618 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
11619 copy decls and set the DECL_ABSTRACT flag on them instead of
11620 sharing them.
11622 ??? Duplicated blocks have been rewritten to use .debug_ranges.
11624 ??? The declare_in_namespace support causes us to get two DIEs for one
11625 variable, both of which are declarations. We want to avoid considering
11626 one to be a specification, so we must test that this DIE is not a
11627 declaration. */
11628 else if (old_die && TREE_STATIC (decl) && ! declaration
11629 && get_AT_flag (old_die, DW_AT_declaration) == 1)
11631 /* This is a definition of a C++ class level static. */
11632 add_AT_specification (var_die, old_die);
11633 if (DECL_NAME (decl))
11635 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11636 unsigned file_index = lookup_filename (s.file);
11638 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11639 add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
11641 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11642 != (unsigned) s.line)
11644 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
11647 else
11649 add_name_and_src_coords_attributes (var_die, decl);
11650 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
11651 TREE_THIS_VOLATILE (decl), context_die);
11653 if (TREE_PUBLIC (decl))
11654 add_AT_flag (var_die, DW_AT_external, 1);
11656 if (DECL_ARTIFICIAL (decl))
11657 add_AT_flag (var_die, DW_AT_artificial, 1);
11659 if (TREE_PROTECTED (decl))
11660 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
11661 else if (TREE_PRIVATE (decl))
11662 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
11665 if (declaration)
11666 add_AT_flag (var_die, DW_AT_declaration, 1);
11668 if (DECL_ABSTRACT (decl) || declaration)
11669 equate_decl_number_to_die (decl, var_die);
11671 if (! declaration && ! DECL_ABSTRACT (decl))
11673 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
11674 add_pubname (decl, var_die);
11676 else
11677 tree_add_const_value_attribute (var_die, decl);
11680 /* Generate a DIE to represent a label identifier. */
11682 static void
11683 gen_label_die (tree decl, dw_die_ref context_die)
11685 tree origin = decl_ultimate_origin (decl);
11686 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
11687 rtx insn;
11688 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11690 if (origin != NULL)
11691 add_abstract_origin_attribute (lbl_die, origin);
11692 else
11693 add_name_and_src_coords_attributes (lbl_die, decl);
11695 if (DECL_ABSTRACT (decl))
11696 equate_decl_number_to_die (decl, lbl_die);
11697 else
11699 insn = DECL_RTL_IF_SET (decl);
11701 /* Deleted labels are programmer specified labels which have been
11702 eliminated because of various optimizations. We still emit them
11703 here so that it is possible to put breakpoints on them. */
11704 if (insn
11705 && (LABEL_P (insn)
11706 || ((NOTE_P (insn)
11707 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
11709 /* When optimization is enabled (via -O) some parts of the compiler
11710 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
11711 represent source-level labels which were explicitly declared by
11712 the user. This really shouldn't be happening though, so catch
11713 it if it ever does happen. */
11714 gcc_assert (!INSN_DELETED_P (insn));
11716 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
11717 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
11722 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
11723 Add low_pc and high_pc attributes to the DIE for a block STMT. */
11725 static inline void
11726 add_high_low_attributes (tree stmt, dw_die_ref die)
11728 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11730 if (BLOCK_FRAGMENT_CHAIN (stmt))
11732 tree chain;
11734 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
11736 chain = BLOCK_FRAGMENT_CHAIN (stmt);
11739 add_ranges (chain);
11740 chain = BLOCK_FRAGMENT_CHAIN (chain);
11742 while (chain);
11743 add_ranges (NULL);
11745 else
11747 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
11748 BLOCK_NUMBER (stmt));
11749 add_AT_lbl_id (die, DW_AT_low_pc, label);
11750 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
11751 BLOCK_NUMBER (stmt));
11752 add_AT_lbl_id (die, DW_AT_high_pc, label);
11756 /* Generate a DIE for a lexical block. */
11758 static void
11759 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
11761 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
11763 if (! BLOCK_ABSTRACT (stmt))
11764 add_high_low_attributes (stmt, stmt_die);
11766 decls_for_scope (stmt, stmt_die, depth);
11769 /* Generate a DIE for an inlined subprogram. */
11771 static void
11772 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
11774 tree decl = block_ultimate_origin (stmt);
11776 /* Emit info for the abstract instance first, if we haven't yet. We
11777 must emit this even if the block is abstract, otherwise when we
11778 emit the block below (or elsewhere), we may end up trying to emit
11779 a die whose origin die hasn't been emitted, and crashing. */
11780 dwarf2out_abstract_function (decl);
11782 if (! BLOCK_ABSTRACT (stmt))
11784 dw_die_ref subr_die
11785 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
11787 add_abstract_origin_attribute (subr_die, decl);
11788 add_high_low_attributes (stmt, subr_die);
11790 decls_for_scope (stmt, subr_die, depth);
11791 current_function_has_inlines = 1;
11793 else
11794 /* We may get here if we're the outer block of function A that was
11795 inlined into function B that was inlined into function C. When
11796 generating debugging info for C, dwarf2out_abstract_function(B)
11797 would mark all inlined blocks as abstract, including this one.
11798 So, we wouldn't (and shouldn't) expect labels to be generated
11799 for this one. Instead, just emit debugging info for
11800 declarations within the block. This is particularly important
11801 in the case of initializers of arguments passed from B to us:
11802 if they're statement expressions containing declarations, we
11803 wouldn't generate dies for their abstract variables, and then,
11804 when generating dies for the real variables, we'd die (pun
11805 intended :-) */
11806 gen_lexical_block_die (stmt, context_die, depth);
11809 /* Generate a DIE for a field in a record, or structure. */
11811 static void
11812 gen_field_die (tree decl, dw_die_ref context_die)
11814 dw_die_ref decl_die;
11816 if (TREE_TYPE (decl) == error_mark_node)
11817 return;
11819 decl_die = new_die (DW_TAG_member, context_die, decl);
11820 add_name_and_src_coords_attributes (decl_die, decl);
11821 add_type_attribute (decl_die, member_declared_type (decl),
11822 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
11823 context_die);
11825 if (DECL_BIT_FIELD_TYPE (decl))
11827 add_byte_size_attribute (decl_die, decl);
11828 add_bit_size_attribute (decl_die, decl);
11829 add_bit_offset_attribute (decl_die, decl);
11832 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
11833 add_data_member_location_attribute (decl_die, decl);
11835 if (DECL_ARTIFICIAL (decl))
11836 add_AT_flag (decl_die, DW_AT_artificial, 1);
11838 if (TREE_PROTECTED (decl))
11839 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
11840 else if (TREE_PRIVATE (decl))
11841 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
11843 /* Equate decl number to die, so that we can look up this decl later on. */
11844 equate_decl_number_to_die (decl, decl_die);
11847 #if 0
11848 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
11849 Use modified_type_die instead.
11850 We keep this code here just in case these types of DIEs may be needed to
11851 represent certain things in other languages (e.g. Pascal) someday. */
11853 static void
11854 gen_pointer_type_die (tree type, dw_die_ref context_die)
11856 dw_die_ref ptr_die
11857 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
11859 equate_type_number_to_die (type, ptr_die);
11860 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
11861 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
11864 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
11865 Use modified_type_die instead.
11866 We keep this code here just in case these types of DIEs may be needed to
11867 represent certain things in other languages (e.g. Pascal) someday. */
11869 static void
11870 gen_reference_type_die (tree type, dw_die_ref context_die)
11872 dw_die_ref ref_die
11873 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
11875 equate_type_number_to_die (type, ref_die);
11876 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
11877 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
11879 #endif
11881 /* Generate a DIE for a pointer to a member type. */
11883 static void
11884 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
11886 dw_die_ref ptr_die
11887 = new_die (DW_TAG_ptr_to_member_type,
11888 scope_die_for (type, context_die), type);
11890 equate_type_number_to_die (type, ptr_die);
11891 add_AT_die_ref (ptr_die, DW_AT_containing_type,
11892 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
11893 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
11896 /* Generate the DIE for the compilation unit. */
11898 static dw_die_ref
11899 gen_compile_unit_die (const char *filename)
11901 dw_die_ref die;
11902 char producer[250];
11903 const char *language_string = lang_hooks.name;
11904 int language;
11906 die = new_die (DW_TAG_compile_unit, NULL, NULL);
11908 if (filename)
11910 add_name_attribute (die, filename);
11911 /* Don't add cwd for <built-in>. */
11912 if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
11913 add_comp_dir_attribute (die);
11916 sprintf (producer, "%s %s", language_string, version_string);
11918 #ifdef MIPS_DEBUGGING_INFO
11919 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
11920 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
11921 not appear in the producer string, the debugger reaches the conclusion
11922 that the object file is stripped and has no debugging information.
11923 To get the MIPS/SGI debugger to believe that there is debugging
11924 information in the object file, we add a -g to the producer string. */
11925 if (debug_info_level > DINFO_LEVEL_TERSE)
11926 strcat (producer, " -g");
11927 #endif
11929 add_AT_string (die, DW_AT_producer, producer);
11931 if (strcmp (language_string, "GNU C++") == 0)
11932 language = DW_LANG_C_plus_plus;
11933 else if (strcmp (language_string, "GNU Ada") == 0)
11934 language = DW_LANG_Ada95;
11935 else if (strcmp (language_string, "GNU F77") == 0)
11936 language = DW_LANG_Fortran77;
11937 else if (strcmp (language_string, "GNU F95") == 0)
11938 language = DW_LANG_Fortran95;
11939 else if (strcmp (language_string, "GNU Pascal") == 0)
11940 language = DW_LANG_Pascal83;
11941 else if (strcmp (language_string, "GNU Java") == 0)
11942 language = DW_LANG_Java;
11943 else
11944 language = DW_LANG_C89;
11946 add_AT_unsigned (die, DW_AT_language, language);
11947 return die;
11950 /* Generate a DIE for a string type. */
11952 static void
11953 gen_string_type_die (tree type, dw_die_ref context_die)
11955 dw_die_ref type_die
11956 = new_die (DW_TAG_string_type, scope_die_for (type, context_die), type);
11958 equate_type_number_to_die (type, type_die);
11960 /* ??? Fudge the string length attribute for now.
11961 TODO: add string length info. */
11962 #if 0
11963 string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
11964 bound_representation (upper_bound, 0, 'u');
11965 #endif
11968 /* Generate the DIE for a base class. */
11970 static void
11971 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
11973 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
11975 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
11976 add_data_member_location_attribute (die, binfo);
11978 if (BINFO_VIRTUAL_P (binfo))
11979 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11981 if (access == access_public_node)
11982 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
11983 else if (access == access_protected_node)
11984 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
11987 /* Generate a DIE for a class member. */
11989 static void
11990 gen_member_die (tree type, dw_die_ref context_die)
11992 tree member;
11993 tree binfo = TYPE_BINFO (type);
11994 dw_die_ref child;
11996 /* If this is not an incomplete type, output descriptions of each of its
11997 members. Note that as we output the DIEs necessary to represent the
11998 members of this record or union type, we will also be trying to output
11999 DIEs to represent the *types* of those members. However the `type'
12000 function (above) will specifically avoid generating type DIEs for member
12001 types *within* the list of member DIEs for this (containing) type except
12002 for those types (of members) which are explicitly marked as also being
12003 members of this (containing) type themselves. The g++ front- end can
12004 force any given type to be treated as a member of some other (containing)
12005 type by setting the TYPE_CONTEXT of the given (member) type to point to
12006 the TREE node representing the appropriate (containing) type. */
12008 /* First output info about the base classes. */
12009 if (binfo)
12011 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
12012 int i;
12013 tree base;
12015 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12016 gen_inheritance_die (base,
12017 (accesses ? VEC_index (tree, accesses, i)
12018 : access_public_node), context_die);
12021 /* Now output info about the data members and type members. */
12022 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
12024 /* If we thought we were generating minimal debug info for TYPE
12025 and then changed our minds, some of the member declarations
12026 may have already been defined. Don't define them again, but
12027 do put them in the right order. */
12029 child = lookup_decl_die (member);
12030 if (child)
12031 splice_child_die (context_die, child);
12032 else
12033 gen_decl_die (member, context_die);
12036 /* Now output info about the function members (if any). */
12037 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
12039 /* Don't include clones in the member list. */
12040 if (DECL_ABSTRACT_ORIGIN (member))
12041 continue;
12043 child = lookup_decl_die (member);
12044 if (child)
12045 splice_child_die (context_die, child);
12046 else
12047 gen_decl_die (member, context_die);
12051 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
12052 is set, we pretend that the type was never defined, so we only get the
12053 member DIEs needed by later specification DIEs. */
12055 static void
12056 gen_struct_or_union_type_die (tree type, dw_die_ref context_die)
12058 dw_die_ref type_die = lookup_type_die (type);
12059 dw_die_ref scope_die = 0;
12060 int nested = 0;
12061 int complete = (TYPE_SIZE (type)
12062 && (! TYPE_STUB_DECL (type)
12063 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
12064 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
12066 if (type_die && ! complete)
12067 return;
12069 if (TYPE_CONTEXT (type) != NULL_TREE
12070 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12071 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
12072 nested = 1;
12074 scope_die = scope_die_for (type, context_die);
12076 if (! type_die || (nested && scope_die == comp_unit_die))
12077 /* First occurrence of type or toplevel definition of nested class. */
12079 dw_die_ref old_die = type_die;
12081 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
12082 ? DW_TAG_structure_type : DW_TAG_union_type,
12083 scope_die, type);
12084 equate_type_number_to_die (type, type_die);
12085 if (old_die)
12086 add_AT_specification (type_die, old_die);
12087 else
12088 add_name_attribute (type_die, type_tag (type));
12090 else
12091 remove_AT (type_die, DW_AT_declaration);
12093 /* If this type has been completed, then give it a byte_size attribute and
12094 then give a list of members. */
12095 if (complete && !ns_decl)
12097 /* Prevent infinite recursion in cases where the type of some member of
12098 this type is expressed in terms of this type itself. */
12099 TREE_ASM_WRITTEN (type) = 1;
12100 add_byte_size_attribute (type_die, type);
12101 if (TYPE_STUB_DECL (type) != NULL_TREE)
12102 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12104 /* If the first reference to this type was as the return type of an
12105 inline function, then it may not have a parent. Fix this now. */
12106 if (type_die->die_parent == NULL)
12107 add_child_die (scope_die, type_die);
12109 push_decl_scope (type);
12110 gen_member_die (type, type_die);
12111 pop_decl_scope ();
12113 /* GNU extension: Record what type our vtable lives in. */
12114 if (TYPE_VFIELD (type))
12116 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
12118 gen_type_die (vtype, context_die);
12119 add_AT_die_ref (type_die, DW_AT_containing_type,
12120 lookup_type_die (vtype));
12123 else
12125 add_AT_flag (type_die, DW_AT_declaration, 1);
12127 /* We don't need to do this for function-local types. */
12128 if (TYPE_STUB_DECL (type)
12129 && ! decl_function_context (TYPE_STUB_DECL (type)))
12130 VEC_safe_push (tree, gc, incomplete_types, type);
12134 /* Generate a DIE for a subroutine _type_. */
12136 static void
12137 gen_subroutine_type_die (tree type, dw_die_ref context_die)
12139 tree return_type = TREE_TYPE (type);
12140 dw_die_ref subr_die
12141 = new_die (DW_TAG_subroutine_type,
12142 scope_die_for (type, context_die), type);
12144 equate_type_number_to_die (type, subr_die);
12145 add_prototyped_attribute (subr_die, type);
12146 add_type_attribute (subr_die, return_type, 0, 0, context_die);
12147 gen_formal_types_die (type, subr_die);
12150 /* Generate a DIE for a type definition. */
12152 static void
12153 gen_typedef_die (tree decl, dw_die_ref context_die)
12155 dw_die_ref type_die;
12156 tree origin;
12158 if (TREE_ASM_WRITTEN (decl))
12159 return;
12161 TREE_ASM_WRITTEN (decl) = 1;
12162 type_die = new_die (DW_TAG_typedef, context_die, decl);
12163 origin = decl_ultimate_origin (decl);
12164 if (origin != NULL)
12165 add_abstract_origin_attribute (type_die, origin);
12166 else
12168 tree type;
12170 add_name_and_src_coords_attributes (type_die, decl);
12171 if (DECL_ORIGINAL_TYPE (decl))
12173 type = DECL_ORIGINAL_TYPE (decl);
12175 gcc_assert (type != TREE_TYPE (decl));
12176 equate_type_number_to_die (TREE_TYPE (decl), type_die);
12178 else
12179 type = TREE_TYPE (decl);
12181 add_type_attribute (type_die, type, TREE_READONLY (decl),
12182 TREE_THIS_VOLATILE (decl), context_die);
12185 if (DECL_ABSTRACT (decl))
12186 equate_decl_number_to_die (decl, type_die);
12189 /* Generate a type description DIE. */
12191 static void
12192 gen_type_die (tree type, dw_die_ref context_die)
12194 int need_pop;
12196 if (type == NULL_TREE || type == error_mark_node)
12197 return;
12199 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12200 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
12202 if (TREE_ASM_WRITTEN (type))
12203 return;
12205 /* Prevent broken recursion; we can't hand off to the same type. */
12206 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
12208 TREE_ASM_WRITTEN (type) = 1;
12209 gen_decl_die (TYPE_NAME (type), context_die);
12210 return;
12213 /* We are going to output a DIE to represent the unqualified version
12214 of this type (i.e. without any const or volatile qualifiers) so
12215 get the main variant (i.e. the unqualified version) of this type
12216 now. (Vectors are special because the debugging info is in the
12217 cloned type itself). */
12218 if (TREE_CODE (type) != VECTOR_TYPE)
12219 type = type_main_variant (type);
12221 if (TREE_ASM_WRITTEN (type))
12222 return;
12224 switch (TREE_CODE (type))
12226 case ERROR_MARK:
12227 break;
12229 case POINTER_TYPE:
12230 case REFERENCE_TYPE:
12231 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
12232 ensures that the gen_type_die recursion will terminate even if the
12233 type is recursive. Recursive types are possible in Ada. */
12234 /* ??? We could perhaps do this for all types before the switch
12235 statement. */
12236 TREE_ASM_WRITTEN (type) = 1;
12238 /* For these types, all that is required is that we output a DIE (or a
12239 set of DIEs) to represent the "basis" type. */
12240 gen_type_die (TREE_TYPE (type), context_die);
12241 break;
12243 case OFFSET_TYPE:
12244 /* This code is used for C++ pointer-to-data-member types.
12245 Output a description of the relevant class type. */
12246 gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
12248 /* Output a description of the type of the object pointed to. */
12249 gen_type_die (TREE_TYPE (type), context_die);
12251 /* Now output a DIE to represent this pointer-to-data-member type
12252 itself. */
12253 gen_ptr_to_mbr_type_die (type, context_die);
12254 break;
12256 case FUNCTION_TYPE:
12257 /* Force out return type (in case it wasn't forced out already). */
12258 gen_type_die (TREE_TYPE (type), context_die);
12259 gen_subroutine_type_die (type, context_die);
12260 break;
12262 case METHOD_TYPE:
12263 /* Force out return type (in case it wasn't forced out already). */
12264 gen_type_die (TREE_TYPE (type), context_die);
12265 gen_subroutine_type_die (type, context_die);
12266 break;
12268 case ARRAY_TYPE:
12269 if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
12271 gen_type_die (TREE_TYPE (type), context_die);
12272 gen_string_type_die (type, context_die);
12274 else
12275 gen_array_type_die (type, context_die);
12276 break;
12278 case VECTOR_TYPE:
12279 gen_array_type_die (type, context_die);
12280 break;
12282 case ENUMERAL_TYPE:
12283 case RECORD_TYPE:
12284 case UNION_TYPE:
12285 case QUAL_UNION_TYPE:
12286 /* If this is a nested type whose containing class hasn't been written
12287 out yet, writing it out will cover this one, too. This does not apply
12288 to instantiations of member class templates; they need to be added to
12289 the containing class as they are generated. FIXME: This hurts the
12290 idea of combining type decls from multiple TUs, since we can't predict
12291 what set of template instantiations we'll get. */
12292 if (TYPE_CONTEXT (type)
12293 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12294 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
12296 gen_type_die (TYPE_CONTEXT (type), context_die);
12298 if (TREE_ASM_WRITTEN (type))
12299 return;
12301 /* If that failed, attach ourselves to the stub. */
12302 push_decl_scope (TYPE_CONTEXT (type));
12303 context_die = lookup_type_die (TYPE_CONTEXT (type));
12304 need_pop = 1;
12306 else
12308 declare_in_namespace (type, context_die);
12309 need_pop = 0;
12312 if (TREE_CODE (type) == ENUMERAL_TYPE)
12313 gen_enumeration_type_die (type, context_die);
12314 else
12315 gen_struct_or_union_type_die (type, context_die);
12317 if (need_pop)
12318 pop_decl_scope ();
12320 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
12321 it up if it is ever completed. gen_*_type_die will set it for us
12322 when appropriate. */
12323 return;
12325 case VOID_TYPE:
12326 case INTEGER_TYPE:
12327 case REAL_TYPE:
12328 case COMPLEX_TYPE:
12329 case BOOLEAN_TYPE:
12330 case CHAR_TYPE:
12331 /* No DIEs needed for fundamental types. */
12332 break;
12334 case LANG_TYPE:
12335 /* No Dwarf representation currently defined. */
12336 break;
12338 default:
12339 gcc_unreachable ();
12342 TREE_ASM_WRITTEN (type) = 1;
12345 /* Generate a DIE for a tagged type instantiation. */
12347 static void
12348 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
12350 if (type == NULL_TREE || type == error_mark_node)
12351 return;
12353 /* We are going to output a DIE to represent the unqualified version of
12354 this type (i.e. without any const or volatile qualifiers) so make sure
12355 that we have the main variant (i.e. the unqualified version) of this
12356 type now. */
12357 gcc_assert (type == type_main_variant (type));
12359 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
12360 an instance of an unresolved type. */
12362 switch (TREE_CODE (type))
12364 case ERROR_MARK:
12365 break;
12367 case ENUMERAL_TYPE:
12368 gen_inlined_enumeration_type_die (type, context_die);
12369 break;
12371 case RECORD_TYPE:
12372 gen_inlined_structure_type_die (type, context_die);
12373 break;
12375 case UNION_TYPE:
12376 case QUAL_UNION_TYPE:
12377 gen_inlined_union_type_die (type, context_die);
12378 break;
12380 default:
12381 gcc_unreachable ();
12385 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12386 things which are local to the given block. */
12388 static void
12389 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
12391 int must_output_die = 0;
12392 tree origin;
12393 tree decl;
12394 enum tree_code origin_code;
12396 /* Ignore blocks that are NULL. */
12397 if (stmt == NULL_TREE)
12398 return;
12400 /* If the block is one fragment of a non-contiguous block, do not
12401 process the variables, since they will have been done by the
12402 origin block. Do process subblocks. */
12403 if (BLOCK_FRAGMENT_ORIGIN (stmt))
12405 tree sub;
12407 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
12408 gen_block_die (sub, context_die, depth + 1);
12410 return;
12413 /* Determine the "ultimate origin" of this block. This block may be an
12414 inlined instance of an inlined instance of inline function, so we have
12415 to trace all of the way back through the origin chain to find out what
12416 sort of node actually served as the original seed for the creation of
12417 the current block. */
12418 origin = block_ultimate_origin (stmt);
12419 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
12421 /* Determine if we need to output any Dwarf DIEs at all to represent this
12422 block. */
12423 if (origin_code == FUNCTION_DECL)
12424 /* The outer scopes for inlinings *must* always be represented. We
12425 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
12426 must_output_die = 1;
12427 else
12429 /* In the case where the current block represents an inlining of the
12430 "body block" of an inline function, we must *NOT* output any DIE for
12431 this block because we have already output a DIE to represent the whole
12432 inlined function scope and the "body block" of any function doesn't
12433 really represent a different scope according to ANSI C rules. So we
12434 check here to make sure that this block does not represent a "body
12435 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
12436 if (! is_body_block (origin ? origin : stmt))
12438 /* Determine if this block directly contains any "significant"
12439 local declarations which we will need to output DIEs for. */
12440 if (debug_info_level > DINFO_LEVEL_TERSE)
12441 /* We are not in terse mode so *any* local declaration counts
12442 as being a "significant" one. */
12443 must_output_die = (BLOCK_VARS (stmt) != NULL
12444 && (TREE_USED (stmt)
12445 || TREE_ASM_WRITTEN (stmt)
12446 || BLOCK_ABSTRACT (stmt)));
12447 else
12448 /* We are in terse mode, so only local (nested) function
12449 definitions count as "significant" local declarations. */
12450 for (decl = BLOCK_VARS (stmt);
12451 decl != NULL; decl = TREE_CHAIN (decl))
12452 if (TREE_CODE (decl) == FUNCTION_DECL
12453 && DECL_INITIAL (decl))
12455 must_output_die = 1;
12456 break;
12461 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
12462 DIE for any block which contains no significant local declarations at
12463 all. Rather, in such cases we just call `decls_for_scope' so that any
12464 needed Dwarf info for any sub-blocks will get properly generated. Note
12465 that in terse mode, our definition of what constitutes a "significant"
12466 local declaration gets restricted to include only inlined function
12467 instances and local (nested) function definitions. */
12468 if (must_output_die)
12470 if (origin_code == FUNCTION_DECL)
12471 gen_inlined_subroutine_die (stmt, context_die, depth);
12472 else
12473 gen_lexical_block_die (stmt, context_die, depth);
12475 else
12476 decls_for_scope (stmt, context_die, depth);
12479 /* Generate all of the decls declared within a given scope and (recursively)
12480 all of its sub-blocks. */
12482 static void
12483 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
12485 tree decl;
12486 tree subblocks;
12488 /* Ignore NULL blocks. */
12489 if (stmt == NULL_TREE)
12490 return;
12492 if (TREE_USED (stmt))
12494 /* Output the DIEs to represent all of the data objects and typedefs
12495 declared directly within this block but not within any nested
12496 sub-blocks. Also, nested function and tag DIEs have been
12497 generated with a parent of NULL; fix that up now. */
12498 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
12500 dw_die_ref die;
12502 if (TREE_CODE (decl) == FUNCTION_DECL)
12503 die = lookup_decl_die (decl);
12504 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
12505 die = lookup_type_die (TREE_TYPE (decl));
12506 else
12507 die = NULL;
12509 if (die != NULL && die->die_parent == NULL)
12510 add_child_die (context_die, die);
12511 else
12512 gen_decl_die (decl, context_die);
12516 /* If we're at -g1, we're not interested in subblocks. */
12517 if (debug_info_level <= DINFO_LEVEL_TERSE)
12518 return;
12520 /* Output the DIEs to represent all sub-blocks (and the items declared
12521 therein) of this block. */
12522 for (subblocks = BLOCK_SUBBLOCKS (stmt);
12523 subblocks != NULL;
12524 subblocks = BLOCK_CHAIN (subblocks))
12525 gen_block_die (subblocks, context_die, depth + 1);
12528 /* Is this a typedef we can avoid emitting? */
12530 static inline int
12531 is_redundant_typedef (tree decl)
12533 if (TYPE_DECL_IS_STUB (decl))
12534 return 1;
12536 if (DECL_ARTIFICIAL (decl)
12537 && DECL_CONTEXT (decl)
12538 && is_tagged_type (DECL_CONTEXT (decl))
12539 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
12540 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
12541 /* Also ignore the artificial member typedef for the class name. */
12542 return 1;
12544 return 0;
12547 /* Returns the DIE for decl. A DIE will always be returned. */
12549 static dw_die_ref
12550 force_decl_die (tree decl)
12552 dw_die_ref decl_die;
12553 unsigned saved_external_flag;
12554 tree save_fn = NULL_TREE;
12555 decl_die = lookup_decl_die (decl);
12556 if (!decl_die)
12558 dw_die_ref context_die;
12559 tree decl_context = DECL_CONTEXT (decl);
12560 if (decl_context)
12562 /* Find die that represents this context. */
12563 if (TYPE_P (decl_context))
12564 context_die = force_type_die (decl_context);
12565 else
12566 context_die = force_decl_die (decl_context);
12568 else
12569 context_die = comp_unit_die;
12571 switch (TREE_CODE (decl))
12573 case FUNCTION_DECL:
12574 /* Clear current_function_decl, so that gen_subprogram_die thinks
12575 that this is a declaration. At this point, we just want to force
12576 declaration die. */
12577 save_fn = current_function_decl;
12578 current_function_decl = NULL_TREE;
12579 gen_subprogram_die (decl, context_die);
12580 current_function_decl = save_fn;
12581 break;
12583 case VAR_DECL:
12584 /* Set external flag to force declaration die. Restore it after
12585 gen_decl_die() call. */
12586 saved_external_flag = DECL_EXTERNAL (decl);
12587 DECL_EXTERNAL (decl) = 1;
12588 gen_decl_die (decl, context_die);
12589 DECL_EXTERNAL (decl) = saved_external_flag;
12590 break;
12592 case NAMESPACE_DECL:
12593 dwarf2out_decl (decl);
12594 break;
12596 default:
12597 gcc_unreachable ();
12600 /* We should be able to find the DIE now. */
12601 if (!decl_die)
12602 decl_die = lookup_decl_die (decl);
12603 gcc_assert (decl_die);
12606 return decl_die;
12609 /* Returns the DIE for TYPE. A DIE is always returned. */
12611 static dw_die_ref
12612 force_type_die (tree type)
12614 dw_die_ref type_die;
12616 type_die = lookup_type_die (type);
12617 if (!type_die)
12619 dw_die_ref context_die;
12620 if (TYPE_CONTEXT (type))
12621 if (TYPE_P (TYPE_CONTEXT (type)))
12622 context_die = force_type_die (TYPE_CONTEXT (type));
12623 else
12624 context_die = force_decl_die (TYPE_CONTEXT (type));
12625 else
12626 context_die = comp_unit_die;
12628 gen_type_die (type, context_die);
12629 type_die = lookup_type_die (type);
12630 gcc_assert (type_die);
12632 return type_die;
12635 /* Force out any required namespaces to be able to output DECL,
12636 and return the new context_die for it, if it's changed. */
12638 static dw_die_ref
12639 setup_namespace_context (tree thing, dw_die_ref context_die)
12641 tree context = (DECL_P (thing)
12642 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
12643 if (context && TREE_CODE (context) == NAMESPACE_DECL)
12644 /* Force out the namespace. */
12645 context_die = force_decl_die (context);
12647 return context_die;
12650 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
12651 type) within its namespace, if appropriate.
12653 For compatibility with older debuggers, namespace DIEs only contain
12654 declarations; all definitions are emitted at CU scope. */
12656 static void
12657 declare_in_namespace (tree thing, dw_die_ref context_die)
12659 dw_die_ref ns_context;
12661 if (debug_info_level <= DINFO_LEVEL_TERSE)
12662 return;
12664 /* If this decl is from an inlined function, then don't try to emit it in its
12665 namespace, as we will get confused. It would have already been emitted
12666 when the abstract instance of the inline function was emitted anyways. */
12667 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
12668 return;
12670 ns_context = setup_namespace_context (thing, context_die);
12672 if (ns_context != context_die)
12674 if (DECL_P (thing))
12675 gen_decl_die (thing, ns_context);
12676 else
12677 gen_type_die (thing, ns_context);
12681 /* Generate a DIE for a namespace or namespace alias. */
12683 static void
12684 gen_namespace_die (tree decl)
12686 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
12688 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
12689 they are an alias of. */
12690 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
12692 /* Output a real namespace. */
12693 dw_die_ref namespace_die
12694 = new_die (DW_TAG_namespace, context_die, decl);
12695 add_name_and_src_coords_attributes (namespace_die, decl);
12696 equate_decl_number_to_die (decl, namespace_die);
12698 else
12700 /* Output a namespace alias. */
12702 /* Force out the namespace we are an alias of, if necessary. */
12703 dw_die_ref origin_die
12704 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
12706 /* Now create the namespace alias DIE. */
12707 dw_die_ref namespace_die
12708 = new_die (DW_TAG_imported_declaration, context_die, decl);
12709 add_name_and_src_coords_attributes (namespace_die, decl);
12710 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
12711 equate_decl_number_to_die (decl, namespace_die);
12715 /* Generate Dwarf debug information for a decl described by DECL. */
12717 static void
12718 gen_decl_die (tree decl, dw_die_ref context_die)
12720 tree origin;
12722 if (DECL_P (decl) && DECL_IGNORED_P (decl))
12723 return;
12725 switch (TREE_CODE (decl))
12727 case ERROR_MARK:
12728 break;
12730 case CONST_DECL:
12731 /* The individual enumerators of an enum type get output when we output
12732 the Dwarf representation of the relevant enum type itself. */
12733 break;
12735 case FUNCTION_DECL:
12736 /* Don't output any DIEs to represent mere function declarations,
12737 unless they are class members or explicit block externs. */
12738 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
12739 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
12740 break;
12742 #if 0
12743 /* FIXME */
12744 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
12745 on local redeclarations of global functions. That seems broken. */
12746 if (current_function_decl != decl)
12747 /* This is only a declaration. */;
12748 #endif
12750 /* If we're emitting a clone, emit info for the abstract instance. */
12751 if (DECL_ORIGIN (decl) != decl)
12752 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
12754 /* If we're emitting an out-of-line copy of an inline function,
12755 emit info for the abstract instance and set up to refer to it. */
12756 else if (cgraph_function_possibly_inlined_p (decl)
12757 && ! DECL_ABSTRACT (decl)
12758 && ! class_or_namespace_scope_p (context_die)
12759 /* dwarf2out_abstract_function won't emit a die if this is just
12760 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
12761 that case, because that works only if we have a die. */
12762 && DECL_INITIAL (decl) != NULL_TREE)
12764 dwarf2out_abstract_function (decl);
12765 set_decl_origin_self (decl);
12768 /* Otherwise we're emitting the primary DIE for this decl. */
12769 else if (debug_info_level > DINFO_LEVEL_TERSE)
12771 /* Before we describe the FUNCTION_DECL itself, make sure that we
12772 have described its return type. */
12773 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
12775 /* And its virtual context. */
12776 if (DECL_VINDEX (decl) != NULL_TREE)
12777 gen_type_die (DECL_CONTEXT (decl), context_die);
12779 /* And its containing type. */
12780 origin = decl_class_context (decl);
12781 if (origin != NULL_TREE)
12782 gen_type_die_for_member (origin, decl, context_die);
12784 /* And its containing namespace. */
12785 declare_in_namespace (decl, context_die);
12788 /* Now output a DIE to represent the function itself. */
12789 gen_subprogram_die (decl, context_die);
12790 break;
12792 case TYPE_DECL:
12793 /* If we are in terse mode, don't generate any DIEs to represent any
12794 actual typedefs. */
12795 if (debug_info_level <= DINFO_LEVEL_TERSE)
12796 break;
12798 /* In the special case of a TYPE_DECL node representing the declaration
12799 of some type tag, if the given TYPE_DECL is marked as having been
12800 instantiated from some other (original) TYPE_DECL node (e.g. one which
12801 was generated within the original definition of an inline function) we
12802 have to generate a special (abbreviated) DW_TAG_structure_type,
12803 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
12804 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
12806 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
12807 break;
12810 if (is_redundant_typedef (decl))
12811 gen_type_die (TREE_TYPE (decl), context_die);
12812 else
12813 /* Output a DIE to represent the typedef itself. */
12814 gen_typedef_die (decl, context_die);
12815 break;
12817 case LABEL_DECL:
12818 if (debug_info_level >= DINFO_LEVEL_NORMAL)
12819 gen_label_die (decl, context_die);
12820 break;
12822 case VAR_DECL:
12823 case RESULT_DECL:
12824 /* If we are in terse mode, don't generate any DIEs to represent any
12825 variable declarations or definitions. */
12826 if (debug_info_level <= DINFO_LEVEL_TERSE)
12827 break;
12829 /* Output any DIEs that are needed to specify the type of this data
12830 object. */
12831 gen_type_die (TREE_TYPE (decl), context_die);
12833 /* And its containing type. */
12834 origin = decl_class_context (decl);
12835 if (origin != NULL_TREE)
12836 gen_type_die_for_member (origin, decl, context_die);
12838 /* And its containing namespace. */
12839 declare_in_namespace (decl, context_die);
12841 /* Now output the DIE to represent the data object itself. This gets
12842 complicated because of the possibility that the VAR_DECL really
12843 represents an inlined instance of a formal parameter for an inline
12844 function. */
12845 origin = decl_ultimate_origin (decl);
12846 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
12847 gen_formal_parameter_die (decl, context_die);
12848 else
12849 gen_variable_die (decl, context_die);
12850 break;
12852 case FIELD_DECL:
12853 /* Ignore the nameless fields that are used to skip bits but handle C++
12854 anonymous unions and structs. */
12855 if (DECL_NAME (decl) != NULL_TREE
12856 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
12857 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
12859 gen_type_die (member_declared_type (decl), context_die);
12860 gen_field_die (decl, context_die);
12862 break;
12864 case PARM_DECL:
12865 gen_type_die (TREE_TYPE (decl), context_die);
12866 gen_formal_parameter_die (decl, context_die);
12867 break;
12869 case NAMESPACE_DECL:
12870 gen_namespace_die (decl);
12871 break;
12873 default:
12874 /* Probably some frontend-internal decl. Assume we don't care. */
12875 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
12876 break;
12880 /* Add Ada "use" clause information for SGI Workshop debugger. */
12882 void
12883 dwarf2out_add_library_unit_info (const char *filename, const char *context_list)
12885 unsigned int file_index;
12887 if (filename != NULL)
12889 dw_die_ref unit_die = new_die (DW_TAG_module, comp_unit_die, NULL);
12890 tree context_list_decl
12891 = build_decl (LABEL_DECL, get_identifier (context_list),
12892 void_type_node);
12894 TREE_PUBLIC (context_list_decl) = TRUE;
12895 add_name_attribute (unit_die, context_list);
12896 file_index = lookup_filename (filename);
12897 add_AT_unsigned (unit_die, DW_AT_decl_file, file_index);
12898 add_pubname (context_list_decl, unit_die);
12902 /* Output debug information for global decl DECL. Called from toplev.c after
12903 compilation proper has finished. */
12905 static void
12906 dwarf2out_global_decl (tree decl)
12908 /* Output DWARF2 information for file-scope tentative data object
12909 declarations, file-scope (extern) function declarations (which had no
12910 corresponding body) and file-scope tagged type declarations and
12911 definitions which have not yet been forced out. */
12912 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
12913 dwarf2out_decl (decl);
12916 /* Output debug information for type decl DECL. Called from toplev.c
12917 and from language front ends (to record built-in types). */
12918 static void
12919 dwarf2out_type_decl (tree decl, int local)
12921 if (!local)
12922 dwarf2out_decl (decl);
12925 /* Output debug information for imported module or decl. */
12927 static void
12928 dwarf2out_imported_module_or_decl (tree decl, tree context)
12930 dw_die_ref imported_die, at_import_die;
12931 dw_die_ref scope_die;
12932 unsigned file_index;
12933 expanded_location xloc;
12935 if (debug_info_level <= DINFO_LEVEL_TERSE)
12936 return;
12938 gcc_assert (decl);
12940 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
12941 We need decl DIE for reference and scope die. First, get DIE for the decl
12942 itself. */
12944 /* Get the scope die for decl context. Use comp_unit_die for global module
12945 or decl. If die is not found for non globals, force new die. */
12946 if (!context)
12947 scope_die = comp_unit_die;
12948 else if (TYPE_P (context))
12949 scope_die = force_type_die (context);
12950 else
12951 scope_die = force_decl_die (context);
12953 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
12954 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
12955 at_import_die = force_type_die (TREE_TYPE (decl));
12956 else
12958 at_import_die = lookup_decl_die (decl);
12959 if (!at_import_die)
12961 /* If we're trying to avoid duplicate debug info, we may not have
12962 emitted the member decl for this field. Emit it now. */
12963 if (TREE_CODE (decl) == FIELD_DECL)
12965 tree type = DECL_CONTEXT (decl);
12966 dw_die_ref type_context_die;
12968 if (TYPE_CONTEXT (type))
12969 if (TYPE_P (TYPE_CONTEXT (type)))
12970 type_context_die = force_type_die (TYPE_CONTEXT (type));
12971 else
12972 type_context_die = force_decl_die (TYPE_CONTEXT (type));
12973 else
12974 type_context_die = comp_unit_die;
12975 gen_type_die_for_member (type, decl, type_context_die);
12977 at_import_die = force_decl_die (decl);
12981 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
12982 if (TREE_CODE (decl) == NAMESPACE_DECL)
12983 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
12984 else
12985 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
12987 xloc = expand_location (input_location);
12988 file_index = lookup_filename (xloc.file);
12989 add_AT_unsigned (imported_die, DW_AT_decl_file, file_index);
12990 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
12991 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
12994 /* Write the debugging output for DECL. */
12996 void
12997 dwarf2out_decl (tree decl)
12999 dw_die_ref context_die = comp_unit_die;
13001 switch (TREE_CODE (decl))
13003 case ERROR_MARK:
13004 return;
13006 case FUNCTION_DECL:
13007 /* What we would really like to do here is to filter out all mere
13008 file-scope declarations of file-scope functions which are never
13009 referenced later within this translation unit (and keep all of ones
13010 that *are* referenced later on) but we aren't clairvoyant, so we have
13011 no idea which functions will be referenced in the future (i.e. later
13012 on within the current translation unit). So here we just ignore all
13013 file-scope function declarations which are not also definitions. If
13014 and when the debugger needs to know something about these functions,
13015 it will have to hunt around and find the DWARF information associated
13016 with the definition of the function.
13018 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
13019 nodes represent definitions and which ones represent mere
13020 declarations. We have to check DECL_INITIAL instead. That's because
13021 the C front-end supports some weird semantics for "extern inline"
13022 function definitions. These can get inlined within the current
13023 translation unit (an thus, we need to generate Dwarf info for their
13024 abstract instances so that the Dwarf info for the concrete inlined
13025 instances can have something to refer to) but the compiler never
13026 generates any out-of-lines instances of such things (despite the fact
13027 that they *are* definitions).
13029 The important point is that the C front-end marks these "extern
13030 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
13031 them anyway. Note that the C++ front-end also plays some similar games
13032 for inline function definitions appearing within include files which
13033 also contain `#pragma interface' pragmas. */
13034 if (DECL_INITIAL (decl) == NULL_TREE)
13035 return;
13037 /* If we're a nested function, initially use a parent of NULL; if we're
13038 a plain function, this will be fixed up in decls_for_scope. If
13039 we're a method, it will be ignored, since we already have a DIE. */
13040 if (decl_function_context (decl)
13041 /* But if we're in terse mode, we don't care about scope. */
13042 && debug_info_level > DINFO_LEVEL_TERSE)
13043 context_die = NULL;
13044 break;
13046 case VAR_DECL:
13047 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
13048 declaration and if the declaration was never even referenced from
13049 within this entire compilation unit. We suppress these DIEs in
13050 order to save space in the .debug section (by eliminating entries
13051 which are probably useless). Note that we must not suppress
13052 block-local extern declarations (whether used or not) because that
13053 would screw-up the debugger's name lookup mechanism and cause it to
13054 miss things which really ought to be in scope at a given point. */
13055 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
13056 return;
13058 /* If we are in terse mode, don't generate any DIEs to represent any
13059 variable declarations or definitions. */
13060 if (debug_info_level <= DINFO_LEVEL_TERSE)
13061 return;
13062 break;
13064 case NAMESPACE_DECL:
13065 if (debug_info_level <= DINFO_LEVEL_TERSE)
13066 return;
13067 if (lookup_decl_die (decl) != NULL)
13068 return;
13069 break;
13071 case TYPE_DECL:
13072 /* Don't emit stubs for types unless they are needed by other DIEs. */
13073 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
13074 return;
13076 /* Don't bother trying to generate any DIEs to represent any of the
13077 normal built-in types for the language we are compiling. */
13078 if (DECL_IS_BUILTIN (decl))
13080 /* OK, we need to generate one for `bool' so GDB knows what type
13081 comparisons have. */
13082 if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
13083 == DW_LANG_C_plus_plus)
13084 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
13085 && ! DECL_IGNORED_P (decl))
13086 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
13088 return;
13091 /* If we are in terse mode, don't generate any DIEs for types. */
13092 if (debug_info_level <= DINFO_LEVEL_TERSE)
13093 return;
13095 /* If we're a function-scope tag, initially use a parent of NULL;
13096 this will be fixed up in decls_for_scope. */
13097 if (decl_function_context (decl))
13098 context_die = NULL;
13100 break;
13102 default:
13103 return;
13106 gen_decl_die (decl, context_die);
13109 /* Output a marker (i.e. a label) for the beginning of the generated code for
13110 a lexical block. */
13112 static void
13113 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13114 unsigned int blocknum)
13116 current_function_section (current_function_decl);
13117 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
13120 /* Output a marker (i.e. a label) for the end of the generated code for a
13121 lexical block. */
13123 static void
13124 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
13126 current_function_section (current_function_decl);
13127 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
13130 /* Returns nonzero if it is appropriate not to emit any debugging
13131 information for BLOCK, because it doesn't contain any instructions.
13133 Don't allow this for blocks with nested functions or local classes
13134 as we would end up with orphans, and in the presence of scheduling
13135 we may end up calling them anyway. */
13137 static bool
13138 dwarf2out_ignore_block (tree block)
13140 tree decl;
13142 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
13143 if (TREE_CODE (decl) == FUNCTION_DECL
13144 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
13145 return 0;
13147 return 1;
13150 /* Lookup FILE_NAME (in the list of filenames that we know about here in
13151 dwarf2out.c) and return its "index". The index of each (known) filename is
13152 just a unique number which is associated with only that one filename. We
13153 need such numbers for the sake of generating labels (in the .debug_sfnames
13154 section) and references to those files numbers (in the .debug_srcinfo
13155 and.debug_macinfo sections). If the filename given as an argument is not
13156 found in our current list, add it to the list and assign it the next
13157 available unique index number. In order to speed up searches, we remember
13158 the index of the filename was looked up last. This handles the majority of
13159 all searches. */
13161 static unsigned
13162 lookup_filename (const char *file_name)
13164 size_t i, n;
13165 char *save_file_name;
13167 /* Check to see if the file name that was searched on the previous
13168 call matches this file name. If so, return the index. */
13169 if (file_table_last_lookup_index != 0)
13171 const char *last
13172 = VARRAY_CHAR_PTR (file_table, file_table_last_lookup_index);
13173 if (strcmp (file_name, last) == 0)
13174 return file_table_last_lookup_index;
13177 /* Didn't match the previous lookup, search the table. */
13178 n = VARRAY_ACTIVE_SIZE (file_table);
13179 for (i = 1; i < n; i++)
13180 if (strcmp (file_name, VARRAY_CHAR_PTR (file_table, i)) == 0)
13182 file_table_last_lookup_index = i;
13183 return i;
13186 /* Add the new entry to the end of the filename table. */
13187 file_table_last_lookup_index = n;
13188 save_file_name = (char *) ggc_strdup (file_name);
13189 VARRAY_PUSH_CHAR_PTR (file_table, save_file_name);
13190 VARRAY_PUSH_UINT (file_table_emitted, 0);
13192 /* If the assembler is emitting the file table, and we aren't eliminating
13193 unused debug types, then we must emit .file here. If we are eliminating
13194 unused debug types, then this will be done by the maybe_emit_file call in
13195 prune_unused_types_walk_attribs. */
13197 if (DWARF2_ASM_LINE_DEBUG_INFO && ! flag_eliminate_unused_debug_types)
13198 maybe_emit_file (i);
13200 return i;
13203 static int
13204 maybe_emit_file (int fileno)
13206 if (DWARF2_ASM_LINE_DEBUG_INFO && fileno > 0)
13208 if (!VARRAY_UINT (file_table_emitted, fileno))
13210 VARRAY_UINT (file_table_emitted, fileno) = ++emitcount;
13211 fprintf (asm_out_file, "\t.file %u ",
13212 VARRAY_UINT (file_table_emitted, fileno));
13213 output_quoted_string (asm_out_file,
13214 VARRAY_CHAR_PTR (file_table, fileno));
13215 fputc ('\n', asm_out_file);
13217 return VARRAY_UINT (file_table_emitted, fileno);
13219 else
13220 return fileno;
13223 static void
13224 init_file_table (void)
13226 /* Allocate the initial hunk of the file_table. */
13227 VARRAY_CHAR_PTR_INIT (file_table, 64, "file_table");
13228 VARRAY_UINT_INIT (file_table_emitted, 64, "file_table_emitted");
13230 /* Skip the first entry - file numbers begin at 1. */
13231 VARRAY_PUSH_CHAR_PTR (file_table, NULL);
13232 VARRAY_PUSH_UINT (file_table_emitted, 0);
13233 file_table_last_lookup_index = 0;
13236 /* Called by the final INSN scan whenever we see a var location. We
13237 use it to drop labels in the right places, and throw the location in
13238 our lookup table. */
13240 static void
13241 dwarf2out_var_location (rtx loc_note)
13243 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13244 struct var_loc_node *newloc;
13245 rtx prev_insn;
13246 static rtx last_insn;
13247 static const char *last_label;
13248 tree decl;
13250 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13251 return;
13252 prev_insn = PREV_INSN (loc_note);
13254 newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13255 /* If the insn we processed last time is the previous insn
13256 and it is also a var location note, use the label we emitted
13257 last time. */
13258 if (last_insn != NULL_RTX
13259 && last_insn == prev_insn
13260 && NOTE_P (prev_insn)
13261 && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13263 newloc->label = last_label;
13265 else
13267 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13268 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13269 loclabel_num++;
13270 newloc->label = ggc_strdup (loclabel);
13272 newloc->var_loc_note = loc_note;
13273 newloc->next = NULL;
13275 if (cfun
13276 && (last_text_section == in_unlikely_executed_text
13277 || (last_text_section == in_named
13278 && last_text_section_name == cfun->unlikely_text_section_name)))
13279 newloc->section_label = cfun->cold_section_label;
13280 else
13281 newloc->section_label = text_section_label;
13283 last_insn = loc_note;
13284 last_label = newloc->label;
13285 decl = NOTE_VAR_LOCATION_DECL (loc_note);
13286 if (DECL_DEBUG_EXPR (decl) && DECL_DEBUG_EXPR_IS_FROM (decl)
13287 && DECL_P (DECL_DEBUG_EXPR (decl)))
13288 decl = DECL_DEBUG_EXPR (decl);
13289 add_var_loc_to_decl (decl, newloc);
13292 /* We need to reset the locations at the beginning of each
13293 function. We can't do this in the end_function hook, because the
13294 declarations that use the locations won't have been outputted when
13295 that hook is called. */
13297 static void
13298 dwarf2out_begin_function (tree unused ATTRIBUTE_UNUSED)
13300 htab_empty (decl_loc_table);
13303 /* Output a label to mark the beginning of a source code line entry
13304 and record information relating to this source line, in
13305 'line_info_table' for later output of the .debug_line section. */
13307 static void
13308 dwarf2out_source_line (unsigned int line, const char *filename)
13310 if (debug_info_level >= DINFO_LEVEL_NORMAL
13311 && line != 0)
13313 current_function_section (current_function_decl);
13315 /* If requested, emit something human-readable. */
13316 if (flag_debug_asm)
13317 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13318 filename, line);
13320 if (DWARF2_ASM_LINE_DEBUG_INFO)
13322 unsigned file_num = lookup_filename (filename);
13324 file_num = maybe_emit_file (file_num);
13326 /* Emit the .loc directive understood by GNU as. */
13327 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
13329 /* Indicate that line number info exists. */
13330 line_info_table_in_use++;
13332 /* Indicate that multiple line number tables exist. */
13333 if (DECL_SECTION_NAME (current_function_decl))
13334 separate_line_info_table_in_use++;
13336 else if (DECL_SECTION_NAME (current_function_decl))
13338 dw_separate_line_info_ref line_info;
13339 targetm.asm_out.internal_label (asm_out_file, SEPARATE_LINE_CODE_LABEL,
13340 separate_line_info_table_in_use);
13342 /* Expand the line info table if necessary. */
13343 if (separate_line_info_table_in_use
13344 == separate_line_info_table_allocated)
13346 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13347 separate_line_info_table
13348 = ggc_realloc (separate_line_info_table,
13349 separate_line_info_table_allocated
13350 * sizeof (dw_separate_line_info_entry));
13351 memset (separate_line_info_table
13352 + separate_line_info_table_in_use,
13354 (LINE_INFO_TABLE_INCREMENT
13355 * sizeof (dw_separate_line_info_entry)));
13358 /* Add the new entry at the end of the line_info_table. */
13359 line_info
13360 = &separate_line_info_table[separate_line_info_table_in_use++];
13361 line_info->dw_file_num = lookup_filename (filename);
13362 line_info->dw_line_num = line;
13363 line_info->function = current_function_funcdef_no;
13365 else
13367 dw_line_info_ref line_info;
13369 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
13370 line_info_table_in_use);
13372 /* Expand the line info table if necessary. */
13373 if (line_info_table_in_use == line_info_table_allocated)
13375 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13376 line_info_table
13377 = ggc_realloc (line_info_table,
13378 (line_info_table_allocated
13379 * sizeof (dw_line_info_entry)));
13380 memset (line_info_table + line_info_table_in_use, 0,
13381 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
13384 /* Add the new entry at the end of the line_info_table. */
13385 line_info = &line_info_table[line_info_table_in_use++];
13386 line_info->dw_file_num = lookup_filename (filename);
13387 line_info->dw_line_num = line;
13392 /* Record the beginning of a new source file. */
13394 static void
13395 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
13397 if (flag_eliminate_dwarf2_dups)
13399 /* Record the beginning of the file for break_out_includes. */
13400 dw_die_ref bincl_die;
13402 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
13403 add_AT_string (bincl_die, DW_AT_name, filename);
13406 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13408 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13409 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
13410 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13411 lineno);
13412 maybe_emit_file (lookup_filename (filename));
13413 dw2_asm_output_data_uleb128 (lookup_filename (filename),
13414 "Filename we just started");
13418 /* Record the end of a source file. */
13420 static void
13421 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
13423 if (flag_eliminate_dwarf2_dups)
13424 /* Record the end of the file for break_out_includes. */
13425 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
13427 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13429 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13430 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13434 /* Called from debug_define in toplev.c. The `buffer' parameter contains
13435 the tail part of the directive line, i.e. the part which is past the
13436 initial whitespace, #, whitespace, directive-name, whitespace part. */
13438 static void
13439 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13440 const char *buffer ATTRIBUTE_UNUSED)
13442 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13444 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13445 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
13446 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13447 dw2_asm_output_nstring (buffer, -1, "The macro");
13451 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
13452 the tail part of the directive line, i.e. the part which is past the
13453 initial whitespace, #, whitespace, directive-name, whitespace part. */
13455 static void
13456 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13457 const char *buffer ATTRIBUTE_UNUSED)
13459 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13461 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13462 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
13463 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13464 dw2_asm_output_nstring (buffer, -1, "The macro");
13468 /* Set up for Dwarf output at the start of compilation. */
13470 static void
13471 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
13473 init_file_table ();
13475 /* Allocate the decl_die_table. */
13476 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13477 decl_die_table_eq, NULL);
13479 /* Allocate the decl_loc_table. */
13480 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13481 decl_loc_table_eq, NULL);
13483 /* Allocate the initial hunk of the decl_scope_table. */
13484 decl_scope_table = VEC_alloc (tree, gc, 256);
13486 /* Allocate the initial hunk of the abbrev_die_table. */
13487 abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
13488 * sizeof (dw_die_ref));
13489 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
13490 /* Zero-th entry is allocated, but unused. */
13491 abbrev_die_table_in_use = 1;
13493 /* Allocate the initial hunk of the line_info_table. */
13494 line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
13495 * sizeof (dw_line_info_entry));
13496 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
13498 /* Zero-th entry is allocated, but unused. */
13499 line_info_table_in_use = 1;
13501 /* Generate the initial DIE for the .debug section. Note that the (string)
13502 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
13503 will (typically) be a relative pathname and that this pathname should be
13504 taken as being relative to the directory from which the compiler was
13505 invoked when the given (base) source file was compiled. We will fill
13506 in this value in dwarf2out_finish. */
13507 comp_unit_die = gen_compile_unit_die (NULL);
13509 incomplete_types = VEC_alloc (tree, gc, 64);
13511 used_rtx_array = VEC_alloc (rtx, gc, 32);
13513 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
13514 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
13515 DEBUG_ABBREV_SECTION_LABEL, 0);
13516 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
13517 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
13518 COLD_TEXT_SECTION_LABEL, 0);
13519 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
13521 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
13522 DEBUG_INFO_SECTION_LABEL, 0);
13523 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
13524 DEBUG_LINE_SECTION_LABEL, 0);
13525 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
13526 DEBUG_RANGES_SECTION_LABEL, 0);
13527 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13528 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
13529 named_section_flags (DEBUG_INFO_SECTION, SECTION_DEBUG);
13530 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
13531 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13532 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
13534 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13536 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13537 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
13538 DEBUG_MACINFO_SECTION_LABEL, 0);
13539 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
13542 text_section ();
13543 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
13544 if (flag_reorder_blocks_and_partition)
13546 unlikely_text_section ();
13547 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
13551 /* A helper function for dwarf2out_finish called through
13552 ht_forall. Emit one queued .debug_str string. */
13554 static int
13555 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
13557 struct indirect_string_node *node = (struct indirect_string_node *) *h;
13559 if (node->form == DW_FORM_strp)
13561 named_section_flags (DEBUG_STR_SECTION, DEBUG_STR_SECTION_FLAGS);
13562 ASM_OUTPUT_LABEL (asm_out_file, node->label);
13563 assemble_string (node->str, strlen (node->str) + 1);
13566 return 1;
13571 /* Clear the marks for a die and its children.
13572 Be cool if the mark isn't set. */
13574 static void
13575 prune_unmark_dies (dw_die_ref die)
13577 dw_die_ref c;
13578 die->die_mark = 0;
13579 for (c = die->die_child; c; c = c->die_sib)
13580 prune_unmark_dies (c);
13584 /* Given DIE that we're marking as used, find any other dies
13585 it references as attributes and mark them as used. */
13587 static void
13588 prune_unused_types_walk_attribs (dw_die_ref die)
13590 dw_attr_ref a;
13592 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
13594 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
13596 /* A reference to another DIE.
13597 Make sure that it will get emitted. */
13598 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
13600 else if (a->dw_attr == DW_AT_decl_file)
13602 /* A reference to a file. Make sure the file name is emitted. */
13603 a->dw_attr_val.v.val_unsigned =
13604 maybe_emit_file (a->dw_attr_val.v.val_unsigned);
13610 /* Mark DIE as being used. If DOKIDS is true, then walk down
13611 to DIE's children. */
13613 static void
13614 prune_unused_types_mark (dw_die_ref die, int dokids)
13616 dw_die_ref c;
13618 if (die->die_mark == 0)
13620 /* We haven't done this node yet. Mark it as used. */
13621 die->die_mark = 1;
13623 /* We also have to mark its parents as used.
13624 (But we don't want to mark our parents' kids due to this.) */
13625 if (die->die_parent)
13626 prune_unused_types_mark (die->die_parent, 0);
13628 /* Mark any referenced nodes. */
13629 prune_unused_types_walk_attribs (die);
13631 /* If this node is a specification,
13632 also mark the definition, if it exists. */
13633 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
13634 prune_unused_types_mark (die->die_definition, 1);
13637 if (dokids && die->die_mark != 2)
13639 /* We need to walk the children, but haven't done so yet.
13640 Remember that we've walked the kids. */
13641 die->die_mark = 2;
13643 /* Walk them. */
13644 for (c = die->die_child; c; c = c->die_sib)
13646 /* If this is an array type, we need to make sure our
13647 kids get marked, even if they're types. */
13648 if (die->die_tag == DW_TAG_array_type)
13649 prune_unused_types_mark (c, 1);
13650 else
13651 prune_unused_types_walk (c);
13657 /* Walk the tree DIE and mark types that we actually use. */
13659 static void
13660 prune_unused_types_walk (dw_die_ref die)
13662 dw_die_ref c;
13664 /* Don't do anything if this node is already marked. */
13665 if (die->die_mark)
13666 return;
13668 switch (die->die_tag) {
13669 case DW_TAG_const_type:
13670 case DW_TAG_packed_type:
13671 case DW_TAG_pointer_type:
13672 case DW_TAG_reference_type:
13673 case DW_TAG_volatile_type:
13674 case DW_TAG_typedef:
13675 case DW_TAG_array_type:
13676 case DW_TAG_structure_type:
13677 case DW_TAG_union_type:
13678 case DW_TAG_class_type:
13679 case DW_TAG_friend:
13680 case DW_TAG_variant_part:
13681 case DW_TAG_enumeration_type:
13682 case DW_TAG_subroutine_type:
13683 case DW_TAG_string_type:
13684 case DW_TAG_set_type:
13685 case DW_TAG_subrange_type:
13686 case DW_TAG_ptr_to_member_type:
13687 case DW_TAG_file_type:
13688 /* It's a type node --- don't mark it. */
13689 return;
13691 default:
13692 /* Mark everything else. */
13693 break;
13696 die->die_mark = 1;
13698 /* Now, mark any dies referenced from here. */
13699 prune_unused_types_walk_attribs (die);
13701 /* Mark children. */
13702 for (c = die->die_child; c; c = c->die_sib)
13703 prune_unused_types_walk (c);
13707 /* Remove from the tree DIE any dies that aren't marked. */
13709 static void
13710 prune_unused_types_prune (dw_die_ref die)
13712 dw_die_ref c, p, n;
13714 gcc_assert (die->die_mark);
13716 p = NULL;
13717 for (c = die->die_child; c; c = n)
13719 n = c->die_sib;
13720 if (c->die_mark)
13722 prune_unused_types_prune (c);
13723 p = c;
13725 else
13727 if (p)
13728 p->die_sib = n;
13729 else
13730 die->die_child = n;
13731 free_die (c);
13737 /* Remove dies representing declarations that we never use. */
13739 static void
13740 prune_unused_types (void)
13742 unsigned int i;
13743 limbo_die_node *node;
13745 /* Clear all the marks. */
13746 prune_unmark_dies (comp_unit_die);
13747 for (node = limbo_die_list; node; node = node->next)
13748 prune_unmark_dies (node->die);
13750 /* Set the mark on nodes that are actually used. */
13751 prune_unused_types_walk (comp_unit_die);
13752 for (node = limbo_die_list; node; node = node->next)
13753 prune_unused_types_walk (node->die);
13755 /* Also set the mark on nodes referenced from the
13756 pubname_table or arange_table. */
13757 for (i = 0; i < pubname_table_in_use; i++)
13758 prune_unused_types_mark (pubname_table[i].die, 1);
13759 for (i = 0; i < arange_table_in_use; i++)
13760 prune_unused_types_mark (arange_table[i], 1);
13762 /* Get rid of nodes that aren't marked. */
13763 prune_unused_types_prune (comp_unit_die);
13764 for (node = limbo_die_list; node; node = node->next)
13765 prune_unused_types_prune (node->die);
13767 /* Leave the marks clear. */
13768 prune_unmark_dies (comp_unit_die);
13769 for (node = limbo_die_list; node; node = node->next)
13770 prune_unmark_dies (node->die);
13773 /* Output stuff that dwarf requires at the end of every file,
13774 and generate the DWARF-2 debugging info. */
13776 static void
13777 dwarf2out_finish (const char *filename)
13779 limbo_die_node *node, *next_node;
13780 dw_die_ref die = 0;
13782 /* Add the name for the main input file now. We delayed this from
13783 dwarf2out_init to avoid complications with PCH. */
13784 add_name_attribute (comp_unit_die, filename);
13785 if (filename[0] != DIR_SEPARATOR)
13786 add_comp_dir_attribute (comp_unit_die);
13787 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
13789 size_t i;
13790 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
13791 if (VARRAY_CHAR_PTR (file_table, i)[0] != DIR_SEPARATOR
13792 /* Don't add cwd for <built-in>. */
13793 && VARRAY_CHAR_PTR (file_table, i)[0] != '<')
13795 add_comp_dir_attribute (comp_unit_die);
13796 break;
13800 /* Traverse the limbo die list, and add parent/child links. The only
13801 dies without parents that should be here are concrete instances of
13802 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
13803 For concrete instances, we can get the parent die from the abstract
13804 instance. */
13805 for (node = limbo_die_list; node; node = next_node)
13807 next_node = node->next;
13808 die = node->die;
13810 if (die->die_parent == NULL)
13812 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
13814 if (origin)
13815 add_child_die (origin->die_parent, die);
13816 else if (die == comp_unit_die)
13818 else if (errorcount > 0 || sorrycount > 0)
13819 /* It's OK to be confused by errors in the input. */
13820 add_child_die (comp_unit_die, die);
13821 else
13823 /* In certain situations, the lexical block containing a
13824 nested function can be optimized away, which results
13825 in the nested function die being orphaned. Likewise
13826 with the return type of that nested function. Force
13827 this to be a child of the containing function.
13829 It may happen that even the containing function got fully
13830 inlined and optimized out. In that case we are lost and
13831 assign the empty child. This should not be big issue as
13832 the function is likely unreachable too. */
13833 tree context = NULL_TREE;
13835 gcc_assert (node->created_for);
13837 if (DECL_P (node->created_for))
13838 context = DECL_CONTEXT (node->created_for);
13839 else if (TYPE_P (node->created_for))
13840 context = TYPE_CONTEXT (node->created_for);
13842 gcc_assert (context && TREE_CODE (context) == FUNCTION_DECL);
13844 origin = lookup_decl_die (context);
13845 if (origin)
13846 add_child_die (origin, die);
13847 else
13848 add_child_die (comp_unit_die, die);
13853 limbo_die_list = NULL;
13855 /* Walk through the list of incomplete types again, trying once more to
13856 emit full debugging info for them. */
13857 retry_incomplete_types ();
13859 /* We need to reverse all the dies before break_out_includes, or
13860 we'll see the end of an include file before the beginning. */
13861 reverse_all_dies (comp_unit_die);
13863 if (flag_eliminate_unused_debug_types)
13864 prune_unused_types ();
13866 /* Generate separate CUs for each of the include files we've seen.
13867 They will go into limbo_die_list. */
13868 if (flag_eliminate_dwarf2_dups)
13869 break_out_includes (comp_unit_die);
13871 /* Traverse the DIE's and add add sibling attributes to those DIE's
13872 that have children. */
13873 add_sibling_attributes (comp_unit_die);
13874 for (node = limbo_die_list; node; node = node->next)
13875 add_sibling_attributes (node->die);
13877 /* Output a terminator label for the .text section. */
13878 text_section ();
13879 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
13880 if (flag_reorder_blocks_and_partition)
13882 unlikely_text_section ();
13883 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
13886 /* Output the source line correspondence table. We must do this
13887 even if there is no line information. Otherwise, on an empty
13888 translation unit, we will generate a present, but empty,
13889 .debug_info section. IRIX 6.5 `nm' will then complain when
13890 examining the file. */
13891 if (! DWARF2_ASM_LINE_DEBUG_INFO)
13893 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13894 output_line_info ();
13897 /* Output location list section if necessary. */
13898 if (have_location_lists)
13900 /* Output the location lists info. */
13901 named_section_flags (DEBUG_LOC_SECTION, SECTION_DEBUG);
13902 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
13903 DEBUG_LOC_SECTION_LABEL, 0);
13904 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
13905 output_location_lists (die);
13906 have_location_lists = 0;
13909 /* We can only use the low/high_pc attributes if all of the code was
13910 in .text. */
13911 if (separate_line_info_table_in_use == 0)
13913 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
13914 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
13917 /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
13918 "base address". Use zero so that these addresses become absolute. */
13919 else if (have_location_lists || ranges_table_in_use)
13920 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
13922 if (debug_info_level >= DINFO_LEVEL_NORMAL)
13923 add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
13924 debug_line_section_label);
13926 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13927 add_AT_lbl_offset (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
13929 /* Output all of the compilation units. We put the main one last so that
13930 the offsets are available to output_pubnames. */
13931 for (node = limbo_die_list; node; node = node->next)
13932 output_comp_unit (node->die, 0);
13934 output_comp_unit (comp_unit_die, 0);
13936 /* Output the abbreviation table. */
13937 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13938 output_abbrev_section ();
13940 /* Output public names table if necessary. */
13941 if (pubname_table_in_use)
13943 named_section_flags (DEBUG_PUBNAMES_SECTION, SECTION_DEBUG);
13944 output_pubnames ();
13947 /* Output the address range information. We only put functions in the arange
13948 table, so don't write it out if we don't have any. */
13949 if (fde_table_in_use)
13951 named_section_flags (DEBUG_ARANGES_SECTION, SECTION_DEBUG);
13952 output_aranges ();
13955 /* Output ranges section if necessary. */
13956 if (ranges_table_in_use)
13958 named_section_flags (DEBUG_RANGES_SECTION, SECTION_DEBUG);
13959 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
13960 output_ranges ();
13963 /* Have to end the macro section. */
13964 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13966 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13967 dw2_asm_output_data (1, 0, "End compilation unit");
13970 /* If we emitted any DW_FORM_strp form attribute, output the string
13971 table too. */
13972 if (debug_str_hash)
13973 htab_traverse (debug_str_hash, output_indirect_string, NULL);
13975 #else
13977 /* This should never be used, but its address is needed for comparisons. */
13978 const struct gcc_debug_hooks dwarf2_debug_hooks;
13980 #endif /* DWARF2_DEBUGGING_INFO */
13982 #include "gt-dwarf2out.h"