2005-06-01 Daniel Berlin <dberlin@dberlin.org>
[official-gcc.git] / gcc / dwarf2out.c
blobaf62a770e72b07ff415b90be5841fe38c61f651d
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_HAS_VALUE_EXPR_P (loc))
8976 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
8977 want_address);
8978 /* FALLTHRU */
8980 case RESULT_DECL:
8982 rtx rtl = rtl_for_decl_location (loc);
8984 if (rtl == NULL_RTX)
8985 return 0;
8986 else if (GET_CODE (rtl) == CONST_INT)
8988 HOST_WIDE_INT val = INTVAL (rtl);
8989 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
8990 val &= GET_MODE_MASK (DECL_MODE (loc));
8991 ret = int_loc_descriptor (val);
8993 else if (GET_CODE (rtl) == CONST_STRING)
8994 return 0;
8995 else if (CONSTANT_P (rtl))
8997 ret = new_loc_descr (DW_OP_addr, 0, 0);
8998 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8999 ret->dw_loc_oprnd1.v.val_addr = rtl;
9001 else
9003 enum machine_mode mode;
9005 /* Certain constructs can only be represented at top-level. */
9006 if (want_address == 2)
9007 return loc_descriptor (rtl, false);
9009 mode = GET_MODE (rtl);
9010 if (MEM_P (rtl))
9012 rtl = XEXP (rtl, 0);
9013 have_address = 1;
9015 ret = mem_loc_descriptor (rtl, mode, false);
9018 break;
9020 case INDIRECT_REF:
9021 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9022 have_address = 1;
9023 break;
9025 case COMPOUND_EXPR:
9026 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9028 case NOP_EXPR:
9029 case CONVERT_EXPR:
9030 case NON_LVALUE_EXPR:
9031 case VIEW_CONVERT_EXPR:
9032 case SAVE_EXPR:
9033 case MODIFY_EXPR:
9034 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
9036 case COMPONENT_REF:
9037 case BIT_FIELD_REF:
9038 case ARRAY_REF:
9039 case ARRAY_RANGE_REF:
9041 tree obj, offset;
9042 HOST_WIDE_INT bitsize, bitpos, bytepos;
9043 enum machine_mode mode;
9044 int volatilep;
9046 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9047 &unsignedp, &volatilep, false);
9049 if (obj == loc)
9050 return 0;
9052 ret = loc_descriptor_from_tree_1 (obj, 1);
9053 if (ret == 0
9054 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9055 return 0;
9057 if (offset != NULL_TREE)
9059 /* Variable offset. */
9060 add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9061 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9064 bytepos = bitpos / BITS_PER_UNIT;
9065 if (bytepos > 0)
9066 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9067 else if (bytepos < 0)
9069 add_loc_descr (&ret, int_loc_descriptor (bytepos));
9070 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9073 have_address = 1;
9074 break;
9077 case INTEGER_CST:
9078 if (host_integerp (loc, 0))
9079 ret = int_loc_descriptor (tree_low_cst (loc, 0));
9080 else
9081 return 0;
9082 break;
9084 case CONSTRUCTOR:
9086 /* Get an RTL for this, if something has been emitted. */
9087 rtx rtl = lookup_constant_def (loc);
9088 enum machine_mode mode;
9090 if (!rtl || !MEM_P (rtl))
9091 return 0;
9092 mode = GET_MODE (rtl);
9093 rtl = XEXP (rtl, 0);
9094 ret = mem_loc_descriptor (rtl, mode, false);
9095 have_address = 1;
9096 break;
9099 case TRUTH_AND_EXPR:
9100 case TRUTH_ANDIF_EXPR:
9101 case BIT_AND_EXPR:
9102 op = DW_OP_and;
9103 goto do_binop;
9105 case TRUTH_XOR_EXPR:
9106 case BIT_XOR_EXPR:
9107 op = DW_OP_xor;
9108 goto do_binop;
9110 case TRUTH_OR_EXPR:
9111 case TRUTH_ORIF_EXPR:
9112 case BIT_IOR_EXPR:
9113 op = DW_OP_or;
9114 goto do_binop;
9116 case FLOOR_DIV_EXPR:
9117 case CEIL_DIV_EXPR:
9118 case ROUND_DIV_EXPR:
9119 case TRUNC_DIV_EXPR:
9120 op = DW_OP_div;
9121 goto do_binop;
9123 case MINUS_EXPR:
9124 op = DW_OP_minus;
9125 goto do_binop;
9127 case FLOOR_MOD_EXPR:
9128 case CEIL_MOD_EXPR:
9129 case ROUND_MOD_EXPR:
9130 case TRUNC_MOD_EXPR:
9131 op = DW_OP_mod;
9132 goto do_binop;
9134 case MULT_EXPR:
9135 op = DW_OP_mul;
9136 goto do_binop;
9138 case LSHIFT_EXPR:
9139 op = DW_OP_shl;
9140 goto do_binop;
9142 case RSHIFT_EXPR:
9143 op = (unsignedp ? DW_OP_shr : DW_OP_shra);
9144 goto do_binop;
9146 case PLUS_EXPR:
9147 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9148 && host_integerp (TREE_OPERAND (loc, 1), 0))
9150 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9151 if (ret == 0)
9152 return 0;
9154 add_loc_descr (&ret,
9155 new_loc_descr (DW_OP_plus_uconst,
9156 tree_low_cst (TREE_OPERAND (loc, 1),
9158 0));
9159 break;
9162 op = DW_OP_plus;
9163 goto do_binop;
9165 case LE_EXPR:
9166 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9167 return 0;
9169 op = DW_OP_le;
9170 goto do_binop;
9172 case GE_EXPR:
9173 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9174 return 0;
9176 op = DW_OP_ge;
9177 goto do_binop;
9179 case LT_EXPR:
9180 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9181 return 0;
9183 op = DW_OP_lt;
9184 goto do_binop;
9186 case GT_EXPR:
9187 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9188 return 0;
9190 op = DW_OP_gt;
9191 goto do_binop;
9193 case EQ_EXPR:
9194 op = DW_OP_eq;
9195 goto do_binop;
9197 case NE_EXPR:
9198 op = DW_OP_ne;
9199 goto do_binop;
9201 do_binop:
9202 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9203 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9204 if (ret == 0 || ret1 == 0)
9205 return 0;
9207 add_loc_descr (&ret, ret1);
9208 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9209 break;
9211 case TRUTH_NOT_EXPR:
9212 case BIT_NOT_EXPR:
9213 op = DW_OP_not;
9214 goto do_unop;
9216 case ABS_EXPR:
9217 op = DW_OP_abs;
9218 goto do_unop;
9220 case NEGATE_EXPR:
9221 op = DW_OP_neg;
9222 goto do_unop;
9224 do_unop:
9225 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9226 if (ret == 0)
9227 return 0;
9229 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9230 break;
9232 case MIN_EXPR:
9233 case MAX_EXPR:
9235 const enum tree_code code =
9236 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9238 loc = build3 (COND_EXPR, TREE_TYPE (loc),
9239 build2 (code, integer_type_node,
9240 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9241 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9244 /* ... fall through ... */
9246 case COND_EXPR:
9248 dw_loc_descr_ref lhs
9249 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9250 dw_loc_descr_ref rhs
9251 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9252 dw_loc_descr_ref bra_node, jump_node, tmp;
9254 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9255 if (ret == 0 || lhs == 0 || rhs == 0)
9256 return 0;
9258 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9259 add_loc_descr (&ret, bra_node);
9261 add_loc_descr (&ret, rhs);
9262 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9263 add_loc_descr (&ret, jump_node);
9265 add_loc_descr (&ret, lhs);
9266 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9267 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9269 /* ??? Need a node to point the skip at. Use a nop. */
9270 tmp = new_loc_descr (DW_OP_nop, 0, 0);
9271 add_loc_descr (&ret, tmp);
9272 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9273 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9275 break;
9277 case FIX_TRUNC_EXPR:
9278 case FIX_CEIL_EXPR:
9279 case FIX_FLOOR_EXPR:
9280 case FIX_ROUND_EXPR:
9281 return 0;
9283 default:
9284 /* Leave front-end specific codes as simply unknown. This comes
9285 up, for instance, with the C STMT_EXPR. */
9286 if ((unsigned int) TREE_CODE (loc)
9287 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9288 return 0;
9290 #ifdef ENABLE_CHECKING
9291 /* Otherwise this is a generic code; we should just lists all of
9292 these explicitly. We forgot one. */
9293 gcc_unreachable ();
9294 #else
9295 /* In a release build, we want to degrade gracefully: better to
9296 generate incomplete debugging information than to crash. */
9297 return NULL;
9298 #endif
9301 /* Show if we can't fill the request for an address. */
9302 if (want_address && !have_address)
9303 return 0;
9305 /* If we've got an address and don't want one, dereference. */
9306 if (!want_address && have_address)
9308 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9310 if (size > DWARF2_ADDR_SIZE || size == -1)
9311 return 0;
9312 else if (size == DWARF2_ADDR_SIZE)
9313 op = DW_OP_deref;
9314 else
9315 op = DW_OP_deref_size;
9317 add_loc_descr (&ret, new_loc_descr (op, size, 0));
9320 return ret;
9323 static inline dw_loc_descr_ref
9324 loc_descriptor_from_tree (tree loc)
9326 return loc_descriptor_from_tree_1 (loc, 2);
9329 /* Given a value, round it up to the lowest multiple of `boundary'
9330 which is not less than the value itself. */
9332 static inline HOST_WIDE_INT
9333 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9335 return (((value + boundary - 1) / boundary) * boundary);
9338 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9339 pointer to the declared type for the relevant field variable, or return
9340 `integer_type_node' if the given node turns out to be an
9341 ERROR_MARK node. */
9343 static inline tree
9344 field_type (tree decl)
9346 tree type;
9348 if (TREE_CODE (decl) == ERROR_MARK)
9349 return integer_type_node;
9351 type = DECL_BIT_FIELD_TYPE (decl);
9352 if (type == NULL_TREE)
9353 type = TREE_TYPE (decl);
9355 return type;
9358 /* Given a pointer to a tree node, return the alignment in bits for
9359 it, or else return BITS_PER_WORD if the node actually turns out to
9360 be an ERROR_MARK node. */
9362 static inline unsigned
9363 simple_type_align_in_bits (tree type)
9365 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9368 static inline unsigned
9369 simple_decl_align_in_bits (tree decl)
9371 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9374 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9375 lowest addressed byte of the "containing object" for the given FIELD_DECL,
9376 or return 0 if we are unable to determine what that offset is, either
9377 because the argument turns out to be a pointer to an ERROR_MARK node, or
9378 because the offset is actually variable. (We can't handle the latter case
9379 just yet). */
9381 static HOST_WIDE_INT
9382 field_byte_offset (tree decl)
9384 unsigned int type_align_in_bits;
9385 unsigned int decl_align_in_bits;
9386 unsigned HOST_WIDE_INT type_size_in_bits;
9387 HOST_WIDE_INT object_offset_in_bits;
9388 tree type;
9389 tree field_size_tree;
9390 HOST_WIDE_INT bitpos_int;
9391 HOST_WIDE_INT deepest_bitpos;
9392 unsigned HOST_WIDE_INT field_size_in_bits;
9394 if (TREE_CODE (decl) == ERROR_MARK)
9395 return 0;
9397 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9399 type = field_type (decl);
9400 field_size_tree = DECL_SIZE (decl);
9402 /* The size could be unspecified if there was an error, or for
9403 a flexible array member. */
9404 if (! field_size_tree)
9405 field_size_tree = bitsize_zero_node;
9407 /* We cannot yet cope with fields whose positions are variable, so
9408 for now, when we see such things, we simply return 0. Someday, we may
9409 be able to handle such cases, but it will be damn difficult. */
9410 if (! host_integerp (bit_position (decl), 0))
9411 return 0;
9413 bitpos_int = int_bit_position (decl);
9415 /* If we don't know the size of the field, pretend it's a full word. */
9416 if (host_integerp (field_size_tree, 1))
9417 field_size_in_bits = tree_low_cst (field_size_tree, 1);
9418 else
9419 field_size_in_bits = BITS_PER_WORD;
9421 type_size_in_bits = simple_type_size_in_bits (type);
9422 type_align_in_bits = simple_type_align_in_bits (type);
9423 decl_align_in_bits = simple_decl_align_in_bits (decl);
9425 /* The GCC front-end doesn't make any attempt to keep track of the starting
9426 bit offset (relative to the start of the containing structure type) of the
9427 hypothetical "containing object" for a bit-field. Thus, when computing
9428 the byte offset value for the start of the "containing object" of a
9429 bit-field, we must deduce this information on our own. This can be rather
9430 tricky to do in some cases. For example, handling the following structure
9431 type definition when compiling for an i386/i486 target (which only aligns
9432 long long's to 32-bit boundaries) can be very tricky:
9434 struct S { int field1; long long field2:31; };
9436 Fortunately, there is a simple rule-of-thumb which can be used in such
9437 cases. When compiling for an i386/i486, GCC will allocate 8 bytes for the
9438 structure shown above. It decides to do this based upon one simple rule
9439 for bit-field allocation. GCC allocates each "containing object" for each
9440 bit-field at the first (i.e. lowest addressed) legitimate alignment
9441 boundary (based upon the required minimum alignment for the declared type
9442 of the field) which it can possibly use, subject to the condition that
9443 there is still enough available space remaining in the containing object
9444 (when allocated at the selected point) to fully accommodate all of the
9445 bits of the bit-field itself.
9447 This simple rule makes it obvious why GCC allocates 8 bytes for each
9448 object of the structure type shown above. When looking for a place to
9449 allocate the "containing object" for `field2', the compiler simply tries
9450 to allocate a 64-bit "containing object" at each successive 32-bit
9451 boundary (starting at zero) until it finds a place to allocate that 64-
9452 bit field such that at least 31 contiguous (and previously unallocated)
9453 bits remain within that selected 64 bit field. (As it turns out, for the
9454 example above, the compiler finds it is OK to allocate the "containing
9455 object" 64-bit field at bit-offset zero within the structure type.)
9457 Here we attempt to work backwards from the limited set of facts we're
9458 given, and we try to deduce from those facts, where GCC must have believed
9459 that the containing object started (within the structure type). The value
9460 we deduce is then used (by the callers of this routine) to generate
9461 DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
9462 and, in the case of DW_AT_location, regular fields as well). */
9464 /* Figure out the bit-distance from the start of the structure to the
9465 "deepest" bit of the bit-field. */
9466 deepest_bitpos = bitpos_int + field_size_in_bits;
9468 /* This is the tricky part. Use some fancy footwork to deduce where the
9469 lowest addressed bit of the containing object must be. */
9470 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9472 /* Round up to type_align by default. This works best for bitfields. */
9473 object_offset_in_bits += type_align_in_bits - 1;
9474 object_offset_in_bits /= type_align_in_bits;
9475 object_offset_in_bits *= type_align_in_bits;
9477 if (object_offset_in_bits > bitpos_int)
9479 /* Sigh, the decl must be packed. */
9480 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9482 /* Round up to decl_align instead. */
9483 object_offset_in_bits += decl_align_in_bits - 1;
9484 object_offset_in_bits /= decl_align_in_bits;
9485 object_offset_in_bits *= decl_align_in_bits;
9488 return object_offset_in_bits / BITS_PER_UNIT;
9491 /* The following routines define various Dwarf attributes and any data
9492 associated with them. */
9494 /* Add a location description attribute value to a DIE.
9496 This emits location attributes suitable for whole variables and
9497 whole parameters. Note that the location attributes for struct fields are
9498 generated by the routine `data_member_location_attribute' below. */
9500 static inline void
9501 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9502 dw_loc_descr_ref descr)
9504 if (descr != 0)
9505 add_AT_loc (die, attr_kind, descr);
9508 /* Attach the specialized form of location attribute used for data members of
9509 struct and union types. In the special case of a FIELD_DECL node which
9510 represents a bit-field, the "offset" part of this special location
9511 descriptor must indicate the distance in bytes from the lowest-addressed
9512 byte of the containing struct or union type to the lowest-addressed byte of
9513 the "containing object" for the bit-field. (See the `field_byte_offset'
9514 function above).
9516 For any given bit-field, the "containing object" is a hypothetical object
9517 (of some integral or enum type) within which the given bit-field lives. The
9518 type of this hypothetical "containing object" is always the same as the
9519 declared type of the individual bit-field itself (for GCC anyway... the
9520 DWARF spec doesn't actually mandate this). Note that it is the size (in
9521 bytes) of the hypothetical "containing object" which will be given in the
9522 DW_AT_byte_size attribute for this bit-field. (See the
9523 `byte_size_attribute' function below.) It is also used when calculating the
9524 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
9525 function below.) */
9527 static void
9528 add_data_member_location_attribute (dw_die_ref die, tree decl)
9530 HOST_WIDE_INT offset;
9531 dw_loc_descr_ref loc_descr = 0;
9533 if (TREE_CODE (decl) == TREE_BINFO)
9535 /* We're working on the TAG_inheritance for a base class. */
9536 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
9538 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9539 aren't at a fixed offset from all (sub)objects of the same
9540 type. We need to extract the appropriate offset from our
9541 vtable. The following dwarf expression means
9543 BaseAddr = ObAddr + *((*ObAddr) - Offset)
9545 This is specific to the V3 ABI, of course. */
9547 dw_loc_descr_ref tmp;
9549 /* Make a copy of the object address. */
9550 tmp = new_loc_descr (DW_OP_dup, 0, 0);
9551 add_loc_descr (&loc_descr, tmp);
9553 /* Extract the vtable address. */
9554 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9555 add_loc_descr (&loc_descr, tmp);
9557 /* Calculate the address of the offset. */
9558 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9559 gcc_assert (offset < 0);
9561 tmp = int_loc_descriptor (-offset);
9562 add_loc_descr (&loc_descr, tmp);
9563 tmp = new_loc_descr (DW_OP_minus, 0, 0);
9564 add_loc_descr (&loc_descr, tmp);
9566 /* Extract the offset. */
9567 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9568 add_loc_descr (&loc_descr, tmp);
9570 /* Add it to the object address. */
9571 tmp = new_loc_descr (DW_OP_plus, 0, 0);
9572 add_loc_descr (&loc_descr, tmp);
9574 else
9575 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9577 else
9578 offset = field_byte_offset (decl);
9580 if (! loc_descr)
9582 enum dwarf_location_atom op;
9584 /* The DWARF2 standard says that we should assume that the structure
9585 address is already on the stack, so we can specify a structure field
9586 address by using DW_OP_plus_uconst. */
9588 #ifdef MIPS_DEBUGGING_INFO
9589 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9590 operator correctly. It works only if we leave the offset on the
9591 stack. */
9592 op = DW_OP_constu;
9593 #else
9594 op = DW_OP_plus_uconst;
9595 #endif
9597 loc_descr = new_loc_descr (op, offset, 0);
9600 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9603 /* Writes integer values to dw_vec_const array. */
9605 static void
9606 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9608 while (size != 0)
9610 *dest++ = val & 0xff;
9611 val >>= 8;
9612 --size;
9616 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
9618 static HOST_WIDE_INT
9619 extract_int (const unsigned char *src, unsigned int size)
9621 HOST_WIDE_INT val = 0;
9623 src += size;
9624 while (size != 0)
9626 val <<= 8;
9627 val |= *--src & 0xff;
9628 --size;
9630 return val;
9633 /* Writes floating point values to dw_vec_const array. */
9635 static void
9636 insert_float (rtx rtl, unsigned char *array)
9638 REAL_VALUE_TYPE rv;
9639 long val[4];
9640 int i;
9642 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9643 real_to_target (val, &rv, GET_MODE (rtl));
9645 /* real_to_target puts 32-bit pieces in each long. Pack them. */
9646 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9648 insert_int (val[i], 4, array);
9649 array += 4;
9653 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
9654 does not have a "location" either in memory or in a register. These
9655 things can arise in GNU C when a constant is passed as an actual parameter
9656 to an inlined function. They can also arise in C++ where declared
9657 constants do not necessarily get memory "homes". */
9659 static void
9660 add_const_value_attribute (dw_die_ref die, rtx rtl)
9662 switch (GET_CODE (rtl))
9664 case CONST_INT:
9666 HOST_WIDE_INT val = INTVAL (rtl);
9668 if (val < 0)
9669 add_AT_int (die, DW_AT_const_value, val);
9670 else
9671 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
9673 break;
9675 case CONST_DOUBLE:
9676 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9677 floating-point constant. A CONST_DOUBLE is used whenever the
9678 constant requires more than one word in order to be adequately
9679 represented. We output CONST_DOUBLEs as blocks. */
9681 enum machine_mode mode = GET_MODE (rtl);
9683 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
9685 unsigned int length = GET_MODE_SIZE (mode);
9686 unsigned char *array = ggc_alloc (length);
9688 insert_float (rtl, array);
9689 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
9691 else
9693 /* ??? We really should be using HOST_WIDE_INT throughout. */
9694 gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
9696 add_AT_long_long (die, DW_AT_const_value,
9697 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
9700 break;
9702 case CONST_VECTOR:
9704 enum machine_mode mode = GET_MODE (rtl);
9705 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9706 unsigned int length = CONST_VECTOR_NUNITS (rtl);
9707 unsigned char *array = ggc_alloc (length * elt_size);
9708 unsigned int i;
9709 unsigned char *p;
9711 switch (GET_MODE_CLASS (mode))
9713 case MODE_VECTOR_INT:
9714 for (i = 0, p = array; i < length; i++, p += elt_size)
9716 rtx elt = CONST_VECTOR_ELT (rtl, i);
9717 HOST_WIDE_INT lo, hi;
9719 switch (GET_CODE (elt))
9721 case CONST_INT:
9722 lo = INTVAL (elt);
9723 hi = -(lo < 0);
9724 break;
9726 case CONST_DOUBLE:
9727 lo = CONST_DOUBLE_LOW (elt);
9728 hi = CONST_DOUBLE_HIGH (elt);
9729 break;
9731 default:
9732 gcc_unreachable ();
9735 if (elt_size <= sizeof (HOST_WIDE_INT))
9736 insert_int (lo, elt_size, p);
9737 else
9739 unsigned char *p0 = p;
9740 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9742 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
9743 if (WORDS_BIG_ENDIAN)
9745 p0 = p1;
9746 p1 = p;
9748 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9749 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9752 break;
9754 case MODE_VECTOR_FLOAT:
9755 for (i = 0, p = array; i < length; i++, p += elt_size)
9757 rtx elt = CONST_VECTOR_ELT (rtl, i);
9758 insert_float (elt, p);
9760 break;
9762 default:
9763 gcc_unreachable ();
9766 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
9768 break;
9770 case CONST_STRING:
9771 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
9772 break;
9774 case SYMBOL_REF:
9775 case LABEL_REF:
9776 case CONST:
9777 add_AT_addr (die, DW_AT_const_value, rtl);
9778 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9779 break;
9781 case PLUS:
9782 /* In cases where an inlined instance of an inline function is passed
9783 the address of an `auto' variable (which is local to the caller) we
9784 can get a situation where the DECL_RTL of the artificial local
9785 variable (for the inlining) which acts as a stand-in for the
9786 corresponding formal parameter (of the inline function) will look
9787 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
9788 exactly a compile-time constant expression, but it isn't the address
9789 of the (artificial) local variable either. Rather, it represents the
9790 *value* which the artificial local variable always has during its
9791 lifetime. We currently have no way to represent such quasi-constant
9792 values in Dwarf, so for now we just punt and generate nothing. */
9793 break;
9795 default:
9796 /* No other kinds of rtx should be possible here. */
9797 gcc_unreachable ();
9802 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
9803 for use in a later add_const_value_attribute call. */
9805 static rtx
9806 rtl_for_decl_init (tree init, tree type)
9808 rtx rtl = NULL_RTX;
9810 /* If a variable is initialized with a string constant without embedded
9811 zeros, build CONST_STRING. */
9812 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
9814 tree enttype = TREE_TYPE (type);
9815 tree domain = TYPE_DOMAIN (type);
9816 enum machine_mode mode = TYPE_MODE (enttype);
9818 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
9819 && domain
9820 && integer_zerop (TYPE_MIN_VALUE (domain))
9821 && compare_tree_int (TYPE_MAX_VALUE (domain),
9822 TREE_STRING_LENGTH (init) - 1) == 0
9823 && ((size_t) TREE_STRING_LENGTH (init)
9824 == strlen (TREE_STRING_POINTER (init)) + 1))
9825 rtl = gen_rtx_CONST_STRING (VOIDmode,
9826 ggc_strdup (TREE_STRING_POINTER (init)));
9828 /* If the initializer is something that we know will expand into an
9829 immediate RTL constant, expand it now. Expanding anything else
9830 tends to produce unresolved symbols; see debug/5770 and c++/6381. */
9831 /* Aggregate, vector, and complex types may contain constructors that may
9832 result in code being generated when expand_expr is called, so we can't
9833 handle them here. Integer and float are useful and safe types to handle
9834 here. */
9835 else if ((INTEGRAL_TYPE_P (type) || SCALAR_FLOAT_TYPE_P (type))
9836 && initializer_constant_valid_p (init, type) == null_pointer_node)
9838 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
9840 /* If expand_expr returns a MEM, it wasn't immediate. */
9841 gcc_assert (!rtl || !MEM_P (rtl));
9844 return rtl;
9847 /* Generate RTL for the variable DECL to represent its location. */
9849 static rtx
9850 rtl_for_decl_location (tree decl)
9852 rtx rtl;
9854 /* Here we have to decide where we are going to say the parameter "lives"
9855 (as far as the debugger is concerned). We only have a couple of
9856 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
9858 DECL_RTL normally indicates where the parameter lives during most of the
9859 activation of the function. If optimization is enabled however, this
9860 could be either NULL or else a pseudo-reg. Both of those cases indicate
9861 that the parameter doesn't really live anywhere (as far as the code
9862 generation parts of GCC are concerned) during most of the function's
9863 activation. That will happen (for example) if the parameter is never
9864 referenced within the function.
9866 We could just generate a location descriptor here for all non-NULL
9867 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
9868 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
9869 where DECL_RTL is NULL or is a pseudo-reg.
9871 Note however that we can only get away with using DECL_INCOMING_RTL as
9872 a backup substitute for DECL_RTL in certain limited cases. In cases
9873 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
9874 we can be sure that the parameter was passed using the same type as it is
9875 declared to have within the function, and that its DECL_INCOMING_RTL
9876 points us to a place where a value of that type is passed.
9878 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
9879 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
9880 because in these cases DECL_INCOMING_RTL points us to a value of some
9881 type which is *different* from the type of the parameter itself. Thus,
9882 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
9883 such cases, the debugger would end up (for example) trying to fetch a
9884 `float' from a place which actually contains the first part of a
9885 `double'. That would lead to really incorrect and confusing
9886 output at debug-time.
9888 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
9889 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
9890 are a couple of exceptions however. On little-endian machines we can
9891 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
9892 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
9893 an integral type that is smaller than TREE_TYPE (decl). These cases arise
9894 when (on a little-endian machine) a non-prototyped function has a
9895 parameter declared to be of type `short' or `char'. In such cases,
9896 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
9897 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
9898 passed `int' value. If the debugger then uses that address to fetch
9899 a `short' or a `char' (on a little-endian machine) the result will be
9900 the correct data, so we allow for such exceptional cases below.
9902 Note that our goal here is to describe the place where the given formal
9903 parameter lives during most of the function's activation (i.e. between the
9904 end of the prologue and the start of the epilogue). We'll do that as best
9905 as we can. Note however that if the given formal parameter is modified
9906 sometime during the execution of the function, then a stack backtrace (at
9907 debug-time) will show the function as having been called with the *new*
9908 value rather than the value which was originally passed in. This happens
9909 rarely enough that it is not a major problem, but it *is* a problem, and
9910 I'd like to fix it.
9912 A future version of dwarf2out.c may generate two additional attributes for
9913 any given DW_TAG_formal_parameter DIE which will describe the "passed
9914 type" and the "passed location" for the given formal parameter in addition
9915 to the attributes we now generate to indicate the "declared type" and the
9916 "active location" for each parameter. This additional set of attributes
9917 could be used by debuggers for stack backtraces. Separately, note that
9918 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
9919 This happens (for example) for inlined-instances of inline function formal
9920 parameters which are never referenced. This really shouldn't be
9921 happening. All PARM_DECL nodes should get valid non-NULL
9922 DECL_INCOMING_RTL values. FIXME. */
9924 /* Use DECL_RTL as the "location" unless we find something better. */
9925 rtl = DECL_RTL_IF_SET (decl);
9927 /* When generating abstract instances, ignore everything except
9928 constants, symbols living in memory, and symbols living in
9929 fixed registers. */
9930 if (! reload_completed)
9932 if (rtl
9933 && (CONSTANT_P (rtl)
9934 || (MEM_P (rtl)
9935 && CONSTANT_P (XEXP (rtl, 0)))
9936 || (REG_P (rtl)
9937 && TREE_CODE (decl) == VAR_DECL
9938 && TREE_STATIC (decl))))
9940 rtl = targetm.delegitimize_address (rtl);
9941 return rtl;
9943 rtl = NULL_RTX;
9945 else if (TREE_CODE (decl) == PARM_DECL)
9947 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
9949 tree declared_type = TREE_TYPE (decl);
9950 tree passed_type = DECL_ARG_TYPE (decl);
9951 enum machine_mode dmode = TYPE_MODE (declared_type);
9952 enum machine_mode pmode = TYPE_MODE (passed_type);
9954 /* This decl represents a formal parameter which was optimized out.
9955 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
9956 all cases where (rtl == NULL_RTX) just below. */
9957 if (dmode == pmode)
9958 rtl = DECL_INCOMING_RTL (decl);
9959 else if (SCALAR_INT_MODE_P (dmode)
9960 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
9961 && DECL_INCOMING_RTL (decl))
9963 rtx inc = DECL_INCOMING_RTL (decl);
9964 if (REG_P (inc))
9965 rtl = inc;
9966 else if (MEM_P (inc))
9968 if (BYTES_BIG_ENDIAN)
9969 rtl = adjust_address_nv (inc, dmode,
9970 GET_MODE_SIZE (pmode)
9971 - GET_MODE_SIZE (dmode));
9972 else
9973 rtl = inc;
9978 /* If the parm was passed in registers, but lives on the stack, then
9979 make a big endian correction if the mode of the type of the
9980 parameter is not the same as the mode of the rtl. */
9981 /* ??? This is the same series of checks that are made in dbxout.c before
9982 we reach the big endian correction code there. It isn't clear if all
9983 of these checks are necessary here, but keeping them all is the safe
9984 thing to do. */
9985 else if (MEM_P (rtl)
9986 && XEXP (rtl, 0) != const0_rtx
9987 && ! CONSTANT_P (XEXP (rtl, 0))
9988 /* Not passed in memory. */
9989 && !MEM_P (DECL_INCOMING_RTL (decl))
9990 /* Not passed by invisible reference. */
9991 && (!REG_P (XEXP (rtl, 0))
9992 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
9993 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
9994 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
9995 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
9996 #endif
9998 /* Big endian correction check. */
9999 && BYTES_BIG_ENDIAN
10000 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10001 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10002 < UNITS_PER_WORD))
10004 int offset = (UNITS_PER_WORD
10005 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10007 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10008 plus_constant (XEXP (rtl, 0), offset));
10011 else if (TREE_CODE (decl) == VAR_DECL
10012 && rtl
10013 && MEM_P (rtl)
10014 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10015 && BYTES_BIG_ENDIAN)
10017 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10018 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10020 /* If a variable is declared "register" yet is smaller than
10021 a register, then if we store the variable to memory, it
10022 looks like we're storing a register-sized value, when in
10023 fact we are not. We need to adjust the offset of the
10024 storage location to reflect the actual value's bytes,
10025 else gdb will not be able to display it. */
10026 if (rsize > dsize)
10027 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10028 plus_constant (XEXP (rtl, 0), rsize-dsize));
10031 if (rtl != NULL_RTX)
10033 rtl = eliminate_regs (rtl, 0, NULL_RTX);
10034 #ifdef LEAF_REG_REMAP
10035 if (current_function_uses_only_leaf_regs)
10036 leaf_renumber_regs_insn (rtl);
10037 #endif
10040 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10041 and will have been substituted directly into all expressions that use it.
10042 C does not have such a concept, but C++ and other languages do. */
10043 else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10044 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10046 if (rtl)
10047 rtl = targetm.delegitimize_address (rtl);
10049 /* If we don't look past the constant pool, we risk emitting a
10050 reference to a constant pool entry that isn't referenced from
10051 code, and thus is not emitted. */
10052 if (rtl)
10053 rtl = avoid_constant_pool_reference (rtl);
10055 return rtl;
10058 /* Return true if DECL's containing function has a frame base attribute.
10059 Return false otherwise. */
10061 static bool
10062 containing_function_has_frame_base (tree decl)
10064 tree declcontext = decl_function_context (decl);
10065 dw_die_ref context;
10066 dw_attr_ref attr;
10068 if (!declcontext)
10069 return false;
10071 context = lookup_decl_die (declcontext);
10072 if (!context)
10073 return false;
10075 for (attr = context->die_attr; attr; attr = attr->dw_attr_next)
10076 if (attr->dw_attr == DW_AT_frame_base)
10077 return true;
10078 return false;
10081 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10082 data attribute for a variable or a parameter. We generate the
10083 DW_AT_const_value attribute only in those cases where the given variable
10084 or parameter does not have a true "location" either in memory or in a
10085 register. This can happen (for example) when a constant is passed as an
10086 actual argument in a call to an inline function. (It's possible that
10087 these things can crop up in other ways also.) Note that one type of
10088 constant value which can be passed into an inlined function is a constant
10089 pointer. This can happen for example if an actual argument in an inlined
10090 function call evaluates to a compile-time constant address. */
10092 static void
10093 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10094 enum dwarf_attribute attr)
10096 rtx rtl;
10097 dw_loc_descr_ref descr;
10098 var_loc_list *loc_list;
10099 bool can_use_fb;
10100 struct var_loc_node *node;
10101 if (TREE_CODE (decl) == ERROR_MARK)
10102 return;
10104 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10105 || TREE_CODE (decl) == RESULT_DECL);
10107 can_use_fb = containing_function_has_frame_base (decl);
10109 /* See if we possibly have multiple locations for this variable. */
10110 loc_list = lookup_decl_loc (decl);
10112 /* If it truly has multiple locations, the first and last node will
10113 differ. */
10114 if (loc_list && loc_list->first != loc_list->last)
10116 const char *secname;
10117 const char *endname;
10118 dw_loc_list_ref list;
10119 rtx varloc;
10121 /* We need to figure out what section we should use as the base
10122 for the address ranges where a given location is valid.
10123 1. If this particular DECL has a section associated with it,
10124 use that.
10125 2. If this function has a section associated with it, use
10126 that.
10127 3. Otherwise, use the text section.
10128 XXX: If you split a variable across multiple sections, this
10129 won't notice. */
10131 if (DECL_SECTION_NAME (decl))
10133 tree sectree = DECL_SECTION_NAME (decl);
10134 secname = TREE_STRING_POINTER (sectree);
10136 else if (current_function_decl
10137 && DECL_SECTION_NAME (current_function_decl))
10139 tree sectree = DECL_SECTION_NAME (current_function_decl);
10140 secname = TREE_STRING_POINTER (sectree);
10142 else if (cfun
10143 && (last_text_section == in_unlikely_executed_text
10144 || (last_text_section == in_named
10145 && last_text_section_name ==
10146 cfun->unlikely_text_section_name)))
10147 secname = cfun->cold_section_label;
10148 else
10149 secname = text_section_label;
10151 /* Now that we know what section we are using for a base,
10152 actually construct the list of locations.
10153 The first location information is what is passed to the
10154 function that creates the location list, and the remaining
10155 locations just get added on to that list.
10156 Note that we only know the start address for a location
10157 (IE location changes), so to build the range, we use
10158 the range [current location start, next location start].
10159 This means we have to special case the last node, and generate
10160 a range of [last location start, end of function label]. */
10162 node = loc_list->first;
10163 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10164 list = new_loc_list (loc_descriptor (varloc, can_use_fb),
10165 node->label, node->next->label, secname, 1);
10166 node = node->next;
10168 for (; node->next; node = node->next)
10169 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10171 /* The variable has a location between NODE->LABEL and
10172 NODE->NEXT->LABEL. */
10173 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10174 add_loc_descr_to_loc_list (&list,
10175 loc_descriptor (varloc,
10176 can_use_fb),
10177 node->label, node->next->label, secname);
10180 /* If the variable has a location at the last label
10181 it keeps its location until the end of function. */
10182 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10184 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10186 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10187 if (!current_function_decl)
10188 endname = text_end_label;
10189 else
10191 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10192 current_function_funcdef_no);
10193 endname = ggc_strdup (label_id);
10195 add_loc_descr_to_loc_list (&list,
10196 loc_descriptor (varloc,
10197 can_use_fb),
10198 node->label, endname, secname);
10201 /* Finally, add the location list to the DIE, and we are done. */
10202 add_AT_loc_list (die, attr, list);
10203 return;
10206 /* Try to get some constant RTL for this decl, and use that as the value of
10207 the location. */
10209 rtl = rtl_for_decl_location (decl);
10210 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10212 add_const_value_attribute (die, rtl);
10213 return;
10216 /* We couldn't get any rtl, and we had no >1 element location list, so try
10217 directly generating the location description from the tree. */
10218 descr = loc_descriptor_from_tree (decl);
10219 if (descr)
10221 add_AT_location_description (die, attr, descr);
10222 return;
10225 /* Lastly, if we have tried to generate the location otherwise, and it
10226 didn't work out (we wouldn't be here if we did), and we have a one entry
10227 location list, try generating a location from that. */
10228 if (loc_list && loc_list->first)
10230 node = loc_list->first;
10231 descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note),
10232 can_use_fb);
10233 if (descr)
10234 add_AT_location_description (die, attr, descr);
10238 /* If we don't have a copy of this variable in memory for some reason (such
10239 as a C++ member constant that doesn't have an out-of-line definition),
10240 we should tell the debugger about the constant value. */
10242 static void
10243 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10245 tree init = DECL_INITIAL (decl);
10246 tree type = TREE_TYPE (decl);
10247 rtx rtl;
10249 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10250 /* OK */;
10251 else
10252 return;
10254 rtl = rtl_for_decl_init (init, type);
10255 if (rtl)
10256 add_const_value_attribute (var_die, rtl);
10259 /* Generate a DW_AT_name attribute given some string value to be included as
10260 the value of the attribute. */
10262 static void
10263 add_name_attribute (dw_die_ref die, const char *name_string)
10265 if (name_string != NULL && *name_string != 0)
10267 if (demangle_name_func)
10268 name_string = (*demangle_name_func) (name_string);
10270 add_AT_string (die, DW_AT_name, name_string);
10274 /* Generate a DW_AT_comp_dir attribute for DIE. */
10276 static void
10277 add_comp_dir_attribute (dw_die_ref die)
10279 const char *wd = get_src_pwd ();
10280 if (wd != NULL)
10281 add_AT_string (die, DW_AT_comp_dir, wd);
10284 /* Given a tree node describing an array bound (either lower or upper) output
10285 a representation for that bound. */
10287 static void
10288 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10290 switch (TREE_CODE (bound))
10292 case ERROR_MARK:
10293 return;
10295 /* All fixed-bounds are represented by INTEGER_CST nodes. */
10296 case INTEGER_CST:
10297 if (! host_integerp (bound, 0)
10298 || (bound_attr == DW_AT_lower_bound
10299 && (((is_c_family () || is_java ()) && integer_zerop (bound))
10300 || (is_fortran () && integer_onep (bound)))))
10301 /* Use the default. */
10303 else
10304 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
10305 break;
10307 case CONVERT_EXPR:
10308 case NOP_EXPR:
10309 case NON_LVALUE_EXPR:
10310 case VIEW_CONVERT_EXPR:
10311 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10312 break;
10314 case SAVE_EXPR:
10315 break;
10317 case VAR_DECL:
10318 case PARM_DECL:
10319 case RESULT_DECL:
10321 dw_die_ref decl_die = lookup_decl_die (bound);
10323 /* ??? Can this happen, or should the variable have been bound
10324 first? Probably it can, since I imagine that we try to create
10325 the types of parameters in the order in which they exist in
10326 the list, and won't have created a forward reference to a
10327 later parameter. */
10328 if (decl_die != NULL)
10329 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10330 break;
10333 default:
10335 /* Otherwise try to create a stack operation procedure to
10336 evaluate the value of the array bound. */
10338 dw_die_ref ctx, decl_die;
10339 dw_loc_descr_ref loc;
10341 loc = loc_descriptor_from_tree (bound);
10342 if (loc == NULL)
10343 break;
10345 if (current_function_decl == 0)
10346 ctx = comp_unit_die;
10347 else
10348 ctx = lookup_decl_die (current_function_decl);
10350 decl_die = new_die (DW_TAG_variable, ctx, bound);
10351 add_AT_flag (decl_die, DW_AT_artificial, 1);
10352 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10353 add_AT_loc (decl_die, DW_AT_location, loc);
10355 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10356 break;
10361 /* Note that the block of subscript information for an array type also
10362 includes information about the element type of type given array type. */
10364 static void
10365 add_subscript_info (dw_die_ref type_die, tree type)
10367 #ifndef MIPS_DEBUGGING_INFO
10368 unsigned dimension_number;
10369 #endif
10370 tree lower, upper;
10371 dw_die_ref subrange_die;
10373 /* The GNU compilers represent multidimensional array types as sequences of
10374 one dimensional array types whose element types are themselves array
10375 types. Here we squish that down, so that each multidimensional array
10376 type gets only one array_type DIE in the Dwarf debugging info. The draft
10377 Dwarf specification say that we are allowed to do this kind of
10378 compression in C (because there is no difference between an array or
10379 arrays and a multidimensional array in C) but for other source languages
10380 (e.g. Ada) we probably shouldn't do this. */
10382 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10383 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10384 We work around this by disabling this feature. See also
10385 gen_array_type_die. */
10386 #ifndef MIPS_DEBUGGING_INFO
10387 for (dimension_number = 0;
10388 TREE_CODE (type) == ARRAY_TYPE;
10389 type = TREE_TYPE (type), dimension_number++)
10390 #endif
10392 tree domain = TYPE_DOMAIN (type);
10394 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
10395 and (in GNU C only) variable bounds. Handle all three forms
10396 here. */
10397 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
10398 if (domain)
10400 /* We have an array type with specified bounds. */
10401 lower = TYPE_MIN_VALUE (domain);
10402 upper = TYPE_MAX_VALUE (domain);
10404 /* Define the index type. */
10405 if (TREE_TYPE (domain))
10407 /* ??? This is probably an Ada unnamed subrange type. Ignore the
10408 TREE_TYPE field. We can't emit debug info for this
10409 because it is an unnamed integral type. */
10410 if (TREE_CODE (domain) == INTEGER_TYPE
10411 && TYPE_NAME (domain) == NULL_TREE
10412 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10413 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
10415 else
10416 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10417 type_die);
10420 /* ??? If upper is NULL, the array has unspecified length,
10421 but it does have a lower bound. This happens with Fortran
10422 dimension arr(N:*)
10423 Since the debugger is definitely going to need to know N
10424 to produce useful results, go ahead and output the lower
10425 bound solo, and hope the debugger can cope. */
10427 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
10428 if (upper)
10429 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
10432 /* Otherwise we have an array type with an unspecified length. The
10433 DWARF-2 spec does not say how to handle this; let's just leave out the
10434 bounds. */
10438 static void
10439 add_byte_size_attribute (dw_die_ref die, tree tree_node)
10441 unsigned size;
10443 switch (TREE_CODE (tree_node))
10445 case ERROR_MARK:
10446 size = 0;
10447 break;
10448 case ENUMERAL_TYPE:
10449 case RECORD_TYPE:
10450 case UNION_TYPE:
10451 case QUAL_UNION_TYPE:
10452 size = int_size_in_bytes (tree_node);
10453 break;
10454 case FIELD_DECL:
10455 /* For a data member of a struct or union, the DW_AT_byte_size is
10456 generally given as the number of bytes normally allocated for an
10457 object of the *declared* type of the member itself. This is true
10458 even for bit-fields. */
10459 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10460 break;
10461 default:
10462 gcc_unreachable ();
10465 /* Note that `size' might be -1 when we get to this point. If it is, that
10466 indicates that the byte size of the entity in question is variable. We
10467 have no good way of expressing this fact in Dwarf at the present time,
10468 so just let the -1 pass on through. */
10469 add_AT_unsigned (die, DW_AT_byte_size, size);
10472 /* For a FIELD_DECL node which represents a bit-field, output an attribute
10473 which specifies the distance in bits from the highest order bit of the
10474 "containing object" for the bit-field to the highest order bit of the
10475 bit-field itself.
10477 For any given bit-field, the "containing object" is a hypothetical object
10478 (of some integral or enum type) within which the given bit-field lives. The
10479 type of this hypothetical "containing object" is always the same as the
10480 declared type of the individual bit-field itself. The determination of the
10481 exact location of the "containing object" for a bit-field is rather
10482 complicated. It's handled by the `field_byte_offset' function (above).
10484 Note that it is the size (in bytes) of the hypothetical "containing object"
10485 which will be given in the DW_AT_byte_size attribute for this bit-field.
10486 (See `byte_size_attribute' above). */
10488 static inline void
10489 add_bit_offset_attribute (dw_die_ref die, tree decl)
10491 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10492 tree type = DECL_BIT_FIELD_TYPE (decl);
10493 HOST_WIDE_INT bitpos_int;
10494 HOST_WIDE_INT highest_order_object_bit_offset;
10495 HOST_WIDE_INT highest_order_field_bit_offset;
10496 HOST_WIDE_INT unsigned bit_offset;
10498 /* Must be a field and a bit field. */
10499 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
10501 /* We can't yet handle bit-fields whose offsets are variable, so if we
10502 encounter such things, just return without generating any attribute
10503 whatsoever. Likewise for variable or too large size. */
10504 if (! host_integerp (bit_position (decl), 0)
10505 || ! host_integerp (DECL_SIZE (decl), 1))
10506 return;
10508 bitpos_int = int_bit_position (decl);
10510 /* Note that the bit offset is always the distance (in bits) from the
10511 highest-order bit of the "containing object" to the highest-order bit of
10512 the bit-field itself. Since the "high-order end" of any object or field
10513 is different on big-endian and little-endian machines, the computation
10514 below must take account of these differences. */
10515 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10516 highest_order_field_bit_offset = bitpos_int;
10518 if (! BYTES_BIG_ENDIAN)
10520 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
10521 highest_order_object_bit_offset += simple_type_size_in_bits (type);
10524 bit_offset
10525 = (! BYTES_BIG_ENDIAN
10526 ? highest_order_object_bit_offset - highest_order_field_bit_offset
10527 : highest_order_field_bit_offset - highest_order_object_bit_offset);
10529 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10532 /* For a FIELD_DECL node which represents a bit field, output an attribute
10533 which specifies the length in bits of the given field. */
10535 static inline void
10536 add_bit_size_attribute (dw_die_ref die, tree decl)
10538 /* Must be a field and a bit field. */
10539 gcc_assert (TREE_CODE (decl) == FIELD_DECL
10540 && DECL_BIT_FIELD_TYPE (decl));
10542 if (host_integerp (DECL_SIZE (decl), 1))
10543 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
10546 /* If the compiled language is ANSI C, then add a 'prototyped'
10547 attribute, if arg types are given for the parameters of a function. */
10549 static inline void
10550 add_prototyped_attribute (dw_die_ref die, tree func_type)
10552 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10553 && TYPE_ARG_TYPES (func_type) != NULL)
10554 add_AT_flag (die, DW_AT_prototyped, 1);
10557 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
10558 by looking in either the type declaration or object declaration
10559 equate table. */
10561 static inline void
10562 add_abstract_origin_attribute (dw_die_ref die, tree origin)
10564 dw_die_ref origin_die = NULL;
10566 if (TREE_CODE (origin) != FUNCTION_DECL)
10568 /* We may have gotten separated from the block for the inlined
10569 function, if we're in an exception handler or some such; make
10570 sure that the abstract function has been written out.
10572 Doing this for nested functions is wrong, however; functions are
10573 distinct units, and our context might not even be inline. */
10574 tree fn = origin;
10576 if (TYPE_P (fn))
10577 fn = TYPE_STUB_DECL (fn);
10579 fn = decl_function_context (fn);
10580 if (fn)
10581 dwarf2out_abstract_function (fn);
10584 if (DECL_P (origin))
10585 origin_die = lookup_decl_die (origin);
10586 else if (TYPE_P (origin))
10587 origin_die = lookup_type_die (origin);
10589 /* XXX: Functions that are never lowered don't always have correct block
10590 trees (in the case of java, they simply have no block tree, in some other
10591 languages). For these functions, there is nothing we can really do to
10592 output correct debug info for inlined functions in all cases. Rather
10593 than die, we'll just produce deficient debug info now, in that we will
10594 have variables without a proper abstract origin. In the future, when all
10595 functions are lowered, we should re-add a gcc_assert (origin_die)
10596 here. */
10598 if (origin_die)
10599 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
10602 /* We do not currently support the pure_virtual attribute. */
10604 static inline void
10605 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
10607 if (DECL_VINDEX (func_decl))
10609 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10611 if (host_integerp (DECL_VINDEX (func_decl), 0))
10612 add_AT_loc (die, DW_AT_vtable_elem_location,
10613 new_loc_descr (DW_OP_constu,
10614 tree_low_cst (DECL_VINDEX (func_decl), 0),
10615 0));
10617 /* GNU extension: Record what type this method came from originally. */
10618 if (debug_info_level > DINFO_LEVEL_TERSE)
10619 add_AT_die_ref (die, DW_AT_containing_type,
10620 lookup_type_die (DECL_CONTEXT (func_decl)));
10624 /* Add source coordinate attributes for the given decl. */
10626 static void
10627 add_src_coords_attributes (dw_die_ref die, tree decl)
10629 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
10630 unsigned file_index = lookup_filename (s.file);
10632 add_AT_unsigned (die, DW_AT_decl_file, file_index);
10633 add_AT_unsigned (die, DW_AT_decl_line, s.line);
10636 /* Add a DW_AT_name attribute and source coordinate attribute for the
10637 given decl, but only if it actually has a name. */
10639 static void
10640 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
10642 tree decl_name;
10644 decl_name = DECL_NAME (decl);
10645 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
10647 add_name_attribute (die, dwarf2_name (decl, 0));
10648 if (! DECL_ARTIFICIAL (decl))
10649 add_src_coords_attributes (die, decl);
10651 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
10652 && TREE_PUBLIC (decl)
10653 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
10654 && !DECL_ABSTRACT (decl))
10655 add_AT_string (die, DW_AT_MIPS_linkage_name,
10656 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
10659 #ifdef VMS_DEBUGGING_INFO
10660 /* Get the function's name, as described by its RTL. This may be different
10661 from the DECL_NAME name used in the source file. */
10662 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
10664 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
10665 XEXP (DECL_RTL (decl), 0));
10666 VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
10668 #endif
10671 /* Push a new declaration scope. */
10673 static void
10674 push_decl_scope (tree scope)
10676 VEC_safe_push (tree, gc, decl_scope_table, scope);
10679 /* Pop a declaration scope. */
10681 static inline void
10682 pop_decl_scope (void)
10684 VEC_pop (tree, decl_scope_table);
10687 /* Return the DIE for the scope that immediately contains this type.
10688 Non-named types get global scope. Named types nested in other
10689 types get their containing scope if it's open, or global scope
10690 otherwise. All other types (i.e. function-local named types) get
10691 the current active scope. */
10693 static dw_die_ref
10694 scope_die_for (tree t, dw_die_ref context_die)
10696 dw_die_ref scope_die = NULL;
10697 tree containing_scope;
10698 int i;
10700 /* Non-types always go in the current scope. */
10701 gcc_assert (TYPE_P (t));
10703 containing_scope = TYPE_CONTEXT (t);
10705 /* Use the containing namespace if it was passed in (for a declaration). */
10706 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
10708 if (context_die == lookup_decl_die (containing_scope))
10709 /* OK */;
10710 else
10711 containing_scope = NULL_TREE;
10714 /* Ignore function type "scopes" from the C frontend. They mean that
10715 a tagged type is local to a parmlist of a function declarator, but
10716 that isn't useful to DWARF. */
10717 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
10718 containing_scope = NULL_TREE;
10720 if (containing_scope == NULL_TREE)
10721 scope_die = comp_unit_die;
10722 else if (TYPE_P (containing_scope))
10724 /* For types, we can just look up the appropriate DIE. But
10725 first we check to see if we're in the middle of emitting it
10726 so we know where the new DIE should go. */
10727 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
10728 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
10729 break;
10731 if (i < 0)
10733 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
10734 || TREE_ASM_WRITTEN (containing_scope));
10736 /* If none of the current dies are suitable, we get file scope. */
10737 scope_die = comp_unit_die;
10739 else
10740 scope_die = lookup_type_die (containing_scope);
10742 else
10743 scope_die = context_die;
10745 return scope_die;
10748 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
10750 static inline int
10751 local_scope_p (dw_die_ref context_die)
10753 for (; context_die; context_die = context_die->die_parent)
10754 if (context_die->die_tag == DW_TAG_inlined_subroutine
10755 || context_die->die_tag == DW_TAG_subprogram)
10756 return 1;
10758 return 0;
10761 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
10762 whether or not to treat a DIE in this context as a declaration. */
10764 static inline int
10765 class_or_namespace_scope_p (dw_die_ref context_die)
10767 return (context_die
10768 && (context_die->die_tag == DW_TAG_structure_type
10769 || context_die->die_tag == DW_TAG_union_type
10770 || context_die->die_tag == DW_TAG_namespace));
10773 /* Many forms of DIEs require a "type description" attribute. This
10774 routine locates the proper "type descriptor" die for the type given
10775 by 'type', and adds a DW_AT_type attribute below the given die. */
10777 static void
10778 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
10779 int decl_volatile, dw_die_ref context_die)
10781 enum tree_code code = TREE_CODE (type);
10782 dw_die_ref type_die = NULL;
10784 /* ??? If this type is an unnamed subrange type of an integral or
10785 floating-point type, use the inner type. This is because we have no
10786 support for unnamed types in base_type_die. This can happen if this is
10787 an Ada subrange type. Correct solution is emit a subrange type die. */
10788 if ((code == INTEGER_TYPE || code == REAL_TYPE)
10789 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
10790 type = TREE_TYPE (type), code = TREE_CODE (type);
10792 if (code == ERROR_MARK
10793 /* Handle a special case. For functions whose return type is void, we
10794 generate *no* type attribute. (Note that no object may have type
10795 `void', so this only applies to function return types). */
10796 || code == VOID_TYPE)
10797 return;
10799 type_die = modified_type_die (type,
10800 decl_const || TYPE_READONLY (type),
10801 decl_volatile || TYPE_VOLATILE (type),
10802 context_die);
10804 if (type_die != NULL)
10805 add_AT_die_ref (object_die, DW_AT_type, type_die);
10808 /* Given an object die, add the calling convention attribute for the
10809 function call type. */
10810 static void
10811 add_calling_convention_attribute (dw_die_ref subr_die, tree type)
10813 enum dwarf_calling_convention value = DW_CC_normal;
10815 value = targetm.dwarf_calling_convention (type);
10817 /* Only add the attribute if the backend requests it, and
10818 is not DW_CC_normal. */
10819 if (value && (value != DW_CC_normal))
10820 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
10823 /* Given a tree pointer to a struct, class, union, or enum type node, return
10824 a pointer to the (string) tag name for the given type, or zero if the type
10825 was declared without a tag. */
10827 static const char *
10828 type_tag (tree type)
10830 const char *name = 0;
10832 if (TYPE_NAME (type) != 0)
10834 tree t = 0;
10836 /* Find the IDENTIFIER_NODE for the type name. */
10837 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
10838 t = TYPE_NAME (type);
10840 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
10841 a TYPE_DECL node, regardless of whether or not a `typedef' was
10842 involved. */
10843 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10844 && ! DECL_IGNORED_P (TYPE_NAME (type)))
10845 t = DECL_NAME (TYPE_NAME (type));
10847 /* Now get the name as a string, or invent one. */
10848 if (t != 0)
10849 name = IDENTIFIER_POINTER (t);
10852 return (name == 0 || *name == '\0') ? 0 : name;
10855 /* Return the type associated with a data member, make a special check
10856 for bit field types. */
10858 static inline tree
10859 member_declared_type (tree member)
10861 return (DECL_BIT_FIELD_TYPE (member)
10862 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
10865 /* Get the decl's label, as described by its RTL. This may be different
10866 from the DECL_NAME name used in the source file. */
10868 #if 0
10869 static const char *
10870 decl_start_label (tree decl)
10872 rtx x;
10873 const char *fnname;
10875 x = DECL_RTL (decl);
10876 gcc_assert (MEM_P (x));
10878 x = XEXP (x, 0);
10879 gcc_assert (GET_CODE (x) == SYMBOL_REF);
10881 fnname = XSTR (x, 0);
10882 return fnname;
10884 #endif
10886 /* These routines generate the internal representation of the DIE's for
10887 the compilation unit. Debugging information is collected by walking
10888 the declaration trees passed in from dwarf2out_decl(). */
10890 static void
10891 gen_array_type_die (tree type, dw_die_ref context_die)
10893 dw_die_ref scope_die = scope_die_for (type, context_die);
10894 dw_die_ref array_die;
10895 tree element_type;
10897 /* ??? The SGI dwarf reader fails for array of array of enum types unless
10898 the inner array type comes before the outer array type. Thus we must
10899 call gen_type_die before we call new_die. See below also. */
10900 #ifdef MIPS_DEBUGGING_INFO
10901 gen_type_die (TREE_TYPE (type), context_die);
10902 #endif
10904 array_die = new_die (DW_TAG_array_type, scope_die, type);
10905 add_name_attribute (array_die, type_tag (type));
10906 equate_type_number_to_die (type, array_die);
10908 if (TREE_CODE (type) == VECTOR_TYPE)
10910 /* The frontend feeds us a representation for the vector as a struct
10911 containing an array. Pull out the array type. */
10912 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
10913 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
10916 #if 0
10917 /* We default the array ordering. SDB will probably do
10918 the right things even if DW_AT_ordering is not present. It's not even
10919 an issue until we start to get into multidimensional arrays anyway. If
10920 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
10921 then we'll have to put the DW_AT_ordering attribute back in. (But if
10922 and when we find out that we need to put these in, we will only do so
10923 for multidimensional arrays. */
10924 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
10925 #endif
10927 #ifdef MIPS_DEBUGGING_INFO
10928 /* The SGI compilers handle arrays of unknown bound by setting
10929 AT_declaration and not emitting any subrange DIEs. */
10930 if (! TYPE_DOMAIN (type))
10931 add_AT_flag (array_die, DW_AT_declaration, 1);
10932 else
10933 #endif
10934 add_subscript_info (array_die, type);
10936 /* Add representation of the type of the elements of this array type. */
10937 element_type = TREE_TYPE (type);
10939 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10940 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10941 We work around this by disabling this feature. See also
10942 add_subscript_info. */
10943 #ifndef MIPS_DEBUGGING_INFO
10944 while (TREE_CODE (element_type) == ARRAY_TYPE)
10945 element_type = TREE_TYPE (element_type);
10947 gen_type_die (element_type, context_die);
10948 #endif
10950 add_type_attribute (array_die, element_type, 0, 0, context_die);
10953 #if 0
10954 static void
10955 gen_entry_point_die (tree decl, dw_die_ref context_die)
10957 tree origin = decl_ultimate_origin (decl);
10958 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
10960 if (origin != NULL)
10961 add_abstract_origin_attribute (decl_die, origin);
10962 else
10964 add_name_and_src_coords_attributes (decl_die, decl);
10965 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
10966 0, 0, context_die);
10969 if (DECL_ABSTRACT (decl))
10970 equate_decl_number_to_die (decl, decl_die);
10971 else
10972 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
10974 #endif
10976 /* Walk through the list of incomplete types again, trying once more to
10977 emit full debugging info for them. */
10979 static void
10980 retry_incomplete_types (void)
10982 int i;
10984 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
10985 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
10988 /* Generate a DIE to represent an inlined instance of an enumeration type. */
10990 static void
10991 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
10993 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
10995 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10996 be incomplete and such types are not marked. */
10997 add_abstract_origin_attribute (type_die, type);
11000 /* Generate a DIE to represent an inlined instance of a structure type. */
11002 static void
11003 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
11005 dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
11007 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11008 be incomplete and such types are not marked. */
11009 add_abstract_origin_attribute (type_die, type);
11012 /* Generate a DIE to represent an inlined instance of a union type. */
11014 static void
11015 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
11017 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
11019 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11020 be incomplete and such types are not marked. */
11021 add_abstract_origin_attribute (type_die, type);
11024 /* Generate a DIE to represent an enumeration type. Note that these DIEs
11025 include all of the information about the enumeration values also. Each
11026 enumerated type name/value is listed as a child of the enumerated type
11027 DIE. */
11029 static dw_die_ref
11030 gen_enumeration_type_die (tree type, dw_die_ref context_die)
11032 dw_die_ref type_die = lookup_type_die (type);
11034 if (type_die == NULL)
11036 type_die = new_die (DW_TAG_enumeration_type,
11037 scope_die_for (type, context_die), type);
11038 equate_type_number_to_die (type, type_die);
11039 add_name_attribute (type_die, type_tag (type));
11041 else if (! TYPE_SIZE (type))
11042 return type_die;
11043 else
11044 remove_AT (type_die, DW_AT_declaration);
11046 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
11047 given enum type is incomplete, do not generate the DW_AT_byte_size
11048 attribute or the DW_AT_element_list attribute. */
11049 if (TYPE_SIZE (type))
11051 tree link;
11053 TREE_ASM_WRITTEN (type) = 1;
11054 add_byte_size_attribute (type_die, type);
11055 if (TYPE_STUB_DECL (type) != NULL_TREE)
11056 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11058 /* If the first reference to this type was as the return type of an
11059 inline function, then it may not have a parent. Fix this now. */
11060 if (type_die->die_parent == NULL)
11061 add_child_die (scope_die_for (type, context_die), type_die);
11063 for (link = TYPE_VALUES (type);
11064 link != NULL; link = TREE_CHAIN (link))
11066 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
11067 tree value = TREE_VALUE (link);
11069 add_name_attribute (enum_die,
11070 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
11072 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
11073 /* DWARF2 does not provide a way of indicating whether or
11074 not enumeration constants are signed or unsigned. GDB
11075 always assumes the values are signed, so we output all
11076 values as if they were signed. That means that
11077 enumeration constants with very large unsigned values
11078 will appear to have negative values in the debugger. */
11079 add_AT_int (enum_die, DW_AT_const_value,
11080 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
11083 else
11084 add_AT_flag (type_die, DW_AT_declaration, 1);
11086 return type_die;
11089 /* Generate a DIE to represent either a real live formal parameter decl or to
11090 represent just the type of some formal parameter position in some function
11091 type.
11093 Note that this routine is a bit unusual because its argument may be a
11094 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
11095 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
11096 node. If it's the former then this function is being called to output a
11097 DIE to represent a formal parameter object (or some inlining thereof). If
11098 it's the latter, then this function is only being called to output a
11099 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
11100 argument type of some subprogram type. */
11102 static dw_die_ref
11103 gen_formal_parameter_die (tree node, dw_die_ref context_die)
11105 dw_die_ref parm_die
11106 = new_die (DW_TAG_formal_parameter, context_die, node);
11107 tree origin;
11109 switch (TREE_CODE_CLASS (TREE_CODE (node)))
11111 case tcc_declaration:
11112 origin = decl_ultimate_origin (node);
11113 if (origin != NULL)
11114 add_abstract_origin_attribute (parm_die, origin);
11115 else
11117 add_name_and_src_coords_attributes (parm_die, node);
11118 add_type_attribute (parm_die, TREE_TYPE (node),
11119 TREE_READONLY (node),
11120 TREE_THIS_VOLATILE (node),
11121 context_die);
11122 if (DECL_ARTIFICIAL (node))
11123 add_AT_flag (parm_die, DW_AT_artificial, 1);
11126 equate_decl_number_to_die (node, parm_die);
11127 if (! DECL_ABSTRACT (node))
11128 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
11130 break;
11132 case tcc_type:
11133 /* We were called with some kind of a ..._TYPE node. */
11134 add_type_attribute (parm_die, node, 0, 0, context_die);
11135 break;
11137 default:
11138 gcc_unreachable ();
11141 return parm_die;
11144 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11145 at the end of an (ANSI prototyped) formal parameters list. */
11147 static void
11148 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11150 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11153 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11154 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11155 parameters as specified in some function type specification (except for
11156 those which appear as part of a function *definition*). */
11158 static void
11159 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11161 tree link;
11162 tree formal_type = NULL;
11163 tree first_parm_type;
11164 tree arg;
11166 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11168 arg = DECL_ARGUMENTS (function_or_method_type);
11169 function_or_method_type = TREE_TYPE (function_or_method_type);
11171 else
11172 arg = NULL_TREE;
11174 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11176 /* Make our first pass over the list of formal parameter types and output a
11177 DW_TAG_formal_parameter DIE for each one. */
11178 for (link = first_parm_type; link; )
11180 dw_die_ref parm_die;
11182 formal_type = TREE_VALUE (link);
11183 if (formal_type == void_type_node)
11184 break;
11186 /* Output a (nameless) DIE to represent the formal parameter itself. */
11187 parm_die = gen_formal_parameter_die (formal_type, context_die);
11188 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11189 && link == first_parm_type)
11190 || (arg && DECL_ARTIFICIAL (arg)))
11191 add_AT_flag (parm_die, DW_AT_artificial, 1);
11193 link = TREE_CHAIN (link);
11194 if (arg)
11195 arg = TREE_CHAIN (arg);
11198 /* If this function type has an ellipsis, add a
11199 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
11200 if (formal_type != void_type_node)
11201 gen_unspecified_parameters_die (function_or_method_type, context_die);
11203 /* Make our second (and final) pass over the list of formal parameter types
11204 and output DIEs to represent those types (as necessary). */
11205 for (link = TYPE_ARG_TYPES (function_or_method_type);
11206 link && TREE_VALUE (link);
11207 link = TREE_CHAIN (link))
11208 gen_type_die (TREE_VALUE (link), context_die);
11211 /* We want to generate the DIE for TYPE so that we can generate the
11212 die for MEMBER, which has been defined; we will need to refer back
11213 to the member declaration nested within TYPE. If we're trying to
11214 generate minimal debug info for TYPE, processing TYPE won't do the
11215 trick; we need to attach the member declaration by hand. */
11217 static void
11218 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11220 gen_type_die (type, context_die);
11222 /* If we're trying to avoid duplicate debug info, we may not have
11223 emitted the member decl for this function. Emit it now. */
11224 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11225 && ! lookup_decl_die (member))
11227 dw_die_ref type_die;
11228 gcc_assert (!decl_ultimate_origin (member));
11230 push_decl_scope (type);
11231 type_die = lookup_type_die (type);
11232 if (TREE_CODE (member) == FUNCTION_DECL)
11233 gen_subprogram_die (member, type_die);
11234 else if (TREE_CODE (member) == FIELD_DECL)
11236 /* Ignore the nameless fields that are used to skip bits but handle
11237 C++ anonymous unions and structs. */
11238 if (DECL_NAME (member) != NULL_TREE
11239 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11240 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11242 gen_type_die (member_declared_type (member), type_die);
11243 gen_field_die (member, type_die);
11246 else
11247 gen_variable_die (member, type_die);
11249 pop_decl_scope ();
11253 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11254 may later generate inlined and/or out-of-line instances of. */
11256 static void
11257 dwarf2out_abstract_function (tree decl)
11259 dw_die_ref old_die;
11260 tree save_fn;
11261 tree context;
11262 int was_abstract = DECL_ABSTRACT (decl);
11264 /* Make sure we have the actual abstract inline, not a clone. */
11265 decl = DECL_ORIGIN (decl);
11267 old_die = lookup_decl_die (decl);
11268 if (old_die && get_AT (old_die, DW_AT_inline))
11269 /* We've already generated the abstract instance. */
11270 return;
11272 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11273 we don't get confused by DECL_ABSTRACT. */
11274 if (debug_info_level > DINFO_LEVEL_TERSE)
11276 context = decl_class_context (decl);
11277 if (context)
11278 gen_type_die_for_member
11279 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11282 /* Pretend we've just finished compiling this function. */
11283 save_fn = current_function_decl;
11284 current_function_decl = decl;
11286 set_decl_abstract_flags (decl, 1);
11287 dwarf2out_decl (decl);
11288 if (! was_abstract)
11289 set_decl_abstract_flags (decl, 0);
11291 current_function_decl = save_fn;
11294 /* Generate a DIE to represent a declared function (either file-scope or
11295 block-local). */
11297 static void
11298 gen_subprogram_die (tree decl, dw_die_ref context_die)
11300 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11301 tree origin = decl_ultimate_origin (decl);
11302 dw_die_ref subr_die;
11303 rtx fp_reg;
11304 tree fn_arg_types;
11305 tree outer_scope;
11306 dw_die_ref old_die = lookup_decl_die (decl);
11307 int declaration = (current_function_decl != decl
11308 || class_or_namespace_scope_p (context_die));
11310 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11311 started to generate the abstract instance of an inline, decided to output
11312 its containing class, and proceeded to emit the declaration of the inline
11313 from the member list for the class. If so, DECLARATION takes priority;
11314 we'll get back to the abstract instance when done with the class. */
11316 /* The class-scope declaration DIE must be the primary DIE. */
11317 if (origin && declaration && class_or_namespace_scope_p (context_die))
11319 origin = NULL;
11320 gcc_assert (!old_die);
11323 if (origin != NULL)
11325 gcc_assert (!declaration || local_scope_p (context_die));
11327 /* Fixup die_parent for the abstract instance of a nested
11328 inline function. */
11329 if (old_die && old_die->die_parent == NULL)
11330 add_child_die (context_die, old_die);
11332 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11333 add_abstract_origin_attribute (subr_die, origin);
11335 else if (old_die)
11337 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11338 unsigned file_index = lookup_filename (s.file);
11340 if (!get_AT_flag (old_die, DW_AT_declaration)
11341 /* We can have a normal definition following an inline one in the
11342 case of redefinition of GNU C extern inlines.
11343 It seems reasonable to use AT_specification in this case. */
11344 && !get_AT (old_die, DW_AT_inline))
11346 /* Detect and ignore this case, where we are trying to output
11347 something we have already output. */
11348 return;
11351 /* If the definition comes from the same place as the declaration,
11352 maybe use the old DIE. We always want the DIE for this function
11353 that has the *_pc attributes to be under comp_unit_die so the
11354 debugger can find it. We also need to do this for abstract
11355 instances of inlines, since the spec requires the out-of-line copy
11356 to have the same parent. For local class methods, this doesn't
11357 apply; we just use the old DIE. */
11358 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
11359 && (DECL_ARTIFICIAL (decl)
11360 || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
11361 && (get_AT_unsigned (old_die, DW_AT_decl_line)
11362 == (unsigned) s.line))))
11364 subr_die = old_die;
11366 /* Clear out the declaration attribute and the formal parameters.
11367 Do not remove all children, because it is possible that this
11368 declaration die was forced using force_decl_die(). In such
11369 cases die that forced declaration die (e.g. TAG_imported_module)
11370 is one of the children that we do not want to remove. */
11371 remove_AT (subr_die, DW_AT_declaration);
11372 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
11374 else
11376 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11377 add_AT_specification (subr_die, old_die);
11378 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11379 add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
11380 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11381 != (unsigned) s.line)
11382 add_AT_unsigned
11383 (subr_die, DW_AT_decl_line, s.line);
11386 else
11388 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11390 if (TREE_PUBLIC (decl))
11391 add_AT_flag (subr_die, DW_AT_external, 1);
11393 add_name_and_src_coords_attributes (subr_die, decl);
11394 if (debug_info_level > DINFO_LEVEL_TERSE)
11396 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11397 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11398 0, 0, context_die);
11401 add_pure_or_virtual_attribute (subr_die, decl);
11402 if (DECL_ARTIFICIAL (decl))
11403 add_AT_flag (subr_die, DW_AT_artificial, 1);
11405 if (TREE_PROTECTED (decl))
11406 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11407 else if (TREE_PRIVATE (decl))
11408 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
11411 if (declaration)
11413 if (!old_die || !get_AT (old_die, DW_AT_inline))
11415 add_AT_flag (subr_die, DW_AT_declaration, 1);
11417 /* The first time we see a member function, it is in the context of
11418 the class to which it belongs. We make sure of this by emitting
11419 the class first. The next time is the definition, which is
11420 handled above. The two may come from the same source text.
11422 Note that force_decl_die() forces function declaration die. It is
11423 later reused to represent definition. */
11424 equate_decl_number_to_die (decl, subr_die);
11427 else if (DECL_ABSTRACT (decl))
11429 if (DECL_DECLARED_INLINE_P (decl))
11431 if (cgraph_function_possibly_inlined_p (decl))
11432 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11433 else
11434 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
11436 else
11438 if (cgraph_function_possibly_inlined_p (decl))
11439 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11440 else
11441 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11444 equate_decl_number_to_die (decl, subr_die);
11446 else if (!DECL_EXTERNAL (decl))
11448 if (!old_die || !get_AT (old_die, DW_AT_inline))
11449 equate_decl_number_to_die (decl, subr_die);
11451 if (!flag_reorder_blocks_and_partition)
11453 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11454 current_function_funcdef_no);
11455 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11456 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11457 current_function_funcdef_no);
11458 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11460 add_pubname (decl, subr_die);
11461 add_arange (decl, subr_die);
11463 else
11464 { /* Do nothing for now; maybe need to duplicate die, one for
11465 hot section and ond for cold section, then use the hot/cold
11466 section begin/end labels to generate the aranges... */
11468 add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
11469 add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
11470 add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
11471 add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
11473 add_pubname (decl, subr_die);
11474 add_arange (decl, subr_die);
11475 add_arange (decl, subr_die);
11479 #ifdef MIPS_DEBUGGING_INFO
11480 /* Add a reference to the FDE for this routine. */
11481 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11482 #endif
11484 /* Define the "frame base" location for this routine. We use the
11485 frame pointer or stack pointer registers, since the RTL for local
11486 variables is relative to one of them. */
11487 if (frame_base_decl && lookup_decl_loc (frame_base_decl) != NULL)
11489 add_location_or_const_value_attribute (subr_die, frame_base_decl,
11490 DW_AT_frame_base);
11492 else
11494 fp_reg
11495 = frame_pointer_needed ? hard_frame_pointer_rtx : stack_pointer_rtx;
11496 add_AT_loc (subr_die, DW_AT_frame_base, reg_loc_descriptor (fp_reg));
11499 if (cfun->static_chain_decl)
11500 add_AT_location_description (subr_die, DW_AT_static_link,
11501 loc_descriptor_from_tree (cfun->static_chain_decl));
11504 /* Now output descriptions of the arguments for this function. This gets
11505 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
11506 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
11507 `...' at the end of the formal parameter list. In order to find out if
11508 there was a trailing ellipsis or not, we must instead look at the type
11509 associated with the FUNCTION_DECL. This will be a node of type
11510 FUNCTION_TYPE. If the chain of type nodes hanging off of this
11511 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
11512 an ellipsis at the end. */
11514 /* In the case where we are describing a mere function declaration, all we
11515 need to do here (and all we *can* do here) is to describe the *types* of
11516 its formal parameters. */
11517 if (debug_info_level <= DINFO_LEVEL_TERSE)
11519 else if (declaration)
11520 gen_formal_types_die (decl, subr_die);
11521 else
11523 /* Generate DIEs to represent all known formal parameters. */
11524 tree arg_decls = DECL_ARGUMENTS (decl);
11525 tree parm;
11527 /* When generating DIEs, generate the unspecified_parameters DIE
11528 instead if we come across the arg "__builtin_va_alist" */
11529 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
11530 if (TREE_CODE (parm) == PARM_DECL)
11532 if (DECL_NAME (parm)
11533 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
11534 "__builtin_va_alist"))
11535 gen_unspecified_parameters_die (parm, subr_die);
11536 else
11537 gen_decl_die (parm, subr_die);
11540 /* Decide whether we need an unspecified_parameters DIE at the end.
11541 There are 2 more cases to do this for: 1) the ansi ... declaration -
11542 this is detectable when the end of the arg list is not a
11543 void_type_node 2) an unprototyped function declaration (not a
11544 definition). This just means that we have no info about the
11545 parameters at all. */
11546 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
11547 if (fn_arg_types != NULL)
11549 /* This is the prototyped case, check for.... */
11550 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
11551 gen_unspecified_parameters_die (decl, subr_die);
11553 else if (DECL_INITIAL (decl) == NULL_TREE)
11554 gen_unspecified_parameters_die (decl, subr_die);
11557 /* Output Dwarf info for all of the stuff within the body of the function
11558 (if it has one - it may be just a declaration). */
11559 outer_scope = DECL_INITIAL (decl);
11561 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
11562 a function. This BLOCK actually represents the outermost binding contour
11563 for the function, i.e. the contour in which the function's formal
11564 parameters and labels get declared. Curiously, it appears that the front
11565 end doesn't actually put the PARM_DECL nodes for the current function onto
11566 the BLOCK_VARS list for this outer scope, but are strung off of the
11567 DECL_ARGUMENTS list for the function instead.
11569 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
11570 the LABEL_DECL nodes for the function however, and we output DWARF info
11571 for those in decls_for_scope. Just within the `outer_scope' there will be
11572 a BLOCK node representing the function's outermost pair of curly braces,
11573 and any blocks used for the base and member initializers of a C++
11574 constructor function. */
11575 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
11577 /* Emit a DW_TAG_variable DIE for a named return value. */
11578 if (DECL_NAME (DECL_RESULT (decl)))
11579 gen_decl_die (DECL_RESULT (decl), subr_die);
11581 current_function_has_inlines = 0;
11582 decls_for_scope (outer_scope, subr_die, 0);
11584 #if 0 && defined (MIPS_DEBUGGING_INFO)
11585 if (current_function_has_inlines)
11587 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
11588 if (! comp_unit_has_inlines)
11590 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
11591 comp_unit_has_inlines = 1;
11594 #endif
11596 /* Add the calling convention attribute if requested. */
11597 add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
11601 /* Generate a DIE to represent a declared data object. */
11603 static void
11604 gen_variable_die (tree decl, dw_die_ref context_die)
11606 tree origin = decl_ultimate_origin (decl);
11607 dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
11609 dw_die_ref old_die = lookup_decl_die (decl);
11610 int declaration = (DECL_EXTERNAL (decl)
11611 || class_or_namespace_scope_p (context_die));
11613 if (origin != NULL)
11614 add_abstract_origin_attribute (var_die, origin);
11616 /* Loop unrolling can create multiple blocks that refer to the same
11617 static variable, so we must test for the DW_AT_declaration flag.
11619 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
11620 copy decls and set the DECL_ABSTRACT flag on them instead of
11621 sharing them.
11623 ??? Duplicated blocks have been rewritten to use .debug_ranges.
11625 ??? The declare_in_namespace support causes us to get two DIEs for one
11626 variable, both of which are declarations. We want to avoid considering
11627 one to be a specification, so we must test that this DIE is not a
11628 declaration. */
11629 else if (old_die && TREE_STATIC (decl) && ! declaration
11630 && get_AT_flag (old_die, DW_AT_declaration) == 1)
11632 /* This is a definition of a C++ class level static. */
11633 add_AT_specification (var_die, old_die);
11634 if (DECL_NAME (decl))
11636 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11637 unsigned file_index = lookup_filename (s.file);
11639 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11640 add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
11642 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11643 != (unsigned) s.line)
11645 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
11648 else
11650 add_name_and_src_coords_attributes (var_die, decl);
11651 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
11652 TREE_THIS_VOLATILE (decl), context_die);
11654 if (TREE_PUBLIC (decl))
11655 add_AT_flag (var_die, DW_AT_external, 1);
11657 if (DECL_ARTIFICIAL (decl))
11658 add_AT_flag (var_die, DW_AT_artificial, 1);
11660 if (TREE_PROTECTED (decl))
11661 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
11662 else if (TREE_PRIVATE (decl))
11663 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
11666 if (declaration)
11667 add_AT_flag (var_die, DW_AT_declaration, 1);
11669 if (DECL_ABSTRACT (decl) || declaration)
11670 equate_decl_number_to_die (decl, var_die);
11672 if (! declaration && ! DECL_ABSTRACT (decl))
11674 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
11675 add_pubname (decl, var_die);
11677 else
11678 tree_add_const_value_attribute (var_die, decl);
11681 /* Generate a DIE to represent a label identifier. */
11683 static void
11684 gen_label_die (tree decl, dw_die_ref context_die)
11686 tree origin = decl_ultimate_origin (decl);
11687 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
11688 rtx insn;
11689 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11691 if (origin != NULL)
11692 add_abstract_origin_attribute (lbl_die, origin);
11693 else
11694 add_name_and_src_coords_attributes (lbl_die, decl);
11696 if (DECL_ABSTRACT (decl))
11697 equate_decl_number_to_die (decl, lbl_die);
11698 else
11700 insn = DECL_RTL_IF_SET (decl);
11702 /* Deleted labels are programmer specified labels which have been
11703 eliminated because of various optimizations. We still emit them
11704 here so that it is possible to put breakpoints on them. */
11705 if (insn
11706 && (LABEL_P (insn)
11707 || ((NOTE_P (insn)
11708 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
11710 /* When optimization is enabled (via -O) some parts of the compiler
11711 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
11712 represent source-level labels which were explicitly declared by
11713 the user. This really shouldn't be happening though, so catch
11714 it if it ever does happen. */
11715 gcc_assert (!INSN_DELETED_P (insn));
11717 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
11718 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
11723 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
11724 Add low_pc and high_pc attributes to the DIE for a block STMT. */
11726 static inline void
11727 add_high_low_attributes (tree stmt, dw_die_ref die)
11729 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11731 if (BLOCK_FRAGMENT_CHAIN (stmt))
11733 tree chain;
11735 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
11737 chain = BLOCK_FRAGMENT_CHAIN (stmt);
11740 add_ranges (chain);
11741 chain = BLOCK_FRAGMENT_CHAIN (chain);
11743 while (chain);
11744 add_ranges (NULL);
11746 else
11748 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
11749 BLOCK_NUMBER (stmt));
11750 add_AT_lbl_id (die, DW_AT_low_pc, label);
11751 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
11752 BLOCK_NUMBER (stmt));
11753 add_AT_lbl_id (die, DW_AT_high_pc, label);
11757 /* Generate a DIE for a lexical block. */
11759 static void
11760 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
11762 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
11764 if (! BLOCK_ABSTRACT (stmt))
11765 add_high_low_attributes (stmt, stmt_die);
11767 decls_for_scope (stmt, stmt_die, depth);
11770 /* Generate a DIE for an inlined subprogram. */
11772 static void
11773 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
11775 tree decl = block_ultimate_origin (stmt);
11777 /* Emit info for the abstract instance first, if we haven't yet. We
11778 must emit this even if the block is abstract, otherwise when we
11779 emit the block below (or elsewhere), we may end up trying to emit
11780 a die whose origin die hasn't been emitted, and crashing. */
11781 dwarf2out_abstract_function (decl);
11783 if (! BLOCK_ABSTRACT (stmt))
11785 dw_die_ref subr_die
11786 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
11788 add_abstract_origin_attribute (subr_die, decl);
11789 add_high_low_attributes (stmt, subr_die);
11791 decls_for_scope (stmt, subr_die, depth);
11792 current_function_has_inlines = 1;
11794 else
11795 /* We may get here if we're the outer block of function A that was
11796 inlined into function B that was inlined into function C. When
11797 generating debugging info for C, dwarf2out_abstract_function(B)
11798 would mark all inlined blocks as abstract, including this one.
11799 So, we wouldn't (and shouldn't) expect labels to be generated
11800 for this one. Instead, just emit debugging info for
11801 declarations within the block. This is particularly important
11802 in the case of initializers of arguments passed from B to us:
11803 if they're statement expressions containing declarations, we
11804 wouldn't generate dies for their abstract variables, and then,
11805 when generating dies for the real variables, we'd die (pun
11806 intended :-) */
11807 gen_lexical_block_die (stmt, context_die, depth);
11810 /* Generate a DIE for a field in a record, or structure. */
11812 static void
11813 gen_field_die (tree decl, dw_die_ref context_die)
11815 dw_die_ref decl_die;
11817 if (TREE_TYPE (decl) == error_mark_node)
11818 return;
11820 decl_die = new_die (DW_TAG_member, context_die, decl);
11821 add_name_and_src_coords_attributes (decl_die, decl);
11822 add_type_attribute (decl_die, member_declared_type (decl),
11823 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
11824 context_die);
11826 if (DECL_BIT_FIELD_TYPE (decl))
11828 add_byte_size_attribute (decl_die, decl);
11829 add_bit_size_attribute (decl_die, decl);
11830 add_bit_offset_attribute (decl_die, decl);
11833 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
11834 add_data_member_location_attribute (decl_die, decl);
11836 if (DECL_ARTIFICIAL (decl))
11837 add_AT_flag (decl_die, DW_AT_artificial, 1);
11839 if (TREE_PROTECTED (decl))
11840 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
11841 else if (TREE_PRIVATE (decl))
11842 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
11844 /* Equate decl number to die, so that we can look up this decl later on. */
11845 equate_decl_number_to_die (decl, decl_die);
11848 #if 0
11849 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
11850 Use modified_type_die instead.
11851 We keep this code here just in case these types of DIEs may be needed to
11852 represent certain things in other languages (e.g. Pascal) someday. */
11854 static void
11855 gen_pointer_type_die (tree type, dw_die_ref context_die)
11857 dw_die_ref ptr_die
11858 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
11860 equate_type_number_to_die (type, ptr_die);
11861 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
11862 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
11865 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
11866 Use modified_type_die instead.
11867 We keep this code here just in case these types of DIEs may be needed to
11868 represent certain things in other languages (e.g. Pascal) someday. */
11870 static void
11871 gen_reference_type_die (tree type, dw_die_ref context_die)
11873 dw_die_ref ref_die
11874 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
11876 equate_type_number_to_die (type, ref_die);
11877 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
11878 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
11880 #endif
11882 /* Generate a DIE for a pointer to a member type. */
11884 static void
11885 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
11887 dw_die_ref ptr_die
11888 = new_die (DW_TAG_ptr_to_member_type,
11889 scope_die_for (type, context_die), type);
11891 equate_type_number_to_die (type, ptr_die);
11892 add_AT_die_ref (ptr_die, DW_AT_containing_type,
11893 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
11894 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
11897 /* Generate the DIE for the compilation unit. */
11899 static dw_die_ref
11900 gen_compile_unit_die (const char *filename)
11902 dw_die_ref die;
11903 char producer[250];
11904 const char *language_string = lang_hooks.name;
11905 int language;
11907 die = new_die (DW_TAG_compile_unit, NULL, NULL);
11909 if (filename)
11911 add_name_attribute (die, filename);
11912 /* Don't add cwd for <built-in>. */
11913 if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
11914 add_comp_dir_attribute (die);
11917 sprintf (producer, "%s %s", language_string, version_string);
11919 #ifdef MIPS_DEBUGGING_INFO
11920 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
11921 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
11922 not appear in the producer string, the debugger reaches the conclusion
11923 that the object file is stripped and has no debugging information.
11924 To get the MIPS/SGI debugger to believe that there is debugging
11925 information in the object file, we add a -g to the producer string. */
11926 if (debug_info_level > DINFO_LEVEL_TERSE)
11927 strcat (producer, " -g");
11928 #endif
11930 add_AT_string (die, DW_AT_producer, producer);
11932 if (strcmp (language_string, "GNU C++") == 0)
11933 language = DW_LANG_C_plus_plus;
11934 else if (strcmp (language_string, "GNU Ada") == 0)
11935 language = DW_LANG_Ada95;
11936 else if (strcmp (language_string, "GNU F77") == 0)
11937 language = DW_LANG_Fortran77;
11938 else if (strcmp (language_string, "GNU F95") == 0)
11939 language = DW_LANG_Fortran95;
11940 else if (strcmp (language_string, "GNU Pascal") == 0)
11941 language = DW_LANG_Pascal83;
11942 else if (strcmp (language_string, "GNU Java") == 0)
11943 language = DW_LANG_Java;
11944 else
11945 language = DW_LANG_C89;
11947 add_AT_unsigned (die, DW_AT_language, language);
11948 return die;
11951 /* Generate a DIE for a string type. */
11953 static void
11954 gen_string_type_die (tree type, dw_die_ref context_die)
11956 dw_die_ref type_die
11957 = new_die (DW_TAG_string_type, scope_die_for (type, context_die), type);
11959 equate_type_number_to_die (type, type_die);
11961 /* ??? Fudge the string length attribute for now.
11962 TODO: add string length info. */
11963 #if 0
11964 string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
11965 bound_representation (upper_bound, 0, 'u');
11966 #endif
11969 /* Generate the DIE for a base class. */
11971 static void
11972 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
11974 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
11976 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
11977 add_data_member_location_attribute (die, binfo);
11979 if (BINFO_VIRTUAL_P (binfo))
11980 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11982 if (access == access_public_node)
11983 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
11984 else if (access == access_protected_node)
11985 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
11988 /* Generate a DIE for a class member. */
11990 static void
11991 gen_member_die (tree type, dw_die_ref context_die)
11993 tree member;
11994 tree binfo = TYPE_BINFO (type);
11995 dw_die_ref child;
11997 /* If this is not an incomplete type, output descriptions of each of its
11998 members. Note that as we output the DIEs necessary to represent the
11999 members of this record or union type, we will also be trying to output
12000 DIEs to represent the *types* of those members. However the `type'
12001 function (above) will specifically avoid generating type DIEs for member
12002 types *within* the list of member DIEs for this (containing) type except
12003 for those types (of members) which are explicitly marked as also being
12004 members of this (containing) type themselves. The g++ front- end can
12005 force any given type to be treated as a member of some other (containing)
12006 type by setting the TYPE_CONTEXT of the given (member) type to point to
12007 the TREE node representing the appropriate (containing) type. */
12009 /* First output info about the base classes. */
12010 if (binfo)
12012 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
12013 int i;
12014 tree base;
12016 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12017 gen_inheritance_die (base,
12018 (accesses ? VEC_index (tree, accesses, i)
12019 : access_public_node), context_die);
12022 /* Now output info about the data members and type members. */
12023 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
12025 /* If we thought we were generating minimal debug info for TYPE
12026 and then changed our minds, some of the member declarations
12027 may have already been defined. Don't define them again, but
12028 do put them in the right order. */
12030 child = lookup_decl_die (member);
12031 if (child)
12032 splice_child_die (context_die, child);
12033 else
12034 gen_decl_die (member, context_die);
12037 /* Now output info about the function members (if any). */
12038 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
12040 /* Don't include clones in the member list. */
12041 if (DECL_ABSTRACT_ORIGIN (member))
12042 continue;
12044 child = lookup_decl_die (member);
12045 if (child)
12046 splice_child_die (context_die, child);
12047 else
12048 gen_decl_die (member, context_die);
12052 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
12053 is set, we pretend that the type was never defined, so we only get the
12054 member DIEs needed by later specification DIEs. */
12056 static void
12057 gen_struct_or_union_type_die (tree type, dw_die_ref context_die)
12059 dw_die_ref type_die = lookup_type_die (type);
12060 dw_die_ref scope_die = 0;
12061 int nested = 0;
12062 int complete = (TYPE_SIZE (type)
12063 && (! TYPE_STUB_DECL (type)
12064 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
12065 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
12067 if (type_die && ! complete)
12068 return;
12070 if (TYPE_CONTEXT (type) != NULL_TREE
12071 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12072 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
12073 nested = 1;
12075 scope_die = scope_die_for (type, context_die);
12077 if (! type_die || (nested && scope_die == comp_unit_die))
12078 /* First occurrence of type or toplevel definition of nested class. */
12080 dw_die_ref old_die = type_die;
12082 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
12083 ? DW_TAG_structure_type : DW_TAG_union_type,
12084 scope_die, type);
12085 equate_type_number_to_die (type, type_die);
12086 if (old_die)
12087 add_AT_specification (type_die, old_die);
12088 else
12089 add_name_attribute (type_die, type_tag (type));
12091 else
12092 remove_AT (type_die, DW_AT_declaration);
12094 /* If this type has been completed, then give it a byte_size attribute and
12095 then give a list of members. */
12096 if (complete && !ns_decl)
12098 /* Prevent infinite recursion in cases where the type of some member of
12099 this type is expressed in terms of this type itself. */
12100 TREE_ASM_WRITTEN (type) = 1;
12101 add_byte_size_attribute (type_die, type);
12102 if (TYPE_STUB_DECL (type) != NULL_TREE)
12103 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12105 /* If the first reference to this type was as the return type of an
12106 inline function, then it may not have a parent. Fix this now. */
12107 if (type_die->die_parent == NULL)
12108 add_child_die (scope_die, type_die);
12110 push_decl_scope (type);
12111 gen_member_die (type, type_die);
12112 pop_decl_scope ();
12114 /* GNU extension: Record what type our vtable lives in. */
12115 if (TYPE_VFIELD (type))
12117 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
12119 gen_type_die (vtype, context_die);
12120 add_AT_die_ref (type_die, DW_AT_containing_type,
12121 lookup_type_die (vtype));
12124 else
12126 add_AT_flag (type_die, DW_AT_declaration, 1);
12128 /* We don't need to do this for function-local types. */
12129 if (TYPE_STUB_DECL (type)
12130 && ! decl_function_context (TYPE_STUB_DECL (type)))
12131 VEC_safe_push (tree, gc, incomplete_types, type);
12135 /* Generate a DIE for a subroutine _type_. */
12137 static void
12138 gen_subroutine_type_die (tree type, dw_die_ref context_die)
12140 tree return_type = TREE_TYPE (type);
12141 dw_die_ref subr_die
12142 = new_die (DW_TAG_subroutine_type,
12143 scope_die_for (type, context_die), type);
12145 equate_type_number_to_die (type, subr_die);
12146 add_prototyped_attribute (subr_die, type);
12147 add_type_attribute (subr_die, return_type, 0, 0, context_die);
12148 gen_formal_types_die (type, subr_die);
12151 /* Generate a DIE for a type definition. */
12153 static void
12154 gen_typedef_die (tree decl, dw_die_ref context_die)
12156 dw_die_ref type_die;
12157 tree origin;
12159 if (TREE_ASM_WRITTEN (decl))
12160 return;
12162 TREE_ASM_WRITTEN (decl) = 1;
12163 type_die = new_die (DW_TAG_typedef, context_die, decl);
12164 origin = decl_ultimate_origin (decl);
12165 if (origin != NULL)
12166 add_abstract_origin_attribute (type_die, origin);
12167 else
12169 tree type;
12171 add_name_and_src_coords_attributes (type_die, decl);
12172 if (DECL_ORIGINAL_TYPE (decl))
12174 type = DECL_ORIGINAL_TYPE (decl);
12176 gcc_assert (type != TREE_TYPE (decl));
12177 equate_type_number_to_die (TREE_TYPE (decl), type_die);
12179 else
12180 type = TREE_TYPE (decl);
12182 add_type_attribute (type_die, type, TREE_READONLY (decl),
12183 TREE_THIS_VOLATILE (decl), context_die);
12186 if (DECL_ABSTRACT (decl))
12187 equate_decl_number_to_die (decl, type_die);
12190 /* Generate a type description DIE. */
12192 static void
12193 gen_type_die (tree type, dw_die_ref context_die)
12195 int need_pop;
12197 if (type == NULL_TREE || type == error_mark_node)
12198 return;
12200 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12201 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
12203 if (TREE_ASM_WRITTEN (type))
12204 return;
12206 /* Prevent broken recursion; we can't hand off to the same type. */
12207 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
12209 TREE_ASM_WRITTEN (type) = 1;
12210 gen_decl_die (TYPE_NAME (type), context_die);
12211 return;
12214 /* We are going to output a DIE to represent the unqualified version
12215 of this type (i.e. without any const or volatile qualifiers) so
12216 get the main variant (i.e. the unqualified version) of this type
12217 now. (Vectors are special because the debugging info is in the
12218 cloned type itself). */
12219 if (TREE_CODE (type) != VECTOR_TYPE)
12220 type = type_main_variant (type);
12222 if (TREE_ASM_WRITTEN (type))
12223 return;
12225 switch (TREE_CODE (type))
12227 case ERROR_MARK:
12228 break;
12230 case POINTER_TYPE:
12231 case REFERENCE_TYPE:
12232 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
12233 ensures that the gen_type_die recursion will terminate even if the
12234 type is recursive. Recursive types are possible in Ada. */
12235 /* ??? We could perhaps do this for all types before the switch
12236 statement. */
12237 TREE_ASM_WRITTEN (type) = 1;
12239 /* For these types, all that is required is that we output a DIE (or a
12240 set of DIEs) to represent the "basis" type. */
12241 gen_type_die (TREE_TYPE (type), context_die);
12242 break;
12244 case OFFSET_TYPE:
12245 /* This code is used for C++ pointer-to-data-member types.
12246 Output a description of the relevant class type. */
12247 gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
12249 /* Output a description of the type of the object pointed to. */
12250 gen_type_die (TREE_TYPE (type), context_die);
12252 /* Now output a DIE to represent this pointer-to-data-member type
12253 itself. */
12254 gen_ptr_to_mbr_type_die (type, context_die);
12255 break;
12257 case FUNCTION_TYPE:
12258 /* Force out return type (in case it wasn't forced out already). */
12259 gen_type_die (TREE_TYPE (type), context_die);
12260 gen_subroutine_type_die (type, context_die);
12261 break;
12263 case METHOD_TYPE:
12264 /* Force out return type (in case it wasn't forced out already). */
12265 gen_type_die (TREE_TYPE (type), context_die);
12266 gen_subroutine_type_die (type, context_die);
12267 break;
12269 case ARRAY_TYPE:
12270 if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
12272 gen_type_die (TREE_TYPE (type), context_die);
12273 gen_string_type_die (type, context_die);
12275 else
12276 gen_array_type_die (type, context_die);
12277 break;
12279 case VECTOR_TYPE:
12280 gen_array_type_die (type, context_die);
12281 break;
12283 case ENUMERAL_TYPE:
12284 case RECORD_TYPE:
12285 case UNION_TYPE:
12286 case QUAL_UNION_TYPE:
12287 /* If this is a nested type whose containing class hasn't been written
12288 out yet, writing it out will cover this one, too. This does not apply
12289 to instantiations of member class templates; they need to be added to
12290 the containing class as they are generated. FIXME: This hurts the
12291 idea of combining type decls from multiple TUs, since we can't predict
12292 what set of template instantiations we'll get. */
12293 if (TYPE_CONTEXT (type)
12294 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12295 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
12297 gen_type_die (TYPE_CONTEXT (type), context_die);
12299 if (TREE_ASM_WRITTEN (type))
12300 return;
12302 /* If that failed, attach ourselves to the stub. */
12303 push_decl_scope (TYPE_CONTEXT (type));
12304 context_die = lookup_type_die (TYPE_CONTEXT (type));
12305 need_pop = 1;
12307 else
12309 declare_in_namespace (type, context_die);
12310 need_pop = 0;
12313 if (TREE_CODE (type) == ENUMERAL_TYPE)
12314 gen_enumeration_type_die (type, context_die);
12315 else
12316 gen_struct_or_union_type_die (type, context_die);
12318 if (need_pop)
12319 pop_decl_scope ();
12321 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
12322 it up if it is ever completed. gen_*_type_die will set it for us
12323 when appropriate. */
12324 return;
12326 case VOID_TYPE:
12327 case INTEGER_TYPE:
12328 case REAL_TYPE:
12329 case COMPLEX_TYPE:
12330 case BOOLEAN_TYPE:
12331 case CHAR_TYPE:
12332 /* No DIEs needed for fundamental types. */
12333 break;
12335 case LANG_TYPE:
12336 /* No Dwarf representation currently defined. */
12337 break;
12339 default:
12340 gcc_unreachable ();
12343 TREE_ASM_WRITTEN (type) = 1;
12346 /* Generate a DIE for a tagged type instantiation. */
12348 static void
12349 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
12351 if (type == NULL_TREE || type == error_mark_node)
12352 return;
12354 /* We are going to output a DIE to represent the unqualified version of
12355 this type (i.e. without any const or volatile qualifiers) so make sure
12356 that we have the main variant (i.e. the unqualified version) of this
12357 type now. */
12358 gcc_assert (type == type_main_variant (type));
12360 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
12361 an instance of an unresolved type. */
12363 switch (TREE_CODE (type))
12365 case ERROR_MARK:
12366 break;
12368 case ENUMERAL_TYPE:
12369 gen_inlined_enumeration_type_die (type, context_die);
12370 break;
12372 case RECORD_TYPE:
12373 gen_inlined_structure_type_die (type, context_die);
12374 break;
12376 case UNION_TYPE:
12377 case QUAL_UNION_TYPE:
12378 gen_inlined_union_type_die (type, context_die);
12379 break;
12381 default:
12382 gcc_unreachable ();
12386 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12387 things which are local to the given block. */
12389 static void
12390 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
12392 int must_output_die = 0;
12393 tree origin;
12394 tree decl;
12395 enum tree_code origin_code;
12397 /* Ignore blocks that are NULL. */
12398 if (stmt == NULL_TREE)
12399 return;
12401 /* If the block is one fragment of a non-contiguous block, do not
12402 process the variables, since they will have been done by the
12403 origin block. Do process subblocks. */
12404 if (BLOCK_FRAGMENT_ORIGIN (stmt))
12406 tree sub;
12408 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
12409 gen_block_die (sub, context_die, depth + 1);
12411 return;
12414 /* Determine the "ultimate origin" of this block. This block may be an
12415 inlined instance of an inlined instance of inline function, so we have
12416 to trace all of the way back through the origin chain to find out what
12417 sort of node actually served as the original seed for the creation of
12418 the current block. */
12419 origin = block_ultimate_origin (stmt);
12420 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
12422 /* Determine if we need to output any Dwarf DIEs at all to represent this
12423 block. */
12424 if (origin_code == FUNCTION_DECL)
12425 /* The outer scopes for inlinings *must* always be represented. We
12426 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
12427 must_output_die = 1;
12428 else
12430 /* In the case where the current block represents an inlining of the
12431 "body block" of an inline function, we must *NOT* output any DIE for
12432 this block because we have already output a DIE to represent the whole
12433 inlined function scope and the "body block" of any function doesn't
12434 really represent a different scope according to ANSI C rules. So we
12435 check here to make sure that this block does not represent a "body
12436 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
12437 if (! is_body_block (origin ? origin : stmt))
12439 /* Determine if this block directly contains any "significant"
12440 local declarations which we will need to output DIEs for. */
12441 if (debug_info_level > DINFO_LEVEL_TERSE)
12442 /* We are not in terse mode so *any* local declaration counts
12443 as being a "significant" one. */
12444 must_output_die = (BLOCK_VARS (stmt) != NULL
12445 && (TREE_USED (stmt)
12446 || TREE_ASM_WRITTEN (stmt)
12447 || BLOCK_ABSTRACT (stmt)));
12448 else
12449 /* We are in terse mode, so only local (nested) function
12450 definitions count as "significant" local declarations. */
12451 for (decl = BLOCK_VARS (stmt);
12452 decl != NULL; decl = TREE_CHAIN (decl))
12453 if (TREE_CODE (decl) == FUNCTION_DECL
12454 && DECL_INITIAL (decl))
12456 must_output_die = 1;
12457 break;
12462 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
12463 DIE for any block which contains no significant local declarations at
12464 all. Rather, in such cases we just call `decls_for_scope' so that any
12465 needed Dwarf info for any sub-blocks will get properly generated. Note
12466 that in terse mode, our definition of what constitutes a "significant"
12467 local declaration gets restricted to include only inlined function
12468 instances and local (nested) function definitions. */
12469 if (must_output_die)
12471 if (origin_code == FUNCTION_DECL)
12472 gen_inlined_subroutine_die (stmt, context_die, depth);
12473 else
12474 gen_lexical_block_die (stmt, context_die, depth);
12476 else
12477 decls_for_scope (stmt, context_die, depth);
12480 /* Generate all of the decls declared within a given scope and (recursively)
12481 all of its sub-blocks. */
12483 static void
12484 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
12486 tree decl;
12487 tree subblocks;
12489 /* Ignore NULL blocks. */
12490 if (stmt == NULL_TREE)
12491 return;
12493 if (TREE_USED (stmt))
12495 /* Output the DIEs to represent all of the data objects and typedefs
12496 declared directly within this block but not within any nested
12497 sub-blocks. Also, nested function and tag DIEs have been
12498 generated with a parent of NULL; fix that up now. */
12499 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
12501 dw_die_ref die;
12503 if (TREE_CODE (decl) == FUNCTION_DECL)
12504 die = lookup_decl_die (decl);
12505 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
12506 die = lookup_type_die (TREE_TYPE (decl));
12507 else
12508 die = NULL;
12510 if (die != NULL && die->die_parent == NULL)
12511 add_child_die (context_die, die);
12512 else
12513 gen_decl_die (decl, context_die);
12517 /* If we're at -g1, we're not interested in subblocks. */
12518 if (debug_info_level <= DINFO_LEVEL_TERSE)
12519 return;
12521 /* Output the DIEs to represent all sub-blocks (and the items declared
12522 therein) of this block. */
12523 for (subblocks = BLOCK_SUBBLOCKS (stmt);
12524 subblocks != NULL;
12525 subblocks = BLOCK_CHAIN (subblocks))
12526 gen_block_die (subblocks, context_die, depth + 1);
12529 /* Is this a typedef we can avoid emitting? */
12531 static inline int
12532 is_redundant_typedef (tree decl)
12534 if (TYPE_DECL_IS_STUB (decl))
12535 return 1;
12537 if (DECL_ARTIFICIAL (decl)
12538 && DECL_CONTEXT (decl)
12539 && is_tagged_type (DECL_CONTEXT (decl))
12540 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
12541 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
12542 /* Also ignore the artificial member typedef for the class name. */
12543 return 1;
12545 return 0;
12548 /* Returns the DIE for decl. A DIE will always be returned. */
12550 static dw_die_ref
12551 force_decl_die (tree decl)
12553 dw_die_ref decl_die;
12554 unsigned saved_external_flag;
12555 tree save_fn = NULL_TREE;
12556 decl_die = lookup_decl_die (decl);
12557 if (!decl_die)
12559 dw_die_ref context_die;
12560 tree decl_context = DECL_CONTEXT (decl);
12561 if (decl_context)
12563 /* Find die that represents this context. */
12564 if (TYPE_P (decl_context))
12565 context_die = force_type_die (decl_context);
12566 else
12567 context_die = force_decl_die (decl_context);
12569 else
12570 context_die = comp_unit_die;
12572 switch (TREE_CODE (decl))
12574 case FUNCTION_DECL:
12575 /* Clear current_function_decl, so that gen_subprogram_die thinks
12576 that this is a declaration. At this point, we just want to force
12577 declaration die. */
12578 save_fn = current_function_decl;
12579 current_function_decl = NULL_TREE;
12580 gen_subprogram_die (decl, context_die);
12581 current_function_decl = save_fn;
12582 break;
12584 case VAR_DECL:
12585 /* Set external flag to force declaration die. Restore it after
12586 gen_decl_die() call. */
12587 saved_external_flag = DECL_EXTERNAL (decl);
12588 DECL_EXTERNAL (decl) = 1;
12589 gen_decl_die (decl, context_die);
12590 DECL_EXTERNAL (decl) = saved_external_flag;
12591 break;
12593 case NAMESPACE_DECL:
12594 dwarf2out_decl (decl);
12595 break;
12597 default:
12598 gcc_unreachable ();
12601 /* We should be able to find the DIE now. */
12602 if (!decl_die)
12603 decl_die = lookup_decl_die (decl);
12604 gcc_assert (decl_die);
12607 return decl_die;
12610 /* Returns the DIE for TYPE. A DIE is always returned. */
12612 static dw_die_ref
12613 force_type_die (tree type)
12615 dw_die_ref type_die;
12617 type_die = lookup_type_die (type);
12618 if (!type_die)
12620 dw_die_ref context_die;
12621 if (TYPE_CONTEXT (type))
12622 if (TYPE_P (TYPE_CONTEXT (type)))
12623 context_die = force_type_die (TYPE_CONTEXT (type));
12624 else
12625 context_die = force_decl_die (TYPE_CONTEXT (type));
12626 else
12627 context_die = comp_unit_die;
12629 gen_type_die (type, context_die);
12630 type_die = lookup_type_die (type);
12631 gcc_assert (type_die);
12633 return type_die;
12636 /* Force out any required namespaces to be able to output DECL,
12637 and return the new context_die for it, if it's changed. */
12639 static dw_die_ref
12640 setup_namespace_context (tree thing, dw_die_ref context_die)
12642 tree context = (DECL_P (thing)
12643 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
12644 if (context && TREE_CODE (context) == NAMESPACE_DECL)
12645 /* Force out the namespace. */
12646 context_die = force_decl_die (context);
12648 return context_die;
12651 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
12652 type) within its namespace, if appropriate.
12654 For compatibility with older debuggers, namespace DIEs only contain
12655 declarations; all definitions are emitted at CU scope. */
12657 static void
12658 declare_in_namespace (tree thing, dw_die_ref context_die)
12660 dw_die_ref ns_context;
12662 if (debug_info_level <= DINFO_LEVEL_TERSE)
12663 return;
12665 /* If this decl is from an inlined function, then don't try to emit it in its
12666 namespace, as we will get confused. It would have already been emitted
12667 when the abstract instance of the inline function was emitted anyways. */
12668 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
12669 return;
12671 ns_context = setup_namespace_context (thing, context_die);
12673 if (ns_context != context_die)
12675 if (DECL_P (thing))
12676 gen_decl_die (thing, ns_context);
12677 else
12678 gen_type_die (thing, ns_context);
12682 /* Generate a DIE for a namespace or namespace alias. */
12684 static void
12685 gen_namespace_die (tree decl)
12687 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
12689 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
12690 they are an alias of. */
12691 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
12693 /* Output a real namespace. */
12694 dw_die_ref namespace_die
12695 = new_die (DW_TAG_namespace, context_die, decl);
12696 add_name_and_src_coords_attributes (namespace_die, decl);
12697 equate_decl_number_to_die (decl, namespace_die);
12699 else
12701 /* Output a namespace alias. */
12703 /* Force out the namespace we are an alias of, if necessary. */
12704 dw_die_ref origin_die
12705 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
12707 /* Now create the namespace alias DIE. */
12708 dw_die_ref namespace_die
12709 = new_die (DW_TAG_imported_declaration, context_die, decl);
12710 add_name_and_src_coords_attributes (namespace_die, decl);
12711 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
12712 equate_decl_number_to_die (decl, namespace_die);
12716 /* Generate Dwarf debug information for a decl described by DECL. */
12718 static void
12719 gen_decl_die (tree decl, dw_die_ref context_die)
12721 tree origin;
12723 if (DECL_P (decl) && DECL_IGNORED_P (decl))
12724 return;
12726 switch (TREE_CODE (decl))
12728 case ERROR_MARK:
12729 break;
12731 case CONST_DECL:
12732 /* The individual enumerators of an enum type get output when we output
12733 the Dwarf representation of the relevant enum type itself. */
12734 break;
12736 case FUNCTION_DECL:
12737 /* Don't output any DIEs to represent mere function declarations,
12738 unless they are class members or explicit block externs. */
12739 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
12740 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
12741 break;
12743 #if 0
12744 /* FIXME */
12745 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
12746 on local redeclarations of global functions. That seems broken. */
12747 if (current_function_decl != decl)
12748 /* This is only a declaration. */;
12749 #endif
12751 /* If we're emitting a clone, emit info for the abstract instance. */
12752 if (DECL_ORIGIN (decl) != decl)
12753 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
12755 /* If we're emitting an out-of-line copy of an inline function,
12756 emit info for the abstract instance and set up to refer to it. */
12757 else if (cgraph_function_possibly_inlined_p (decl)
12758 && ! DECL_ABSTRACT (decl)
12759 && ! class_or_namespace_scope_p (context_die)
12760 /* dwarf2out_abstract_function won't emit a die if this is just
12761 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
12762 that case, because that works only if we have a die. */
12763 && DECL_INITIAL (decl) != NULL_TREE)
12765 dwarf2out_abstract_function (decl);
12766 set_decl_origin_self (decl);
12769 /* Otherwise we're emitting the primary DIE for this decl. */
12770 else if (debug_info_level > DINFO_LEVEL_TERSE)
12772 /* Before we describe the FUNCTION_DECL itself, make sure that we
12773 have described its return type. */
12774 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
12776 /* And its virtual context. */
12777 if (DECL_VINDEX (decl) != NULL_TREE)
12778 gen_type_die (DECL_CONTEXT (decl), context_die);
12780 /* And its containing type. */
12781 origin = decl_class_context (decl);
12782 if (origin != NULL_TREE)
12783 gen_type_die_for_member (origin, decl, context_die);
12785 /* And its containing namespace. */
12786 declare_in_namespace (decl, context_die);
12789 /* Now output a DIE to represent the function itself. */
12790 gen_subprogram_die (decl, context_die);
12791 break;
12793 case TYPE_DECL:
12794 /* If we are in terse mode, don't generate any DIEs to represent any
12795 actual typedefs. */
12796 if (debug_info_level <= DINFO_LEVEL_TERSE)
12797 break;
12799 /* In the special case of a TYPE_DECL node representing the declaration
12800 of some type tag, if the given TYPE_DECL is marked as having been
12801 instantiated from some other (original) TYPE_DECL node (e.g. one which
12802 was generated within the original definition of an inline function) we
12803 have to generate a special (abbreviated) DW_TAG_structure_type,
12804 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
12805 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
12807 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
12808 break;
12811 if (is_redundant_typedef (decl))
12812 gen_type_die (TREE_TYPE (decl), context_die);
12813 else
12814 /* Output a DIE to represent the typedef itself. */
12815 gen_typedef_die (decl, context_die);
12816 break;
12818 case LABEL_DECL:
12819 if (debug_info_level >= DINFO_LEVEL_NORMAL)
12820 gen_label_die (decl, context_die);
12821 break;
12823 case VAR_DECL:
12824 case RESULT_DECL:
12825 /* If we are in terse mode, don't generate any DIEs to represent any
12826 variable declarations or definitions. */
12827 if (debug_info_level <= DINFO_LEVEL_TERSE)
12828 break;
12830 /* Output any DIEs that are needed to specify the type of this data
12831 object. */
12832 gen_type_die (TREE_TYPE (decl), context_die);
12834 /* And its containing type. */
12835 origin = decl_class_context (decl);
12836 if (origin != NULL_TREE)
12837 gen_type_die_for_member (origin, decl, context_die);
12839 /* And its containing namespace. */
12840 declare_in_namespace (decl, context_die);
12842 /* Now output the DIE to represent the data object itself. This gets
12843 complicated because of the possibility that the VAR_DECL really
12844 represents an inlined instance of a formal parameter for an inline
12845 function. */
12846 origin = decl_ultimate_origin (decl);
12847 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
12848 gen_formal_parameter_die (decl, context_die);
12849 else
12850 gen_variable_die (decl, context_die);
12851 break;
12853 case FIELD_DECL:
12854 /* Ignore the nameless fields that are used to skip bits but handle C++
12855 anonymous unions and structs. */
12856 if (DECL_NAME (decl) != NULL_TREE
12857 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
12858 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
12860 gen_type_die (member_declared_type (decl), context_die);
12861 gen_field_die (decl, context_die);
12863 break;
12865 case PARM_DECL:
12866 gen_type_die (TREE_TYPE (decl), context_die);
12867 gen_formal_parameter_die (decl, context_die);
12868 break;
12870 case NAMESPACE_DECL:
12871 gen_namespace_die (decl);
12872 break;
12874 default:
12875 /* Probably some frontend-internal decl. Assume we don't care. */
12876 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
12877 break;
12881 /* Add Ada "use" clause information for SGI Workshop debugger. */
12883 void
12884 dwarf2out_add_library_unit_info (const char *filename, const char *context_list)
12886 unsigned int file_index;
12888 if (filename != NULL)
12890 dw_die_ref unit_die = new_die (DW_TAG_module, comp_unit_die, NULL);
12891 tree context_list_decl
12892 = build_decl (LABEL_DECL, get_identifier (context_list),
12893 void_type_node);
12895 TREE_PUBLIC (context_list_decl) = TRUE;
12896 add_name_attribute (unit_die, context_list);
12897 file_index = lookup_filename (filename);
12898 add_AT_unsigned (unit_die, DW_AT_decl_file, file_index);
12899 add_pubname (context_list_decl, unit_die);
12903 /* Output debug information for global decl DECL. Called from toplev.c after
12904 compilation proper has finished. */
12906 static void
12907 dwarf2out_global_decl (tree decl)
12909 /* Output DWARF2 information for file-scope tentative data object
12910 declarations, file-scope (extern) function declarations (which had no
12911 corresponding body) and file-scope tagged type declarations and
12912 definitions which have not yet been forced out. */
12913 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
12914 dwarf2out_decl (decl);
12917 /* Output debug information for type decl DECL. Called from toplev.c
12918 and from language front ends (to record built-in types). */
12919 static void
12920 dwarf2out_type_decl (tree decl, int local)
12922 if (!local)
12923 dwarf2out_decl (decl);
12926 /* Output debug information for imported module or decl. */
12928 static void
12929 dwarf2out_imported_module_or_decl (tree decl, tree context)
12931 dw_die_ref imported_die, at_import_die;
12932 dw_die_ref scope_die;
12933 unsigned file_index;
12934 expanded_location xloc;
12936 if (debug_info_level <= DINFO_LEVEL_TERSE)
12937 return;
12939 gcc_assert (decl);
12941 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
12942 We need decl DIE for reference and scope die. First, get DIE for the decl
12943 itself. */
12945 /* Get the scope die for decl context. Use comp_unit_die for global module
12946 or decl. If die is not found for non globals, force new die. */
12947 if (!context)
12948 scope_die = comp_unit_die;
12949 else if (TYPE_P (context))
12950 scope_die = force_type_die (context);
12951 else
12952 scope_die = force_decl_die (context);
12954 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
12955 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
12956 at_import_die = force_type_die (TREE_TYPE (decl));
12957 else
12959 at_import_die = lookup_decl_die (decl);
12960 if (!at_import_die)
12962 /* If we're trying to avoid duplicate debug info, we may not have
12963 emitted the member decl for this field. Emit it now. */
12964 if (TREE_CODE (decl) == FIELD_DECL)
12966 tree type = DECL_CONTEXT (decl);
12967 dw_die_ref type_context_die;
12969 if (TYPE_CONTEXT (type))
12970 if (TYPE_P (TYPE_CONTEXT (type)))
12971 type_context_die = force_type_die (TYPE_CONTEXT (type));
12972 else
12973 type_context_die = force_decl_die (TYPE_CONTEXT (type));
12974 else
12975 type_context_die = comp_unit_die;
12976 gen_type_die_for_member (type, decl, type_context_die);
12978 at_import_die = force_decl_die (decl);
12982 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
12983 if (TREE_CODE (decl) == NAMESPACE_DECL)
12984 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
12985 else
12986 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
12988 xloc = expand_location (input_location);
12989 file_index = lookup_filename (xloc.file);
12990 add_AT_unsigned (imported_die, DW_AT_decl_file, file_index);
12991 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
12992 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
12995 /* Write the debugging output for DECL. */
12997 void
12998 dwarf2out_decl (tree decl)
13000 dw_die_ref context_die = comp_unit_die;
13002 switch (TREE_CODE (decl))
13004 case ERROR_MARK:
13005 return;
13007 case FUNCTION_DECL:
13008 /* What we would really like to do here is to filter out all mere
13009 file-scope declarations of file-scope functions which are never
13010 referenced later within this translation unit (and keep all of ones
13011 that *are* referenced later on) but we aren't clairvoyant, so we have
13012 no idea which functions will be referenced in the future (i.e. later
13013 on within the current translation unit). So here we just ignore all
13014 file-scope function declarations which are not also definitions. If
13015 and when the debugger needs to know something about these functions,
13016 it will have to hunt around and find the DWARF information associated
13017 with the definition of the function.
13019 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
13020 nodes represent definitions and which ones represent mere
13021 declarations. We have to check DECL_INITIAL instead. That's because
13022 the C front-end supports some weird semantics for "extern inline"
13023 function definitions. These can get inlined within the current
13024 translation unit (an thus, we need to generate Dwarf info for their
13025 abstract instances so that the Dwarf info for the concrete inlined
13026 instances can have something to refer to) but the compiler never
13027 generates any out-of-lines instances of such things (despite the fact
13028 that they *are* definitions).
13030 The important point is that the C front-end marks these "extern
13031 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
13032 them anyway. Note that the C++ front-end also plays some similar games
13033 for inline function definitions appearing within include files which
13034 also contain `#pragma interface' pragmas. */
13035 if (DECL_INITIAL (decl) == NULL_TREE)
13036 return;
13038 /* If we're a nested function, initially use a parent of NULL; if we're
13039 a plain function, this will be fixed up in decls_for_scope. If
13040 we're a method, it will be ignored, since we already have a DIE. */
13041 if (decl_function_context (decl)
13042 /* But if we're in terse mode, we don't care about scope. */
13043 && debug_info_level > DINFO_LEVEL_TERSE)
13044 context_die = NULL;
13045 break;
13047 case VAR_DECL:
13048 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
13049 declaration and if the declaration was never even referenced from
13050 within this entire compilation unit. We suppress these DIEs in
13051 order to save space in the .debug section (by eliminating entries
13052 which are probably useless). Note that we must not suppress
13053 block-local extern declarations (whether used or not) because that
13054 would screw-up the debugger's name lookup mechanism and cause it to
13055 miss things which really ought to be in scope at a given point. */
13056 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
13057 return;
13059 /* If we are in terse mode, don't generate any DIEs to represent any
13060 variable declarations or definitions. */
13061 if (debug_info_level <= DINFO_LEVEL_TERSE)
13062 return;
13063 break;
13065 case NAMESPACE_DECL:
13066 if (debug_info_level <= DINFO_LEVEL_TERSE)
13067 return;
13068 if (lookup_decl_die (decl) != NULL)
13069 return;
13070 break;
13072 case TYPE_DECL:
13073 /* Don't emit stubs for types unless they are needed by other DIEs. */
13074 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
13075 return;
13077 /* Don't bother trying to generate any DIEs to represent any of the
13078 normal built-in types for the language we are compiling. */
13079 if (DECL_IS_BUILTIN (decl))
13081 /* OK, we need to generate one for `bool' so GDB knows what type
13082 comparisons have. */
13083 if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
13084 == DW_LANG_C_plus_plus)
13085 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
13086 && ! DECL_IGNORED_P (decl))
13087 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
13089 return;
13092 /* If we are in terse mode, don't generate any DIEs for types. */
13093 if (debug_info_level <= DINFO_LEVEL_TERSE)
13094 return;
13096 /* If we're a function-scope tag, initially use a parent of NULL;
13097 this will be fixed up in decls_for_scope. */
13098 if (decl_function_context (decl))
13099 context_die = NULL;
13101 break;
13103 default:
13104 return;
13107 gen_decl_die (decl, context_die);
13110 /* Output a marker (i.e. a label) for the beginning of the generated code for
13111 a lexical block. */
13113 static void
13114 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13115 unsigned int blocknum)
13117 current_function_section (current_function_decl);
13118 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
13121 /* Output a marker (i.e. a label) for the end of the generated code for a
13122 lexical block. */
13124 static void
13125 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
13127 current_function_section (current_function_decl);
13128 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
13131 /* Returns nonzero if it is appropriate not to emit any debugging
13132 information for BLOCK, because it doesn't contain any instructions.
13134 Don't allow this for blocks with nested functions or local classes
13135 as we would end up with orphans, and in the presence of scheduling
13136 we may end up calling them anyway. */
13138 static bool
13139 dwarf2out_ignore_block (tree block)
13141 tree decl;
13143 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
13144 if (TREE_CODE (decl) == FUNCTION_DECL
13145 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
13146 return 0;
13148 return 1;
13151 /* Lookup FILE_NAME (in the list of filenames that we know about here in
13152 dwarf2out.c) and return its "index". The index of each (known) filename is
13153 just a unique number which is associated with only that one filename. We
13154 need such numbers for the sake of generating labels (in the .debug_sfnames
13155 section) and references to those files numbers (in the .debug_srcinfo
13156 and.debug_macinfo sections). If the filename given as an argument is not
13157 found in our current list, add it to the list and assign it the next
13158 available unique index number. In order to speed up searches, we remember
13159 the index of the filename was looked up last. This handles the majority of
13160 all searches. */
13162 static unsigned
13163 lookup_filename (const char *file_name)
13165 size_t i, n;
13166 char *save_file_name;
13168 /* Check to see if the file name that was searched on the previous
13169 call matches this file name. If so, return the index. */
13170 if (file_table_last_lookup_index != 0)
13172 const char *last
13173 = VARRAY_CHAR_PTR (file_table, file_table_last_lookup_index);
13174 if (strcmp (file_name, last) == 0)
13175 return file_table_last_lookup_index;
13178 /* Didn't match the previous lookup, search the table. */
13179 n = VARRAY_ACTIVE_SIZE (file_table);
13180 for (i = 1; i < n; i++)
13181 if (strcmp (file_name, VARRAY_CHAR_PTR (file_table, i)) == 0)
13183 file_table_last_lookup_index = i;
13184 return i;
13187 /* Add the new entry to the end of the filename table. */
13188 file_table_last_lookup_index = n;
13189 save_file_name = (char *) ggc_strdup (file_name);
13190 VARRAY_PUSH_CHAR_PTR (file_table, save_file_name);
13191 VARRAY_PUSH_UINT (file_table_emitted, 0);
13193 /* If the assembler is emitting the file table, and we aren't eliminating
13194 unused debug types, then we must emit .file here. If we are eliminating
13195 unused debug types, then this will be done by the maybe_emit_file call in
13196 prune_unused_types_walk_attribs. */
13198 if (DWARF2_ASM_LINE_DEBUG_INFO && ! flag_eliminate_unused_debug_types)
13199 maybe_emit_file (i);
13201 return i;
13204 static int
13205 maybe_emit_file (int fileno)
13207 if (DWARF2_ASM_LINE_DEBUG_INFO && fileno > 0)
13209 if (!VARRAY_UINT (file_table_emitted, fileno))
13211 VARRAY_UINT (file_table_emitted, fileno) = ++emitcount;
13212 fprintf (asm_out_file, "\t.file %u ",
13213 VARRAY_UINT (file_table_emitted, fileno));
13214 output_quoted_string (asm_out_file,
13215 VARRAY_CHAR_PTR (file_table, fileno));
13216 fputc ('\n', asm_out_file);
13218 return VARRAY_UINT (file_table_emitted, fileno);
13220 else
13221 return fileno;
13224 static void
13225 init_file_table (void)
13227 /* Allocate the initial hunk of the file_table. */
13228 VARRAY_CHAR_PTR_INIT (file_table, 64, "file_table");
13229 VARRAY_UINT_INIT (file_table_emitted, 64, "file_table_emitted");
13231 /* Skip the first entry - file numbers begin at 1. */
13232 VARRAY_PUSH_CHAR_PTR (file_table, NULL);
13233 VARRAY_PUSH_UINT (file_table_emitted, 0);
13234 file_table_last_lookup_index = 0;
13237 /* Called by the final INSN scan whenever we see a var location. We
13238 use it to drop labels in the right places, and throw the location in
13239 our lookup table. */
13241 static void
13242 dwarf2out_var_location (rtx loc_note)
13244 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13245 struct var_loc_node *newloc;
13246 rtx prev_insn;
13247 static rtx last_insn;
13248 static const char *last_label;
13249 tree decl;
13251 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13252 return;
13253 prev_insn = PREV_INSN (loc_note);
13255 newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13256 /* If the insn we processed last time is the previous insn
13257 and it is also a var location note, use the label we emitted
13258 last time. */
13259 if (last_insn != NULL_RTX
13260 && last_insn == prev_insn
13261 && NOTE_P (prev_insn)
13262 && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13264 newloc->label = last_label;
13266 else
13268 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13269 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13270 loclabel_num++;
13271 newloc->label = ggc_strdup (loclabel);
13273 newloc->var_loc_note = loc_note;
13274 newloc->next = NULL;
13276 if (cfun
13277 && (last_text_section == in_unlikely_executed_text
13278 || (last_text_section == in_named
13279 && last_text_section_name == cfun->unlikely_text_section_name)))
13280 newloc->section_label = cfun->cold_section_label;
13281 else
13282 newloc->section_label = text_section_label;
13284 last_insn = loc_note;
13285 last_label = newloc->label;
13286 decl = NOTE_VAR_LOCATION_DECL (loc_note);
13287 if (DECL_DEBUG_EXPR_IS_FROM (decl) && DECL_DEBUG_EXPR (decl)
13288 && DECL_P (DECL_DEBUG_EXPR (decl)))
13289 decl = DECL_DEBUG_EXPR (decl);
13290 add_var_loc_to_decl (decl, newloc);
13293 /* We need to reset the locations at the beginning of each
13294 function. We can't do this in the end_function hook, because the
13295 declarations that use the locations won't have been outputted when
13296 that hook is called. */
13298 static void
13299 dwarf2out_begin_function (tree unused ATTRIBUTE_UNUSED)
13301 htab_empty (decl_loc_table);
13304 /* Output a label to mark the beginning of a source code line entry
13305 and record information relating to this source line, in
13306 'line_info_table' for later output of the .debug_line section. */
13308 static void
13309 dwarf2out_source_line (unsigned int line, const char *filename)
13311 if (debug_info_level >= DINFO_LEVEL_NORMAL
13312 && line != 0)
13314 current_function_section (current_function_decl);
13316 /* If requested, emit something human-readable. */
13317 if (flag_debug_asm)
13318 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13319 filename, line);
13321 if (DWARF2_ASM_LINE_DEBUG_INFO)
13323 unsigned file_num = lookup_filename (filename);
13325 file_num = maybe_emit_file (file_num);
13327 /* Emit the .loc directive understood by GNU as. */
13328 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
13330 /* Indicate that line number info exists. */
13331 line_info_table_in_use++;
13333 /* Indicate that multiple line number tables exist. */
13334 if (DECL_SECTION_NAME (current_function_decl))
13335 separate_line_info_table_in_use++;
13337 else if (DECL_SECTION_NAME (current_function_decl))
13339 dw_separate_line_info_ref line_info;
13340 targetm.asm_out.internal_label (asm_out_file, SEPARATE_LINE_CODE_LABEL,
13341 separate_line_info_table_in_use);
13343 /* Expand the line info table if necessary. */
13344 if (separate_line_info_table_in_use
13345 == separate_line_info_table_allocated)
13347 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13348 separate_line_info_table
13349 = ggc_realloc (separate_line_info_table,
13350 separate_line_info_table_allocated
13351 * sizeof (dw_separate_line_info_entry));
13352 memset (separate_line_info_table
13353 + separate_line_info_table_in_use,
13355 (LINE_INFO_TABLE_INCREMENT
13356 * sizeof (dw_separate_line_info_entry)));
13359 /* Add the new entry at the end of the line_info_table. */
13360 line_info
13361 = &separate_line_info_table[separate_line_info_table_in_use++];
13362 line_info->dw_file_num = lookup_filename (filename);
13363 line_info->dw_line_num = line;
13364 line_info->function = current_function_funcdef_no;
13366 else
13368 dw_line_info_ref line_info;
13370 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
13371 line_info_table_in_use);
13373 /* Expand the line info table if necessary. */
13374 if (line_info_table_in_use == line_info_table_allocated)
13376 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13377 line_info_table
13378 = ggc_realloc (line_info_table,
13379 (line_info_table_allocated
13380 * sizeof (dw_line_info_entry)));
13381 memset (line_info_table + line_info_table_in_use, 0,
13382 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
13385 /* Add the new entry at the end of the line_info_table. */
13386 line_info = &line_info_table[line_info_table_in_use++];
13387 line_info->dw_file_num = lookup_filename (filename);
13388 line_info->dw_line_num = line;
13393 /* Record the beginning of a new source file. */
13395 static void
13396 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
13398 if (flag_eliminate_dwarf2_dups)
13400 /* Record the beginning of the file for break_out_includes. */
13401 dw_die_ref bincl_die;
13403 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
13404 add_AT_string (bincl_die, DW_AT_name, filename);
13407 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13409 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13410 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
13411 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13412 lineno);
13413 maybe_emit_file (lookup_filename (filename));
13414 dw2_asm_output_data_uleb128 (lookup_filename (filename),
13415 "Filename we just started");
13419 /* Record the end of a source file. */
13421 static void
13422 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
13424 if (flag_eliminate_dwarf2_dups)
13425 /* Record the end of the file for break_out_includes. */
13426 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
13428 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13430 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13431 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13435 /* Called from debug_define in toplev.c. The `buffer' parameter contains
13436 the tail part of the directive line, i.e. the part which is past the
13437 initial whitespace, #, whitespace, directive-name, whitespace part. */
13439 static void
13440 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13441 const char *buffer ATTRIBUTE_UNUSED)
13443 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13445 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13446 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
13447 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13448 dw2_asm_output_nstring (buffer, -1, "The macro");
13452 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
13453 the tail part of the directive line, i.e. the part which is past the
13454 initial whitespace, #, whitespace, directive-name, whitespace part. */
13456 static void
13457 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13458 const char *buffer ATTRIBUTE_UNUSED)
13460 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13462 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13463 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
13464 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13465 dw2_asm_output_nstring (buffer, -1, "The macro");
13469 /* Set up for Dwarf output at the start of compilation. */
13471 static void
13472 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
13474 init_file_table ();
13476 /* Allocate the decl_die_table. */
13477 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13478 decl_die_table_eq, NULL);
13480 /* Allocate the decl_loc_table. */
13481 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13482 decl_loc_table_eq, NULL);
13484 /* Allocate the initial hunk of the decl_scope_table. */
13485 decl_scope_table = VEC_alloc (tree, gc, 256);
13487 /* Allocate the initial hunk of the abbrev_die_table. */
13488 abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
13489 * sizeof (dw_die_ref));
13490 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
13491 /* Zero-th entry is allocated, but unused. */
13492 abbrev_die_table_in_use = 1;
13494 /* Allocate the initial hunk of the line_info_table. */
13495 line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
13496 * sizeof (dw_line_info_entry));
13497 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
13499 /* Zero-th entry is allocated, but unused. */
13500 line_info_table_in_use = 1;
13502 /* Generate the initial DIE for the .debug section. Note that the (string)
13503 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
13504 will (typically) be a relative pathname and that this pathname should be
13505 taken as being relative to the directory from which the compiler was
13506 invoked when the given (base) source file was compiled. We will fill
13507 in this value in dwarf2out_finish. */
13508 comp_unit_die = gen_compile_unit_die (NULL);
13510 incomplete_types = VEC_alloc (tree, gc, 64);
13512 used_rtx_array = VEC_alloc (rtx, gc, 32);
13514 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
13515 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
13516 DEBUG_ABBREV_SECTION_LABEL, 0);
13517 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
13518 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
13519 COLD_TEXT_SECTION_LABEL, 0);
13520 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
13522 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
13523 DEBUG_INFO_SECTION_LABEL, 0);
13524 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
13525 DEBUG_LINE_SECTION_LABEL, 0);
13526 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
13527 DEBUG_RANGES_SECTION_LABEL, 0);
13528 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13529 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
13530 named_section_flags (DEBUG_INFO_SECTION, SECTION_DEBUG);
13531 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
13532 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13533 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
13535 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13537 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13538 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
13539 DEBUG_MACINFO_SECTION_LABEL, 0);
13540 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
13543 text_section ();
13544 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
13545 if (flag_reorder_blocks_and_partition)
13547 unlikely_text_section ();
13548 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
13552 /* A helper function for dwarf2out_finish called through
13553 ht_forall. Emit one queued .debug_str string. */
13555 static int
13556 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
13558 struct indirect_string_node *node = (struct indirect_string_node *) *h;
13560 if (node->form == DW_FORM_strp)
13562 named_section_flags (DEBUG_STR_SECTION, DEBUG_STR_SECTION_FLAGS);
13563 ASM_OUTPUT_LABEL (asm_out_file, node->label);
13564 assemble_string (node->str, strlen (node->str) + 1);
13567 return 1;
13572 /* Clear the marks for a die and its children.
13573 Be cool if the mark isn't set. */
13575 static void
13576 prune_unmark_dies (dw_die_ref die)
13578 dw_die_ref c;
13579 die->die_mark = 0;
13580 for (c = die->die_child; c; c = c->die_sib)
13581 prune_unmark_dies (c);
13585 /* Given DIE that we're marking as used, find any other dies
13586 it references as attributes and mark them as used. */
13588 static void
13589 prune_unused_types_walk_attribs (dw_die_ref die)
13591 dw_attr_ref a;
13593 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
13595 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
13597 /* A reference to another DIE.
13598 Make sure that it will get emitted. */
13599 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
13601 else if (a->dw_attr == DW_AT_decl_file)
13603 /* A reference to a file. Make sure the file name is emitted. */
13604 a->dw_attr_val.v.val_unsigned =
13605 maybe_emit_file (a->dw_attr_val.v.val_unsigned);
13611 /* Mark DIE as being used. If DOKIDS is true, then walk down
13612 to DIE's children. */
13614 static void
13615 prune_unused_types_mark (dw_die_ref die, int dokids)
13617 dw_die_ref c;
13619 if (die->die_mark == 0)
13621 /* We haven't done this node yet. Mark it as used. */
13622 die->die_mark = 1;
13624 /* We also have to mark its parents as used.
13625 (But we don't want to mark our parents' kids due to this.) */
13626 if (die->die_parent)
13627 prune_unused_types_mark (die->die_parent, 0);
13629 /* Mark any referenced nodes. */
13630 prune_unused_types_walk_attribs (die);
13632 /* If this node is a specification,
13633 also mark the definition, if it exists. */
13634 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
13635 prune_unused_types_mark (die->die_definition, 1);
13638 if (dokids && die->die_mark != 2)
13640 /* We need to walk the children, but haven't done so yet.
13641 Remember that we've walked the kids. */
13642 die->die_mark = 2;
13644 /* Walk them. */
13645 for (c = die->die_child; c; c = c->die_sib)
13647 /* If this is an array type, we need to make sure our
13648 kids get marked, even if they're types. */
13649 if (die->die_tag == DW_TAG_array_type)
13650 prune_unused_types_mark (c, 1);
13651 else
13652 prune_unused_types_walk (c);
13658 /* Walk the tree DIE and mark types that we actually use. */
13660 static void
13661 prune_unused_types_walk (dw_die_ref die)
13663 dw_die_ref c;
13665 /* Don't do anything if this node is already marked. */
13666 if (die->die_mark)
13667 return;
13669 switch (die->die_tag) {
13670 case DW_TAG_const_type:
13671 case DW_TAG_packed_type:
13672 case DW_TAG_pointer_type:
13673 case DW_TAG_reference_type:
13674 case DW_TAG_volatile_type:
13675 case DW_TAG_typedef:
13676 case DW_TAG_array_type:
13677 case DW_TAG_structure_type:
13678 case DW_TAG_union_type:
13679 case DW_TAG_class_type:
13680 case DW_TAG_friend:
13681 case DW_TAG_variant_part:
13682 case DW_TAG_enumeration_type:
13683 case DW_TAG_subroutine_type:
13684 case DW_TAG_string_type:
13685 case DW_TAG_set_type:
13686 case DW_TAG_subrange_type:
13687 case DW_TAG_ptr_to_member_type:
13688 case DW_TAG_file_type:
13689 /* It's a type node --- don't mark it. */
13690 return;
13692 default:
13693 /* Mark everything else. */
13694 break;
13697 die->die_mark = 1;
13699 /* Now, mark any dies referenced from here. */
13700 prune_unused_types_walk_attribs (die);
13702 /* Mark children. */
13703 for (c = die->die_child; c; c = c->die_sib)
13704 prune_unused_types_walk (c);
13708 /* Remove from the tree DIE any dies that aren't marked. */
13710 static void
13711 prune_unused_types_prune (dw_die_ref die)
13713 dw_die_ref c, p, n;
13715 gcc_assert (die->die_mark);
13717 p = NULL;
13718 for (c = die->die_child; c; c = n)
13720 n = c->die_sib;
13721 if (c->die_mark)
13723 prune_unused_types_prune (c);
13724 p = c;
13726 else
13728 if (p)
13729 p->die_sib = n;
13730 else
13731 die->die_child = n;
13732 free_die (c);
13738 /* Remove dies representing declarations that we never use. */
13740 static void
13741 prune_unused_types (void)
13743 unsigned int i;
13744 limbo_die_node *node;
13746 /* Clear all the marks. */
13747 prune_unmark_dies (comp_unit_die);
13748 for (node = limbo_die_list; node; node = node->next)
13749 prune_unmark_dies (node->die);
13751 /* Set the mark on nodes that are actually used. */
13752 prune_unused_types_walk (comp_unit_die);
13753 for (node = limbo_die_list; node; node = node->next)
13754 prune_unused_types_walk (node->die);
13756 /* Also set the mark on nodes referenced from the
13757 pubname_table or arange_table. */
13758 for (i = 0; i < pubname_table_in_use; i++)
13759 prune_unused_types_mark (pubname_table[i].die, 1);
13760 for (i = 0; i < arange_table_in_use; i++)
13761 prune_unused_types_mark (arange_table[i], 1);
13763 /* Get rid of nodes that aren't marked. */
13764 prune_unused_types_prune (comp_unit_die);
13765 for (node = limbo_die_list; node; node = node->next)
13766 prune_unused_types_prune (node->die);
13768 /* Leave the marks clear. */
13769 prune_unmark_dies (comp_unit_die);
13770 for (node = limbo_die_list; node; node = node->next)
13771 prune_unmark_dies (node->die);
13774 /* Output stuff that dwarf requires at the end of every file,
13775 and generate the DWARF-2 debugging info. */
13777 static void
13778 dwarf2out_finish (const char *filename)
13780 limbo_die_node *node, *next_node;
13781 dw_die_ref die = 0;
13783 /* Add the name for the main input file now. We delayed this from
13784 dwarf2out_init to avoid complications with PCH. */
13785 add_name_attribute (comp_unit_die, filename);
13786 if (filename[0] != DIR_SEPARATOR)
13787 add_comp_dir_attribute (comp_unit_die);
13788 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
13790 size_t i;
13791 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
13792 if (VARRAY_CHAR_PTR (file_table, i)[0] != DIR_SEPARATOR
13793 /* Don't add cwd for <built-in>. */
13794 && VARRAY_CHAR_PTR (file_table, i)[0] != '<')
13796 add_comp_dir_attribute (comp_unit_die);
13797 break;
13801 /* Traverse the limbo die list, and add parent/child links. The only
13802 dies without parents that should be here are concrete instances of
13803 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
13804 For concrete instances, we can get the parent die from the abstract
13805 instance. */
13806 for (node = limbo_die_list; node; node = next_node)
13808 next_node = node->next;
13809 die = node->die;
13811 if (die->die_parent == NULL)
13813 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
13815 if (origin)
13816 add_child_die (origin->die_parent, die);
13817 else if (die == comp_unit_die)
13819 else if (errorcount > 0 || sorrycount > 0)
13820 /* It's OK to be confused by errors in the input. */
13821 add_child_die (comp_unit_die, die);
13822 else
13824 /* In certain situations, the lexical block containing a
13825 nested function can be optimized away, which results
13826 in the nested function die being orphaned. Likewise
13827 with the return type of that nested function. Force
13828 this to be a child of the containing function.
13830 It may happen that even the containing function got fully
13831 inlined and optimized out. In that case we are lost and
13832 assign the empty child. This should not be big issue as
13833 the function is likely unreachable too. */
13834 tree context = NULL_TREE;
13836 gcc_assert (node->created_for);
13838 if (DECL_P (node->created_for))
13839 context = DECL_CONTEXT (node->created_for);
13840 else if (TYPE_P (node->created_for))
13841 context = TYPE_CONTEXT (node->created_for);
13843 gcc_assert (context && TREE_CODE (context) == FUNCTION_DECL);
13845 origin = lookup_decl_die (context);
13846 if (origin)
13847 add_child_die (origin, die);
13848 else
13849 add_child_die (comp_unit_die, die);
13854 limbo_die_list = NULL;
13856 /* Walk through the list of incomplete types again, trying once more to
13857 emit full debugging info for them. */
13858 retry_incomplete_types ();
13860 /* We need to reverse all the dies before break_out_includes, or
13861 we'll see the end of an include file before the beginning. */
13862 reverse_all_dies (comp_unit_die);
13864 if (flag_eliminate_unused_debug_types)
13865 prune_unused_types ();
13867 /* Generate separate CUs for each of the include files we've seen.
13868 They will go into limbo_die_list. */
13869 if (flag_eliminate_dwarf2_dups)
13870 break_out_includes (comp_unit_die);
13872 /* Traverse the DIE's and add add sibling attributes to those DIE's
13873 that have children. */
13874 add_sibling_attributes (comp_unit_die);
13875 for (node = limbo_die_list; node; node = node->next)
13876 add_sibling_attributes (node->die);
13878 /* Output a terminator label for the .text section. */
13879 text_section ();
13880 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
13881 if (flag_reorder_blocks_and_partition)
13883 unlikely_text_section ();
13884 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
13887 /* Output the source line correspondence table. We must do this
13888 even if there is no line information. Otherwise, on an empty
13889 translation unit, we will generate a present, but empty,
13890 .debug_info section. IRIX 6.5 `nm' will then complain when
13891 examining the file. */
13892 if (! DWARF2_ASM_LINE_DEBUG_INFO)
13894 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13895 output_line_info ();
13898 /* Output location list section if necessary. */
13899 if (have_location_lists)
13901 /* Output the location lists info. */
13902 named_section_flags (DEBUG_LOC_SECTION, SECTION_DEBUG);
13903 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
13904 DEBUG_LOC_SECTION_LABEL, 0);
13905 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
13906 output_location_lists (die);
13907 have_location_lists = 0;
13910 /* We can only use the low/high_pc attributes if all of the code was
13911 in .text. */
13912 if (separate_line_info_table_in_use == 0)
13914 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
13915 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
13918 /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
13919 "base address". Use zero so that these addresses become absolute. */
13920 else if (have_location_lists || ranges_table_in_use)
13921 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
13923 if (debug_info_level >= DINFO_LEVEL_NORMAL)
13924 add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
13925 debug_line_section_label);
13927 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13928 add_AT_lbl_offset (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
13930 /* Output all of the compilation units. We put the main one last so that
13931 the offsets are available to output_pubnames. */
13932 for (node = limbo_die_list; node; node = node->next)
13933 output_comp_unit (node->die, 0);
13935 output_comp_unit (comp_unit_die, 0);
13937 /* Output the abbreviation table. */
13938 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13939 output_abbrev_section ();
13941 /* Output public names table if necessary. */
13942 if (pubname_table_in_use)
13944 named_section_flags (DEBUG_PUBNAMES_SECTION, SECTION_DEBUG);
13945 output_pubnames ();
13948 /* Output the address range information. We only put functions in the arange
13949 table, so don't write it out if we don't have any. */
13950 if (fde_table_in_use)
13952 named_section_flags (DEBUG_ARANGES_SECTION, SECTION_DEBUG);
13953 output_aranges ();
13956 /* Output ranges section if necessary. */
13957 if (ranges_table_in_use)
13959 named_section_flags (DEBUG_RANGES_SECTION, SECTION_DEBUG);
13960 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
13961 output_ranges ();
13964 /* Have to end the macro section. */
13965 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13967 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13968 dw2_asm_output_data (1, 0, "End compilation unit");
13971 /* If we emitted any DW_FORM_strp form attribute, output the string
13972 table too. */
13973 if (debug_str_hash)
13974 htab_traverse (debug_str_hash, output_indirect_string, NULL);
13976 #else
13978 /* This should never be used, but its address is needed for comparisons. */
13979 const struct gcc_debug_hooks dwarf2_debug_hooks;
13981 #endif /* DWARF2_DEBUGGING_INFO */
13983 #include "gt-dwarf2out.h"