2005-06-07 Adrian Straetling <straetling@de.ibm.com>
[official-gcc.git] / gcc / dwarf2out.c
blobe11cd806376605c64616f8bb3839ad89b7e2d5af
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 void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
2663 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2664 static unsigned long size_of_locs (dw_loc_descr_ref);
2665 static void output_loc_operands (dw_loc_descr_ref);
2666 static void output_loc_sequence (dw_loc_descr_ref);
2668 /* Convert a DWARF stack opcode into its string name. */
2670 static const char *
2671 dwarf_stack_op_name (unsigned int op)
2673 switch (op)
2675 case DW_OP_addr:
2676 case INTERNAL_DW_OP_tls_addr:
2677 return "DW_OP_addr";
2678 case DW_OP_deref:
2679 return "DW_OP_deref";
2680 case DW_OP_const1u:
2681 return "DW_OP_const1u";
2682 case DW_OP_const1s:
2683 return "DW_OP_const1s";
2684 case DW_OP_const2u:
2685 return "DW_OP_const2u";
2686 case DW_OP_const2s:
2687 return "DW_OP_const2s";
2688 case DW_OP_const4u:
2689 return "DW_OP_const4u";
2690 case DW_OP_const4s:
2691 return "DW_OP_const4s";
2692 case DW_OP_const8u:
2693 return "DW_OP_const8u";
2694 case DW_OP_const8s:
2695 return "DW_OP_const8s";
2696 case DW_OP_constu:
2697 return "DW_OP_constu";
2698 case DW_OP_consts:
2699 return "DW_OP_consts";
2700 case DW_OP_dup:
2701 return "DW_OP_dup";
2702 case DW_OP_drop:
2703 return "DW_OP_drop";
2704 case DW_OP_over:
2705 return "DW_OP_over";
2706 case DW_OP_pick:
2707 return "DW_OP_pick";
2708 case DW_OP_swap:
2709 return "DW_OP_swap";
2710 case DW_OP_rot:
2711 return "DW_OP_rot";
2712 case DW_OP_xderef:
2713 return "DW_OP_xderef";
2714 case DW_OP_abs:
2715 return "DW_OP_abs";
2716 case DW_OP_and:
2717 return "DW_OP_and";
2718 case DW_OP_div:
2719 return "DW_OP_div";
2720 case DW_OP_minus:
2721 return "DW_OP_minus";
2722 case DW_OP_mod:
2723 return "DW_OP_mod";
2724 case DW_OP_mul:
2725 return "DW_OP_mul";
2726 case DW_OP_neg:
2727 return "DW_OP_neg";
2728 case DW_OP_not:
2729 return "DW_OP_not";
2730 case DW_OP_or:
2731 return "DW_OP_or";
2732 case DW_OP_plus:
2733 return "DW_OP_plus";
2734 case DW_OP_plus_uconst:
2735 return "DW_OP_plus_uconst";
2736 case DW_OP_shl:
2737 return "DW_OP_shl";
2738 case DW_OP_shr:
2739 return "DW_OP_shr";
2740 case DW_OP_shra:
2741 return "DW_OP_shra";
2742 case DW_OP_xor:
2743 return "DW_OP_xor";
2744 case DW_OP_bra:
2745 return "DW_OP_bra";
2746 case DW_OP_eq:
2747 return "DW_OP_eq";
2748 case DW_OP_ge:
2749 return "DW_OP_ge";
2750 case DW_OP_gt:
2751 return "DW_OP_gt";
2752 case DW_OP_le:
2753 return "DW_OP_le";
2754 case DW_OP_lt:
2755 return "DW_OP_lt";
2756 case DW_OP_ne:
2757 return "DW_OP_ne";
2758 case DW_OP_skip:
2759 return "DW_OP_skip";
2760 case DW_OP_lit0:
2761 return "DW_OP_lit0";
2762 case DW_OP_lit1:
2763 return "DW_OP_lit1";
2764 case DW_OP_lit2:
2765 return "DW_OP_lit2";
2766 case DW_OP_lit3:
2767 return "DW_OP_lit3";
2768 case DW_OP_lit4:
2769 return "DW_OP_lit4";
2770 case DW_OP_lit5:
2771 return "DW_OP_lit5";
2772 case DW_OP_lit6:
2773 return "DW_OP_lit6";
2774 case DW_OP_lit7:
2775 return "DW_OP_lit7";
2776 case DW_OP_lit8:
2777 return "DW_OP_lit8";
2778 case DW_OP_lit9:
2779 return "DW_OP_lit9";
2780 case DW_OP_lit10:
2781 return "DW_OP_lit10";
2782 case DW_OP_lit11:
2783 return "DW_OP_lit11";
2784 case DW_OP_lit12:
2785 return "DW_OP_lit12";
2786 case DW_OP_lit13:
2787 return "DW_OP_lit13";
2788 case DW_OP_lit14:
2789 return "DW_OP_lit14";
2790 case DW_OP_lit15:
2791 return "DW_OP_lit15";
2792 case DW_OP_lit16:
2793 return "DW_OP_lit16";
2794 case DW_OP_lit17:
2795 return "DW_OP_lit17";
2796 case DW_OP_lit18:
2797 return "DW_OP_lit18";
2798 case DW_OP_lit19:
2799 return "DW_OP_lit19";
2800 case DW_OP_lit20:
2801 return "DW_OP_lit20";
2802 case DW_OP_lit21:
2803 return "DW_OP_lit21";
2804 case DW_OP_lit22:
2805 return "DW_OP_lit22";
2806 case DW_OP_lit23:
2807 return "DW_OP_lit23";
2808 case DW_OP_lit24:
2809 return "DW_OP_lit24";
2810 case DW_OP_lit25:
2811 return "DW_OP_lit25";
2812 case DW_OP_lit26:
2813 return "DW_OP_lit26";
2814 case DW_OP_lit27:
2815 return "DW_OP_lit27";
2816 case DW_OP_lit28:
2817 return "DW_OP_lit28";
2818 case DW_OP_lit29:
2819 return "DW_OP_lit29";
2820 case DW_OP_lit30:
2821 return "DW_OP_lit30";
2822 case DW_OP_lit31:
2823 return "DW_OP_lit31";
2824 case DW_OP_reg0:
2825 return "DW_OP_reg0";
2826 case DW_OP_reg1:
2827 return "DW_OP_reg1";
2828 case DW_OP_reg2:
2829 return "DW_OP_reg2";
2830 case DW_OP_reg3:
2831 return "DW_OP_reg3";
2832 case DW_OP_reg4:
2833 return "DW_OP_reg4";
2834 case DW_OP_reg5:
2835 return "DW_OP_reg5";
2836 case DW_OP_reg6:
2837 return "DW_OP_reg6";
2838 case DW_OP_reg7:
2839 return "DW_OP_reg7";
2840 case DW_OP_reg8:
2841 return "DW_OP_reg8";
2842 case DW_OP_reg9:
2843 return "DW_OP_reg9";
2844 case DW_OP_reg10:
2845 return "DW_OP_reg10";
2846 case DW_OP_reg11:
2847 return "DW_OP_reg11";
2848 case DW_OP_reg12:
2849 return "DW_OP_reg12";
2850 case DW_OP_reg13:
2851 return "DW_OP_reg13";
2852 case DW_OP_reg14:
2853 return "DW_OP_reg14";
2854 case DW_OP_reg15:
2855 return "DW_OP_reg15";
2856 case DW_OP_reg16:
2857 return "DW_OP_reg16";
2858 case DW_OP_reg17:
2859 return "DW_OP_reg17";
2860 case DW_OP_reg18:
2861 return "DW_OP_reg18";
2862 case DW_OP_reg19:
2863 return "DW_OP_reg19";
2864 case DW_OP_reg20:
2865 return "DW_OP_reg20";
2866 case DW_OP_reg21:
2867 return "DW_OP_reg21";
2868 case DW_OP_reg22:
2869 return "DW_OP_reg22";
2870 case DW_OP_reg23:
2871 return "DW_OP_reg23";
2872 case DW_OP_reg24:
2873 return "DW_OP_reg24";
2874 case DW_OP_reg25:
2875 return "DW_OP_reg25";
2876 case DW_OP_reg26:
2877 return "DW_OP_reg26";
2878 case DW_OP_reg27:
2879 return "DW_OP_reg27";
2880 case DW_OP_reg28:
2881 return "DW_OP_reg28";
2882 case DW_OP_reg29:
2883 return "DW_OP_reg29";
2884 case DW_OP_reg30:
2885 return "DW_OP_reg30";
2886 case DW_OP_reg31:
2887 return "DW_OP_reg31";
2888 case DW_OP_breg0:
2889 return "DW_OP_breg0";
2890 case DW_OP_breg1:
2891 return "DW_OP_breg1";
2892 case DW_OP_breg2:
2893 return "DW_OP_breg2";
2894 case DW_OP_breg3:
2895 return "DW_OP_breg3";
2896 case DW_OP_breg4:
2897 return "DW_OP_breg4";
2898 case DW_OP_breg5:
2899 return "DW_OP_breg5";
2900 case DW_OP_breg6:
2901 return "DW_OP_breg6";
2902 case DW_OP_breg7:
2903 return "DW_OP_breg7";
2904 case DW_OP_breg8:
2905 return "DW_OP_breg8";
2906 case DW_OP_breg9:
2907 return "DW_OP_breg9";
2908 case DW_OP_breg10:
2909 return "DW_OP_breg10";
2910 case DW_OP_breg11:
2911 return "DW_OP_breg11";
2912 case DW_OP_breg12:
2913 return "DW_OP_breg12";
2914 case DW_OP_breg13:
2915 return "DW_OP_breg13";
2916 case DW_OP_breg14:
2917 return "DW_OP_breg14";
2918 case DW_OP_breg15:
2919 return "DW_OP_breg15";
2920 case DW_OP_breg16:
2921 return "DW_OP_breg16";
2922 case DW_OP_breg17:
2923 return "DW_OP_breg17";
2924 case DW_OP_breg18:
2925 return "DW_OP_breg18";
2926 case DW_OP_breg19:
2927 return "DW_OP_breg19";
2928 case DW_OP_breg20:
2929 return "DW_OP_breg20";
2930 case DW_OP_breg21:
2931 return "DW_OP_breg21";
2932 case DW_OP_breg22:
2933 return "DW_OP_breg22";
2934 case DW_OP_breg23:
2935 return "DW_OP_breg23";
2936 case DW_OP_breg24:
2937 return "DW_OP_breg24";
2938 case DW_OP_breg25:
2939 return "DW_OP_breg25";
2940 case DW_OP_breg26:
2941 return "DW_OP_breg26";
2942 case DW_OP_breg27:
2943 return "DW_OP_breg27";
2944 case DW_OP_breg28:
2945 return "DW_OP_breg28";
2946 case DW_OP_breg29:
2947 return "DW_OP_breg29";
2948 case DW_OP_breg30:
2949 return "DW_OP_breg30";
2950 case DW_OP_breg31:
2951 return "DW_OP_breg31";
2952 case DW_OP_regx:
2953 return "DW_OP_regx";
2954 case DW_OP_fbreg:
2955 return "DW_OP_fbreg";
2956 case DW_OP_bregx:
2957 return "DW_OP_bregx";
2958 case DW_OP_piece:
2959 return "DW_OP_piece";
2960 case DW_OP_deref_size:
2961 return "DW_OP_deref_size";
2962 case DW_OP_xderef_size:
2963 return "DW_OP_xderef_size";
2964 case DW_OP_nop:
2965 return "DW_OP_nop";
2966 case DW_OP_push_object_address:
2967 return "DW_OP_push_object_address";
2968 case DW_OP_call2:
2969 return "DW_OP_call2";
2970 case DW_OP_call4:
2971 return "DW_OP_call4";
2972 case DW_OP_call_ref:
2973 return "DW_OP_call_ref";
2974 case DW_OP_GNU_push_tls_address:
2975 return "DW_OP_GNU_push_tls_address";
2976 default:
2977 return "OP_<unknown>";
2981 /* Return a pointer to a newly allocated location description. Location
2982 descriptions are simple expression terms that can be strung
2983 together to form more complicated location (address) descriptions. */
2985 static inline dw_loc_descr_ref
2986 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
2987 unsigned HOST_WIDE_INT oprnd2)
2989 dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
2991 descr->dw_loc_opc = op;
2992 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
2993 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
2994 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
2995 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
2997 return descr;
3001 /* Add a location description term to a location description expression. */
3003 static inline void
3004 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3006 dw_loc_descr_ref *d;
3008 /* Find the end of the chain. */
3009 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3012 *d = descr;
3016 /* Optionally add a DW_OP_piece term to a location description expression.
3017 DW_OP_piece is only added if the location description expression already
3018 doesn't end with DW_OP_piece. */
3020 static void
3021 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
3023 dw_loc_descr_ref loc;
3025 if (*list_head != NULL)
3027 /* Find the end of the chain. */
3028 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
3031 if (loc->dw_loc_opc != DW_OP_piece)
3032 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
3036 /* Return the size of a location descriptor. */
3038 static unsigned long
3039 size_of_loc_descr (dw_loc_descr_ref loc)
3041 unsigned long size = 1;
3043 switch (loc->dw_loc_opc)
3045 case DW_OP_addr:
3046 case INTERNAL_DW_OP_tls_addr:
3047 size += DWARF2_ADDR_SIZE;
3048 break;
3049 case DW_OP_const1u:
3050 case DW_OP_const1s:
3051 size += 1;
3052 break;
3053 case DW_OP_const2u:
3054 case DW_OP_const2s:
3055 size += 2;
3056 break;
3057 case DW_OP_const4u:
3058 case DW_OP_const4s:
3059 size += 4;
3060 break;
3061 case DW_OP_const8u:
3062 case DW_OP_const8s:
3063 size += 8;
3064 break;
3065 case DW_OP_constu:
3066 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3067 break;
3068 case DW_OP_consts:
3069 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3070 break;
3071 case DW_OP_pick:
3072 size += 1;
3073 break;
3074 case DW_OP_plus_uconst:
3075 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3076 break;
3077 case DW_OP_skip:
3078 case DW_OP_bra:
3079 size += 2;
3080 break;
3081 case DW_OP_breg0:
3082 case DW_OP_breg1:
3083 case DW_OP_breg2:
3084 case DW_OP_breg3:
3085 case DW_OP_breg4:
3086 case DW_OP_breg5:
3087 case DW_OP_breg6:
3088 case DW_OP_breg7:
3089 case DW_OP_breg8:
3090 case DW_OP_breg9:
3091 case DW_OP_breg10:
3092 case DW_OP_breg11:
3093 case DW_OP_breg12:
3094 case DW_OP_breg13:
3095 case DW_OP_breg14:
3096 case DW_OP_breg15:
3097 case DW_OP_breg16:
3098 case DW_OP_breg17:
3099 case DW_OP_breg18:
3100 case DW_OP_breg19:
3101 case DW_OP_breg20:
3102 case DW_OP_breg21:
3103 case DW_OP_breg22:
3104 case DW_OP_breg23:
3105 case DW_OP_breg24:
3106 case DW_OP_breg25:
3107 case DW_OP_breg26:
3108 case DW_OP_breg27:
3109 case DW_OP_breg28:
3110 case DW_OP_breg29:
3111 case DW_OP_breg30:
3112 case DW_OP_breg31:
3113 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3114 break;
3115 case DW_OP_regx:
3116 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3117 break;
3118 case DW_OP_fbreg:
3119 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3120 break;
3121 case DW_OP_bregx:
3122 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3123 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3124 break;
3125 case DW_OP_piece:
3126 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3127 break;
3128 case DW_OP_deref_size:
3129 case DW_OP_xderef_size:
3130 size += 1;
3131 break;
3132 case DW_OP_call2:
3133 size += 2;
3134 break;
3135 case DW_OP_call4:
3136 size += 4;
3137 break;
3138 case DW_OP_call_ref:
3139 size += DWARF2_ADDR_SIZE;
3140 break;
3141 default:
3142 break;
3145 return size;
3148 /* Return the size of a series of location descriptors. */
3150 static unsigned long
3151 size_of_locs (dw_loc_descr_ref loc)
3153 unsigned long size;
3155 for (size = 0; loc != NULL; loc = loc->dw_loc_next)
3157 loc->dw_loc_addr = size;
3158 size += size_of_loc_descr (loc);
3161 return size;
3164 /* Output location description stack opcode's operands (if any). */
3166 static void
3167 output_loc_operands (dw_loc_descr_ref loc)
3169 dw_val_ref val1 = &loc->dw_loc_oprnd1;
3170 dw_val_ref val2 = &loc->dw_loc_oprnd2;
3172 switch (loc->dw_loc_opc)
3174 #ifdef DWARF2_DEBUGGING_INFO
3175 case DW_OP_addr:
3176 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3177 break;
3178 case DW_OP_const2u:
3179 case DW_OP_const2s:
3180 dw2_asm_output_data (2, val1->v.val_int, NULL);
3181 break;
3182 case DW_OP_const4u:
3183 case DW_OP_const4s:
3184 dw2_asm_output_data (4, val1->v.val_int, NULL);
3185 break;
3186 case DW_OP_const8u:
3187 case DW_OP_const8s:
3188 gcc_assert (HOST_BITS_PER_LONG >= 64);
3189 dw2_asm_output_data (8, val1->v.val_int, NULL);
3190 break;
3191 case DW_OP_skip:
3192 case DW_OP_bra:
3194 int offset;
3196 gcc_assert (val1->val_class == dw_val_class_loc);
3197 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3199 dw2_asm_output_data (2, offset, NULL);
3201 break;
3202 #else
3203 case DW_OP_addr:
3204 case DW_OP_const2u:
3205 case DW_OP_const2s:
3206 case DW_OP_const4u:
3207 case DW_OP_const4s:
3208 case DW_OP_const8u:
3209 case DW_OP_const8s:
3210 case DW_OP_skip:
3211 case DW_OP_bra:
3212 /* We currently don't make any attempt to make sure these are
3213 aligned properly like we do for the main unwind info, so
3214 don't support emitting things larger than a byte if we're
3215 only doing unwinding. */
3216 gcc_unreachable ();
3217 #endif
3218 case DW_OP_const1u:
3219 case DW_OP_const1s:
3220 dw2_asm_output_data (1, val1->v.val_int, NULL);
3221 break;
3222 case DW_OP_constu:
3223 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3224 break;
3225 case DW_OP_consts:
3226 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3227 break;
3228 case DW_OP_pick:
3229 dw2_asm_output_data (1, val1->v.val_int, NULL);
3230 break;
3231 case DW_OP_plus_uconst:
3232 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3233 break;
3234 case DW_OP_breg0:
3235 case DW_OP_breg1:
3236 case DW_OP_breg2:
3237 case DW_OP_breg3:
3238 case DW_OP_breg4:
3239 case DW_OP_breg5:
3240 case DW_OP_breg6:
3241 case DW_OP_breg7:
3242 case DW_OP_breg8:
3243 case DW_OP_breg9:
3244 case DW_OP_breg10:
3245 case DW_OP_breg11:
3246 case DW_OP_breg12:
3247 case DW_OP_breg13:
3248 case DW_OP_breg14:
3249 case DW_OP_breg15:
3250 case DW_OP_breg16:
3251 case DW_OP_breg17:
3252 case DW_OP_breg18:
3253 case DW_OP_breg19:
3254 case DW_OP_breg20:
3255 case DW_OP_breg21:
3256 case DW_OP_breg22:
3257 case DW_OP_breg23:
3258 case DW_OP_breg24:
3259 case DW_OP_breg25:
3260 case DW_OP_breg26:
3261 case DW_OP_breg27:
3262 case DW_OP_breg28:
3263 case DW_OP_breg29:
3264 case DW_OP_breg30:
3265 case DW_OP_breg31:
3266 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3267 break;
3268 case DW_OP_regx:
3269 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3270 break;
3271 case DW_OP_fbreg:
3272 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3273 break;
3274 case DW_OP_bregx:
3275 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3276 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3277 break;
3278 case DW_OP_piece:
3279 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3280 break;
3281 case DW_OP_deref_size:
3282 case DW_OP_xderef_size:
3283 dw2_asm_output_data (1, val1->v.val_int, NULL);
3284 break;
3286 case INTERNAL_DW_OP_tls_addr:
3287 #ifdef ASM_OUTPUT_DWARF_DTPREL
3288 ASM_OUTPUT_DWARF_DTPREL (asm_out_file, DWARF2_ADDR_SIZE,
3289 val1->v.val_addr);
3290 fputc ('\n', asm_out_file);
3291 #else
3292 gcc_unreachable ();
3293 #endif
3294 break;
3296 default:
3297 /* Other codes have no operands. */
3298 break;
3302 /* Output a sequence of location operations. */
3304 static void
3305 output_loc_sequence (dw_loc_descr_ref loc)
3307 for (; loc != NULL; loc = loc->dw_loc_next)
3309 /* Output the opcode. */
3310 dw2_asm_output_data (1, loc->dw_loc_opc,
3311 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3313 /* Output the operand(s) (if any). */
3314 output_loc_operands (loc);
3318 /* This routine will generate the correct assembly data for a location
3319 description based on a cfi entry with a complex address. */
3321 static void
3322 output_cfa_loc (dw_cfi_ref cfi)
3324 dw_loc_descr_ref loc;
3325 unsigned long size;
3327 /* Output the size of the block. */
3328 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3329 size = size_of_locs (loc);
3330 dw2_asm_output_data_uleb128 (size, NULL);
3332 /* Now output the operations themselves. */
3333 output_loc_sequence (loc);
3336 /* This function builds a dwarf location descriptor sequence from
3337 a dw_cfa_location. */
3339 static struct dw_loc_descr_struct *
3340 build_cfa_loc (dw_cfa_location *cfa)
3342 struct dw_loc_descr_struct *head, *tmp;
3344 gcc_assert (cfa->indirect);
3346 if (cfa->base_offset)
3348 if (cfa->reg <= 31)
3349 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3350 else
3351 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3353 else if (cfa->reg <= 31)
3354 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3355 else
3356 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3358 head->dw_loc_oprnd1.val_class = dw_val_class_const;
3359 tmp = new_loc_descr (DW_OP_deref, 0, 0);
3360 add_loc_descr (&head, tmp);
3361 if (cfa->offset != 0)
3363 tmp = new_loc_descr (DW_OP_plus_uconst, cfa->offset, 0);
3364 add_loc_descr (&head, tmp);
3367 return head;
3370 /* This function fills in aa dw_cfa_location structure from a dwarf location
3371 descriptor sequence. */
3373 static void
3374 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3376 struct dw_loc_descr_struct *ptr;
3377 cfa->offset = 0;
3378 cfa->base_offset = 0;
3379 cfa->indirect = 0;
3380 cfa->reg = -1;
3382 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3384 enum dwarf_location_atom op = ptr->dw_loc_opc;
3386 switch (op)
3388 case DW_OP_reg0:
3389 case DW_OP_reg1:
3390 case DW_OP_reg2:
3391 case DW_OP_reg3:
3392 case DW_OP_reg4:
3393 case DW_OP_reg5:
3394 case DW_OP_reg6:
3395 case DW_OP_reg7:
3396 case DW_OP_reg8:
3397 case DW_OP_reg9:
3398 case DW_OP_reg10:
3399 case DW_OP_reg11:
3400 case DW_OP_reg12:
3401 case DW_OP_reg13:
3402 case DW_OP_reg14:
3403 case DW_OP_reg15:
3404 case DW_OP_reg16:
3405 case DW_OP_reg17:
3406 case DW_OP_reg18:
3407 case DW_OP_reg19:
3408 case DW_OP_reg20:
3409 case DW_OP_reg21:
3410 case DW_OP_reg22:
3411 case DW_OP_reg23:
3412 case DW_OP_reg24:
3413 case DW_OP_reg25:
3414 case DW_OP_reg26:
3415 case DW_OP_reg27:
3416 case DW_OP_reg28:
3417 case DW_OP_reg29:
3418 case DW_OP_reg30:
3419 case DW_OP_reg31:
3420 cfa->reg = op - DW_OP_reg0;
3421 break;
3422 case DW_OP_regx:
3423 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3424 break;
3425 case DW_OP_breg0:
3426 case DW_OP_breg1:
3427 case DW_OP_breg2:
3428 case DW_OP_breg3:
3429 case DW_OP_breg4:
3430 case DW_OP_breg5:
3431 case DW_OP_breg6:
3432 case DW_OP_breg7:
3433 case DW_OP_breg8:
3434 case DW_OP_breg9:
3435 case DW_OP_breg10:
3436 case DW_OP_breg11:
3437 case DW_OP_breg12:
3438 case DW_OP_breg13:
3439 case DW_OP_breg14:
3440 case DW_OP_breg15:
3441 case DW_OP_breg16:
3442 case DW_OP_breg17:
3443 case DW_OP_breg18:
3444 case DW_OP_breg19:
3445 case DW_OP_breg20:
3446 case DW_OP_breg21:
3447 case DW_OP_breg22:
3448 case DW_OP_breg23:
3449 case DW_OP_breg24:
3450 case DW_OP_breg25:
3451 case DW_OP_breg26:
3452 case DW_OP_breg27:
3453 case DW_OP_breg28:
3454 case DW_OP_breg29:
3455 case DW_OP_breg30:
3456 case DW_OP_breg31:
3457 cfa->reg = op - DW_OP_breg0;
3458 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3459 break;
3460 case DW_OP_bregx:
3461 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3462 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3463 break;
3464 case DW_OP_deref:
3465 cfa->indirect = 1;
3466 break;
3467 case DW_OP_plus_uconst:
3468 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3469 break;
3470 default:
3471 internal_error ("DW_LOC_OP %s not implemented\n",
3472 dwarf_stack_op_name (ptr->dw_loc_opc));
3476 #endif /* .debug_frame support */
3478 /* And now, the support for symbolic debugging information. */
3479 #ifdef DWARF2_DEBUGGING_INFO
3481 /* .debug_str support. */
3482 static int output_indirect_string (void **, void *);
3484 static void dwarf2out_init (const char *);
3485 static void dwarf2out_finish (const char *);
3486 static void dwarf2out_define (unsigned int, const char *);
3487 static void dwarf2out_undef (unsigned int, const char *);
3488 static void dwarf2out_start_source_file (unsigned, const char *);
3489 static void dwarf2out_end_source_file (unsigned);
3490 static void dwarf2out_begin_block (unsigned, unsigned);
3491 static void dwarf2out_end_block (unsigned, unsigned);
3492 static bool dwarf2out_ignore_block (tree);
3493 static void dwarf2out_global_decl (tree);
3494 static void dwarf2out_type_decl (tree, int);
3495 static void dwarf2out_imported_module_or_decl (tree, tree);
3496 static void dwarf2out_abstract_function (tree);
3497 static void dwarf2out_var_location (rtx);
3498 static void dwarf2out_begin_function (tree);
3499 static void dwarf2out_switch_text_section (void);
3501 /* The debug hooks structure. */
3503 const struct gcc_debug_hooks dwarf2_debug_hooks =
3505 dwarf2out_init,
3506 dwarf2out_finish,
3507 dwarf2out_define,
3508 dwarf2out_undef,
3509 dwarf2out_start_source_file,
3510 dwarf2out_end_source_file,
3511 dwarf2out_begin_block,
3512 dwarf2out_end_block,
3513 dwarf2out_ignore_block,
3514 dwarf2out_source_line,
3515 dwarf2out_begin_prologue,
3516 debug_nothing_int_charstar, /* end_prologue */
3517 dwarf2out_end_epilogue,
3518 dwarf2out_begin_function,
3519 debug_nothing_int, /* end_function */
3520 dwarf2out_decl, /* function_decl */
3521 dwarf2out_global_decl,
3522 dwarf2out_type_decl, /* type_decl */
3523 dwarf2out_imported_module_or_decl,
3524 debug_nothing_tree, /* deferred_inline_function */
3525 /* The DWARF 2 backend tries to reduce debugging bloat by not
3526 emitting the abstract description of inline functions until
3527 something tries to reference them. */
3528 dwarf2out_abstract_function, /* outlining_inline_function */
3529 debug_nothing_rtx, /* label */
3530 debug_nothing_int, /* handle_pch */
3531 dwarf2out_var_location,
3532 dwarf2out_switch_text_section,
3533 1 /* start_end_main_source_file */
3535 #endif
3537 /* NOTE: In the comments in this file, many references are made to
3538 "Debugging Information Entries". This term is abbreviated as `DIE'
3539 throughout the remainder of this file. */
3541 /* An internal representation of the DWARF output is built, and then
3542 walked to generate the DWARF debugging info. The walk of the internal
3543 representation is done after the entire program has been compiled.
3544 The types below are used to describe the internal representation. */
3546 /* Various DIE's use offsets relative to the beginning of the
3547 .debug_info section to refer to each other. */
3549 typedef long int dw_offset;
3551 /* Define typedefs here to avoid circular dependencies. */
3553 typedef struct dw_attr_struct *dw_attr_ref;
3554 typedef struct dw_line_info_struct *dw_line_info_ref;
3555 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3556 typedef struct pubname_struct *pubname_ref;
3557 typedef struct dw_ranges_struct *dw_ranges_ref;
3559 /* Each entry in the line_info_table maintains the file and
3560 line number associated with the label generated for that
3561 entry. The label gives the PC value associated with
3562 the line number entry. */
3564 typedef struct dw_line_info_struct GTY(())
3566 unsigned long dw_file_num;
3567 unsigned long dw_line_num;
3569 dw_line_info_entry;
3571 /* Line information for functions in separate sections; each one gets its
3572 own sequence. */
3573 typedef struct dw_separate_line_info_struct GTY(())
3575 unsigned long dw_file_num;
3576 unsigned long dw_line_num;
3577 unsigned long function;
3579 dw_separate_line_info_entry;
3581 /* Each DIE attribute has a field specifying the attribute kind,
3582 a link to the next attribute in the chain, and an attribute value.
3583 Attributes are typically linked below the DIE they modify. */
3585 typedef struct dw_attr_struct GTY(())
3587 enum dwarf_attribute dw_attr;
3588 dw_attr_ref dw_attr_next;
3589 dw_val_node dw_attr_val;
3591 dw_attr_node;
3593 /* The Debugging Information Entry (DIE) structure */
3595 typedef struct die_struct GTY(())
3597 enum dwarf_tag die_tag;
3598 char *die_symbol;
3599 dw_attr_ref die_attr;
3600 dw_die_ref die_parent;
3601 dw_die_ref die_child;
3602 dw_die_ref die_sib;
3603 dw_die_ref die_definition; /* ref from a specification to its definition */
3604 dw_offset die_offset;
3605 unsigned long die_abbrev;
3606 int die_mark;
3607 unsigned int decl_id;
3609 die_node;
3611 /* The pubname structure */
3613 typedef struct pubname_struct GTY(())
3615 dw_die_ref die;
3616 char *name;
3618 pubname_entry;
3620 struct dw_ranges_struct GTY(())
3622 int block_num;
3625 /* The limbo die list structure. */
3626 typedef struct limbo_die_struct GTY(())
3628 dw_die_ref die;
3629 tree created_for;
3630 struct limbo_die_struct *next;
3632 limbo_die_node;
3634 /* How to start an assembler comment. */
3635 #ifndef ASM_COMMENT_START
3636 #define ASM_COMMENT_START ";#"
3637 #endif
3639 /* Define a macro which returns nonzero for a TYPE_DECL which was
3640 implicitly generated for a tagged type.
3642 Note that unlike the gcc front end (which generates a NULL named
3643 TYPE_DECL node for each complete tagged type, each array type, and
3644 each function type node created) the g++ front end generates a
3645 _named_ TYPE_DECL node for each tagged type node created.
3646 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3647 generate a DW_TAG_typedef DIE for them. */
3649 #define TYPE_DECL_IS_STUB(decl) \
3650 (DECL_NAME (decl) == NULL_TREE \
3651 || (DECL_ARTIFICIAL (decl) \
3652 && is_tagged_type (TREE_TYPE (decl)) \
3653 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3654 /* This is necessary for stub decls that \
3655 appear in nested inline functions. */ \
3656 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3657 && (decl_ultimate_origin (decl) \
3658 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3660 /* Information concerning the compilation unit's programming
3661 language, and compiler version. */
3663 /* Fixed size portion of the DWARF compilation unit header. */
3664 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3665 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3667 /* Fixed size portion of public names info. */
3668 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3670 /* Fixed size portion of the address range info. */
3671 #define DWARF_ARANGES_HEADER_SIZE \
3672 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3673 DWARF2_ADDR_SIZE * 2) \
3674 - DWARF_INITIAL_LENGTH_SIZE)
3676 /* Size of padding portion in the address range info. It must be
3677 aligned to twice the pointer size. */
3678 #define DWARF_ARANGES_PAD_SIZE \
3679 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3680 DWARF2_ADDR_SIZE * 2) \
3681 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3683 /* Use assembler line directives if available. */
3684 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3685 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3686 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3687 #else
3688 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3689 #endif
3690 #endif
3692 /* Minimum line offset in a special line info. opcode.
3693 This value was chosen to give a reasonable range of values. */
3694 #define DWARF_LINE_BASE -10
3696 /* First special line opcode - leave room for the standard opcodes. */
3697 #define DWARF_LINE_OPCODE_BASE 10
3699 /* Range of line offsets in a special line info. opcode. */
3700 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3702 /* Flag that indicates the initial value of the is_stmt_start flag.
3703 In the present implementation, we do not mark any lines as
3704 the beginning of a source statement, because that information
3705 is not made available by the GCC front-end. */
3706 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3708 #ifdef DWARF2_DEBUGGING_INFO
3709 /* This location is used by calc_die_sizes() to keep track
3710 the offset of each DIE within the .debug_info section. */
3711 static unsigned long next_die_offset;
3712 #endif
3714 /* Record the root of the DIE's built for the current compilation unit. */
3715 static GTY(()) dw_die_ref comp_unit_die;
3717 /* A list of DIEs with a NULL parent waiting to be relocated. */
3718 static GTY(()) limbo_die_node *limbo_die_list;
3720 /* Filenames referenced by this compilation unit. */
3721 static GTY(()) varray_type file_table;
3722 static GTY(()) varray_type file_table_emitted;
3723 static GTY(()) size_t file_table_last_lookup_index;
3725 /* A hash table of references to DIE's that describe declarations.
3726 The key is a DECL_UID() which is a unique number identifying each decl. */
3727 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3729 /* Node of the variable location list. */
3730 struct var_loc_node GTY ((chain_next ("%h.next")))
3732 rtx GTY (()) var_loc_note;
3733 const char * GTY (()) label;
3734 const char * GTY (()) section_label;
3735 struct var_loc_node * GTY (()) next;
3738 /* Variable location list. */
3739 struct var_loc_list_def GTY (())
3741 struct var_loc_node * GTY (()) first;
3743 /* Do not mark the last element of the chained list because
3744 it is marked through the chain. */
3745 struct var_loc_node * GTY ((skip ("%h"))) last;
3747 /* DECL_UID of the variable decl. */
3748 unsigned int decl_id;
3750 typedef struct var_loc_list_def var_loc_list;
3753 /* Table of decl location linked lists. */
3754 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3756 /* A pointer to the base of a list of references to DIE's that
3757 are uniquely identified by their tag, presence/absence of
3758 children DIE's, and list of attribute/value pairs. */
3759 static GTY((length ("abbrev_die_table_allocated")))
3760 dw_die_ref *abbrev_die_table;
3762 /* Number of elements currently allocated for abbrev_die_table. */
3763 static GTY(()) unsigned abbrev_die_table_allocated;
3765 /* Number of elements in type_die_table currently in use. */
3766 static GTY(()) unsigned abbrev_die_table_in_use;
3768 /* Size (in elements) of increments by which we may expand the
3769 abbrev_die_table. */
3770 #define ABBREV_DIE_TABLE_INCREMENT 256
3772 /* A pointer to the base of a table that contains line information
3773 for each source code line in .text in the compilation unit. */
3774 static GTY((length ("line_info_table_allocated")))
3775 dw_line_info_ref line_info_table;
3777 /* Number of elements currently allocated for line_info_table. */
3778 static GTY(()) unsigned line_info_table_allocated;
3780 /* Number of elements in line_info_table currently in use. */
3781 static GTY(()) unsigned line_info_table_in_use;
3783 /* A pointer to the base of a table that contains line information
3784 for each source code line outside of .text in the compilation unit. */
3785 static GTY ((length ("separate_line_info_table_allocated")))
3786 dw_separate_line_info_ref separate_line_info_table;
3788 /* Number of elements currently allocated for separate_line_info_table. */
3789 static GTY(()) unsigned separate_line_info_table_allocated;
3791 /* Number of elements in separate_line_info_table currently in use. */
3792 static GTY(()) unsigned separate_line_info_table_in_use;
3794 /* Size (in elements) of increments by which we may expand the
3795 line_info_table. */
3796 #define LINE_INFO_TABLE_INCREMENT 1024
3798 /* A pointer to the base of a table that contains a list of publicly
3799 accessible names. */
3800 static GTY ((length ("pubname_table_allocated"))) pubname_ref pubname_table;
3802 /* Number of elements currently allocated for pubname_table. */
3803 static GTY(()) unsigned pubname_table_allocated;
3805 /* Number of elements in pubname_table currently in use. */
3806 static GTY(()) unsigned pubname_table_in_use;
3808 /* Size (in elements) of increments by which we may expand the
3809 pubname_table. */
3810 #define PUBNAME_TABLE_INCREMENT 64
3812 /* Array of dies for which we should generate .debug_arange info. */
3813 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3815 /* Number of elements currently allocated for arange_table. */
3816 static GTY(()) unsigned arange_table_allocated;
3818 /* Number of elements in arange_table currently in use. */
3819 static GTY(()) unsigned arange_table_in_use;
3821 /* Size (in elements) of increments by which we may expand the
3822 arange_table. */
3823 #define ARANGE_TABLE_INCREMENT 64
3825 /* Array of dies for which we should generate .debug_ranges info. */
3826 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3828 /* Number of elements currently allocated for ranges_table. */
3829 static GTY(()) unsigned ranges_table_allocated;
3831 /* Number of elements in ranges_table currently in use. */
3832 static GTY(()) unsigned ranges_table_in_use;
3834 /* Size (in elements) of increments by which we may expand the
3835 ranges_table. */
3836 #define RANGES_TABLE_INCREMENT 64
3838 /* Whether we have location lists that need outputting */
3839 static GTY(()) unsigned have_location_lists;
3841 /* Unique label counter. */
3842 static GTY(()) unsigned int loclabel_num;
3844 #ifdef DWARF2_DEBUGGING_INFO
3845 /* Record whether the function being analyzed contains inlined functions. */
3846 static int current_function_has_inlines;
3847 #endif
3848 #if 0 && defined (MIPS_DEBUGGING_INFO)
3849 static int comp_unit_has_inlines;
3850 #endif
3852 /* Number of file tables emitted in maybe_emit_file(). */
3853 static GTY(()) int emitcount = 0;
3855 /* Number of internal labels generated by gen_internal_sym(). */
3856 static GTY(()) int label_num;
3858 #ifdef DWARF2_DEBUGGING_INFO
3860 /* Forward declarations for functions defined in this file. */
3862 static int is_pseudo_reg (rtx);
3863 static tree type_main_variant (tree);
3864 static int is_tagged_type (tree);
3865 static const char *dwarf_tag_name (unsigned);
3866 static const char *dwarf_attr_name (unsigned);
3867 static const char *dwarf_form_name (unsigned);
3868 static tree decl_ultimate_origin (tree);
3869 static tree block_ultimate_origin (tree);
3870 static tree decl_class_context (tree);
3871 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3872 static inline enum dw_val_class AT_class (dw_attr_ref);
3873 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3874 static inline unsigned AT_flag (dw_attr_ref);
3875 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3876 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3877 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3878 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3879 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
3880 unsigned long);
3881 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3882 unsigned int, unsigned char *);
3883 static hashval_t debug_str_do_hash (const void *);
3884 static int debug_str_eq (const void *, const void *);
3885 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3886 static inline const char *AT_string (dw_attr_ref);
3887 static int AT_string_form (dw_attr_ref);
3888 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3889 static void add_AT_specification (dw_die_ref, dw_die_ref);
3890 static inline dw_die_ref AT_ref (dw_attr_ref);
3891 static inline int AT_ref_external (dw_attr_ref);
3892 static inline void set_AT_ref_external (dw_attr_ref, int);
3893 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3894 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3895 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3896 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3897 dw_loc_list_ref);
3898 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3899 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3900 static inline rtx AT_addr (dw_attr_ref);
3901 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3902 static void add_AT_lbl_offset (dw_die_ref, enum dwarf_attribute, const char *);
3903 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3904 unsigned HOST_WIDE_INT);
3905 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3906 unsigned long);
3907 static inline const char *AT_lbl (dw_attr_ref);
3908 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3909 static const char *get_AT_low_pc (dw_die_ref);
3910 static const char *get_AT_hi_pc (dw_die_ref);
3911 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3912 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3913 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3914 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3915 static bool is_c_family (void);
3916 static bool is_cxx (void);
3917 static bool is_java (void);
3918 static bool is_fortran (void);
3919 static bool is_ada (void);
3920 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3921 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3922 static inline void free_die (dw_die_ref);
3923 static void remove_children (dw_die_ref);
3924 static void add_child_die (dw_die_ref, dw_die_ref);
3925 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3926 static dw_die_ref lookup_type_die (tree);
3927 static void equate_type_number_to_die (tree, dw_die_ref);
3928 static hashval_t decl_die_table_hash (const void *);
3929 static int decl_die_table_eq (const void *, const void *);
3930 static dw_die_ref lookup_decl_die (tree);
3931 static hashval_t decl_loc_table_hash (const void *);
3932 static int decl_loc_table_eq (const void *, const void *);
3933 static var_loc_list *lookup_decl_loc (tree);
3934 static void equate_decl_number_to_die (tree, dw_die_ref);
3935 static void add_var_loc_to_decl (tree, struct var_loc_node *);
3936 static void print_spaces (FILE *);
3937 static void print_die (dw_die_ref, FILE *);
3938 static void print_dwarf_line_table (FILE *);
3939 static void reverse_die_lists (dw_die_ref);
3940 static void reverse_all_dies (dw_die_ref);
3941 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3942 static dw_die_ref pop_compile_unit (dw_die_ref);
3943 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3944 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3945 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3946 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3947 static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
3948 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3949 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3950 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3951 static void compute_section_prefix (dw_die_ref);
3952 static int is_type_die (dw_die_ref);
3953 static int is_comdat_die (dw_die_ref);
3954 static int is_symbol_die (dw_die_ref);
3955 static void assign_symbol_names (dw_die_ref);
3956 static void break_out_includes (dw_die_ref);
3957 static hashval_t htab_cu_hash (const void *);
3958 static int htab_cu_eq (const void *, const void *);
3959 static void htab_cu_del (void *);
3960 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3961 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3962 static void add_sibling_attributes (dw_die_ref);
3963 static void build_abbrev_table (dw_die_ref);
3964 static void output_location_lists (dw_die_ref);
3965 static int constant_size (long unsigned);
3966 static unsigned long size_of_die (dw_die_ref);
3967 static void calc_die_sizes (dw_die_ref);
3968 static void mark_dies (dw_die_ref);
3969 static void unmark_dies (dw_die_ref);
3970 static void unmark_all_dies (dw_die_ref);
3971 static unsigned long size_of_pubnames (void);
3972 static unsigned long size_of_aranges (void);
3973 static enum dwarf_form value_format (dw_attr_ref);
3974 static void output_value_format (dw_attr_ref);
3975 static void output_abbrev_section (void);
3976 static void output_die_symbol (dw_die_ref);
3977 static void output_die (dw_die_ref);
3978 static void output_compilation_unit_header (void);
3979 static void output_comp_unit (dw_die_ref, int);
3980 static const char *dwarf2_name (tree, int);
3981 static void add_pubname (tree, dw_die_ref);
3982 static void output_pubnames (void);
3983 static void add_arange (tree, dw_die_ref);
3984 static void output_aranges (void);
3985 static unsigned int add_ranges (tree);
3986 static void output_ranges (void);
3987 static void output_line_info (void);
3988 static void output_file_names (void);
3989 static dw_die_ref base_type_die (tree);
3990 static tree root_type (tree);
3991 static int is_base_type (tree);
3992 static bool is_subrange_type (tree);
3993 static dw_die_ref subrange_type_die (tree, dw_die_ref);
3994 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3995 static int type_is_enum (tree);
3996 static unsigned int dbx_reg_number (rtx);
3997 static dw_loc_descr_ref reg_loc_descriptor (rtx);
3998 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
3999 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
4000 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4001 static dw_loc_descr_ref based_loc_descr (unsigned, HOST_WIDE_INT, bool);
4002 static int is_based_loc (rtx);
4003 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode, bool);
4004 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
4005 static dw_loc_descr_ref loc_descriptor (rtx, bool);
4006 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4007 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4008 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4009 static tree field_type (tree);
4010 static unsigned int simple_type_align_in_bits (tree);
4011 static unsigned int simple_decl_align_in_bits (tree);
4012 static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
4013 static HOST_WIDE_INT field_byte_offset (tree);
4014 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4015 dw_loc_descr_ref);
4016 static void add_data_member_location_attribute (dw_die_ref, tree);
4017 static void add_const_value_attribute (dw_die_ref, rtx);
4018 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4019 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4020 static void insert_float (rtx, unsigned char *);
4021 static rtx rtl_for_decl_location (tree);
4022 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4023 enum dwarf_attribute);
4024 static void tree_add_const_value_attribute (dw_die_ref, tree);
4025 static void add_name_attribute (dw_die_ref, const char *);
4026 static void add_comp_dir_attribute (dw_die_ref);
4027 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4028 static void add_subscript_info (dw_die_ref, tree);
4029 static void add_byte_size_attribute (dw_die_ref, tree);
4030 static void add_bit_offset_attribute (dw_die_ref, tree);
4031 static void add_bit_size_attribute (dw_die_ref, tree);
4032 static void add_prototyped_attribute (dw_die_ref, tree);
4033 static void add_abstract_origin_attribute (dw_die_ref, tree);
4034 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4035 static void add_src_coords_attributes (dw_die_ref, tree);
4036 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4037 static void push_decl_scope (tree);
4038 static void pop_decl_scope (void);
4039 static dw_die_ref scope_die_for (tree, dw_die_ref);
4040 static inline int local_scope_p (dw_die_ref);
4041 static inline int class_or_namespace_scope_p (dw_die_ref);
4042 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4043 static void add_calling_convention_attribute (dw_die_ref, tree);
4044 static const char *type_tag (tree);
4045 static tree member_declared_type (tree);
4046 #if 0
4047 static const char *decl_start_label (tree);
4048 #endif
4049 static void gen_array_type_die (tree, dw_die_ref);
4050 #if 0
4051 static void gen_entry_point_die (tree, dw_die_ref);
4052 #endif
4053 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4054 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4055 static void gen_inlined_union_type_die (tree, dw_die_ref);
4056 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4057 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4058 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4059 static void gen_formal_types_die (tree, dw_die_ref);
4060 static void gen_subprogram_die (tree, dw_die_ref);
4061 static void gen_variable_die (tree, dw_die_ref);
4062 static void gen_label_die (tree, dw_die_ref);
4063 static void gen_lexical_block_die (tree, dw_die_ref, int);
4064 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4065 static void gen_field_die (tree, dw_die_ref);
4066 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4067 static dw_die_ref gen_compile_unit_die (const char *);
4068 static void gen_string_type_die (tree, dw_die_ref);
4069 static void gen_inheritance_die (tree, tree, dw_die_ref);
4070 static void gen_member_die (tree, dw_die_ref);
4071 static void gen_struct_or_union_type_die (tree, dw_die_ref);
4072 static void gen_subroutine_type_die (tree, dw_die_ref);
4073 static void gen_typedef_die (tree, dw_die_ref);
4074 static void gen_type_die (tree, dw_die_ref);
4075 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4076 static void gen_block_die (tree, dw_die_ref, int);
4077 static void decls_for_scope (tree, dw_die_ref, int);
4078 static int is_redundant_typedef (tree);
4079 static void gen_namespace_die (tree);
4080 static void gen_decl_die (tree, dw_die_ref);
4081 static dw_die_ref force_decl_die (tree);
4082 static dw_die_ref force_type_die (tree);
4083 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4084 static void declare_in_namespace (tree, dw_die_ref);
4085 static unsigned lookup_filename (const char *);
4086 static void init_file_table (void);
4087 static void retry_incomplete_types (void);
4088 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4089 static void splice_child_die (dw_die_ref, dw_die_ref);
4090 static int file_info_cmp (const void *, const void *);
4091 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4092 const char *, const char *, unsigned);
4093 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4094 const char *, const char *,
4095 const char *);
4096 static void output_loc_list (dw_loc_list_ref);
4097 static char *gen_internal_sym (const char *);
4099 static void prune_unmark_dies (dw_die_ref);
4100 static void prune_unused_types_mark (dw_die_ref, int);
4101 static void prune_unused_types_walk (dw_die_ref);
4102 static void prune_unused_types_walk_attribs (dw_die_ref);
4103 static void prune_unused_types_prune (dw_die_ref);
4104 static void prune_unused_types (void);
4105 static int maybe_emit_file (int);
4107 /* Section names used to hold DWARF debugging information. */
4108 #ifndef DEBUG_INFO_SECTION
4109 #define DEBUG_INFO_SECTION ".debug_info"
4110 #endif
4111 #ifndef DEBUG_ABBREV_SECTION
4112 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4113 #endif
4114 #ifndef DEBUG_ARANGES_SECTION
4115 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4116 #endif
4117 #ifndef DEBUG_MACINFO_SECTION
4118 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4119 #endif
4120 #ifndef DEBUG_LINE_SECTION
4121 #define DEBUG_LINE_SECTION ".debug_line"
4122 #endif
4123 #ifndef DEBUG_LOC_SECTION
4124 #define DEBUG_LOC_SECTION ".debug_loc"
4125 #endif
4126 #ifndef DEBUG_PUBNAMES_SECTION
4127 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
4128 #endif
4129 #ifndef DEBUG_STR_SECTION
4130 #define DEBUG_STR_SECTION ".debug_str"
4131 #endif
4132 #ifndef DEBUG_RANGES_SECTION
4133 #define DEBUG_RANGES_SECTION ".debug_ranges"
4134 #endif
4136 /* Standard ELF section names for compiled code and data. */
4137 #ifndef TEXT_SECTION_NAME
4138 #define TEXT_SECTION_NAME ".text"
4139 #endif
4141 /* Section flags for .debug_str section. */
4142 #define DEBUG_STR_SECTION_FLAGS \
4143 (HAVE_GAS_SHF_MERGE && flag_merge_constants \
4144 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4145 : SECTION_DEBUG)
4147 /* Labels we insert at beginning sections we can reference instead of
4148 the section names themselves. */
4150 #ifndef TEXT_SECTION_LABEL
4151 #define TEXT_SECTION_LABEL "Ltext"
4152 #endif
4153 #ifndef COLD_TEXT_SECTION_LABEL
4154 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4155 #endif
4156 #ifndef DEBUG_LINE_SECTION_LABEL
4157 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4158 #endif
4159 #ifndef DEBUG_INFO_SECTION_LABEL
4160 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4161 #endif
4162 #ifndef DEBUG_ABBREV_SECTION_LABEL
4163 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4164 #endif
4165 #ifndef DEBUG_LOC_SECTION_LABEL
4166 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4167 #endif
4168 #ifndef DEBUG_RANGES_SECTION_LABEL
4169 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4170 #endif
4171 #ifndef DEBUG_MACINFO_SECTION_LABEL
4172 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4173 #endif
4175 /* Definitions of defaults for formats and names of various special
4176 (artificial) labels which may be generated within this file (when the -g
4177 options is used and DWARF2_DEBUGGING_INFO is in effect.
4178 If necessary, these may be overridden from within the tm.h file, but
4179 typically, overriding these defaults is unnecessary. */
4181 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4182 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4183 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4184 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4185 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4186 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4187 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4188 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4189 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4190 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4192 #ifndef TEXT_END_LABEL
4193 #define TEXT_END_LABEL "Letext"
4194 #endif
4195 #ifndef COLD_END_LABEL
4196 #define COLD_END_LABEL "Letext_cold"
4197 #endif
4198 #ifndef BLOCK_BEGIN_LABEL
4199 #define BLOCK_BEGIN_LABEL "LBB"
4200 #endif
4201 #ifndef BLOCK_END_LABEL
4202 #define BLOCK_END_LABEL "LBE"
4203 #endif
4204 #ifndef LINE_CODE_LABEL
4205 #define LINE_CODE_LABEL "LM"
4206 #endif
4207 #ifndef SEPARATE_LINE_CODE_LABEL
4208 #define SEPARATE_LINE_CODE_LABEL "LSM"
4209 #endif
4211 /* We allow a language front-end to designate a function that is to be
4212 called to "demangle" any name before it is put into a DIE. */
4214 static const char *(*demangle_name_func) (const char *);
4216 void
4217 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4219 demangle_name_func = func;
4222 /* Test if rtl node points to a pseudo register. */
4224 static inline int
4225 is_pseudo_reg (rtx rtl)
4227 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4228 || (GET_CODE (rtl) == SUBREG
4229 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4232 /* Return a reference to a type, with its const and volatile qualifiers
4233 removed. */
4235 static inline tree
4236 type_main_variant (tree type)
4238 type = TYPE_MAIN_VARIANT (type);
4240 /* ??? There really should be only one main variant among any group of
4241 variants of a given type (and all of the MAIN_VARIANT values for all
4242 members of the group should point to that one type) but sometimes the C
4243 front-end messes this up for array types, so we work around that bug
4244 here. */
4245 if (TREE_CODE (type) == ARRAY_TYPE)
4246 while (type != TYPE_MAIN_VARIANT (type))
4247 type = TYPE_MAIN_VARIANT (type);
4249 return type;
4252 /* Return nonzero if the given type node represents a tagged type. */
4254 static inline int
4255 is_tagged_type (tree type)
4257 enum tree_code code = TREE_CODE (type);
4259 return (code == RECORD_TYPE || code == UNION_TYPE
4260 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4263 /* Convert a DIE tag into its string name. */
4265 static const char *
4266 dwarf_tag_name (unsigned int tag)
4268 switch (tag)
4270 case DW_TAG_padding:
4271 return "DW_TAG_padding";
4272 case DW_TAG_array_type:
4273 return "DW_TAG_array_type";
4274 case DW_TAG_class_type:
4275 return "DW_TAG_class_type";
4276 case DW_TAG_entry_point:
4277 return "DW_TAG_entry_point";
4278 case DW_TAG_enumeration_type:
4279 return "DW_TAG_enumeration_type";
4280 case DW_TAG_formal_parameter:
4281 return "DW_TAG_formal_parameter";
4282 case DW_TAG_imported_declaration:
4283 return "DW_TAG_imported_declaration";
4284 case DW_TAG_label:
4285 return "DW_TAG_label";
4286 case DW_TAG_lexical_block:
4287 return "DW_TAG_lexical_block";
4288 case DW_TAG_member:
4289 return "DW_TAG_member";
4290 case DW_TAG_pointer_type:
4291 return "DW_TAG_pointer_type";
4292 case DW_TAG_reference_type:
4293 return "DW_TAG_reference_type";
4294 case DW_TAG_compile_unit:
4295 return "DW_TAG_compile_unit";
4296 case DW_TAG_string_type:
4297 return "DW_TAG_string_type";
4298 case DW_TAG_structure_type:
4299 return "DW_TAG_structure_type";
4300 case DW_TAG_subroutine_type:
4301 return "DW_TAG_subroutine_type";
4302 case DW_TAG_typedef:
4303 return "DW_TAG_typedef";
4304 case DW_TAG_union_type:
4305 return "DW_TAG_union_type";
4306 case DW_TAG_unspecified_parameters:
4307 return "DW_TAG_unspecified_parameters";
4308 case DW_TAG_variant:
4309 return "DW_TAG_variant";
4310 case DW_TAG_common_block:
4311 return "DW_TAG_common_block";
4312 case DW_TAG_common_inclusion:
4313 return "DW_TAG_common_inclusion";
4314 case DW_TAG_inheritance:
4315 return "DW_TAG_inheritance";
4316 case DW_TAG_inlined_subroutine:
4317 return "DW_TAG_inlined_subroutine";
4318 case DW_TAG_module:
4319 return "DW_TAG_module";
4320 case DW_TAG_ptr_to_member_type:
4321 return "DW_TAG_ptr_to_member_type";
4322 case DW_TAG_set_type:
4323 return "DW_TAG_set_type";
4324 case DW_TAG_subrange_type:
4325 return "DW_TAG_subrange_type";
4326 case DW_TAG_with_stmt:
4327 return "DW_TAG_with_stmt";
4328 case DW_TAG_access_declaration:
4329 return "DW_TAG_access_declaration";
4330 case DW_TAG_base_type:
4331 return "DW_TAG_base_type";
4332 case DW_TAG_catch_block:
4333 return "DW_TAG_catch_block";
4334 case DW_TAG_const_type:
4335 return "DW_TAG_const_type";
4336 case DW_TAG_constant:
4337 return "DW_TAG_constant";
4338 case DW_TAG_enumerator:
4339 return "DW_TAG_enumerator";
4340 case DW_TAG_file_type:
4341 return "DW_TAG_file_type";
4342 case DW_TAG_friend:
4343 return "DW_TAG_friend";
4344 case DW_TAG_namelist:
4345 return "DW_TAG_namelist";
4346 case DW_TAG_namelist_item:
4347 return "DW_TAG_namelist_item";
4348 case DW_TAG_namespace:
4349 return "DW_TAG_namespace";
4350 case DW_TAG_packed_type:
4351 return "DW_TAG_packed_type";
4352 case DW_TAG_subprogram:
4353 return "DW_TAG_subprogram";
4354 case DW_TAG_template_type_param:
4355 return "DW_TAG_template_type_param";
4356 case DW_TAG_template_value_param:
4357 return "DW_TAG_template_value_param";
4358 case DW_TAG_thrown_type:
4359 return "DW_TAG_thrown_type";
4360 case DW_TAG_try_block:
4361 return "DW_TAG_try_block";
4362 case DW_TAG_variant_part:
4363 return "DW_TAG_variant_part";
4364 case DW_TAG_variable:
4365 return "DW_TAG_variable";
4366 case DW_TAG_volatile_type:
4367 return "DW_TAG_volatile_type";
4368 case DW_TAG_imported_module:
4369 return "DW_TAG_imported_module";
4370 case DW_TAG_MIPS_loop:
4371 return "DW_TAG_MIPS_loop";
4372 case DW_TAG_format_label:
4373 return "DW_TAG_format_label";
4374 case DW_TAG_function_template:
4375 return "DW_TAG_function_template";
4376 case DW_TAG_class_template:
4377 return "DW_TAG_class_template";
4378 case DW_TAG_GNU_BINCL:
4379 return "DW_TAG_GNU_BINCL";
4380 case DW_TAG_GNU_EINCL:
4381 return "DW_TAG_GNU_EINCL";
4382 default:
4383 return "DW_TAG_<unknown>";
4387 /* Convert a DWARF attribute code into its string name. */
4389 static const char *
4390 dwarf_attr_name (unsigned int attr)
4392 switch (attr)
4394 case DW_AT_sibling:
4395 return "DW_AT_sibling";
4396 case DW_AT_location:
4397 return "DW_AT_location";
4398 case DW_AT_name:
4399 return "DW_AT_name";
4400 case DW_AT_ordering:
4401 return "DW_AT_ordering";
4402 case DW_AT_subscr_data:
4403 return "DW_AT_subscr_data";
4404 case DW_AT_byte_size:
4405 return "DW_AT_byte_size";
4406 case DW_AT_bit_offset:
4407 return "DW_AT_bit_offset";
4408 case DW_AT_bit_size:
4409 return "DW_AT_bit_size";
4410 case DW_AT_element_list:
4411 return "DW_AT_element_list";
4412 case DW_AT_stmt_list:
4413 return "DW_AT_stmt_list";
4414 case DW_AT_low_pc:
4415 return "DW_AT_low_pc";
4416 case DW_AT_high_pc:
4417 return "DW_AT_high_pc";
4418 case DW_AT_language:
4419 return "DW_AT_language";
4420 case DW_AT_member:
4421 return "DW_AT_member";
4422 case DW_AT_discr:
4423 return "DW_AT_discr";
4424 case DW_AT_discr_value:
4425 return "DW_AT_discr_value";
4426 case DW_AT_visibility:
4427 return "DW_AT_visibility";
4428 case DW_AT_import:
4429 return "DW_AT_import";
4430 case DW_AT_string_length:
4431 return "DW_AT_string_length";
4432 case DW_AT_common_reference:
4433 return "DW_AT_common_reference";
4434 case DW_AT_comp_dir:
4435 return "DW_AT_comp_dir";
4436 case DW_AT_const_value:
4437 return "DW_AT_const_value";
4438 case DW_AT_containing_type:
4439 return "DW_AT_containing_type";
4440 case DW_AT_default_value:
4441 return "DW_AT_default_value";
4442 case DW_AT_inline:
4443 return "DW_AT_inline";
4444 case DW_AT_is_optional:
4445 return "DW_AT_is_optional";
4446 case DW_AT_lower_bound:
4447 return "DW_AT_lower_bound";
4448 case DW_AT_producer:
4449 return "DW_AT_producer";
4450 case DW_AT_prototyped:
4451 return "DW_AT_prototyped";
4452 case DW_AT_return_addr:
4453 return "DW_AT_return_addr";
4454 case DW_AT_start_scope:
4455 return "DW_AT_start_scope";
4456 case DW_AT_stride_size:
4457 return "DW_AT_stride_size";
4458 case DW_AT_upper_bound:
4459 return "DW_AT_upper_bound";
4460 case DW_AT_abstract_origin:
4461 return "DW_AT_abstract_origin";
4462 case DW_AT_accessibility:
4463 return "DW_AT_accessibility";
4464 case DW_AT_address_class:
4465 return "DW_AT_address_class";
4466 case DW_AT_artificial:
4467 return "DW_AT_artificial";
4468 case DW_AT_base_types:
4469 return "DW_AT_base_types";
4470 case DW_AT_calling_convention:
4471 return "DW_AT_calling_convention";
4472 case DW_AT_count:
4473 return "DW_AT_count";
4474 case DW_AT_data_member_location:
4475 return "DW_AT_data_member_location";
4476 case DW_AT_decl_column:
4477 return "DW_AT_decl_column";
4478 case DW_AT_decl_file:
4479 return "DW_AT_decl_file";
4480 case DW_AT_decl_line:
4481 return "DW_AT_decl_line";
4482 case DW_AT_declaration:
4483 return "DW_AT_declaration";
4484 case DW_AT_discr_list:
4485 return "DW_AT_discr_list";
4486 case DW_AT_encoding:
4487 return "DW_AT_encoding";
4488 case DW_AT_external:
4489 return "DW_AT_external";
4490 case DW_AT_frame_base:
4491 return "DW_AT_frame_base";
4492 case DW_AT_friend:
4493 return "DW_AT_friend";
4494 case DW_AT_identifier_case:
4495 return "DW_AT_identifier_case";
4496 case DW_AT_macro_info:
4497 return "DW_AT_macro_info";
4498 case DW_AT_namelist_items:
4499 return "DW_AT_namelist_items";
4500 case DW_AT_priority:
4501 return "DW_AT_priority";
4502 case DW_AT_segment:
4503 return "DW_AT_segment";
4504 case DW_AT_specification:
4505 return "DW_AT_specification";
4506 case DW_AT_static_link:
4507 return "DW_AT_static_link";
4508 case DW_AT_type:
4509 return "DW_AT_type";
4510 case DW_AT_use_location:
4511 return "DW_AT_use_location";
4512 case DW_AT_variable_parameter:
4513 return "DW_AT_variable_parameter";
4514 case DW_AT_virtuality:
4515 return "DW_AT_virtuality";
4516 case DW_AT_vtable_elem_location:
4517 return "DW_AT_vtable_elem_location";
4519 case DW_AT_allocated:
4520 return "DW_AT_allocated";
4521 case DW_AT_associated:
4522 return "DW_AT_associated";
4523 case DW_AT_data_location:
4524 return "DW_AT_data_location";
4525 case DW_AT_stride:
4526 return "DW_AT_stride";
4527 case DW_AT_entry_pc:
4528 return "DW_AT_entry_pc";
4529 case DW_AT_use_UTF8:
4530 return "DW_AT_use_UTF8";
4531 case DW_AT_extension:
4532 return "DW_AT_extension";
4533 case DW_AT_ranges:
4534 return "DW_AT_ranges";
4535 case DW_AT_trampoline:
4536 return "DW_AT_trampoline";
4537 case DW_AT_call_column:
4538 return "DW_AT_call_column";
4539 case DW_AT_call_file:
4540 return "DW_AT_call_file";
4541 case DW_AT_call_line:
4542 return "DW_AT_call_line";
4544 case DW_AT_MIPS_fde:
4545 return "DW_AT_MIPS_fde";
4546 case DW_AT_MIPS_loop_begin:
4547 return "DW_AT_MIPS_loop_begin";
4548 case DW_AT_MIPS_tail_loop_begin:
4549 return "DW_AT_MIPS_tail_loop_begin";
4550 case DW_AT_MIPS_epilog_begin:
4551 return "DW_AT_MIPS_epilog_begin";
4552 case DW_AT_MIPS_loop_unroll_factor:
4553 return "DW_AT_MIPS_loop_unroll_factor";
4554 case DW_AT_MIPS_software_pipeline_depth:
4555 return "DW_AT_MIPS_software_pipeline_depth";
4556 case DW_AT_MIPS_linkage_name:
4557 return "DW_AT_MIPS_linkage_name";
4558 case DW_AT_MIPS_stride:
4559 return "DW_AT_MIPS_stride";
4560 case DW_AT_MIPS_abstract_name:
4561 return "DW_AT_MIPS_abstract_name";
4562 case DW_AT_MIPS_clone_origin:
4563 return "DW_AT_MIPS_clone_origin";
4564 case DW_AT_MIPS_has_inlines:
4565 return "DW_AT_MIPS_has_inlines";
4567 case DW_AT_sf_names:
4568 return "DW_AT_sf_names";
4569 case DW_AT_src_info:
4570 return "DW_AT_src_info";
4571 case DW_AT_mac_info:
4572 return "DW_AT_mac_info";
4573 case DW_AT_src_coords:
4574 return "DW_AT_src_coords";
4575 case DW_AT_body_begin:
4576 return "DW_AT_body_begin";
4577 case DW_AT_body_end:
4578 return "DW_AT_body_end";
4579 case DW_AT_GNU_vector:
4580 return "DW_AT_GNU_vector";
4582 case DW_AT_VMS_rtnbeg_pd_address:
4583 return "DW_AT_VMS_rtnbeg_pd_address";
4585 default:
4586 return "DW_AT_<unknown>";
4590 /* Convert a DWARF value form code into its string name. */
4592 static const char *
4593 dwarf_form_name (unsigned int form)
4595 switch (form)
4597 case DW_FORM_addr:
4598 return "DW_FORM_addr";
4599 case DW_FORM_block2:
4600 return "DW_FORM_block2";
4601 case DW_FORM_block4:
4602 return "DW_FORM_block4";
4603 case DW_FORM_data2:
4604 return "DW_FORM_data2";
4605 case DW_FORM_data4:
4606 return "DW_FORM_data4";
4607 case DW_FORM_data8:
4608 return "DW_FORM_data8";
4609 case DW_FORM_string:
4610 return "DW_FORM_string";
4611 case DW_FORM_block:
4612 return "DW_FORM_block";
4613 case DW_FORM_block1:
4614 return "DW_FORM_block1";
4615 case DW_FORM_data1:
4616 return "DW_FORM_data1";
4617 case DW_FORM_flag:
4618 return "DW_FORM_flag";
4619 case DW_FORM_sdata:
4620 return "DW_FORM_sdata";
4621 case DW_FORM_strp:
4622 return "DW_FORM_strp";
4623 case DW_FORM_udata:
4624 return "DW_FORM_udata";
4625 case DW_FORM_ref_addr:
4626 return "DW_FORM_ref_addr";
4627 case DW_FORM_ref1:
4628 return "DW_FORM_ref1";
4629 case DW_FORM_ref2:
4630 return "DW_FORM_ref2";
4631 case DW_FORM_ref4:
4632 return "DW_FORM_ref4";
4633 case DW_FORM_ref8:
4634 return "DW_FORM_ref8";
4635 case DW_FORM_ref_udata:
4636 return "DW_FORM_ref_udata";
4637 case DW_FORM_indirect:
4638 return "DW_FORM_indirect";
4639 default:
4640 return "DW_FORM_<unknown>";
4644 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4645 instance of an inlined instance of a decl 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 decl_ultimate_origin (tree decl)
4653 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4654 nodes in the function to point to themselves; ignore that if
4655 we're trying to output the abstract instance of this function. */
4656 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4657 return NULL_TREE;
4659 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4660 most distant ancestor, this should never happen. */
4661 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4663 return DECL_ABSTRACT_ORIGIN (decl);
4666 /* Determine the "ultimate origin" of a block. The block may be an inlined
4667 instance of an inlined instance of a block which is local to an inline
4668 function, so we have to trace all of the way back through the origin chain
4669 to find out what sort of node actually served as the original seed for the
4670 given block. */
4672 static tree
4673 block_ultimate_origin (tree block)
4675 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4677 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4678 nodes in the function to point to themselves; ignore that if
4679 we're trying to output the abstract instance of this function. */
4680 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4681 return NULL_TREE;
4683 if (immediate_origin == NULL_TREE)
4684 return NULL_TREE;
4685 else
4687 tree ret_val;
4688 tree lookahead = immediate_origin;
4692 ret_val = lookahead;
4693 lookahead = (TREE_CODE (ret_val) == BLOCK
4694 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4696 while (lookahead != NULL && lookahead != ret_val);
4698 /* The block's abstract origin chain may not be the *ultimate* origin of
4699 the block. It could lead to a DECL that has an abstract origin set.
4700 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4701 will give us if it has one). Note that DECL's abstract origins are
4702 supposed to be the most distant ancestor (or so decl_ultimate_origin
4703 claims), so we don't need to loop following the DECL origins. */
4704 if (DECL_P (ret_val))
4705 return DECL_ORIGIN (ret_val);
4707 return ret_val;
4711 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4712 of a virtual function may refer to a base class, so we check the 'this'
4713 parameter. */
4715 static tree
4716 decl_class_context (tree decl)
4718 tree context = NULL_TREE;
4720 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4721 context = DECL_CONTEXT (decl);
4722 else
4723 context = TYPE_MAIN_VARIANT
4724 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4726 if (context && !TYPE_P (context))
4727 context = NULL_TREE;
4729 return context;
4732 /* Add an attribute/value pair to a DIE. We build the lists up in reverse
4733 addition order, and correct that in reverse_all_dies. */
4735 static inline void
4736 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4738 if (die != NULL && attr != NULL)
4740 attr->dw_attr_next = die->die_attr;
4741 die->die_attr = attr;
4745 static inline enum dw_val_class
4746 AT_class (dw_attr_ref a)
4748 return a->dw_attr_val.val_class;
4751 /* Add a flag value attribute to a DIE. */
4753 static inline void
4754 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4756 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4758 attr->dw_attr_next = NULL;
4759 attr->dw_attr = attr_kind;
4760 attr->dw_attr_val.val_class = dw_val_class_flag;
4761 attr->dw_attr_val.v.val_flag = flag;
4762 add_dwarf_attr (die, attr);
4765 static inline unsigned
4766 AT_flag (dw_attr_ref a)
4768 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4769 return a->dw_attr_val.v.val_flag;
4772 /* Add a signed integer attribute value to a DIE. */
4774 static inline void
4775 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_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_const;
4782 attr->dw_attr_val.v.val_int = int_val;
4783 add_dwarf_attr (die, attr);
4786 static inline HOST_WIDE_INT
4787 AT_int (dw_attr_ref a)
4789 gcc_assert (a && AT_class (a) == dw_val_class_const);
4790 return a->dw_attr_val.v.val_int;
4793 /* Add an unsigned integer attribute value to a DIE. */
4795 static inline void
4796 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4797 unsigned HOST_WIDE_INT unsigned_val)
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_unsigned_const;
4804 attr->dw_attr_val.v.val_unsigned = unsigned_val;
4805 add_dwarf_attr (die, attr);
4808 static inline unsigned HOST_WIDE_INT
4809 AT_unsigned (dw_attr_ref a)
4811 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4812 return a->dw_attr_val.v.val_unsigned;
4815 /* Add an unsigned double integer attribute value to a DIE. */
4817 static inline void
4818 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4819 long unsigned int val_hi, long unsigned int val_low)
4821 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4823 attr->dw_attr_next = NULL;
4824 attr->dw_attr = attr_kind;
4825 attr->dw_attr_val.val_class = dw_val_class_long_long;
4826 attr->dw_attr_val.v.val_long_long.hi = val_hi;
4827 attr->dw_attr_val.v.val_long_long.low = val_low;
4828 add_dwarf_attr (die, attr);
4831 /* Add a floating point attribute value to a DIE and return it. */
4833 static inline void
4834 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4835 unsigned int length, unsigned int elt_size, unsigned char *array)
4837 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4839 attr->dw_attr_next = NULL;
4840 attr->dw_attr = attr_kind;
4841 attr->dw_attr_val.val_class = dw_val_class_vec;
4842 attr->dw_attr_val.v.val_vec.length = length;
4843 attr->dw_attr_val.v.val_vec.elt_size = elt_size;
4844 attr->dw_attr_val.v.val_vec.array = array;
4845 add_dwarf_attr (die, attr);
4848 /* Hash and equality functions for debug_str_hash. */
4850 static hashval_t
4851 debug_str_do_hash (const void *x)
4853 return htab_hash_string (((const struct indirect_string_node *)x)->str);
4856 static int
4857 debug_str_eq (const void *x1, const void *x2)
4859 return strcmp ((((const struct indirect_string_node *)x1)->str),
4860 (const char *)x2) == 0;
4863 /* Add a string attribute value to a DIE. */
4865 static inline void
4866 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4868 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4869 struct indirect_string_node *node;
4870 void **slot;
4872 if (! debug_str_hash)
4873 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4874 debug_str_eq, NULL);
4876 slot = htab_find_slot_with_hash (debug_str_hash, str,
4877 htab_hash_string (str), INSERT);
4878 if (*slot == NULL)
4879 *slot = ggc_alloc_cleared (sizeof (struct indirect_string_node));
4880 node = (struct indirect_string_node *) *slot;
4881 node->str = ggc_strdup (str);
4882 node->refcount++;
4884 attr->dw_attr_next = NULL;
4885 attr->dw_attr = attr_kind;
4886 attr->dw_attr_val.val_class = dw_val_class_str;
4887 attr->dw_attr_val.v.val_str = node;
4888 add_dwarf_attr (die, attr);
4891 static inline const char *
4892 AT_string (dw_attr_ref a)
4894 gcc_assert (a && AT_class (a) == dw_val_class_str);
4895 return a->dw_attr_val.v.val_str->str;
4898 /* Find out whether a string should be output inline in DIE
4899 or out-of-line in .debug_str section. */
4901 static int
4902 AT_string_form (dw_attr_ref a)
4904 struct indirect_string_node *node;
4905 unsigned int len;
4906 char label[32];
4908 gcc_assert (a && AT_class (a) == dw_val_class_str);
4910 node = a->dw_attr_val.v.val_str;
4911 if (node->form)
4912 return node->form;
4914 len = strlen (node->str) + 1;
4916 /* If the string is shorter or equal to the size of the reference, it is
4917 always better to put it inline. */
4918 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4919 return node->form = DW_FORM_string;
4921 /* If we cannot expect the linker to merge strings in .debug_str
4922 section, only put it into .debug_str if it is worth even in this
4923 single module. */
4924 if ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) == 0
4925 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
4926 return node->form = DW_FORM_string;
4928 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4929 ++dw2_string_counter;
4930 node->label = xstrdup (label);
4932 return node->form = DW_FORM_strp;
4935 /* Add a DIE reference attribute value to a DIE. */
4937 static inline void
4938 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4940 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4942 attr->dw_attr_next = NULL;
4943 attr->dw_attr = attr_kind;
4944 attr->dw_attr_val.val_class = dw_val_class_die_ref;
4945 attr->dw_attr_val.v.val_die_ref.die = targ_die;
4946 attr->dw_attr_val.v.val_die_ref.external = 0;
4947 add_dwarf_attr (die, attr);
4950 /* Add an AT_specification attribute to a DIE, and also make the back
4951 pointer from the specification to the definition. */
4953 static inline void
4954 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4956 add_AT_die_ref (die, DW_AT_specification, targ_die);
4957 gcc_assert (!targ_die->die_definition);
4958 targ_die->die_definition = die;
4961 static inline dw_die_ref
4962 AT_ref (dw_attr_ref a)
4964 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4965 return a->dw_attr_val.v.val_die_ref.die;
4968 static inline int
4969 AT_ref_external (dw_attr_ref a)
4971 if (a && AT_class (a) == dw_val_class_die_ref)
4972 return a->dw_attr_val.v.val_die_ref.external;
4974 return 0;
4977 static inline void
4978 set_AT_ref_external (dw_attr_ref a, int i)
4980 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4981 a->dw_attr_val.v.val_die_ref.external = i;
4984 /* Add an FDE reference attribute value to a DIE. */
4986 static inline void
4987 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4989 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4991 attr->dw_attr_next = NULL;
4992 attr->dw_attr = attr_kind;
4993 attr->dw_attr_val.val_class = dw_val_class_fde_ref;
4994 attr->dw_attr_val.v.val_fde_index = targ_fde;
4995 add_dwarf_attr (die, attr);
4998 /* Add a location description attribute value to a DIE. */
5000 static inline void
5001 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5003 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5005 attr->dw_attr_next = NULL;
5006 attr->dw_attr = attr_kind;
5007 attr->dw_attr_val.val_class = dw_val_class_loc;
5008 attr->dw_attr_val.v.val_loc = loc;
5009 add_dwarf_attr (die, attr);
5012 static inline dw_loc_descr_ref
5013 AT_loc (dw_attr_ref a)
5015 gcc_assert (a && AT_class (a) == dw_val_class_loc);
5016 return a->dw_attr_val.v.val_loc;
5019 static inline void
5020 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
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_loc_list;
5027 attr->dw_attr_val.v.val_loc_list = loc_list;
5028 add_dwarf_attr (die, attr);
5029 have_location_lists = 1;
5032 static inline dw_loc_list_ref
5033 AT_loc_list (dw_attr_ref a)
5035 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5036 return a->dw_attr_val.v.val_loc_list;
5039 /* Add an address constant attribute value to a DIE. */
5041 static inline void
5042 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5044 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5046 attr->dw_attr_next = NULL;
5047 attr->dw_attr = attr_kind;
5048 attr->dw_attr_val.val_class = dw_val_class_addr;
5049 attr->dw_attr_val.v.val_addr = addr;
5050 add_dwarf_attr (die, attr);
5053 static inline rtx
5054 AT_addr (dw_attr_ref a)
5056 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5057 return a->dw_attr_val.v.val_addr;
5060 /* Add a label identifier attribute value to a DIE. */
5062 static inline void
5063 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5065 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5067 attr->dw_attr_next = NULL;
5068 attr->dw_attr = attr_kind;
5069 attr->dw_attr_val.val_class = dw_val_class_lbl_id;
5070 attr->dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5071 add_dwarf_attr (die, attr);
5074 /* Add a section offset attribute value to a DIE. */
5076 static inline void
5077 add_AT_lbl_offset (dw_die_ref die, enum dwarf_attribute attr_kind, const char *label)
5079 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5081 attr->dw_attr_next = NULL;
5082 attr->dw_attr = attr_kind;
5083 attr->dw_attr_val.val_class = dw_val_class_lbl_offset;
5084 attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
5085 add_dwarf_attr (die, attr);
5088 /* Add an offset attribute value to a DIE. */
5090 static inline void
5091 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5092 unsigned HOST_WIDE_INT offset)
5094 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5096 attr->dw_attr_next = NULL;
5097 attr->dw_attr = attr_kind;
5098 attr->dw_attr_val.val_class = dw_val_class_offset;
5099 attr->dw_attr_val.v.val_offset = offset;
5100 add_dwarf_attr (die, attr);
5103 /* Add an range_list attribute value to a DIE. */
5105 static void
5106 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5107 long unsigned int offset)
5109 dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5111 attr->dw_attr_next = NULL;
5112 attr->dw_attr = attr_kind;
5113 attr->dw_attr_val.val_class = dw_val_class_range_list;
5114 attr->dw_attr_val.v.val_offset = offset;
5115 add_dwarf_attr (die, attr);
5118 static inline const char *
5119 AT_lbl (dw_attr_ref a)
5121 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5122 || AT_class (a) == dw_val_class_lbl_offset));
5123 return a->dw_attr_val.v.val_lbl_id;
5126 /* Get the attribute of type attr_kind. */
5128 static dw_attr_ref
5129 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5131 dw_attr_ref a;
5132 dw_die_ref spec = NULL;
5134 if (die != NULL)
5136 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5137 if (a->dw_attr == attr_kind)
5138 return a;
5139 else if (a->dw_attr == DW_AT_specification
5140 || a->dw_attr == DW_AT_abstract_origin)
5141 spec = AT_ref (a);
5143 if (spec)
5144 return get_AT (spec, attr_kind);
5147 return NULL;
5150 /* Return the "low pc" attribute value, typically associated with a subprogram
5151 DIE. Return null if the "low pc" attribute is either not present, or if it
5152 cannot be represented as an assembler label identifier. */
5154 static inline const char *
5155 get_AT_low_pc (dw_die_ref die)
5157 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5159 return a ? AT_lbl (a) : NULL;
5162 /* Return the "high pc" attribute value, typically associated with a subprogram
5163 DIE. Return null if the "high pc" attribute is either not present, or if it
5164 cannot be represented as an assembler label identifier. */
5166 static inline const char *
5167 get_AT_hi_pc (dw_die_ref die)
5169 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5171 return a ? AT_lbl (a) : NULL;
5174 /* Return the value of the string attribute designated by ATTR_KIND, or
5175 NULL if it is not present. */
5177 static inline const char *
5178 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5180 dw_attr_ref a = get_AT (die, attr_kind);
5182 return a ? AT_string (a) : NULL;
5185 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5186 if it is not present. */
5188 static inline int
5189 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5191 dw_attr_ref a = get_AT (die, attr_kind);
5193 return a ? AT_flag (a) : 0;
5196 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5197 if it is not present. */
5199 static inline unsigned
5200 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5202 dw_attr_ref a = get_AT (die, attr_kind);
5204 return a ? AT_unsigned (a) : 0;
5207 static inline dw_die_ref
5208 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5210 dw_attr_ref a = get_AT (die, attr_kind);
5212 return a ? AT_ref (a) : NULL;
5215 /* Return TRUE if the language is C or C++. */
5217 static inline bool
5218 is_c_family (void)
5220 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5222 return (lang == DW_LANG_C || lang == DW_LANG_C89
5223 || lang == DW_LANG_C_plus_plus);
5226 /* Return TRUE if the language is C++. */
5228 static inline bool
5229 is_cxx (void)
5231 return (get_AT_unsigned (comp_unit_die, DW_AT_language)
5232 == DW_LANG_C_plus_plus);
5235 /* Return TRUE if the language is Fortran. */
5237 static inline bool
5238 is_fortran (void)
5240 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5242 return (lang == DW_LANG_Fortran77
5243 || lang == DW_LANG_Fortran90
5244 || lang == DW_LANG_Fortran95);
5247 /* Return TRUE if the language is Java. */
5249 static inline bool
5250 is_java (void)
5252 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5254 return lang == DW_LANG_Java;
5257 /* Return TRUE if the language is Ada. */
5259 static inline bool
5260 is_ada (void)
5262 unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5264 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5267 /* Free up the memory used by A. */
5269 static inline void free_AT (dw_attr_ref);
5270 static inline void
5271 free_AT (dw_attr_ref a)
5273 if (AT_class (a) == dw_val_class_str)
5274 if (a->dw_attr_val.v.val_str->refcount)
5275 a->dw_attr_val.v.val_str->refcount--;
5278 /* Remove the specified attribute if present. */
5280 static void
5281 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5283 dw_attr_ref *p;
5284 dw_attr_ref removed = NULL;
5286 if (die != NULL)
5288 for (p = &(die->die_attr); *p; p = &((*p)->dw_attr_next))
5289 if ((*p)->dw_attr == attr_kind)
5291 removed = *p;
5292 *p = (*p)->dw_attr_next;
5293 break;
5296 if (removed != 0)
5297 free_AT (removed);
5301 /* Remove child die whose die_tag is specified tag. */
5303 static void
5304 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5306 dw_die_ref current, prev, next;
5307 current = die->die_child;
5308 prev = NULL;
5309 while (current != NULL)
5311 if (current->die_tag == tag)
5313 next = current->die_sib;
5314 if (prev == NULL)
5315 die->die_child = next;
5316 else
5317 prev->die_sib = next;
5318 free_die (current);
5319 current = next;
5321 else
5323 prev = current;
5324 current = current->die_sib;
5329 /* Free up the memory used by DIE. */
5331 static inline void
5332 free_die (dw_die_ref die)
5334 remove_children (die);
5337 /* Discard the children of this DIE. */
5339 static void
5340 remove_children (dw_die_ref die)
5342 dw_die_ref child_die = die->die_child;
5344 die->die_child = NULL;
5346 while (child_die != NULL)
5348 dw_die_ref tmp_die = child_die;
5349 dw_attr_ref a;
5351 child_die = child_die->die_sib;
5353 for (a = tmp_die->die_attr; a != NULL;)
5355 dw_attr_ref tmp_a = a;
5357 a = a->dw_attr_next;
5358 free_AT (tmp_a);
5361 free_die (tmp_die);
5365 /* Add a child DIE below its parent. We build the lists up in reverse
5366 addition order, and correct that in reverse_all_dies. */
5368 static inline void
5369 add_child_die (dw_die_ref die, dw_die_ref child_die)
5371 if (die != NULL && child_die != NULL)
5373 gcc_assert (die != child_die);
5375 child_die->die_parent = die;
5376 child_die->die_sib = die->die_child;
5377 die->die_child = child_die;
5381 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5382 is the specification, to the front of PARENT's list of children. */
5384 static void
5385 splice_child_die (dw_die_ref parent, dw_die_ref child)
5387 dw_die_ref *p;
5389 /* We want the declaration DIE from inside the class, not the
5390 specification DIE at toplevel. */
5391 if (child->die_parent != parent)
5393 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5395 if (tmp)
5396 child = tmp;
5399 gcc_assert (child->die_parent == parent
5400 || (child->die_parent
5401 == get_AT_ref (parent, DW_AT_specification)));
5403 for (p = &(child->die_parent->die_child); *p; p = &((*p)->die_sib))
5404 if (*p == child)
5406 *p = child->die_sib;
5407 break;
5410 child->die_parent = parent;
5411 child->die_sib = parent->die_child;
5412 parent->die_child = child;
5415 /* Return a pointer to a newly created DIE node. */
5417 static inline dw_die_ref
5418 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5420 dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5422 die->die_tag = tag_value;
5424 if (parent_die != NULL)
5425 add_child_die (parent_die, die);
5426 else
5428 limbo_die_node *limbo_node;
5430 limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5431 limbo_node->die = die;
5432 limbo_node->created_for = t;
5433 limbo_node->next = limbo_die_list;
5434 limbo_die_list = limbo_node;
5437 return die;
5440 /* Return the DIE associated with the given type specifier. */
5442 static inline dw_die_ref
5443 lookup_type_die (tree type)
5445 return TYPE_SYMTAB_DIE (type);
5448 /* Equate a DIE to a given type specifier. */
5450 static inline void
5451 equate_type_number_to_die (tree type, dw_die_ref type_die)
5453 TYPE_SYMTAB_DIE (type) = type_die;
5456 /* Returns a hash value for X (which really is a die_struct). */
5458 static hashval_t
5459 decl_die_table_hash (const void *x)
5461 return (hashval_t) ((const dw_die_ref) x)->decl_id;
5464 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5466 static int
5467 decl_die_table_eq (const void *x, const void *y)
5469 return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5472 /* Return the DIE associated with a given declaration. */
5474 static inline dw_die_ref
5475 lookup_decl_die (tree decl)
5477 return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5480 /* Returns a hash value for X (which really is a var_loc_list). */
5482 static hashval_t
5483 decl_loc_table_hash (const void *x)
5485 return (hashval_t) ((const var_loc_list *) x)->decl_id;
5488 /* Return nonzero if decl_id of var_loc_list X is the same as
5489 UID of decl *Y. */
5491 static int
5492 decl_loc_table_eq (const void *x, const void *y)
5494 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5497 /* Return the var_loc list associated with a given declaration. */
5499 static inline var_loc_list *
5500 lookup_decl_loc (tree decl)
5502 return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5505 /* Equate a DIE to a particular declaration. */
5507 static void
5508 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5510 unsigned int decl_id = DECL_UID (decl);
5511 void **slot;
5513 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5514 *slot = decl_die;
5515 decl_die->decl_id = decl_id;
5518 /* Add a variable location node to the linked list for DECL. */
5520 static void
5521 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5523 unsigned int decl_id = DECL_UID (decl);
5524 var_loc_list *temp;
5525 void **slot;
5527 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5528 if (*slot == NULL)
5530 temp = ggc_alloc_cleared (sizeof (var_loc_list));
5531 temp->decl_id = decl_id;
5532 *slot = temp;
5534 else
5535 temp = *slot;
5537 if (temp->last)
5539 /* If the current location is the same as the end of the list,
5540 we have nothing to do. */
5541 if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5542 NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5544 /* Add LOC to the end of list and update LAST. */
5545 temp->last->next = loc;
5546 temp->last = loc;
5549 /* Do not add empty location to the beginning of the list. */
5550 else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5552 temp->first = loc;
5553 temp->last = loc;
5557 /* Keep track of the number of spaces used to indent the
5558 output of the debugging routines that print the structure of
5559 the DIE internal representation. */
5560 static int print_indent;
5562 /* Indent the line the number of spaces given by print_indent. */
5564 static inline void
5565 print_spaces (FILE *outfile)
5567 fprintf (outfile, "%*s", print_indent, "");
5570 /* Print the information associated with a given DIE, and its children.
5571 This routine is a debugging aid only. */
5573 static void
5574 print_die (dw_die_ref die, FILE *outfile)
5576 dw_attr_ref a;
5577 dw_die_ref c;
5579 print_spaces (outfile);
5580 fprintf (outfile, "DIE %4lu: %s\n",
5581 die->die_offset, dwarf_tag_name (die->die_tag));
5582 print_spaces (outfile);
5583 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5584 fprintf (outfile, " offset: %lu\n", die->die_offset);
5586 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5588 print_spaces (outfile);
5589 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5591 switch (AT_class (a))
5593 case dw_val_class_addr:
5594 fprintf (outfile, "address");
5595 break;
5596 case dw_val_class_offset:
5597 fprintf (outfile, "offset");
5598 break;
5599 case dw_val_class_loc:
5600 fprintf (outfile, "location descriptor");
5601 break;
5602 case dw_val_class_loc_list:
5603 fprintf (outfile, "location list -> label:%s",
5604 AT_loc_list (a)->ll_symbol);
5605 break;
5606 case dw_val_class_range_list:
5607 fprintf (outfile, "range list");
5608 break;
5609 case dw_val_class_const:
5610 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5611 break;
5612 case dw_val_class_unsigned_const:
5613 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5614 break;
5615 case dw_val_class_long_long:
5616 fprintf (outfile, "constant (%lu,%lu)",
5617 a->dw_attr_val.v.val_long_long.hi,
5618 a->dw_attr_val.v.val_long_long.low);
5619 break;
5620 case dw_val_class_vec:
5621 fprintf (outfile, "floating-point or vector constant");
5622 break;
5623 case dw_val_class_flag:
5624 fprintf (outfile, "%u", AT_flag (a));
5625 break;
5626 case dw_val_class_die_ref:
5627 if (AT_ref (a) != NULL)
5629 if (AT_ref (a)->die_symbol)
5630 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5631 else
5632 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5634 else
5635 fprintf (outfile, "die -> <null>");
5636 break;
5637 case dw_val_class_lbl_id:
5638 case dw_val_class_lbl_offset:
5639 fprintf (outfile, "label: %s", AT_lbl (a));
5640 break;
5641 case dw_val_class_str:
5642 if (AT_string (a) != NULL)
5643 fprintf (outfile, "\"%s\"", AT_string (a));
5644 else
5645 fprintf (outfile, "<null>");
5646 break;
5647 default:
5648 break;
5651 fprintf (outfile, "\n");
5654 if (die->die_child != NULL)
5656 print_indent += 4;
5657 for (c = die->die_child; c != NULL; c = c->die_sib)
5658 print_die (c, outfile);
5660 print_indent -= 4;
5662 if (print_indent == 0)
5663 fprintf (outfile, "\n");
5666 /* Print the contents of the source code line number correspondence table.
5667 This routine is a debugging aid only. */
5669 static void
5670 print_dwarf_line_table (FILE *outfile)
5672 unsigned i;
5673 dw_line_info_ref line_info;
5675 fprintf (outfile, "\n\nDWARF source line information\n");
5676 for (i = 1; i < line_info_table_in_use; i++)
5678 line_info = &line_info_table[i];
5679 fprintf (outfile, "%5d: ", i);
5680 fprintf (outfile, "%-20s",
5681 VARRAY_CHAR_PTR (file_table, line_info->dw_file_num));
5682 fprintf (outfile, "%6ld", line_info->dw_line_num);
5683 fprintf (outfile, "\n");
5686 fprintf (outfile, "\n\n");
5689 /* Print the information collected for a given DIE. */
5691 void
5692 debug_dwarf_die (dw_die_ref die)
5694 print_die (die, stderr);
5697 /* Print all DWARF information collected for the compilation unit.
5698 This routine is a debugging aid only. */
5700 void
5701 debug_dwarf (void)
5703 print_indent = 0;
5704 print_die (comp_unit_die, stderr);
5705 if (! DWARF2_ASM_LINE_DEBUG_INFO)
5706 print_dwarf_line_table (stderr);
5709 /* We build up the lists of children and attributes by pushing new ones
5710 onto the beginning of the list. Reverse the lists for DIE so that
5711 they are in order of addition. */
5713 static void
5714 reverse_die_lists (dw_die_ref die)
5716 dw_die_ref c, cp, cn;
5717 dw_attr_ref a, ap, an;
5719 for (a = die->die_attr, ap = 0; a; a = an)
5721 an = a->dw_attr_next;
5722 a->dw_attr_next = ap;
5723 ap = a;
5726 die->die_attr = ap;
5728 for (c = die->die_child, cp = 0; c; c = cn)
5730 cn = c->die_sib;
5731 c->die_sib = cp;
5732 cp = c;
5735 die->die_child = cp;
5738 /* reverse_die_lists only reverses the single die you pass it. Since we used to
5739 reverse all dies in add_sibling_attributes, which runs through all the dies,
5740 it would reverse all the dies. Now, however, since we don't call
5741 reverse_die_lists in add_sibling_attributes, we need a routine to
5742 recursively reverse all the dies. This is that routine. */
5744 static void
5745 reverse_all_dies (dw_die_ref die)
5747 dw_die_ref c;
5749 reverse_die_lists (die);
5751 for (c = die->die_child; c; c = c->die_sib)
5752 reverse_all_dies (c);
5755 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5756 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5757 DIE that marks the start of the DIEs for this include file. */
5759 static dw_die_ref
5760 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5762 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5763 dw_die_ref new_unit = gen_compile_unit_die (filename);
5765 new_unit->die_sib = old_unit;
5766 return new_unit;
5769 /* Close an include-file CU and reopen the enclosing one. */
5771 static dw_die_ref
5772 pop_compile_unit (dw_die_ref old_unit)
5774 dw_die_ref new_unit = old_unit->die_sib;
5776 old_unit->die_sib = NULL;
5777 return new_unit;
5780 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5781 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5783 /* Calculate the checksum of a location expression. */
5785 static inline void
5786 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5788 CHECKSUM (loc->dw_loc_opc);
5789 CHECKSUM (loc->dw_loc_oprnd1);
5790 CHECKSUM (loc->dw_loc_oprnd2);
5793 /* Calculate the checksum of an attribute. */
5795 static void
5796 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5798 dw_loc_descr_ref loc;
5799 rtx r;
5801 CHECKSUM (at->dw_attr);
5803 /* We don't care about differences in file numbering. */
5804 if (at->dw_attr == DW_AT_decl_file
5805 /* Or that this was compiled with a different compiler snapshot; if
5806 the output is the same, that's what matters. */
5807 || at->dw_attr == DW_AT_producer)
5808 return;
5810 switch (AT_class (at))
5812 case dw_val_class_const:
5813 CHECKSUM (at->dw_attr_val.v.val_int);
5814 break;
5815 case dw_val_class_unsigned_const:
5816 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5817 break;
5818 case dw_val_class_long_long:
5819 CHECKSUM (at->dw_attr_val.v.val_long_long);
5820 break;
5821 case dw_val_class_vec:
5822 CHECKSUM (at->dw_attr_val.v.val_vec);
5823 break;
5824 case dw_val_class_flag:
5825 CHECKSUM (at->dw_attr_val.v.val_flag);
5826 break;
5827 case dw_val_class_str:
5828 CHECKSUM_STRING (AT_string (at));
5829 break;
5831 case dw_val_class_addr:
5832 r = AT_addr (at);
5833 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5834 CHECKSUM_STRING (XSTR (r, 0));
5835 break;
5837 case dw_val_class_offset:
5838 CHECKSUM (at->dw_attr_val.v.val_offset);
5839 break;
5841 case dw_val_class_loc:
5842 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5843 loc_checksum (loc, ctx);
5844 break;
5846 case dw_val_class_die_ref:
5847 die_checksum (AT_ref (at), ctx, mark);
5848 break;
5850 case dw_val_class_fde_ref:
5851 case dw_val_class_lbl_id:
5852 case dw_val_class_lbl_offset:
5853 break;
5855 default:
5856 break;
5860 /* Calculate the checksum of a DIE. */
5862 static void
5863 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5865 dw_die_ref c;
5866 dw_attr_ref a;
5868 /* To avoid infinite recursion. */
5869 if (die->die_mark)
5871 CHECKSUM (die->die_mark);
5872 return;
5874 die->die_mark = ++(*mark);
5876 CHECKSUM (die->die_tag);
5878 for (a = die->die_attr; a; a = a->dw_attr_next)
5879 attr_checksum (a, ctx, mark);
5881 for (c = die->die_child; c; c = c->die_sib)
5882 die_checksum (c, ctx, mark);
5885 #undef CHECKSUM
5886 #undef CHECKSUM_STRING
5888 /* Do the location expressions look same? */
5889 static inline int
5890 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
5892 return loc1->dw_loc_opc == loc2->dw_loc_opc
5893 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
5894 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
5897 /* Do the values look the same? */
5898 static int
5899 same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
5901 dw_loc_descr_ref loc1, loc2;
5902 rtx r1, r2;
5904 if (v1->val_class != v2->val_class)
5905 return 0;
5907 switch (v1->val_class)
5909 case dw_val_class_const:
5910 return v1->v.val_int == v2->v.val_int;
5911 case dw_val_class_unsigned_const:
5912 return v1->v.val_unsigned == v2->v.val_unsigned;
5913 case dw_val_class_long_long:
5914 return v1->v.val_long_long.hi == v2->v.val_long_long.hi
5915 && v1->v.val_long_long.low == v2->v.val_long_long.low;
5916 case dw_val_class_vec:
5917 if (v1->v.val_vec.length != v2->v.val_vec.length
5918 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
5919 return 0;
5920 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
5921 v1->v.val_vec.length * v1->v.val_vec.elt_size))
5922 return 0;
5923 return 1;
5924 case dw_val_class_flag:
5925 return v1->v.val_flag == v2->v.val_flag;
5926 case dw_val_class_str:
5927 return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
5929 case dw_val_class_addr:
5930 r1 = v1->v.val_addr;
5931 r2 = v2->v.val_addr;
5932 if (GET_CODE (r1) != GET_CODE (r2))
5933 return 0;
5934 gcc_assert (GET_CODE (r1) == SYMBOL_REF);
5935 return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
5937 case dw_val_class_offset:
5938 return v1->v.val_offset == v2->v.val_offset;
5940 case dw_val_class_loc:
5941 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
5942 loc1 && loc2;
5943 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
5944 if (!same_loc_p (loc1, loc2, mark))
5945 return 0;
5946 return !loc1 && !loc2;
5948 case dw_val_class_die_ref:
5949 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
5951 case dw_val_class_fde_ref:
5952 case dw_val_class_lbl_id:
5953 case dw_val_class_lbl_offset:
5954 return 1;
5956 default:
5957 return 1;
5961 /* Do the attributes look the same? */
5963 static int
5964 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
5966 if (at1->dw_attr != at2->dw_attr)
5967 return 0;
5969 /* We don't care about differences in file numbering. */
5970 if (at1->dw_attr == DW_AT_decl_file
5971 /* Or that this was compiled with a different compiler snapshot; if
5972 the output is the same, that's what matters. */
5973 || at1->dw_attr == DW_AT_producer)
5974 return 1;
5976 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
5979 /* Do the dies look the same? */
5981 static int
5982 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
5984 dw_die_ref c1, c2;
5985 dw_attr_ref a1, a2;
5987 /* To avoid infinite recursion. */
5988 if (die1->die_mark)
5989 return die1->die_mark == die2->die_mark;
5990 die1->die_mark = die2->die_mark = ++(*mark);
5992 if (die1->die_tag != die2->die_tag)
5993 return 0;
5995 for (a1 = die1->die_attr, a2 = die2->die_attr;
5996 a1 && a2;
5997 a1 = a1->dw_attr_next, a2 = a2->dw_attr_next)
5998 if (!same_attr_p (a1, a2, mark))
5999 return 0;
6000 if (a1 || a2)
6001 return 0;
6003 for (c1 = die1->die_child, c2 = die2->die_child;
6004 c1 && c2;
6005 c1 = c1->die_sib, c2 = c2->die_sib)
6006 if (!same_die_p (c1, c2, mark))
6007 return 0;
6008 if (c1 || c2)
6009 return 0;
6011 return 1;
6014 /* Do the dies look the same? Wrapper around same_die_p. */
6016 static int
6017 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6019 int mark = 0;
6020 int ret = same_die_p (die1, die2, &mark);
6022 unmark_all_dies (die1);
6023 unmark_all_dies (die2);
6025 return ret;
6028 /* The prefix to attach to symbols on DIEs in the current comdat debug
6029 info section. */
6030 static char *comdat_symbol_id;
6032 /* The index of the current symbol within the current comdat CU. */
6033 static unsigned int comdat_symbol_number;
6035 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6036 children, and set comdat_symbol_id accordingly. */
6038 static void
6039 compute_section_prefix (dw_die_ref unit_die)
6041 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6042 const char *base = die_name ? lbasename (die_name) : "anonymous";
6043 char *name = alloca (strlen (base) + 64);
6044 char *p;
6045 int i, mark;
6046 unsigned char checksum[16];
6047 struct md5_ctx ctx;
6049 /* Compute the checksum of the DIE, then append part of it as hex digits to
6050 the name filename of the unit. */
6052 md5_init_ctx (&ctx);
6053 mark = 0;
6054 die_checksum (unit_die, &ctx, &mark);
6055 unmark_all_dies (unit_die);
6056 md5_finish_ctx (&ctx, checksum);
6058 sprintf (name, "%s.", base);
6059 clean_symbol_name (name);
6061 p = name + strlen (name);
6062 for (i = 0; i < 4; i++)
6064 sprintf (p, "%.2x", checksum[i]);
6065 p += 2;
6068 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6069 comdat_symbol_number = 0;
6072 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6074 static int
6075 is_type_die (dw_die_ref die)
6077 switch (die->die_tag)
6079 case DW_TAG_array_type:
6080 case DW_TAG_class_type:
6081 case DW_TAG_enumeration_type:
6082 case DW_TAG_pointer_type:
6083 case DW_TAG_reference_type:
6084 case DW_TAG_string_type:
6085 case DW_TAG_structure_type:
6086 case DW_TAG_subroutine_type:
6087 case DW_TAG_union_type:
6088 case DW_TAG_ptr_to_member_type:
6089 case DW_TAG_set_type:
6090 case DW_TAG_subrange_type:
6091 case DW_TAG_base_type:
6092 case DW_TAG_const_type:
6093 case DW_TAG_file_type:
6094 case DW_TAG_packed_type:
6095 case DW_TAG_volatile_type:
6096 case DW_TAG_typedef:
6097 return 1;
6098 default:
6099 return 0;
6103 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6104 Basically, we want to choose the bits that are likely to be shared between
6105 compilations (types) and leave out the bits that are specific to individual
6106 compilations (functions). */
6108 static int
6109 is_comdat_die (dw_die_ref c)
6111 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6112 we do for stabs. The advantage is a greater likelihood of sharing between
6113 objects that don't include headers in the same order (and therefore would
6114 put the base types in a different comdat). jason 8/28/00 */
6116 if (c->die_tag == DW_TAG_base_type)
6117 return 0;
6119 if (c->die_tag == DW_TAG_pointer_type
6120 || c->die_tag == DW_TAG_reference_type
6121 || c->die_tag == DW_TAG_const_type
6122 || c->die_tag == DW_TAG_volatile_type)
6124 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6126 return t ? is_comdat_die (t) : 0;
6129 return is_type_die (c);
6132 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6133 compilation unit. */
6135 static int
6136 is_symbol_die (dw_die_ref c)
6138 return (is_type_die (c)
6139 || (get_AT (c, DW_AT_declaration)
6140 && !get_AT (c, DW_AT_specification)));
6143 static char *
6144 gen_internal_sym (const char *prefix)
6146 char buf[256];
6148 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6149 return xstrdup (buf);
6152 /* Assign symbols to all worthy DIEs under DIE. */
6154 static void
6155 assign_symbol_names (dw_die_ref die)
6157 dw_die_ref c;
6159 if (is_symbol_die (die))
6161 if (comdat_symbol_id)
6163 char *p = alloca (strlen (comdat_symbol_id) + 64);
6165 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6166 comdat_symbol_id, comdat_symbol_number++);
6167 die->die_symbol = xstrdup (p);
6169 else
6170 die->die_symbol = gen_internal_sym ("LDIE");
6173 for (c = die->die_child; c != NULL; c = c->die_sib)
6174 assign_symbol_names (c);
6177 struct cu_hash_table_entry
6179 dw_die_ref cu;
6180 unsigned min_comdat_num, max_comdat_num;
6181 struct cu_hash_table_entry *next;
6184 /* Routines to manipulate hash table of CUs. */
6185 static hashval_t
6186 htab_cu_hash (const void *of)
6188 const struct cu_hash_table_entry *entry = of;
6190 return htab_hash_string (entry->cu->die_symbol);
6193 static int
6194 htab_cu_eq (const void *of1, const void *of2)
6196 const struct cu_hash_table_entry *entry1 = of1;
6197 const struct die_struct *entry2 = of2;
6199 return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6202 static void
6203 htab_cu_del (void *what)
6205 struct cu_hash_table_entry *next, *entry = what;
6207 while (entry)
6209 next = entry->next;
6210 free (entry);
6211 entry = next;
6215 /* Check whether we have already seen this CU and set up SYM_NUM
6216 accordingly. */
6217 static int
6218 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6220 struct cu_hash_table_entry dummy;
6221 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6223 dummy.max_comdat_num = 0;
6225 slot = (struct cu_hash_table_entry **)
6226 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6227 INSERT);
6228 entry = *slot;
6230 for (; entry; last = entry, entry = entry->next)
6232 if (same_die_p_wrap (cu, entry->cu))
6233 break;
6236 if (entry)
6238 *sym_num = entry->min_comdat_num;
6239 return 1;
6242 entry = xcalloc (1, sizeof (struct cu_hash_table_entry));
6243 entry->cu = cu;
6244 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6245 entry->next = *slot;
6246 *slot = entry;
6248 return 0;
6251 /* Record SYM_NUM to record of CU in HTABLE. */
6252 static void
6253 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6255 struct cu_hash_table_entry **slot, *entry;
6257 slot = (struct cu_hash_table_entry **)
6258 htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6259 NO_INSERT);
6260 entry = *slot;
6262 entry->max_comdat_num = sym_num;
6265 /* Traverse the DIE (which is always comp_unit_die), and set up
6266 additional compilation units for each of the include files we see
6267 bracketed by BINCL/EINCL. */
6269 static void
6270 break_out_includes (dw_die_ref die)
6272 dw_die_ref *ptr;
6273 dw_die_ref unit = NULL;
6274 limbo_die_node *node, **pnode;
6275 htab_t cu_hash_table;
6277 for (ptr = &(die->die_child); *ptr;)
6279 dw_die_ref c = *ptr;
6281 if (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6282 || (unit && is_comdat_die (c)))
6284 /* This DIE is for a secondary CU; remove it from the main one. */
6285 *ptr = c->die_sib;
6287 if (c->die_tag == DW_TAG_GNU_BINCL)
6289 unit = push_new_compile_unit (unit, c);
6290 free_die (c);
6292 else if (c->die_tag == DW_TAG_GNU_EINCL)
6294 unit = pop_compile_unit (unit);
6295 free_die (c);
6297 else
6298 add_child_die (unit, c);
6300 else
6302 /* Leave this DIE in the main CU. */
6303 ptr = &(c->die_sib);
6304 continue;
6308 #if 0
6309 /* We can only use this in debugging, since the frontend doesn't check
6310 to make sure that we leave every include file we enter. */
6311 gcc_assert (!unit);
6312 #endif
6314 assign_symbol_names (die);
6315 cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6316 for (node = limbo_die_list, pnode = &limbo_die_list;
6317 node;
6318 node = node->next)
6320 int is_dupl;
6322 compute_section_prefix (node->die);
6323 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6324 &comdat_symbol_number);
6325 assign_symbol_names (node->die);
6326 if (is_dupl)
6327 *pnode = node->next;
6328 else
6330 pnode = &node->next;
6331 record_comdat_symbol_number (node->die, cu_hash_table,
6332 comdat_symbol_number);
6335 htab_delete (cu_hash_table);
6338 /* Traverse the DIE and add a sibling attribute if it may have the
6339 effect of speeding up access to siblings. To save some space,
6340 avoid generating sibling attributes for DIE's without children. */
6342 static void
6343 add_sibling_attributes (dw_die_ref die)
6345 dw_die_ref c;
6347 if (die->die_tag != DW_TAG_compile_unit
6348 && die->die_sib && die->die_child != NULL)
6349 /* Add the sibling link to the front of the attribute list. */
6350 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6352 for (c = die->die_child; c != NULL; c = c->die_sib)
6353 add_sibling_attributes (c);
6356 /* Output all location lists for the DIE and its children. */
6358 static void
6359 output_location_lists (dw_die_ref die)
6361 dw_die_ref c;
6362 dw_attr_ref d_attr;
6364 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6365 if (AT_class (d_attr) == dw_val_class_loc_list)
6366 output_loc_list (AT_loc_list (d_attr));
6368 for (c = die->die_child; c != NULL; c = c->die_sib)
6369 output_location_lists (c);
6373 /* The format of each DIE (and its attribute value pairs) is encoded in an
6374 abbreviation table. This routine builds the abbreviation table and assigns
6375 a unique abbreviation id for each abbreviation entry. The children of each
6376 die are visited recursively. */
6378 static void
6379 build_abbrev_table (dw_die_ref die)
6381 unsigned long abbrev_id;
6382 unsigned int n_alloc;
6383 dw_die_ref c;
6384 dw_attr_ref d_attr, a_attr;
6386 /* Scan the DIE references, and mark as external any that refer to
6387 DIEs from other CUs (i.e. those which are not marked). */
6388 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6389 if (AT_class (d_attr) == dw_val_class_die_ref
6390 && AT_ref (d_attr)->die_mark == 0)
6392 gcc_assert (AT_ref (d_attr)->die_symbol);
6394 set_AT_ref_external (d_attr, 1);
6397 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6399 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6401 if (abbrev->die_tag == die->die_tag)
6403 if ((abbrev->die_child != NULL) == (die->die_child != NULL))
6405 a_attr = abbrev->die_attr;
6406 d_attr = die->die_attr;
6408 while (a_attr != NULL && d_attr != NULL)
6410 if ((a_attr->dw_attr != d_attr->dw_attr)
6411 || (value_format (a_attr) != value_format (d_attr)))
6412 break;
6414 a_attr = a_attr->dw_attr_next;
6415 d_attr = d_attr->dw_attr_next;
6418 if (a_attr == NULL && d_attr == NULL)
6419 break;
6424 if (abbrev_id >= abbrev_die_table_in_use)
6426 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6428 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6429 abbrev_die_table = ggc_realloc (abbrev_die_table,
6430 sizeof (dw_die_ref) * n_alloc);
6432 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6433 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6434 abbrev_die_table_allocated = n_alloc;
6437 ++abbrev_die_table_in_use;
6438 abbrev_die_table[abbrev_id] = die;
6441 die->die_abbrev = abbrev_id;
6442 for (c = die->die_child; c != NULL; c = c->die_sib)
6443 build_abbrev_table (c);
6446 /* Return the power-of-two number of bytes necessary to represent VALUE. */
6448 static int
6449 constant_size (long unsigned int value)
6451 int log;
6453 if (value == 0)
6454 log = 0;
6455 else
6456 log = floor_log2 (value);
6458 log = log / 8;
6459 log = 1 << (floor_log2 (log) + 1);
6461 return log;
6464 /* Return the size of a DIE as it is represented in the
6465 .debug_info section. */
6467 static unsigned long
6468 size_of_die (dw_die_ref die)
6470 unsigned long size = 0;
6471 dw_attr_ref a;
6473 size += size_of_uleb128 (die->die_abbrev);
6474 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6476 switch (AT_class (a))
6478 case dw_val_class_addr:
6479 size += DWARF2_ADDR_SIZE;
6480 break;
6481 case dw_val_class_offset:
6482 size += DWARF_OFFSET_SIZE;
6483 break;
6484 case dw_val_class_loc:
6486 unsigned long lsize = size_of_locs (AT_loc (a));
6488 /* Block length. */
6489 size += constant_size (lsize);
6490 size += lsize;
6492 break;
6493 case dw_val_class_loc_list:
6494 size += DWARF_OFFSET_SIZE;
6495 break;
6496 case dw_val_class_range_list:
6497 size += DWARF_OFFSET_SIZE;
6498 break;
6499 case dw_val_class_const:
6500 size += size_of_sleb128 (AT_int (a));
6501 break;
6502 case dw_val_class_unsigned_const:
6503 size += constant_size (AT_unsigned (a));
6504 break;
6505 case dw_val_class_long_long:
6506 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6507 break;
6508 case dw_val_class_vec:
6509 size += 1 + (a->dw_attr_val.v.val_vec.length
6510 * a->dw_attr_val.v.val_vec.elt_size); /* block */
6511 break;
6512 case dw_val_class_flag:
6513 size += 1;
6514 break;
6515 case dw_val_class_die_ref:
6516 if (AT_ref_external (a))
6517 size += DWARF2_ADDR_SIZE;
6518 else
6519 size += DWARF_OFFSET_SIZE;
6520 break;
6521 case dw_val_class_fde_ref:
6522 size += DWARF_OFFSET_SIZE;
6523 break;
6524 case dw_val_class_lbl_id:
6525 size += DWARF2_ADDR_SIZE;
6526 break;
6527 case dw_val_class_lbl_offset:
6528 size += DWARF_OFFSET_SIZE;
6529 break;
6530 case dw_val_class_str:
6531 if (AT_string_form (a) == DW_FORM_strp)
6532 size += DWARF_OFFSET_SIZE;
6533 else
6534 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6535 break;
6536 default:
6537 gcc_unreachable ();
6541 return size;
6544 /* Size the debugging information associated with a given DIE. Visits the
6545 DIE's children recursively. Updates the global variable next_die_offset, on
6546 each time through. Uses the current value of next_die_offset to update the
6547 die_offset field in each DIE. */
6549 static void
6550 calc_die_sizes (dw_die_ref die)
6552 dw_die_ref c;
6554 die->die_offset = next_die_offset;
6555 next_die_offset += size_of_die (die);
6557 for (c = die->die_child; c != NULL; c = c->die_sib)
6558 calc_die_sizes (c);
6560 if (die->die_child != NULL)
6561 /* Count the null byte used to terminate sibling lists. */
6562 next_die_offset += 1;
6565 /* Set the marks for a die and its children. We do this so
6566 that we know whether or not a reference needs to use FORM_ref_addr; only
6567 DIEs in the same CU will be marked. We used to clear out the offset
6568 and use that as the flag, but ran into ordering problems. */
6570 static void
6571 mark_dies (dw_die_ref die)
6573 dw_die_ref c;
6575 gcc_assert (!die->die_mark);
6577 die->die_mark = 1;
6578 for (c = die->die_child; c; c = c->die_sib)
6579 mark_dies (c);
6582 /* Clear the marks for a die and its children. */
6584 static void
6585 unmark_dies (dw_die_ref die)
6587 dw_die_ref c;
6589 gcc_assert (die->die_mark);
6591 die->die_mark = 0;
6592 for (c = die->die_child; c; c = c->die_sib)
6593 unmark_dies (c);
6596 /* Clear the marks for a die, its children and referred dies. */
6598 static void
6599 unmark_all_dies (dw_die_ref die)
6601 dw_die_ref c;
6602 dw_attr_ref a;
6604 if (!die->die_mark)
6605 return;
6606 die->die_mark = 0;
6608 for (c = die->die_child; c; c = c->die_sib)
6609 unmark_all_dies (c);
6611 for (a = die->die_attr; a; a = a->dw_attr_next)
6612 if (AT_class (a) == dw_val_class_die_ref)
6613 unmark_all_dies (AT_ref (a));
6616 /* Return the size of the .debug_pubnames table generated for the
6617 compilation unit. */
6619 static unsigned long
6620 size_of_pubnames (void)
6622 unsigned long size;
6623 unsigned i;
6625 size = DWARF_PUBNAMES_HEADER_SIZE;
6626 for (i = 0; i < pubname_table_in_use; i++)
6628 pubname_ref p = &pubname_table[i];
6629 size += DWARF_OFFSET_SIZE + strlen (p->name) + 1;
6632 size += DWARF_OFFSET_SIZE;
6633 return size;
6636 /* Return the size of the information in the .debug_aranges section. */
6638 static unsigned long
6639 size_of_aranges (void)
6641 unsigned long size;
6643 size = DWARF_ARANGES_HEADER_SIZE;
6645 /* Count the address/length pair for this compilation unit. */
6646 size += 2 * DWARF2_ADDR_SIZE;
6647 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6649 /* Count the two zero words used to terminated the address range table. */
6650 size += 2 * DWARF2_ADDR_SIZE;
6651 return size;
6654 /* Select the encoding of an attribute value. */
6656 static enum dwarf_form
6657 value_format (dw_attr_ref a)
6659 switch (a->dw_attr_val.val_class)
6661 case dw_val_class_addr:
6662 return DW_FORM_addr;
6663 case dw_val_class_range_list:
6664 case dw_val_class_offset:
6665 switch (DWARF_OFFSET_SIZE)
6667 case 4:
6668 return DW_FORM_data4;
6669 case 8:
6670 return DW_FORM_data8;
6671 default:
6672 gcc_unreachable ();
6674 case dw_val_class_loc_list:
6675 /* FIXME: Could be DW_FORM_data8, with a > 32 bit size
6676 .debug_loc section */
6677 return DW_FORM_data4;
6678 case dw_val_class_loc:
6679 switch (constant_size (size_of_locs (AT_loc (a))))
6681 case 1:
6682 return DW_FORM_block1;
6683 case 2:
6684 return DW_FORM_block2;
6685 default:
6686 gcc_unreachable ();
6688 case dw_val_class_const:
6689 return DW_FORM_sdata;
6690 case dw_val_class_unsigned_const:
6691 switch (constant_size (AT_unsigned (a)))
6693 case 1:
6694 return DW_FORM_data1;
6695 case 2:
6696 return DW_FORM_data2;
6697 case 4:
6698 return DW_FORM_data4;
6699 case 8:
6700 return DW_FORM_data8;
6701 default:
6702 gcc_unreachable ();
6704 case dw_val_class_long_long:
6705 return DW_FORM_block1;
6706 case dw_val_class_vec:
6707 return DW_FORM_block1;
6708 case dw_val_class_flag:
6709 return DW_FORM_flag;
6710 case dw_val_class_die_ref:
6711 if (AT_ref_external (a))
6712 return DW_FORM_ref_addr;
6713 else
6714 return DW_FORM_ref;
6715 case dw_val_class_fde_ref:
6716 return DW_FORM_data;
6717 case dw_val_class_lbl_id:
6718 return DW_FORM_addr;
6719 case dw_val_class_lbl_offset:
6720 return DW_FORM_data;
6721 case dw_val_class_str:
6722 return AT_string_form (a);
6724 default:
6725 gcc_unreachable ();
6729 /* Output the encoding of an attribute value. */
6731 static void
6732 output_value_format (dw_attr_ref a)
6734 enum dwarf_form form = value_format (a);
6736 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6739 /* Output the .debug_abbrev section which defines the DIE abbreviation
6740 table. */
6742 static void
6743 output_abbrev_section (void)
6745 unsigned long abbrev_id;
6747 dw_attr_ref a_attr;
6749 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6751 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6753 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6754 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6755 dwarf_tag_name (abbrev->die_tag));
6757 if (abbrev->die_child != NULL)
6758 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6759 else
6760 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6762 for (a_attr = abbrev->die_attr; a_attr != NULL;
6763 a_attr = a_attr->dw_attr_next)
6765 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6766 dwarf_attr_name (a_attr->dw_attr));
6767 output_value_format (a_attr);
6770 dw2_asm_output_data (1, 0, NULL);
6771 dw2_asm_output_data (1, 0, NULL);
6774 /* Terminate the table. */
6775 dw2_asm_output_data (1, 0, NULL);
6778 /* Output a symbol we can use to refer to this DIE from another CU. */
6780 static inline void
6781 output_die_symbol (dw_die_ref die)
6783 char *sym = die->die_symbol;
6785 if (sym == 0)
6786 return;
6788 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6789 /* We make these global, not weak; if the target doesn't support
6790 .linkonce, it doesn't support combining the sections, so debugging
6791 will break. */
6792 targetm.asm_out.globalize_label (asm_out_file, sym);
6794 ASM_OUTPUT_LABEL (asm_out_file, sym);
6797 /* Return a new location list, given the begin and end range, and the
6798 expression. gensym tells us whether to generate a new internal symbol for
6799 this location list node, which is done for the head of the list only. */
6801 static inline dw_loc_list_ref
6802 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6803 const char *section, unsigned int gensym)
6805 dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
6807 retlist->begin = begin;
6808 retlist->end = end;
6809 retlist->expr = expr;
6810 retlist->section = section;
6811 if (gensym)
6812 retlist->ll_symbol = gen_internal_sym ("LLST");
6814 return retlist;
6817 /* Add a location description expression to a location list. */
6819 static inline void
6820 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6821 const char *begin, const char *end,
6822 const char *section)
6824 dw_loc_list_ref *d;
6826 /* Find the end of the chain. */
6827 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
6830 /* Add a new location list node to the list. */
6831 *d = new_loc_list (descr, begin, end, section, 0);
6834 static void
6835 dwarf2out_switch_text_section (void)
6837 dw_fde_ref fde;
6839 gcc_assert (cfun);
6841 fde = &fde_table[fde_table_in_use - 1];
6842 fde->dw_fde_switched_sections = true;
6843 fde->dw_fde_hot_section_label = cfun->hot_section_label;
6844 fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
6845 fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
6846 fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
6847 separate_line_info_table_in_use++;
6850 /* Output the location list given to us. */
6852 static void
6853 output_loc_list (dw_loc_list_ref list_head)
6855 dw_loc_list_ref curr = list_head;
6857 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
6859 /* Walk the location list, and output each range + expression. */
6860 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
6862 unsigned long size;
6863 if (separate_line_info_table_in_use == 0)
6865 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
6866 "Location list begin address (%s)",
6867 list_head->ll_symbol);
6868 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
6869 "Location list end address (%s)",
6870 list_head->ll_symbol);
6872 else
6874 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
6875 "Location list begin address (%s)",
6876 list_head->ll_symbol);
6877 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
6878 "Location list end address (%s)",
6879 list_head->ll_symbol);
6881 size = size_of_locs (curr->expr);
6883 /* Output the block length for this list of location operations. */
6884 gcc_assert (size <= 0xffff);
6885 dw2_asm_output_data (2, size, "%s", "Location expression size");
6887 output_loc_sequence (curr->expr);
6890 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6891 "Location list terminator begin (%s)",
6892 list_head->ll_symbol);
6893 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6894 "Location list terminator end (%s)",
6895 list_head->ll_symbol);
6898 /* Output the DIE and its attributes. Called recursively to generate
6899 the definitions of each child DIE. */
6901 static void
6902 output_die (dw_die_ref die)
6904 dw_attr_ref a;
6905 dw_die_ref c;
6906 unsigned long size;
6908 /* If someone in another CU might refer to us, set up a symbol for
6909 them to point to. */
6910 if (die->die_symbol)
6911 output_die_symbol (die);
6913 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
6914 die->die_offset, dwarf_tag_name (die->die_tag));
6916 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6918 const char *name = dwarf_attr_name (a->dw_attr);
6920 switch (AT_class (a))
6922 case dw_val_class_addr:
6923 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
6924 break;
6926 case dw_val_class_offset:
6927 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
6928 "%s", name);
6929 break;
6931 case dw_val_class_range_list:
6933 char *p = strchr (ranges_section_label, '\0');
6935 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
6936 a->dw_attr_val.v.val_offset);
6937 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
6938 "%s", name);
6939 *p = '\0';
6941 break;
6943 case dw_val_class_loc:
6944 size = size_of_locs (AT_loc (a));
6946 /* Output the block length for this list of location operations. */
6947 dw2_asm_output_data (constant_size (size), size, "%s", name);
6949 output_loc_sequence (AT_loc (a));
6950 break;
6952 case dw_val_class_const:
6953 /* ??? It would be slightly more efficient to use a scheme like is
6954 used for unsigned constants below, but gdb 4.x does not sign
6955 extend. Gdb 5.x does sign extend. */
6956 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
6957 break;
6959 case dw_val_class_unsigned_const:
6960 dw2_asm_output_data (constant_size (AT_unsigned (a)),
6961 AT_unsigned (a), "%s", name);
6962 break;
6964 case dw_val_class_long_long:
6966 unsigned HOST_WIDE_INT first, second;
6968 dw2_asm_output_data (1,
6969 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6970 "%s", name);
6972 if (WORDS_BIG_ENDIAN)
6974 first = a->dw_attr_val.v.val_long_long.hi;
6975 second = a->dw_attr_val.v.val_long_long.low;
6977 else
6979 first = a->dw_attr_val.v.val_long_long.low;
6980 second = a->dw_attr_val.v.val_long_long.hi;
6983 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6984 first, "long long constant");
6985 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6986 second, NULL);
6988 break;
6990 case dw_val_class_vec:
6992 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
6993 unsigned int len = a->dw_attr_val.v.val_vec.length;
6994 unsigned int i;
6995 unsigned char *p;
6997 dw2_asm_output_data (1, len * elt_size, "%s", name);
6998 if (elt_size > sizeof (HOST_WIDE_INT))
7000 elt_size /= 2;
7001 len *= 2;
7003 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7004 i < len;
7005 i++, p += elt_size)
7006 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7007 "fp or vector constant word %u", i);
7008 break;
7011 case dw_val_class_flag:
7012 dw2_asm_output_data (1, AT_flag (a), "%s", name);
7013 break;
7015 case dw_val_class_loc_list:
7017 char *sym = AT_loc_list (a)->ll_symbol;
7019 gcc_assert (sym);
7020 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, "%s", name);
7022 break;
7024 case dw_val_class_die_ref:
7025 if (AT_ref_external (a))
7027 char *sym = AT_ref (a)->die_symbol;
7029 gcc_assert (sym);
7030 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, "%s", name);
7032 else
7034 gcc_assert (AT_ref (a)->die_offset);
7035 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7036 "%s", name);
7038 break;
7040 case dw_val_class_fde_ref:
7042 char l1[20];
7044 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7045 a->dw_attr_val.v.val_fde_index * 2);
7046 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, "%s", name);
7048 break;
7050 case dw_val_class_lbl_id:
7051 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7052 break;
7054 case dw_val_class_lbl_offset:
7055 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a), "%s", name);
7056 break;
7058 case dw_val_class_str:
7059 if (AT_string_form (a) == DW_FORM_strp)
7060 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7061 a->dw_attr_val.v.val_str->label,
7062 "%s: \"%s\"", name, AT_string (a));
7063 else
7064 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7065 break;
7067 default:
7068 gcc_unreachable ();
7072 for (c = die->die_child; c != NULL; c = c->die_sib)
7073 output_die (c);
7075 /* Add null byte to terminate sibling list. */
7076 if (die->die_child != NULL)
7077 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7078 die->die_offset);
7081 /* Output the compilation unit that appears at the beginning of the
7082 .debug_info section, and precedes the DIE descriptions. */
7084 static void
7085 output_compilation_unit_header (void)
7087 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7088 dw2_asm_output_data (4, 0xffffffff,
7089 "Initial length escape value indicating 64-bit DWARF extension");
7090 dw2_asm_output_data (DWARF_OFFSET_SIZE,
7091 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7092 "Length of Compilation Unit Info");
7093 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7094 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7095 "Offset Into Abbrev. Section");
7096 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7099 /* Output the compilation unit DIE and its children. */
7101 static void
7102 output_comp_unit (dw_die_ref die, int output_if_empty)
7104 const char *secname;
7105 char *oldsym, *tmp;
7107 /* Unless we are outputting main CU, we may throw away empty ones. */
7108 if (!output_if_empty && die->die_child == NULL)
7109 return;
7111 /* Even if there are no children of this DIE, we must output the information
7112 about the compilation unit. Otherwise, on an empty translation unit, we
7113 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
7114 will then complain when examining the file. First mark all the DIEs in
7115 this CU so we know which get local refs. */
7116 mark_dies (die);
7118 build_abbrev_table (die);
7120 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
7121 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7122 calc_die_sizes (die);
7124 oldsym = die->die_symbol;
7125 if (oldsym)
7127 tmp = alloca (strlen (oldsym) + 24);
7129 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7130 secname = tmp;
7131 die->die_symbol = NULL;
7133 else
7134 secname = (const char *) DEBUG_INFO_SECTION;
7136 /* Output debugging information. */
7137 named_section_flags (secname, SECTION_DEBUG);
7138 output_compilation_unit_header ();
7139 output_die (die);
7141 /* Leave the marks on the main CU, so we can check them in
7142 output_pubnames. */
7143 if (oldsym)
7145 unmark_dies (die);
7146 die->die_symbol = oldsym;
7150 /* The DWARF2 pubname for a nested thingy looks like "A::f". The
7151 output of lang_hooks.decl_printable_name for C++ looks like
7152 "A::f(int)". Let's drop the argument list, and maybe the scope. */
7154 static const char *
7155 dwarf2_name (tree decl, int scope)
7157 return lang_hooks.decl_printable_name (decl, scope ? 1 : 0);
7160 /* Add a new entry to .debug_pubnames if appropriate. */
7162 static void
7163 add_pubname (tree decl, dw_die_ref die)
7165 pubname_ref p;
7167 if (! TREE_PUBLIC (decl))
7168 return;
7170 if (pubname_table_in_use == pubname_table_allocated)
7172 pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
7173 pubname_table
7174 = ggc_realloc (pubname_table,
7175 (pubname_table_allocated * sizeof (pubname_entry)));
7176 memset (pubname_table + pubname_table_in_use, 0,
7177 PUBNAME_TABLE_INCREMENT * sizeof (pubname_entry));
7180 p = &pubname_table[pubname_table_in_use++];
7181 p->die = die;
7182 p->name = xstrdup (dwarf2_name (decl, 1));
7185 /* Output the public names table used to speed up access to externally
7186 visible names. For now, only generate entries for externally
7187 visible procedures. */
7189 static void
7190 output_pubnames (void)
7192 unsigned i;
7193 unsigned long pubnames_length = size_of_pubnames ();
7195 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7196 dw2_asm_output_data (4, 0xffffffff,
7197 "Initial length escape value indicating 64-bit DWARF extension");
7198 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7199 "Length of Public Names Info");
7200 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7201 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7202 "Offset of Compilation Unit Info");
7203 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7204 "Compilation Unit Length");
7206 for (i = 0; i < pubname_table_in_use; i++)
7208 pubname_ref pub = &pubname_table[i];
7210 /* We shouldn't see pubnames for DIEs outside of the main CU. */
7211 gcc_assert (pub->die->die_mark);
7213 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7214 "DIE offset");
7216 dw2_asm_output_nstring (pub->name, -1, "external name");
7219 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7222 /* Add a new entry to .debug_aranges if appropriate. */
7224 static void
7225 add_arange (tree decl, dw_die_ref die)
7227 if (! DECL_SECTION_NAME (decl))
7228 return;
7230 if (arange_table_in_use == arange_table_allocated)
7232 arange_table_allocated += ARANGE_TABLE_INCREMENT;
7233 arange_table = ggc_realloc (arange_table,
7234 (arange_table_allocated
7235 * sizeof (dw_die_ref)));
7236 memset (arange_table + arange_table_in_use, 0,
7237 ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7240 arange_table[arange_table_in_use++] = die;
7243 /* Output the information that goes into the .debug_aranges table.
7244 Namely, define the beginning and ending address range of the
7245 text section generated for this compilation unit. */
7247 static void
7248 output_aranges (void)
7250 unsigned i;
7251 unsigned long aranges_length = size_of_aranges ();
7253 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7254 dw2_asm_output_data (4, 0xffffffff,
7255 "Initial length escape value indicating 64-bit DWARF extension");
7256 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7257 "Length of Address Ranges Info");
7258 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7259 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7260 "Offset of Compilation Unit Info");
7261 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7262 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7264 /* We need to align to twice the pointer size here. */
7265 if (DWARF_ARANGES_PAD_SIZE)
7267 /* Pad using a 2 byte words so that padding is correct for any
7268 pointer size. */
7269 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7270 2 * DWARF2_ADDR_SIZE);
7271 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7272 dw2_asm_output_data (2, 0, NULL);
7275 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7276 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7277 text_section_label, "Length");
7278 if (flag_reorder_blocks_and_partition)
7280 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7281 "Address");
7282 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7283 cold_text_section_label, "Length");
7286 for (i = 0; i < arange_table_in_use; i++)
7288 dw_die_ref die = arange_table[i];
7290 /* We shouldn't see aranges for DIEs outside of the main CU. */
7291 gcc_assert (die->die_mark);
7293 if (die->die_tag == DW_TAG_subprogram)
7295 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7296 "Address");
7297 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7298 get_AT_low_pc (die), "Length");
7300 else
7302 /* A static variable; extract the symbol from DW_AT_location.
7303 Note that this code isn't currently hit, as we only emit
7304 aranges for functions (jason 9/23/99). */
7305 dw_attr_ref a = get_AT (die, DW_AT_location);
7306 dw_loc_descr_ref loc;
7308 gcc_assert (a && AT_class (a) == dw_val_class_loc);
7310 loc = AT_loc (a);
7311 gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7313 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7314 loc->dw_loc_oprnd1.v.val_addr, "Address");
7315 dw2_asm_output_data (DWARF2_ADDR_SIZE,
7316 get_AT_unsigned (die, DW_AT_byte_size),
7317 "Length");
7321 /* Output the terminator words. */
7322 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7323 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7326 /* Add a new entry to .debug_ranges. Return the offset at which it
7327 was placed. */
7329 static unsigned int
7330 add_ranges (tree block)
7332 unsigned int in_use = ranges_table_in_use;
7334 if (in_use == ranges_table_allocated)
7336 ranges_table_allocated += RANGES_TABLE_INCREMENT;
7337 ranges_table
7338 = ggc_realloc (ranges_table, (ranges_table_allocated
7339 * sizeof (struct dw_ranges_struct)));
7340 memset (ranges_table + ranges_table_in_use, 0,
7341 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7344 ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7345 ranges_table_in_use = in_use + 1;
7347 return in_use * 2 * DWARF2_ADDR_SIZE;
7350 static void
7351 output_ranges (void)
7353 unsigned i;
7354 static const char *const start_fmt = "Offset 0x%x";
7355 const char *fmt = start_fmt;
7357 for (i = 0; i < ranges_table_in_use; i++)
7359 int block_num = ranges_table[i].block_num;
7361 if (block_num)
7363 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7364 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7366 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7367 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7369 /* If all code is in the text section, then the compilation
7370 unit base address defaults to DW_AT_low_pc, which is the
7371 base of the text section. */
7372 if (separate_line_info_table_in_use == 0)
7374 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7375 text_section_label,
7376 fmt, i * 2 * DWARF2_ADDR_SIZE);
7377 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7378 text_section_label, NULL);
7381 /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7382 compilation unit base address to zero, which allows us to
7383 use absolute addresses, and not worry about whether the
7384 target supports cross-section arithmetic. */
7385 else
7387 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7388 fmt, i * 2 * DWARF2_ADDR_SIZE);
7389 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7392 fmt = NULL;
7394 else
7396 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7397 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7398 fmt = start_fmt;
7403 /* Data structure containing information about input files. */
7404 struct file_info
7406 char *path; /* Complete file name. */
7407 char *fname; /* File name part. */
7408 int length; /* Length of entire string. */
7409 int file_idx; /* Index in input file table. */
7410 int dir_idx; /* Index in directory table. */
7413 /* Data structure containing information about directories with source
7414 files. */
7415 struct dir_info
7417 char *path; /* Path including directory name. */
7418 int length; /* Path length. */
7419 int prefix; /* Index of directory entry which is a prefix. */
7420 int count; /* Number of files in this directory. */
7421 int dir_idx; /* Index of directory used as base. */
7422 int used; /* Used in the end? */
7425 /* Callback function for file_info comparison. We sort by looking at
7426 the directories in the path. */
7428 static int
7429 file_info_cmp (const void *p1, const void *p2)
7431 const struct file_info *s1 = p1;
7432 const struct file_info *s2 = p2;
7433 unsigned char *cp1;
7434 unsigned char *cp2;
7436 /* Take care of file names without directories. We need to make sure that
7437 we return consistent values to qsort since some will get confused if
7438 we return the same value when identical operands are passed in opposite
7439 orders. So if neither has a directory, return 0 and otherwise return
7440 1 or -1 depending on which one has the directory. */
7441 if ((s1->path == s1->fname || s2->path == s2->fname))
7442 return (s2->path == s2->fname) - (s1->path == s1->fname);
7444 cp1 = (unsigned char *) s1->path;
7445 cp2 = (unsigned char *) s2->path;
7447 while (1)
7449 ++cp1;
7450 ++cp2;
7451 /* Reached the end of the first path? If so, handle like above. */
7452 if ((cp1 == (unsigned char *) s1->fname)
7453 || (cp2 == (unsigned char *) s2->fname))
7454 return ((cp2 == (unsigned char *) s2->fname)
7455 - (cp1 == (unsigned char *) s1->fname));
7457 /* Character of current path component the same? */
7458 else if (*cp1 != *cp2)
7459 return *cp1 - *cp2;
7463 /* Output the directory table and the file name table. We try to minimize
7464 the total amount of memory needed. A heuristic is used to avoid large
7465 slowdowns with many input files. */
7467 static void
7468 output_file_names (void)
7470 struct file_info *files;
7471 struct dir_info *dirs;
7472 int *saved;
7473 int *savehere;
7474 int *backmap;
7475 size_t ndirs;
7476 int idx_offset;
7477 size_t i;
7478 int idx;
7480 /* Handle the case where file_table is empty. */
7481 if (VARRAY_ACTIVE_SIZE (file_table) <= 1)
7483 dw2_asm_output_data (1, 0, "End directory table");
7484 dw2_asm_output_data (1, 0, "End file name table");
7485 return;
7488 /* Allocate the various arrays we need. */
7489 files = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct file_info));
7490 dirs = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct dir_info));
7492 /* Sort the file names. */
7493 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7495 char *f;
7497 /* Skip all leading "./". */
7498 f = VARRAY_CHAR_PTR (file_table, i);
7499 while (f[0] == '.' && f[1] == '/')
7500 f += 2;
7502 /* Create a new array entry. */
7503 files[i].path = f;
7504 files[i].length = strlen (f);
7505 files[i].file_idx = i;
7507 /* Search for the file name part. */
7508 f = strrchr (f, '/');
7509 files[i].fname = f == NULL ? files[i].path : f + 1;
7512 qsort (files + 1, VARRAY_ACTIVE_SIZE (file_table) - 1,
7513 sizeof (files[0]), file_info_cmp);
7515 /* Find all the different directories used. */
7516 dirs[0].path = files[1].path;
7517 dirs[0].length = files[1].fname - files[1].path;
7518 dirs[0].prefix = -1;
7519 dirs[0].count = 1;
7520 dirs[0].dir_idx = 0;
7521 dirs[0].used = 0;
7522 files[1].dir_idx = 0;
7523 ndirs = 1;
7525 for (i = 2; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7526 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7527 && memcmp (dirs[ndirs - 1].path, files[i].path,
7528 dirs[ndirs - 1].length) == 0)
7530 /* Same directory as last entry. */
7531 files[i].dir_idx = ndirs - 1;
7532 ++dirs[ndirs - 1].count;
7534 else
7536 size_t j;
7538 /* This is a new directory. */
7539 dirs[ndirs].path = files[i].path;
7540 dirs[ndirs].length = files[i].fname - files[i].path;
7541 dirs[ndirs].count = 1;
7542 dirs[ndirs].dir_idx = ndirs;
7543 dirs[ndirs].used = 0;
7544 files[i].dir_idx = ndirs;
7546 /* Search for a prefix. */
7547 dirs[ndirs].prefix = -1;
7548 for (j = 0; j < ndirs; j++)
7549 if (dirs[j].length < dirs[ndirs].length
7550 && dirs[j].length > 1
7551 && (dirs[ndirs].prefix == -1
7552 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7553 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7554 dirs[ndirs].prefix = j;
7556 ++ndirs;
7559 /* Now to the actual work. We have to find a subset of the directories which
7560 allow expressing the file name using references to the directory table
7561 with the least amount of characters. We do not do an exhaustive search
7562 where we would have to check out every combination of every single
7563 possible prefix. Instead we use a heuristic which provides nearly optimal
7564 results in most cases and never is much off. */
7565 saved = alloca (ndirs * sizeof (int));
7566 savehere = alloca (ndirs * sizeof (int));
7568 memset (saved, '\0', ndirs * sizeof (saved[0]));
7569 for (i = 0; i < ndirs; i++)
7571 size_t j;
7572 int total;
7574 /* We can always save some space for the current directory. But this
7575 does not mean it will be enough to justify adding the directory. */
7576 savehere[i] = dirs[i].length;
7577 total = (savehere[i] - saved[i]) * dirs[i].count;
7579 for (j = i + 1; j < ndirs; j++)
7581 savehere[j] = 0;
7582 if (saved[j] < dirs[i].length)
7584 /* Determine whether the dirs[i] path is a prefix of the
7585 dirs[j] path. */
7586 int k;
7588 k = dirs[j].prefix;
7589 while (k != -1 && k != (int) i)
7590 k = dirs[k].prefix;
7592 if (k == (int) i)
7594 /* Yes it is. We can possibly safe some memory but
7595 writing the filenames in dirs[j] relative to
7596 dirs[i]. */
7597 savehere[j] = dirs[i].length;
7598 total += (savehere[j] - saved[j]) * dirs[j].count;
7603 /* Check whether we can safe enough to justify adding the dirs[i]
7604 directory. */
7605 if (total > dirs[i].length + 1)
7607 /* It's worthwhile adding. */
7608 for (j = i; j < ndirs; j++)
7609 if (savehere[j] > 0)
7611 /* Remember how much we saved for this directory so far. */
7612 saved[j] = savehere[j];
7614 /* Remember the prefix directory. */
7615 dirs[j].dir_idx = i;
7620 /* We have to emit them in the order they appear in the file_table array
7621 since the index is used in the debug info generation. To do this
7622 efficiently we generate a back-mapping of the indices first. */
7623 backmap = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (int));
7624 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7626 backmap[files[i].file_idx] = i;
7628 /* Mark this directory as used. */
7629 dirs[dirs[files[i].dir_idx].dir_idx].used = 1;
7632 /* That was it. We are ready to emit the information. First emit the
7633 directory name table. We have to make sure the first actually emitted
7634 directory name has index one; zero is reserved for the current working
7635 directory. Make sure we do not confuse these indices with the one for the
7636 constructed table (even though most of the time they are identical). */
7637 idx = 1;
7638 idx_offset = dirs[0].length > 0 ? 1 : 0;
7639 for (i = 1 - idx_offset; i < ndirs; i++)
7640 if (dirs[i].used != 0)
7642 dirs[i].used = idx++;
7643 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7644 "Directory Entry: 0x%x", dirs[i].used);
7647 dw2_asm_output_data (1, 0, "End directory table");
7649 /* Correct the index for the current working directory entry if it
7650 exists. */
7651 if (idx_offset == 0)
7652 dirs[0].used = 0;
7654 /* Now write all the file names. */
7655 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7657 int file_idx = backmap[i];
7658 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7660 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7661 "File Entry: 0x%lx", (unsigned long) i);
7663 /* Include directory index. */
7664 dw2_asm_output_data_uleb128 (dirs[dir_idx].used, NULL);
7666 /* Modification time. */
7667 dw2_asm_output_data_uleb128 (0, NULL);
7669 /* File length in bytes. */
7670 dw2_asm_output_data_uleb128 (0, NULL);
7673 dw2_asm_output_data (1, 0, "End file name table");
7677 /* Output the source line number correspondence information. This
7678 information goes into the .debug_line section. */
7680 static void
7681 output_line_info (void)
7683 char l1[20], l2[20], p1[20], p2[20];
7684 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7685 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7686 unsigned opc;
7687 unsigned n_op_args;
7688 unsigned long lt_index;
7689 unsigned long current_line;
7690 long line_offset;
7691 long line_delta;
7692 unsigned long current_file;
7693 unsigned long function;
7695 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7696 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7697 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7698 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7700 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7701 dw2_asm_output_data (4, 0xffffffff,
7702 "Initial length escape value indicating 64-bit DWARF extension");
7703 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7704 "Length of Source Line Info");
7705 ASM_OUTPUT_LABEL (asm_out_file, l1);
7707 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7708 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7709 ASM_OUTPUT_LABEL (asm_out_file, p1);
7711 /* Define the architecture-dependent minimum instruction length (in
7712 bytes). In this implementation of DWARF, this field is used for
7713 information purposes only. Since GCC generates assembly language,
7714 we have no a priori knowledge of how many instruction bytes are
7715 generated for each source line, and therefore can use only the
7716 DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7717 commands. Accordingly, we fix this as `1', which is "correct
7718 enough" for all architectures, and don't let the target override. */
7719 dw2_asm_output_data (1, 1,
7720 "Minimum Instruction Length");
7722 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7723 "Default is_stmt_start flag");
7724 dw2_asm_output_data (1, DWARF_LINE_BASE,
7725 "Line Base Value (Special Opcodes)");
7726 dw2_asm_output_data (1, DWARF_LINE_RANGE,
7727 "Line Range Value (Special Opcodes)");
7728 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7729 "Special Opcode Base");
7731 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7733 switch (opc)
7735 case DW_LNS_advance_pc:
7736 case DW_LNS_advance_line:
7737 case DW_LNS_set_file:
7738 case DW_LNS_set_column:
7739 case DW_LNS_fixed_advance_pc:
7740 n_op_args = 1;
7741 break;
7742 default:
7743 n_op_args = 0;
7744 break;
7747 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
7748 opc, n_op_args);
7751 /* Write out the information about the files we use. */
7752 output_file_names ();
7753 ASM_OUTPUT_LABEL (asm_out_file, p2);
7755 /* We used to set the address register to the first location in the text
7756 section here, but that didn't accomplish anything since we already
7757 have a line note for the opening brace of the first function. */
7759 /* Generate the line number to PC correspondence table, encoded as
7760 a series of state machine operations. */
7761 current_file = 1;
7762 current_line = 1;
7764 if (cfun
7765 && (last_text_section == in_unlikely_executed_text
7766 || (last_text_section == in_named
7767 && last_text_section_name == cfun->unlikely_text_section_name)))
7768 strcpy (prev_line_label, cfun->cold_section_label);
7769 else
7770 strcpy (prev_line_label, text_section_label);
7771 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
7773 dw_line_info_ref line_info = &line_info_table[lt_index];
7775 #if 0
7776 /* Disable this optimization for now; GDB wants to see two line notes
7777 at the beginning of a function so it can find the end of the
7778 prologue. */
7780 /* Don't emit anything for redundant notes. Just updating the
7781 address doesn't accomplish anything, because we already assume
7782 that anything after the last address is this line. */
7783 if (line_info->dw_line_num == current_line
7784 && line_info->dw_file_num == current_file)
7785 continue;
7786 #endif
7788 /* Emit debug info for the address of the current line.
7790 Unfortunately, we have little choice here currently, and must always
7791 use the most general form. GCC does not know the address delta
7792 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
7793 attributes which will give an upper bound on the address range. We
7794 could perhaps use length attributes to determine when it is safe to
7795 use DW_LNS_fixed_advance_pc. */
7797 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
7798 if (0)
7800 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
7801 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7802 "DW_LNS_fixed_advance_pc");
7803 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7805 else
7807 /* This can handle any delta. This takes
7808 4+DWARF2_ADDR_SIZE bytes. */
7809 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7810 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7811 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7812 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7815 strcpy (prev_line_label, line_label);
7817 /* Emit debug info for the source file of the current line, if
7818 different from the previous line. */
7819 if (line_info->dw_file_num != current_file)
7821 current_file = line_info->dw_file_num;
7822 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7823 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7824 VARRAY_CHAR_PTR (file_table,
7825 current_file));
7828 /* Emit debug info for the current line number, choosing the encoding
7829 that uses the least amount of space. */
7830 if (line_info->dw_line_num != current_line)
7832 line_offset = line_info->dw_line_num - current_line;
7833 line_delta = line_offset - DWARF_LINE_BASE;
7834 current_line = line_info->dw_line_num;
7835 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7836 /* This can handle deltas from -10 to 234, using the current
7837 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
7838 takes 1 byte. */
7839 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7840 "line %lu", current_line);
7841 else
7843 /* This can handle any delta. This takes at least 4 bytes,
7844 depending on the value being encoded. */
7845 dw2_asm_output_data (1, DW_LNS_advance_line,
7846 "advance to line %lu", current_line);
7847 dw2_asm_output_data_sleb128 (line_offset, NULL);
7848 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7851 else
7852 /* We still need to start a new row, so output a copy insn. */
7853 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7856 /* Emit debug info for the address of the end of the function. */
7857 if (0)
7859 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7860 "DW_LNS_fixed_advance_pc");
7861 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
7863 else
7865 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7866 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7867 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7868 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
7871 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7872 dw2_asm_output_data_uleb128 (1, NULL);
7873 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7875 function = 0;
7876 current_file = 1;
7877 current_line = 1;
7878 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
7880 dw_separate_line_info_ref line_info
7881 = &separate_line_info_table[lt_index];
7883 #if 0
7884 /* Don't emit anything for redundant notes. */
7885 if (line_info->dw_line_num == current_line
7886 && line_info->dw_file_num == current_file
7887 && line_info->function == function)
7888 goto cont;
7889 #endif
7891 /* Emit debug info for the address of the current line. If this is
7892 a new function, or the first line of a function, then we need
7893 to handle it differently. */
7894 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
7895 lt_index);
7896 if (function != line_info->function)
7898 function = line_info->function;
7900 /* Set the address register to the first line in the function. */
7901 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7902 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7903 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7904 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7906 else
7908 /* ??? See the DW_LNS_advance_pc comment above. */
7909 if (0)
7911 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7912 "DW_LNS_fixed_advance_pc");
7913 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7915 else
7917 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7918 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7919 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7920 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7924 strcpy (prev_line_label, line_label);
7926 /* Emit debug info for the source file of the current line, if
7927 different from the previous line. */
7928 if (line_info->dw_file_num != current_file)
7930 current_file = line_info->dw_file_num;
7931 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7932 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7933 VARRAY_CHAR_PTR (file_table,
7934 current_file));
7937 /* Emit debug info for the current line number, choosing the encoding
7938 that uses the least amount of space. */
7939 if (line_info->dw_line_num != current_line)
7941 line_offset = line_info->dw_line_num - current_line;
7942 line_delta = line_offset - DWARF_LINE_BASE;
7943 current_line = line_info->dw_line_num;
7944 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7945 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7946 "line %lu", current_line);
7947 else
7949 dw2_asm_output_data (1, DW_LNS_advance_line,
7950 "advance to line %lu", current_line);
7951 dw2_asm_output_data_sleb128 (line_offset, NULL);
7952 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7955 else
7956 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7958 #if 0
7959 cont:
7960 #endif
7962 lt_index++;
7964 /* If we're done with a function, end its sequence. */
7965 if (lt_index == separate_line_info_table_in_use
7966 || separate_line_info_table[lt_index].function != function)
7968 current_file = 1;
7969 current_line = 1;
7971 /* Emit debug info for the address of the end of the function. */
7972 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
7973 if (0)
7975 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7976 "DW_LNS_fixed_advance_pc");
7977 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7979 else
7981 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7982 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7983 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7984 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7987 /* Output the marker for the end of this sequence. */
7988 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7989 dw2_asm_output_data_uleb128 (1, NULL);
7990 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7994 /* Output the marker for the end of the line number info. */
7995 ASM_OUTPUT_LABEL (asm_out_file, l2);
7998 /* Given a pointer to a tree node for some base type, return a pointer to
7999 a DIE that describes the given type.
8001 This routine must only be called for GCC type nodes that correspond to
8002 Dwarf base (fundamental) types. */
8004 static dw_die_ref
8005 base_type_die (tree type)
8007 dw_die_ref base_type_result;
8008 const char *type_name;
8009 enum dwarf_type encoding;
8010 tree name = TYPE_NAME (type);
8012 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8013 return 0;
8015 if (name)
8017 if (TREE_CODE (name) == TYPE_DECL)
8018 name = DECL_NAME (name);
8020 type_name = IDENTIFIER_POINTER (name);
8022 else
8023 type_name = "__unknown__";
8025 switch (TREE_CODE (type))
8027 case INTEGER_TYPE:
8028 /* Carefully distinguish the C character types, without messing
8029 up if the language is not C. Note that we check only for the names
8030 that contain spaces; other names might occur by coincidence in other
8031 languages. */
8032 if (! (TYPE_PRECISION (type) == CHAR_TYPE_SIZE
8033 && (type == char_type_node
8034 || ! strcmp (type_name, "signed char")
8035 || ! strcmp (type_name, "unsigned char"))))
8037 if (TYPE_UNSIGNED (type))
8038 encoding = DW_ATE_unsigned;
8039 else
8040 encoding = DW_ATE_signed;
8041 break;
8043 /* else fall through. */
8045 case CHAR_TYPE:
8046 /* GNU Pascal/Ada CHAR type. Not used in C. */
8047 if (TYPE_UNSIGNED (type))
8048 encoding = DW_ATE_unsigned_char;
8049 else
8050 encoding = DW_ATE_signed_char;
8051 break;
8053 case REAL_TYPE:
8054 encoding = DW_ATE_float;
8055 break;
8057 /* Dwarf2 doesn't know anything about complex ints, so use
8058 a user defined type for it. */
8059 case COMPLEX_TYPE:
8060 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8061 encoding = DW_ATE_complex_float;
8062 else
8063 encoding = DW_ATE_lo_user;
8064 break;
8066 case BOOLEAN_TYPE:
8067 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
8068 encoding = DW_ATE_boolean;
8069 break;
8071 default:
8072 /* No other TREE_CODEs are Dwarf fundamental types. */
8073 gcc_unreachable ();
8076 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8077 if (demangle_name_func)
8078 type_name = (*demangle_name_func) (type_name);
8080 add_AT_string (base_type_result, DW_AT_name, type_name);
8081 add_AT_unsigned (base_type_result, DW_AT_byte_size,
8082 int_size_in_bytes (type));
8083 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8085 return base_type_result;
8088 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
8089 the Dwarf "root" type for the given input type. The Dwarf "root" type of
8090 a given type is generally the same as the given type, except that if the
8091 given type is a pointer or reference type, then the root type of the given
8092 type is the root type of the "basis" type for the pointer or reference
8093 type. (This definition of the "root" type is recursive.) Also, the root
8094 type of a `const' qualified type or a `volatile' qualified type is the
8095 root type of the given type without the qualifiers. */
8097 static tree
8098 root_type (tree type)
8100 if (TREE_CODE (type) == ERROR_MARK)
8101 return error_mark_node;
8103 switch (TREE_CODE (type))
8105 case ERROR_MARK:
8106 return error_mark_node;
8108 case POINTER_TYPE:
8109 case REFERENCE_TYPE:
8110 return type_main_variant (root_type (TREE_TYPE (type)));
8112 default:
8113 return type_main_variant (type);
8117 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8118 given input type is a Dwarf "fundamental" type. Otherwise return null. */
8120 static inline int
8121 is_base_type (tree type)
8123 switch (TREE_CODE (type))
8125 case ERROR_MARK:
8126 case VOID_TYPE:
8127 case INTEGER_TYPE:
8128 case REAL_TYPE:
8129 case COMPLEX_TYPE:
8130 case BOOLEAN_TYPE:
8131 case CHAR_TYPE:
8132 return 1;
8134 case ARRAY_TYPE:
8135 case RECORD_TYPE:
8136 case UNION_TYPE:
8137 case QUAL_UNION_TYPE:
8138 case ENUMERAL_TYPE:
8139 case FUNCTION_TYPE:
8140 case METHOD_TYPE:
8141 case POINTER_TYPE:
8142 case REFERENCE_TYPE:
8143 case OFFSET_TYPE:
8144 case LANG_TYPE:
8145 case VECTOR_TYPE:
8146 return 0;
8148 default:
8149 gcc_unreachable ();
8152 return 0;
8155 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8156 node, return the size in bits for the type if it is a constant, or else
8157 return the alignment for the type if the type's size is not constant, or
8158 else return BITS_PER_WORD if the type actually turns out to be an
8159 ERROR_MARK node. */
8161 static inline unsigned HOST_WIDE_INT
8162 simple_type_size_in_bits (tree type)
8164 if (TREE_CODE (type) == ERROR_MARK)
8165 return BITS_PER_WORD;
8166 else if (TYPE_SIZE (type) == NULL_TREE)
8167 return 0;
8168 else if (host_integerp (TYPE_SIZE (type), 1))
8169 return tree_low_cst (TYPE_SIZE (type), 1);
8170 else
8171 return TYPE_ALIGN (type);
8174 /* Return true if the debug information for the given type should be
8175 emitted as a subrange type. */
8177 static inline bool
8178 is_subrange_type (tree type)
8180 tree subtype = TREE_TYPE (type);
8182 /* Subrange types are identified by the fact that they are integer
8183 types, and that they have a subtype which is either an integer type
8184 or an enumeral type. */
8186 if (TREE_CODE (type) != INTEGER_TYPE
8187 || subtype == NULL_TREE)
8188 return false;
8190 if (TREE_CODE (subtype) != INTEGER_TYPE
8191 && TREE_CODE (subtype) != ENUMERAL_TYPE)
8192 return false;
8194 if (TREE_CODE (type) == TREE_CODE (subtype)
8195 && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8196 && TYPE_MIN_VALUE (type) != NULL
8197 && TYPE_MIN_VALUE (subtype) != NULL
8198 && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8199 && TYPE_MAX_VALUE (type) != NULL
8200 && TYPE_MAX_VALUE (subtype) != NULL
8201 && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8203 /* The type and its subtype have the same representation. If in
8204 addition the two types also have the same name, then the given
8205 type is not a subrange type, but rather a plain base type. */
8206 /* FIXME: brobecker/2004-03-22:
8207 Sizetype INTEGER_CSTs nodes are canonicalized. It should
8208 therefore be sufficient to check the TYPE_SIZE node pointers
8209 rather than checking the actual size. Unfortunately, we have
8210 found some cases, such as in the Ada "integer" type, where
8211 this is not the case. Until this problem is solved, we need to
8212 keep checking the actual size. */
8213 tree type_name = TYPE_NAME (type);
8214 tree subtype_name = TYPE_NAME (subtype);
8216 if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8217 type_name = DECL_NAME (type_name);
8219 if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8220 subtype_name = DECL_NAME (subtype_name);
8222 if (type_name == subtype_name)
8223 return false;
8226 return true;
8229 /* Given a pointer to a tree node for a subrange type, return a pointer
8230 to a DIE that describes the given type. */
8232 static dw_die_ref
8233 subrange_type_die (tree type, dw_die_ref context_die)
8235 dw_die_ref subtype_die;
8236 dw_die_ref subrange_die;
8237 tree name = TYPE_NAME (type);
8238 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8239 tree subtype = TREE_TYPE (type);
8241 if (context_die == NULL)
8242 context_die = comp_unit_die;
8244 if (TREE_CODE (subtype) == ENUMERAL_TYPE)
8245 subtype_die = gen_enumeration_type_die (subtype, context_die);
8246 else
8247 subtype_die = base_type_die (subtype);
8249 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8251 if (name != NULL)
8253 if (TREE_CODE (name) == TYPE_DECL)
8254 name = DECL_NAME (name);
8255 add_name_attribute (subrange_die, IDENTIFIER_POINTER (name));
8258 if (int_size_in_bytes (subtype) != size_in_bytes)
8260 /* The size of the subrange type and its base type do not match,
8261 so we need to generate a size attribute for the subrange type. */
8262 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8265 if (TYPE_MIN_VALUE (type) != NULL)
8266 add_bound_info (subrange_die, DW_AT_lower_bound,
8267 TYPE_MIN_VALUE (type));
8268 if (TYPE_MAX_VALUE (type) != NULL)
8269 add_bound_info (subrange_die, DW_AT_upper_bound,
8270 TYPE_MAX_VALUE (type));
8271 add_AT_die_ref (subrange_die, DW_AT_type, subtype_die);
8273 return subrange_die;
8276 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8277 entry that chains various modifiers in front of the given type. */
8279 static dw_die_ref
8280 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8281 dw_die_ref context_die)
8283 enum tree_code code = TREE_CODE (type);
8284 dw_die_ref mod_type_die = NULL;
8285 dw_die_ref sub_die = NULL;
8286 tree item_type = NULL;
8288 if (code != ERROR_MARK)
8290 tree qualified_type;
8292 /* See if we already have the appropriately qualified variant of
8293 this type. */
8294 qualified_type
8295 = get_qualified_type (type,
8296 ((is_const_type ? TYPE_QUAL_CONST : 0)
8297 | (is_volatile_type
8298 ? TYPE_QUAL_VOLATILE : 0)));
8300 /* If we do, then we can just use its DIE, if it exists. */
8301 if (qualified_type)
8303 mod_type_die = lookup_type_die (qualified_type);
8304 if (mod_type_die)
8305 return mod_type_die;
8308 /* Handle C typedef types. */
8309 if (qualified_type && TYPE_NAME (qualified_type)
8310 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL
8311 && DECL_ORIGINAL_TYPE (TYPE_NAME (qualified_type)))
8313 tree type_name = TYPE_NAME (qualified_type);
8314 tree dtype = TREE_TYPE (type_name);
8316 if (qualified_type == dtype)
8318 /* For a named type, use the typedef. */
8319 gen_type_die (qualified_type, context_die);
8320 mod_type_die = lookup_type_die (qualified_type);
8322 else if (is_const_type < TYPE_READONLY (dtype)
8323 || is_volatile_type < TYPE_VOLATILE (dtype))
8324 /* cv-unqualified version of named type. Just use the unnamed
8325 type to which it refers. */
8326 mod_type_die
8327 = modified_type_die (DECL_ORIGINAL_TYPE (type_name),
8328 is_const_type, is_volatile_type,
8329 context_die);
8331 /* Else cv-qualified version of named type; fall through. */
8334 if (mod_type_die)
8335 /* OK. */
8337 else if (is_const_type)
8339 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8340 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8342 else if (is_volatile_type)
8344 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8345 sub_die = modified_type_die (type, 0, 0, context_die);
8347 else if (code == POINTER_TYPE)
8349 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8350 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8351 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8352 #if 0
8353 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8354 #endif
8355 item_type = TREE_TYPE (type);
8357 else if (code == REFERENCE_TYPE)
8359 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8360 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8361 simple_type_size_in_bits (type) / BITS_PER_UNIT);
8362 #if 0
8363 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8364 #endif
8365 item_type = TREE_TYPE (type);
8367 else if (is_subrange_type (type))
8368 mod_type_die = subrange_type_die (type, context_die);
8369 else if (is_base_type (type))
8370 mod_type_die = base_type_die (type);
8371 else
8373 gen_type_die (type, context_die);
8375 /* We have to get the type_main_variant here (and pass that to the
8376 `lookup_type_die' routine) because the ..._TYPE node we have
8377 might simply be a *copy* of some original type node (where the
8378 copy was created to help us keep track of typedef names) and
8379 that copy might have a different TYPE_UID from the original
8380 ..._TYPE node. */
8381 if (TREE_CODE (type) != VECTOR_TYPE)
8382 mod_type_die = lookup_type_die (type_main_variant (type));
8383 else
8384 /* Vectors have the debugging information in the type,
8385 not the main variant. */
8386 mod_type_die = lookup_type_die (type);
8387 gcc_assert (mod_type_die);
8390 /* We want to equate the qualified type to the die below. */
8391 type = qualified_type;
8394 if (type)
8395 equate_type_number_to_die (type, mod_type_die);
8396 if (item_type)
8397 /* We must do this after the equate_type_number_to_die call, in case
8398 this is a recursive type. This ensures that the modified_type_die
8399 recursion will terminate even if the type is recursive. Recursive
8400 types are possible in Ada. */
8401 sub_die = modified_type_die (item_type,
8402 TYPE_READONLY (item_type),
8403 TYPE_VOLATILE (item_type),
8404 context_die);
8406 if (sub_die != NULL)
8407 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8409 return mod_type_die;
8412 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8413 an enumerated type. */
8415 static inline int
8416 type_is_enum (tree type)
8418 return TREE_CODE (type) == ENUMERAL_TYPE;
8421 /* Return the DBX register number described by a given RTL node. */
8423 static unsigned int
8424 dbx_reg_number (rtx rtl)
8426 unsigned regno = REGNO (rtl);
8428 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8430 return DBX_REGISTER_NUMBER (regno);
8433 /* Return a location descriptor that designates a machine register or
8434 zero if there is none. */
8436 static dw_loc_descr_ref
8437 reg_loc_descriptor (rtx rtl)
8439 unsigned reg;
8440 rtx regs;
8442 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8443 return 0;
8445 reg = dbx_reg_number (rtl);
8446 regs = targetm.dwarf_register_span (rtl);
8448 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1
8449 || regs)
8450 return multiple_reg_loc_descriptor (rtl, regs);
8451 else
8452 return one_reg_loc_descriptor (reg);
8455 /* Return a location descriptor that designates a machine register for
8456 a given hard register number. */
8458 static dw_loc_descr_ref
8459 one_reg_loc_descriptor (unsigned int regno)
8461 if (regno <= 31)
8462 return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8463 else
8464 return new_loc_descr (DW_OP_regx, regno, 0);
8467 /* Given an RTL of a register, return a location descriptor that
8468 designates a value that spans more than one register. */
8470 static dw_loc_descr_ref
8471 multiple_reg_loc_descriptor (rtx rtl, rtx regs)
8473 int nregs, size, i;
8474 unsigned reg;
8475 dw_loc_descr_ref loc_result = NULL;
8477 reg = dbx_reg_number (rtl);
8478 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8480 /* Simple, contiguous registers. */
8481 if (regs == NULL_RTX)
8483 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8485 loc_result = NULL;
8486 while (nregs--)
8488 dw_loc_descr_ref t;
8490 t = one_reg_loc_descriptor (reg);
8491 add_loc_descr (&loc_result, t);
8492 add_loc_descr_op_piece (&loc_result, size);
8493 ++reg;
8495 return loc_result;
8498 /* Now onto stupid register sets in non contiguous locations. */
8500 gcc_assert (GET_CODE (regs) == PARALLEL);
8502 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8503 loc_result = NULL;
8505 for (i = 0; i < XVECLEN (regs, 0); ++i)
8507 dw_loc_descr_ref t;
8509 t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8510 add_loc_descr (&loc_result, t);
8511 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8512 add_loc_descr_op_piece (&loc_result, size);
8514 return loc_result;
8517 /* Return a location descriptor that designates a constant. */
8519 static dw_loc_descr_ref
8520 int_loc_descriptor (HOST_WIDE_INT i)
8522 enum dwarf_location_atom op;
8524 /* Pick the smallest representation of a constant, rather than just
8525 defaulting to the LEB encoding. */
8526 if (i >= 0)
8528 if (i <= 31)
8529 op = DW_OP_lit0 + i;
8530 else if (i <= 0xff)
8531 op = DW_OP_const1u;
8532 else if (i <= 0xffff)
8533 op = DW_OP_const2u;
8534 else if (HOST_BITS_PER_WIDE_INT == 32
8535 || i <= 0xffffffff)
8536 op = DW_OP_const4u;
8537 else
8538 op = DW_OP_constu;
8540 else
8542 if (i >= -0x80)
8543 op = DW_OP_const1s;
8544 else if (i >= -0x8000)
8545 op = DW_OP_const2s;
8546 else if (HOST_BITS_PER_WIDE_INT == 32
8547 || i >= -0x80000000)
8548 op = DW_OP_const4s;
8549 else
8550 op = DW_OP_consts;
8553 return new_loc_descr (op, i, 0);
8556 /* Return a location descriptor that designates a base+offset location. */
8558 static dw_loc_descr_ref
8559 based_loc_descr (unsigned int reg, HOST_WIDE_INT offset, bool can_use_fbreg)
8561 dw_loc_descr_ref loc_result;
8562 /* For the "frame base", we use the frame pointer or stack pointer
8563 registers, since the RTL for local variables is relative to one of
8564 them. */
8565 unsigned fp_reg = DBX_REGISTER_NUMBER (frame_pointer_needed
8566 ? HARD_FRAME_POINTER_REGNUM
8567 : STACK_POINTER_REGNUM);
8569 if (reg == fp_reg && can_use_fbreg)
8570 loc_result = new_loc_descr (DW_OP_fbreg, offset, 0);
8571 else if (reg <= 31)
8572 loc_result = new_loc_descr (DW_OP_breg0 + reg, offset, 0);
8573 else
8574 loc_result = new_loc_descr (DW_OP_bregx, reg, offset);
8576 return loc_result;
8579 /* Return true if this RTL expression describes a base+offset calculation. */
8581 static inline int
8582 is_based_loc (rtx rtl)
8584 return (GET_CODE (rtl) == PLUS
8585 && ((REG_P (XEXP (rtl, 0))
8586 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8587 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
8590 /* The following routine converts the RTL for a variable or parameter
8591 (resident in memory) into an equivalent Dwarf representation of a
8592 mechanism for getting the address of that same variable onto the top of a
8593 hypothetical "address evaluation" stack.
8595 When creating memory location descriptors, we are effectively transforming
8596 the RTL for a memory-resident object into its Dwarf postfix expression
8597 equivalent. This routine recursively descends an RTL tree, turning
8598 it into Dwarf postfix code as it goes.
8600 MODE is the mode of the memory reference, needed to handle some
8601 autoincrement addressing modes.
8603 CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the location
8604 list for RTL. We can't use it when we are emitting location list for
8605 virtual variable frame_base_decl (i.e. a location list for DW_AT_frame_base)
8606 which describes how frame base changes when !frame_pointer_needed.
8608 Return 0 if we can't represent the location. */
8610 static dw_loc_descr_ref
8611 mem_loc_descriptor (rtx rtl, enum machine_mode mode, bool can_use_fbreg)
8613 dw_loc_descr_ref mem_loc_result = NULL;
8614 enum dwarf_location_atom op;
8616 /* Note that for a dynamically sized array, the location we will generate a
8617 description of here will be the lowest numbered location which is
8618 actually within the array. That's *not* necessarily the same as the
8619 zeroth element of the array. */
8621 rtl = targetm.delegitimize_address (rtl);
8623 switch (GET_CODE (rtl))
8625 case POST_INC:
8626 case POST_DEC:
8627 case POST_MODIFY:
8628 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
8629 just fall into the SUBREG code. */
8631 /* ... fall through ... */
8633 case SUBREG:
8634 /* The case of a subreg may arise when we have a local (register)
8635 variable or a formal (register) parameter which doesn't quite fill
8636 up an entire register. For now, just assume that it is
8637 legitimate to make the Dwarf info refer to the whole register which
8638 contains the given subreg. */
8639 rtl = SUBREG_REG (rtl);
8641 /* ... fall through ... */
8643 case REG:
8644 /* Whenever a register number forms a part of the description of the
8645 method for calculating the (dynamic) address of a memory resident
8646 object, DWARF rules require the register number be referred to as
8647 a "base register". This distinction is not based in any way upon
8648 what category of register the hardware believes the given register
8649 belongs to. This is strictly DWARF terminology we're dealing with
8650 here. Note that in cases where the location of a memory-resident
8651 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8652 OP_CONST (0)) the actual DWARF location descriptor that we generate
8653 may just be OP_BASEREG (basereg). This may look deceptively like
8654 the object in question was allocated to a register (rather than in
8655 memory) so DWARF consumers need to be aware of the subtle
8656 distinction between OP_REG and OP_BASEREG. */
8657 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8658 mem_loc_result = based_loc_descr (dbx_reg_number (rtl), 0,
8659 can_use_fbreg);
8660 break;
8662 case MEM:
8663 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
8664 can_use_fbreg);
8665 if (mem_loc_result != 0)
8666 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
8667 break;
8669 case LO_SUM:
8670 rtl = XEXP (rtl, 1);
8672 /* ... fall through ... */
8674 case LABEL_REF:
8675 /* Some ports can transform a symbol ref into a label ref, because
8676 the symbol ref is too far away and has to be dumped into a constant
8677 pool. */
8678 case CONST:
8679 case SYMBOL_REF:
8680 /* Alternatively, the symbol in the constant pool might be referenced
8681 by a different symbol. */
8682 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
8684 bool marked;
8685 rtx tmp = get_pool_constant_mark (rtl, &marked);
8687 if (GET_CODE (tmp) == SYMBOL_REF)
8689 rtl = tmp;
8690 if (CONSTANT_POOL_ADDRESS_P (tmp))
8691 get_pool_constant_mark (tmp, &marked);
8692 else
8693 marked = true;
8696 /* If all references to this pool constant were optimized away,
8697 it was not output and thus we can't represent it.
8698 FIXME: might try to use DW_OP_const_value here, though
8699 DW_OP_piece complicates it. */
8700 if (!marked)
8701 return 0;
8704 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8705 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
8706 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8707 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
8708 break;
8710 case PRE_MODIFY:
8711 /* Extract the PLUS expression nested inside and fall into
8712 PLUS code below. */
8713 rtl = XEXP (rtl, 1);
8714 goto plus;
8716 case PRE_INC:
8717 case PRE_DEC:
8718 /* Turn these into a PLUS expression and fall into the PLUS code
8719 below. */
8720 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8721 GEN_INT (GET_CODE (rtl) == PRE_INC
8722 ? GET_MODE_UNIT_SIZE (mode)
8723 : -GET_MODE_UNIT_SIZE (mode)));
8725 /* ... fall through ... */
8727 case PLUS:
8728 plus:
8729 if (is_based_loc (rtl))
8730 mem_loc_result = based_loc_descr (dbx_reg_number (XEXP (rtl, 0)),
8731 INTVAL (XEXP (rtl, 1)),
8732 can_use_fbreg);
8733 else
8735 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
8736 can_use_fbreg);
8737 if (mem_loc_result == 0)
8738 break;
8740 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8741 && INTVAL (XEXP (rtl, 1)) >= 0)
8742 add_loc_descr (&mem_loc_result,
8743 new_loc_descr (DW_OP_plus_uconst,
8744 INTVAL (XEXP (rtl, 1)), 0));
8745 else
8747 add_loc_descr (&mem_loc_result,
8748 mem_loc_descriptor (XEXP (rtl, 1), mode,
8749 can_use_fbreg));
8750 add_loc_descr (&mem_loc_result,
8751 new_loc_descr (DW_OP_plus, 0, 0));
8754 break;
8756 /* If a pseudo-reg is optimized away, it is possible for it to
8757 be replaced with a MEM containing a multiply or shift. */
8758 case MULT:
8759 op = DW_OP_mul;
8760 goto do_binop;
8762 case ASHIFT:
8763 op = DW_OP_shl;
8764 goto do_binop;
8766 case ASHIFTRT:
8767 op = DW_OP_shra;
8768 goto do_binop;
8770 case LSHIFTRT:
8771 op = DW_OP_shr;
8772 goto do_binop;
8774 do_binop:
8776 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
8777 can_use_fbreg);
8778 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
8779 can_use_fbreg);
8781 if (op0 == 0 || op1 == 0)
8782 break;
8784 mem_loc_result = op0;
8785 add_loc_descr (&mem_loc_result, op1);
8786 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
8787 break;
8790 case CONST_INT:
8791 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
8792 break;
8794 default:
8795 gcc_unreachable ();
8798 return mem_loc_result;
8801 /* Return a descriptor that describes the concatenation of two locations.
8802 This is typically a complex variable. */
8804 static dw_loc_descr_ref
8805 concat_loc_descriptor (rtx x0, rtx x1)
8807 dw_loc_descr_ref cc_loc_result = NULL;
8808 dw_loc_descr_ref x0_ref = loc_descriptor (x0, false);
8809 dw_loc_descr_ref x1_ref = loc_descriptor (x1, false);
8811 if (x0_ref == 0 || x1_ref == 0)
8812 return 0;
8814 cc_loc_result = x0_ref;
8815 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
8817 add_loc_descr (&cc_loc_result, x1_ref);
8818 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
8820 return cc_loc_result;
8823 /* Output a proper Dwarf location descriptor for a variable or parameter
8824 which is either allocated in a register or in a memory location. For a
8825 register, we just generate an OP_REG and the register number. For a
8826 memory location we provide a Dwarf postfix expression describing how to
8827 generate the (dynamic) address of the object onto the address stack.
8829 If we don't know how to describe it, return 0. */
8831 static dw_loc_descr_ref
8832 loc_descriptor (rtx rtl, bool can_use_fbreg)
8834 dw_loc_descr_ref loc_result = NULL;
8836 switch (GET_CODE (rtl))
8838 case SUBREG:
8839 /* The case of a subreg may arise when we have a local (register)
8840 variable or a formal (register) parameter which doesn't quite fill
8841 up an entire register. For now, just assume that it is
8842 legitimate to make the Dwarf info refer to the whole register which
8843 contains the given subreg. */
8844 rtl = SUBREG_REG (rtl);
8846 /* ... fall through ... */
8848 case REG:
8849 loc_result = reg_loc_descriptor (rtl);
8850 break;
8852 case MEM:
8853 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
8854 can_use_fbreg);
8855 break;
8857 case CONCAT:
8858 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
8859 break;
8861 case VAR_LOCATION:
8862 /* Single part. */
8863 if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
8865 loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), can_use_fbreg);
8866 break;
8869 rtl = XEXP (rtl, 1);
8870 /* FALLTHRU */
8872 case PARALLEL:
8874 rtvec par_elems = XVEC (rtl, 0);
8875 int num_elem = GET_NUM_ELEM (par_elems);
8876 enum machine_mode mode;
8877 int i;
8879 /* Create the first one, so we have something to add to. */
8880 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
8881 can_use_fbreg);
8882 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
8883 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
8884 for (i = 1; i < num_elem; i++)
8886 dw_loc_descr_ref temp;
8888 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
8889 can_use_fbreg);
8890 add_loc_descr (&loc_result, temp);
8891 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
8892 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
8895 break;
8897 default:
8898 gcc_unreachable ();
8901 return loc_result;
8904 /* Similar, but generate the descriptor from trees instead of rtl. This comes
8905 up particularly with variable length arrays. WANT_ADDRESS is 2 if this is
8906 a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
8907 top-level invocation, and we require the address of LOC; is 0 if we require
8908 the value of LOC. */
8910 static dw_loc_descr_ref
8911 loc_descriptor_from_tree_1 (tree loc, int want_address)
8913 dw_loc_descr_ref ret, ret1;
8914 int have_address = 0;
8915 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
8916 enum dwarf_location_atom op;
8918 /* ??? Most of the time we do not take proper care for sign/zero
8919 extending the values properly. Hopefully this won't be a real
8920 problem... */
8922 switch (TREE_CODE (loc))
8924 case ERROR_MARK:
8925 return 0;
8927 case PLACEHOLDER_EXPR:
8928 /* This case involves extracting fields from an object to determine the
8929 position of other fields. We don't try to encode this here. The
8930 only user of this is Ada, which encodes the needed information using
8931 the names of types. */
8932 return 0;
8934 case CALL_EXPR:
8935 return 0;
8937 case PREINCREMENT_EXPR:
8938 case PREDECREMENT_EXPR:
8939 case POSTINCREMENT_EXPR:
8940 case POSTDECREMENT_EXPR:
8941 /* There are no opcodes for these operations. */
8942 return 0;
8944 case ADDR_EXPR:
8945 /* If we already want an address, there's nothing we can do. */
8946 if (want_address)
8947 return 0;
8949 /* Otherwise, process the argument and look for the address. */
8950 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
8952 case VAR_DECL:
8953 if (DECL_THREAD_LOCAL (loc))
8955 rtx rtl;
8957 #ifndef ASM_OUTPUT_DWARF_DTPREL
8958 /* If this is not defined, we have no way to emit the data. */
8959 return 0;
8960 #endif
8962 /* The way DW_OP_GNU_push_tls_address is specified, we can only
8963 look up addresses of objects in the current module. */
8964 if (DECL_EXTERNAL (loc))
8965 return 0;
8967 rtl = rtl_for_decl_location (loc);
8968 if (rtl == NULL_RTX)
8969 return 0;
8971 if (!MEM_P (rtl))
8972 return 0;
8973 rtl = XEXP (rtl, 0);
8974 if (! CONSTANT_P (rtl))
8975 return 0;
8977 ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
8978 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8979 ret->dw_loc_oprnd1.v.val_addr = rtl;
8981 ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
8982 add_loc_descr (&ret, ret1);
8984 have_address = 1;
8985 break;
8987 /* FALLTHRU */
8989 case PARM_DECL:
8990 if (DECL_HAS_VALUE_EXPR_P (loc))
8991 return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
8992 want_address);
8993 /* FALLTHRU */
8995 case RESULT_DECL:
8997 rtx rtl = rtl_for_decl_location (loc);
8999 if (rtl == NULL_RTX)
9000 return 0;
9001 else if (GET_CODE (rtl) == CONST_INT)
9003 HOST_WIDE_INT val = INTVAL (rtl);
9004 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9005 val &= GET_MODE_MASK (DECL_MODE (loc));
9006 ret = int_loc_descriptor (val);
9008 else if (GET_CODE (rtl) == CONST_STRING)
9009 return 0;
9010 else if (CONSTANT_P (rtl))
9012 ret = new_loc_descr (DW_OP_addr, 0, 0);
9013 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9014 ret->dw_loc_oprnd1.v.val_addr = rtl;
9016 else
9018 enum machine_mode mode;
9020 /* Certain constructs can only be represented at top-level. */
9021 if (want_address == 2)
9022 return loc_descriptor (rtl, false);
9024 mode = GET_MODE (rtl);
9025 if (MEM_P (rtl))
9027 rtl = XEXP (rtl, 0);
9028 have_address = 1;
9030 ret = mem_loc_descriptor (rtl, mode, false);
9033 break;
9035 case INDIRECT_REF:
9036 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9037 have_address = 1;
9038 break;
9040 case COMPOUND_EXPR:
9041 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9043 case NOP_EXPR:
9044 case CONVERT_EXPR:
9045 case NON_LVALUE_EXPR:
9046 case VIEW_CONVERT_EXPR:
9047 case SAVE_EXPR:
9048 case MODIFY_EXPR:
9049 return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
9051 case COMPONENT_REF:
9052 case BIT_FIELD_REF:
9053 case ARRAY_REF:
9054 case ARRAY_RANGE_REF:
9056 tree obj, offset;
9057 HOST_WIDE_INT bitsize, bitpos, bytepos;
9058 enum machine_mode mode;
9059 int volatilep;
9061 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9062 &unsignedp, &volatilep, false);
9064 if (obj == loc)
9065 return 0;
9067 ret = loc_descriptor_from_tree_1 (obj, 1);
9068 if (ret == 0
9069 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9070 return 0;
9072 if (offset != NULL_TREE)
9074 /* Variable offset. */
9075 add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9076 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9079 bytepos = bitpos / BITS_PER_UNIT;
9080 if (bytepos > 0)
9081 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9082 else if (bytepos < 0)
9084 add_loc_descr (&ret, int_loc_descriptor (bytepos));
9085 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9088 have_address = 1;
9089 break;
9092 case INTEGER_CST:
9093 if (host_integerp (loc, 0))
9094 ret = int_loc_descriptor (tree_low_cst (loc, 0));
9095 else
9096 return 0;
9097 break;
9099 case CONSTRUCTOR:
9101 /* Get an RTL for this, if something has been emitted. */
9102 rtx rtl = lookup_constant_def (loc);
9103 enum machine_mode mode;
9105 if (!rtl || !MEM_P (rtl))
9106 return 0;
9107 mode = GET_MODE (rtl);
9108 rtl = XEXP (rtl, 0);
9109 ret = mem_loc_descriptor (rtl, mode, false);
9110 have_address = 1;
9111 break;
9114 case TRUTH_AND_EXPR:
9115 case TRUTH_ANDIF_EXPR:
9116 case BIT_AND_EXPR:
9117 op = DW_OP_and;
9118 goto do_binop;
9120 case TRUTH_XOR_EXPR:
9121 case BIT_XOR_EXPR:
9122 op = DW_OP_xor;
9123 goto do_binop;
9125 case TRUTH_OR_EXPR:
9126 case TRUTH_ORIF_EXPR:
9127 case BIT_IOR_EXPR:
9128 op = DW_OP_or;
9129 goto do_binop;
9131 case FLOOR_DIV_EXPR:
9132 case CEIL_DIV_EXPR:
9133 case ROUND_DIV_EXPR:
9134 case TRUNC_DIV_EXPR:
9135 op = DW_OP_div;
9136 goto do_binop;
9138 case MINUS_EXPR:
9139 op = DW_OP_minus;
9140 goto do_binop;
9142 case FLOOR_MOD_EXPR:
9143 case CEIL_MOD_EXPR:
9144 case ROUND_MOD_EXPR:
9145 case TRUNC_MOD_EXPR:
9146 op = DW_OP_mod;
9147 goto do_binop;
9149 case MULT_EXPR:
9150 op = DW_OP_mul;
9151 goto do_binop;
9153 case LSHIFT_EXPR:
9154 op = DW_OP_shl;
9155 goto do_binop;
9157 case RSHIFT_EXPR:
9158 op = (unsignedp ? DW_OP_shr : DW_OP_shra);
9159 goto do_binop;
9161 case PLUS_EXPR:
9162 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9163 && host_integerp (TREE_OPERAND (loc, 1), 0))
9165 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9166 if (ret == 0)
9167 return 0;
9169 add_loc_descr (&ret,
9170 new_loc_descr (DW_OP_plus_uconst,
9171 tree_low_cst (TREE_OPERAND (loc, 1),
9173 0));
9174 break;
9177 op = DW_OP_plus;
9178 goto do_binop;
9180 case LE_EXPR:
9181 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9182 return 0;
9184 op = DW_OP_le;
9185 goto do_binop;
9187 case GE_EXPR:
9188 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9189 return 0;
9191 op = DW_OP_ge;
9192 goto do_binop;
9194 case LT_EXPR:
9195 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9196 return 0;
9198 op = DW_OP_lt;
9199 goto do_binop;
9201 case GT_EXPR:
9202 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9203 return 0;
9205 op = DW_OP_gt;
9206 goto do_binop;
9208 case EQ_EXPR:
9209 op = DW_OP_eq;
9210 goto do_binop;
9212 case NE_EXPR:
9213 op = DW_OP_ne;
9214 goto do_binop;
9216 do_binop:
9217 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9218 ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9219 if (ret == 0 || ret1 == 0)
9220 return 0;
9222 add_loc_descr (&ret, ret1);
9223 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9224 break;
9226 case TRUTH_NOT_EXPR:
9227 case BIT_NOT_EXPR:
9228 op = DW_OP_not;
9229 goto do_unop;
9231 case ABS_EXPR:
9232 op = DW_OP_abs;
9233 goto do_unop;
9235 case NEGATE_EXPR:
9236 op = DW_OP_neg;
9237 goto do_unop;
9239 do_unop:
9240 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9241 if (ret == 0)
9242 return 0;
9244 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9245 break;
9247 case MIN_EXPR:
9248 case MAX_EXPR:
9250 const enum tree_code code =
9251 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9253 loc = build3 (COND_EXPR, TREE_TYPE (loc),
9254 build2 (code, integer_type_node,
9255 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9256 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9259 /* ... fall through ... */
9261 case COND_EXPR:
9263 dw_loc_descr_ref lhs
9264 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9265 dw_loc_descr_ref rhs
9266 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9267 dw_loc_descr_ref bra_node, jump_node, tmp;
9269 ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9270 if (ret == 0 || lhs == 0 || rhs == 0)
9271 return 0;
9273 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9274 add_loc_descr (&ret, bra_node);
9276 add_loc_descr (&ret, rhs);
9277 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9278 add_loc_descr (&ret, jump_node);
9280 add_loc_descr (&ret, lhs);
9281 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9282 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9284 /* ??? Need a node to point the skip at. Use a nop. */
9285 tmp = new_loc_descr (DW_OP_nop, 0, 0);
9286 add_loc_descr (&ret, tmp);
9287 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9288 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9290 break;
9292 case FIX_TRUNC_EXPR:
9293 case FIX_CEIL_EXPR:
9294 case FIX_FLOOR_EXPR:
9295 case FIX_ROUND_EXPR:
9296 return 0;
9298 default:
9299 /* Leave front-end specific codes as simply unknown. This comes
9300 up, for instance, with the C STMT_EXPR. */
9301 if ((unsigned int) TREE_CODE (loc)
9302 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9303 return 0;
9305 #ifdef ENABLE_CHECKING
9306 /* Otherwise this is a generic code; we should just lists all of
9307 these explicitly. We forgot one. */
9308 gcc_unreachable ();
9309 #else
9310 /* In a release build, we want to degrade gracefully: better to
9311 generate incomplete debugging information than to crash. */
9312 return NULL;
9313 #endif
9316 /* Show if we can't fill the request for an address. */
9317 if (want_address && !have_address)
9318 return 0;
9320 /* If we've got an address and don't want one, dereference. */
9321 if (!want_address && have_address)
9323 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9325 if (size > DWARF2_ADDR_SIZE || size == -1)
9326 return 0;
9327 else if (size == DWARF2_ADDR_SIZE)
9328 op = DW_OP_deref;
9329 else
9330 op = DW_OP_deref_size;
9332 add_loc_descr (&ret, new_loc_descr (op, size, 0));
9335 return ret;
9338 static inline dw_loc_descr_ref
9339 loc_descriptor_from_tree (tree loc)
9341 return loc_descriptor_from_tree_1 (loc, 2);
9344 /* Given a value, round it up to the lowest multiple of `boundary'
9345 which is not less than the value itself. */
9347 static inline HOST_WIDE_INT
9348 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9350 return (((value + boundary - 1) / boundary) * boundary);
9353 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9354 pointer to the declared type for the relevant field variable, or return
9355 `integer_type_node' if the given node turns out to be an
9356 ERROR_MARK node. */
9358 static inline tree
9359 field_type (tree decl)
9361 tree type;
9363 if (TREE_CODE (decl) == ERROR_MARK)
9364 return integer_type_node;
9366 type = DECL_BIT_FIELD_TYPE (decl);
9367 if (type == NULL_TREE)
9368 type = TREE_TYPE (decl);
9370 return type;
9373 /* Given a pointer to a tree node, return the alignment in bits for
9374 it, or else return BITS_PER_WORD if the node actually turns out to
9375 be an ERROR_MARK node. */
9377 static inline unsigned
9378 simple_type_align_in_bits (tree type)
9380 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9383 static inline unsigned
9384 simple_decl_align_in_bits (tree decl)
9386 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9389 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9390 lowest addressed byte of the "containing object" for the given FIELD_DECL,
9391 or return 0 if we are unable to determine what that offset is, either
9392 because the argument turns out to be a pointer to an ERROR_MARK node, or
9393 because the offset is actually variable. (We can't handle the latter case
9394 just yet). */
9396 static HOST_WIDE_INT
9397 field_byte_offset (tree decl)
9399 unsigned int type_align_in_bits;
9400 unsigned int decl_align_in_bits;
9401 unsigned HOST_WIDE_INT type_size_in_bits;
9402 HOST_WIDE_INT object_offset_in_bits;
9403 tree type;
9404 tree field_size_tree;
9405 HOST_WIDE_INT bitpos_int;
9406 HOST_WIDE_INT deepest_bitpos;
9407 unsigned HOST_WIDE_INT field_size_in_bits;
9409 if (TREE_CODE (decl) == ERROR_MARK)
9410 return 0;
9412 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9414 type = field_type (decl);
9415 field_size_tree = DECL_SIZE (decl);
9417 /* The size could be unspecified if there was an error, or for
9418 a flexible array member. */
9419 if (! field_size_tree)
9420 field_size_tree = bitsize_zero_node;
9422 /* We cannot yet cope with fields whose positions are variable, so
9423 for now, when we see such things, we simply return 0. Someday, we may
9424 be able to handle such cases, but it will be damn difficult. */
9425 if (! host_integerp (bit_position (decl), 0))
9426 return 0;
9428 bitpos_int = int_bit_position (decl);
9430 /* If we don't know the size of the field, pretend it's a full word. */
9431 if (host_integerp (field_size_tree, 1))
9432 field_size_in_bits = tree_low_cst (field_size_tree, 1);
9433 else
9434 field_size_in_bits = BITS_PER_WORD;
9436 type_size_in_bits = simple_type_size_in_bits (type);
9437 type_align_in_bits = simple_type_align_in_bits (type);
9438 decl_align_in_bits = simple_decl_align_in_bits (decl);
9440 /* The GCC front-end doesn't make any attempt to keep track of the starting
9441 bit offset (relative to the start of the containing structure type) of the
9442 hypothetical "containing object" for a bit-field. Thus, when computing
9443 the byte offset value for the start of the "containing object" of a
9444 bit-field, we must deduce this information on our own. This can be rather
9445 tricky to do in some cases. For example, handling the following structure
9446 type definition when compiling for an i386/i486 target (which only aligns
9447 long long's to 32-bit boundaries) can be very tricky:
9449 struct S { int field1; long long field2:31; };
9451 Fortunately, there is a simple rule-of-thumb which can be used in such
9452 cases. When compiling for an i386/i486, GCC will allocate 8 bytes for the
9453 structure shown above. It decides to do this based upon one simple rule
9454 for bit-field allocation. GCC allocates each "containing object" for each
9455 bit-field at the first (i.e. lowest addressed) legitimate alignment
9456 boundary (based upon the required minimum alignment for the declared type
9457 of the field) which it can possibly use, subject to the condition that
9458 there is still enough available space remaining in the containing object
9459 (when allocated at the selected point) to fully accommodate all of the
9460 bits of the bit-field itself.
9462 This simple rule makes it obvious why GCC allocates 8 bytes for each
9463 object of the structure type shown above. When looking for a place to
9464 allocate the "containing object" for `field2', the compiler simply tries
9465 to allocate a 64-bit "containing object" at each successive 32-bit
9466 boundary (starting at zero) until it finds a place to allocate that 64-
9467 bit field such that at least 31 contiguous (and previously unallocated)
9468 bits remain within that selected 64 bit field. (As it turns out, for the
9469 example above, the compiler finds it is OK to allocate the "containing
9470 object" 64-bit field at bit-offset zero within the structure type.)
9472 Here we attempt to work backwards from the limited set of facts we're
9473 given, and we try to deduce from those facts, where GCC must have believed
9474 that the containing object started (within the structure type). The value
9475 we deduce is then used (by the callers of this routine) to generate
9476 DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
9477 and, in the case of DW_AT_location, regular fields as well). */
9479 /* Figure out the bit-distance from the start of the structure to the
9480 "deepest" bit of the bit-field. */
9481 deepest_bitpos = bitpos_int + field_size_in_bits;
9483 /* This is the tricky part. Use some fancy footwork to deduce where the
9484 lowest addressed bit of the containing object must be. */
9485 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9487 /* Round up to type_align by default. This works best for bitfields. */
9488 object_offset_in_bits += type_align_in_bits - 1;
9489 object_offset_in_bits /= type_align_in_bits;
9490 object_offset_in_bits *= type_align_in_bits;
9492 if (object_offset_in_bits > bitpos_int)
9494 /* Sigh, the decl must be packed. */
9495 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9497 /* Round up to decl_align instead. */
9498 object_offset_in_bits += decl_align_in_bits - 1;
9499 object_offset_in_bits /= decl_align_in_bits;
9500 object_offset_in_bits *= decl_align_in_bits;
9503 return object_offset_in_bits / BITS_PER_UNIT;
9506 /* The following routines define various Dwarf attributes and any data
9507 associated with them. */
9509 /* Add a location description attribute value to a DIE.
9511 This emits location attributes suitable for whole variables and
9512 whole parameters. Note that the location attributes for struct fields are
9513 generated by the routine `data_member_location_attribute' below. */
9515 static inline void
9516 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9517 dw_loc_descr_ref descr)
9519 if (descr != 0)
9520 add_AT_loc (die, attr_kind, descr);
9523 /* Attach the specialized form of location attribute used for data members of
9524 struct and union types. In the special case of a FIELD_DECL node which
9525 represents a bit-field, the "offset" part of this special location
9526 descriptor must indicate the distance in bytes from the lowest-addressed
9527 byte of the containing struct or union type to the lowest-addressed byte of
9528 the "containing object" for the bit-field. (See the `field_byte_offset'
9529 function above).
9531 For any given bit-field, the "containing object" is a hypothetical object
9532 (of some integral or enum type) within which the given bit-field lives. The
9533 type of this hypothetical "containing object" is always the same as the
9534 declared type of the individual bit-field itself (for GCC anyway... the
9535 DWARF spec doesn't actually mandate this). Note that it is the size (in
9536 bytes) of the hypothetical "containing object" which will be given in the
9537 DW_AT_byte_size attribute for this bit-field. (See the
9538 `byte_size_attribute' function below.) It is also used when calculating the
9539 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
9540 function below.) */
9542 static void
9543 add_data_member_location_attribute (dw_die_ref die, tree decl)
9545 HOST_WIDE_INT offset;
9546 dw_loc_descr_ref loc_descr = 0;
9548 if (TREE_CODE (decl) == TREE_BINFO)
9550 /* We're working on the TAG_inheritance for a base class. */
9551 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
9553 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9554 aren't at a fixed offset from all (sub)objects of the same
9555 type. We need to extract the appropriate offset from our
9556 vtable. The following dwarf expression means
9558 BaseAddr = ObAddr + *((*ObAddr) - Offset)
9560 This is specific to the V3 ABI, of course. */
9562 dw_loc_descr_ref tmp;
9564 /* Make a copy of the object address. */
9565 tmp = new_loc_descr (DW_OP_dup, 0, 0);
9566 add_loc_descr (&loc_descr, tmp);
9568 /* Extract the vtable address. */
9569 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9570 add_loc_descr (&loc_descr, tmp);
9572 /* Calculate the address of the offset. */
9573 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9574 gcc_assert (offset < 0);
9576 tmp = int_loc_descriptor (-offset);
9577 add_loc_descr (&loc_descr, tmp);
9578 tmp = new_loc_descr (DW_OP_minus, 0, 0);
9579 add_loc_descr (&loc_descr, tmp);
9581 /* Extract the offset. */
9582 tmp = new_loc_descr (DW_OP_deref, 0, 0);
9583 add_loc_descr (&loc_descr, tmp);
9585 /* Add it to the object address. */
9586 tmp = new_loc_descr (DW_OP_plus, 0, 0);
9587 add_loc_descr (&loc_descr, tmp);
9589 else
9590 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9592 else
9593 offset = field_byte_offset (decl);
9595 if (! loc_descr)
9597 enum dwarf_location_atom op;
9599 /* The DWARF2 standard says that we should assume that the structure
9600 address is already on the stack, so we can specify a structure field
9601 address by using DW_OP_plus_uconst. */
9603 #ifdef MIPS_DEBUGGING_INFO
9604 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9605 operator correctly. It works only if we leave the offset on the
9606 stack. */
9607 op = DW_OP_constu;
9608 #else
9609 op = DW_OP_plus_uconst;
9610 #endif
9612 loc_descr = new_loc_descr (op, offset, 0);
9615 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9618 /* Writes integer values to dw_vec_const array. */
9620 static void
9621 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9623 while (size != 0)
9625 *dest++ = val & 0xff;
9626 val >>= 8;
9627 --size;
9631 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
9633 static HOST_WIDE_INT
9634 extract_int (const unsigned char *src, unsigned int size)
9636 HOST_WIDE_INT val = 0;
9638 src += size;
9639 while (size != 0)
9641 val <<= 8;
9642 val |= *--src & 0xff;
9643 --size;
9645 return val;
9648 /* Writes floating point values to dw_vec_const array. */
9650 static void
9651 insert_float (rtx rtl, unsigned char *array)
9653 REAL_VALUE_TYPE rv;
9654 long val[4];
9655 int i;
9657 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9658 real_to_target (val, &rv, GET_MODE (rtl));
9660 /* real_to_target puts 32-bit pieces in each long. Pack them. */
9661 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9663 insert_int (val[i], 4, array);
9664 array += 4;
9668 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
9669 does not have a "location" either in memory or in a register. These
9670 things can arise in GNU C when a constant is passed as an actual parameter
9671 to an inlined function. They can also arise in C++ where declared
9672 constants do not necessarily get memory "homes". */
9674 static void
9675 add_const_value_attribute (dw_die_ref die, rtx rtl)
9677 switch (GET_CODE (rtl))
9679 case CONST_INT:
9681 HOST_WIDE_INT val = INTVAL (rtl);
9683 if (val < 0)
9684 add_AT_int (die, DW_AT_const_value, val);
9685 else
9686 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
9688 break;
9690 case CONST_DOUBLE:
9691 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9692 floating-point constant. A CONST_DOUBLE is used whenever the
9693 constant requires more than one word in order to be adequately
9694 represented. We output CONST_DOUBLEs as blocks. */
9696 enum machine_mode mode = GET_MODE (rtl);
9698 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
9700 unsigned int length = GET_MODE_SIZE (mode);
9701 unsigned char *array = ggc_alloc (length);
9703 insert_float (rtl, array);
9704 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
9706 else
9708 /* ??? We really should be using HOST_WIDE_INT throughout. */
9709 gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
9711 add_AT_long_long (die, DW_AT_const_value,
9712 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
9715 break;
9717 case CONST_VECTOR:
9719 enum machine_mode mode = GET_MODE (rtl);
9720 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9721 unsigned int length = CONST_VECTOR_NUNITS (rtl);
9722 unsigned char *array = ggc_alloc (length * elt_size);
9723 unsigned int i;
9724 unsigned char *p;
9726 switch (GET_MODE_CLASS (mode))
9728 case MODE_VECTOR_INT:
9729 for (i = 0, p = array; i < length; i++, p += elt_size)
9731 rtx elt = CONST_VECTOR_ELT (rtl, i);
9732 HOST_WIDE_INT lo, hi;
9734 switch (GET_CODE (elt))
9736 case CONST_INT:
9737 lo = INTVAL (elt);
9738 hi = -(lo < 0);
9739 break;
9741 case CONST_DOUBLE:
9742 lo = CONST_DOUBLE_LOW (elt);
9743 hi = CONST_DOUBLE_HIGH (elt);
9744 break;
9746 default:
9747 gcc_unreachable ();
9750 if (elt_size <= sizeof (HOST_WIDE_INT))
9751 insert_int (lo, elt_size, p);
9752 else
9754 unsigned char *p0 = p;
9755 unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9757 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
9758 if (WORDS_BIG_ENDIAN)
9760 p0 = p1;
9761 p1 = p;
9763 insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9764 insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9767 break;
9769 case MODE_VECTOR_FLOAT:
9770 for (i = 0, p = array; i < length; i++, p += elt_size)
9772 rtx elt = CONST_VECTOR_ELT (rtl, i);
9773 insert_float (elt, p);
9775 break;
9777 default:
9778 gcc_unreachable ();
9781 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
9783 break;
9785 case CONST_STRING:
9786 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
9787 break;
9789 case SYMBOL_REF:
9790 case LABEL_REF:
9791 case CONST:
9792 add_AT_addr (die, DW_AT_const_value, rtl);
9793 VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9794 break;
9796 case PLUS:
9797 /* In cases where an inlined instance of an inline function is passed
9798 the address of an `auto' variable (which is local to the caller) we
9799 can get a situation where the DECL_RTL of the artificial local
9800 variable (for the inlining) which acts as a stand-in for the
9801 corresponding formal parameter (of the inline function) will look
9802 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
9803 exactly a compile-time constant expression, but it isn't the address
9804 of the (artificial) local variable either. Rather, it represents the
9805 *value* which the artificial local variable always has during its
9806 lifetime. We currently have no way to represent such quasi-constant
9807 values in Dwarf, so for now we just punt and generate nothing. */
9808 break;
9810 default:
9811 /* No other kinds of rtx should be possible here. */
9812 gcc_unreachable ();
9817 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
9818 for use in a later add_const_value_attribute call. */
9820 static rtx
9821 rtl_for_decl_init (tree init, tree type)
9823 rtx rtl = NULL_RTX;
9825 /* If a variable is initialized with a string constant without embedded
9826 zeros, build CONST_STRING. */
9827 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
9829 tree enttype = TREE_TYPE (type);
9830 tree domain = TYPE_DOMAIN (type);
9831 enum machine_mode mode = TYPE_MODE (enttype);
9833 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
9834 && domain
9835 && integer_zerop (TYPE_MIN_VALUE (domain))
9836 && compare_tree_int (TYPE_MAX_VALUE (domain),
9837 TREE_STRING_LENGTH (init) - 1) == 0
9838 && ((size_t) TREE_STRING_LENGTH (init)
9839 == strlen (TREE_STRING_POINTER (init)) + 1))
9840 rtl = gen_rtx_CONST_STRING (VOIDmode,
9841 ggc_strdup (TREE_STRING_POINTER (init)));
9843 /* If the initializer is something that we know will expand into an
9844 immediate RTL constant, expand it now. Expanding anything else
9845 tends to produce unresolved symbols; see debug/5770 and c++/6381. */
9846 /* Aggregate, vector, and complex types may contain constructors that may
9847 result in code being generated when expand_expr is called, so we can't
9848 handle them here. Integer and float are useful and safe types to handle
9849 here. */
9850 else if ((INTEGRAL_TYPE_P (type) || SCALAR_FLOAT_TYPE_P (type))
9851 && initializer_constant_valid_p (init, type) == null_pointer_node)
9853 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
9855 /* If expand_expr returns a MEM, it wasn't immediate. */
9856 gcc_assert (!rtl || !MEM_P (rtl));
9859 return rtl;
9862 /* Generate RTL for the variable DECL to represent its location. */
9864 static rtx
9865 rtl_for_decl_location (tree decl)
9867 rtx rtl;
9869 /* Here we have to decide where we are going to say the parameter "lives"
9870 (as far as the debugger is concerned). We only have a couple of
9871 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
9873 DECL_RTL normally indicates where the parameter lives during most of the
9874 activation of the function. If optimization is enabled however, this
9875 could be either NULL or else a pseudo-reg. Both of those cases indicate
9876 that the parameter doesn't really live anywhere (as far as the code
9877 generation parts of GCC are concerned) during most of the function's
9878 activation. That will happen (for example) if the parameter is never
9879 referenced within the function.
9881 We could just generate a location descriptor here for all non-NULL
9882 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
9883 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
9884 where DECL_RTL is NULL or is a pseudo-reg.
9886 Note however that we can only get away with using DECL_INCOMING_RTL as
9887 a backup substitute for DECL_RTL in certain limited cases. In cases
9888 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
9889 we can be sure that the parameter was passed using the same type as it is
9890 declared to have within the function, and that its DECL_INCOMING_RTL
9891 points us to a place where a value of that type is passed.
9893 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
9894 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
9895 because in these cases DECL_INCOMING_RTL points us to a value of some
9896 type which is *different* from the type of the parameter itself. Thus,
9897 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
9898 such cases, the debugger would end up (for example) trying to fetch a
9899 `float' from a place which actually contains the first part of a
9900 `double'. That would lead to really incorrect and confusing
9901 output at debug-time.
9903 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
9904 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
9905 are a couple of exceptions however. On little-endian machines we can
9906 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
9907 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
9908 an integral type that is smaller than TREE_TYPE (decl). These cases arise
9909 when (on a little-endian machine) a non-prototyped function has a
9910 parameter declared to be of type `short' or `char'. In such cases,
9911 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
9912 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
9913 passed `int' value. If the debugger then uses that address to fetch
9914 a `short' or a `char' (on a little-endian machine) the result will be
9915 the correct data, so we allow for such exceptional cases below.
9917 Note that our goal here is to describe the place where the given formal
9918 parameter lives during most of the function's activation (i.e. between the
9919 end of the prologue and the start of the epilogue). We'll do that as best
9920 as we can. Note however that if the given formal parameter is modified
9921 sometime during the execution of the function, then a stack backtrace (at
9922 debug-time) will show the function as having been called with the *new*
9923 value rather than the value which was originally passed in. This happens
9924 rarely enough that it is not a major problem, but it *is* a problem, and
9925 I'd like to fix it.
9927 A future version of dwarf2out.c may generate two additional attributes for
9928 any given DW_TAG_formal_parameter DIE which will describe the "passed
9929 type" and the "passed location" for the given formal parameter in addition
9930 to the attributes we now generate to indicate the "declared type" and the
9931 "active location" for each parameter. This additional set of attributes
9932 could be used by debuggers for stack backtraces. Separately, note that
9933 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
9934 This happens (for example) for inlined-instances of inline function formal
9935 parameters which are never referenced. This really shouldn't be
9936 happening. All PARM_DECL nodes should get valid non-NULL
9937 DECL_INCOMING_RTL values. FIXME. */
9939 /* Use DECL_RTL as the "location" unless we find something better. */
9940 rtl = DECL_RTL_IF_SET (decl);
9942 /* When generating abstract instances, ignore everything except
9943 constants, symbols living in memory, and symbols living in
9944 fixed registers. */
9945 if (! reload_completed)
9947 if (rtl
9948 && (CONSTANT_P (rtl)
9949 || (MEM_P (rtl)
9950 && CONSTANT_P (XEXP (rtl, 0)))
9951 || (REG_P (rtl)
9952 && TREE_CODE (decl) == VAR_DECL
9953 && TREE_STATIC (decl))))
9955 rtl = targetm.delegitimize_address (rtl);
9956 return rtl;
9958 rtl = NULL_RTX;
9960 else if (TREE_CODE (decl) == PARM_DECL)
9962 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
9964 tree declared_type = TREE_TYPE (decl);
9965 tree passed_type = DECL_ARG_TYPE (decl);
9966 enum machine_mode dmode = TYPE_MODE (declared_type);
9967 enum machine_mode pmode = TYPE_MODE (passed_type);
9969 /* This decl represents a formal parameter which was optimized out.
9970 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
9971 all cases where (rtl == NULL_RTX) just below. */
9972 if (dmode == pmode)
9973 rtl = DECL_INCOMING_RTL (decl);
9974 else if (SCALAR_INT_MODE_P (dmode)
9975 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
9976 && DECL_INCOMING_RTL (decl))
9978 rtx inc = DECL_INCOMING_RTL (decl);
9979 if (REG_P (inc))
9980 rtl = inc;
9981 else if (MEM_P (inc))
9983 if (BYTES_BIG_ENDIAN)
9984 rtl = adjust_address_nv (inc, dmode,
9985 GET_MODE_SIZE (pmode)
9986 - GET_MODE_SIZE (dmode));
9987 else
9988 rtl = inc;
9993 /* If the parm was passed in registers, but lives on the stack, then
9994 make a big endian correction if the mode of the type of the
9995 parameter is not the same as the mode of the rtl. */
9996 /* ??? This is the same series of checks that are made in dbxout.c before
9997 we reach the big endian correction code there. It isn't clear if all
9998 of these checks are necessary here, but keeping them all is the safe
9999 thing to do. */
10000 else if (MEM_P (rtl)
10001 && XEXP (rtl, 0) != const0_rtx
10002 && ! CONSTANT_P (XEXP (rtl, 0))
10003 /* Not passed in memory. */
10004 && !MEM_P (DECL_INCOMING_RTL (decl))
10005 /* Not passed by invisible reference. */
10006 && (!REG_P (XEXP (rtl, 0))
10007 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10008 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10009 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10010 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10011 #endif
10013 /* Big endian correction check. */
10014 && BYTES_BIG_ENDIAN
10015 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10016 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10017 < UNITS_PER_WORD))
10019 int offset = (UNITS_PER_WORD
10020 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10022 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10023 plus_constant (XEXP (rtl, 0), offset));
10026 else if (TREE_CODE (decl) == VAR_DECL
10027 && rtl
10028 && MEM_P (rtl)
10029 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10030 && BYTES_BIG_ENDIAN)
10032 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10033 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10035 /* If a variable is declared "register" yet is smaller than
10036 a register, then if we store the variable to memory, it
10037 looks like we're storing a register-sized value, when in
10038 fact we are not. We need to adjust the offset of the
10039 storage location to reflect the actual value's bytes,
10040 else gdb will not be able to display it. */
10041 if (rsize > dsize)
10042 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10043 plus_constant (XEXP (rtl, 0), rsize-dsize));
10046 if (rtl != NULL_RTX)
10048 rtl = eliminate_regs (rtl, 0, NULL_RTX);
10049 #ifdef LEAF_REG_REMAP
10050 if (current_function_uses_only_leaf_regs)
10051 leaf_renumber_regs_insn (rtl);
10052 #endif
10055 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10056 and will have been substituted directly into all expressions that use it.
10057 C does not have such a concept, but C++ and other languages do. */
10058 else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10059 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10061 if (rtl)
10062 rtl = targetm.delegitimize_address (rtl);
10064 /* If we don't look past the constant pool, we risk emitting a
10065 reference to a constant pool entry that isn't referenced from
10066 code, and thus is not emitted. */
10067 if (rtl)
10068 rtl = avoid_constant_pool_reference (rtl);
10070 return rtl;
10073 /* Return true if DECL's containing function has a frame base attribute.
10074 Return false otherwise. */
10076 static bool
10077 containing_function_has_frame_base (tree decl)
10079 tree declcontext = decl_function_context (decl);
10080 dw_die_ref context;
10081 dw_attr_ref attr;
10083 if (!declcontext)
10084 return false;
10086 context = lookup_decl_die (declcontext);
10087 if (!context)
10088 return false;
10090 for (attr = context->die_attr; attr; attr = attr->dw_attr_next)
10091 if (attr->dw_attr == DW_AT_frame_base)
10092 return true;
10093 return false;
10096 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10097 data attribute for a variable or a parameter. We generate the
10098 DW_AT_const_value attribute only in those cases where the given variable
10099 or parameter does not have a true "location" either in memory or in a
10100 register. This can happen (for example) when a constant is passed as an
10101 actual argument in a call to an inline function. (It's possible that
10102 these things can crop up in other ways also.) Note that one type of
10103 constant value which can be passed into an inlined function is a constant
10104 pointer. This can happen for example if an actual argument in an inlined
10105 function call evaluates to a compile-time constant address. */
10107 static void
10108 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10109 enum dwarf_attribute attr)
10111 rtx rtl;
10112 dw_loc_descr_ref descr;
10113 var_loc_list *loc_list;
10114 bool can_use_fb;
10115 struct var_loc_node *node;
10116 if (TREE_CODE (decl) == ERROR_MARK)
10117 return;
10119 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10120 || TREE_CODE (decl) == RESULT_DECL);
10122 can_use_fb = containing_function_has_frame_base (decl);
10124 /* See if we possibly have multiple locations for this variable. */
10125 loc_list = lookup_decl_loc (decl);
10127 /* If it truly has multiple locations, the first and last node will
10128 differ. */
10129 if (loc_list && loc_list->first != loc_list->last)
10131 const char *secname;
10132 const char *endname;
10133 dw_loc_list_ref list;
10134 rtx varloc;
10136 /* We need to figure out what section we should use as the base
10137 for the address ranges where a given location is valid.
10138 1. If this particular DECL has a section associated with it,
10139 use that.
10140 2. If this function has a section associated with it, use
10141 that.
10142 3. Otherwise, use the text section.
10143 XXX: If you split a variable across multiple sections, this
10144 won't notice. */
10146 if (DECL_SECTION_NAME (decl))
10148 tree sectree = DECL_SECTION_NAME (decl);
10149 secname = TREE_STRING_POINTER (sectree);
10151 else if (current_function_decl
10152 && DECL_SECTION_NAME (current_function_decl))
10154 tree sectree = DECL_SECTION_NAME (current_function_decl);
10155 secname = TREE_STRING_POINTER (sectree);
10157 else if (cfun
10158 && (last_text_section == in_unlikely_executed_text
10159 || (last_text_section == in_named
10160 && last_text_section_name ==
10161 cfun->unlikely_text_section_name)))
10162 secname = cfun->cold_section_label;
10163 else
10164 secname = text_section_label;
10166 /* Now that we know what section we are using for a base,
10167 actually construct the list of locations.
10168 The first location information is what is passed to the
10169 function that creates the location list, and the remaining
10170 locations just get added on to that list.
10171 Note that we only know the start address for a location
10172 (IE location changes), so to build the range, we use
10173 the range [current location start, next location start].
10174 This means we have to special case the last node, and generate
10175 a range of [last location start, end of function label]. */
10177 node = loc_list->first;
10178 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10179 list = new_loc_list (loc_descriptor (varloc, can_use_fb),
10180 node->label, node->next->label, secname, 1);
10181 node = node->next;
10183 for (; node->next; node = node->next)
10184 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10186 /* The variable has a location between NODE->LABEL and
10187 NODE->NEXT->LABEL. */
10188 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10189 add_loc_descr_to_loc_list (&list,
10190 loc_descriptor (varloc,
10191 can_use_fb),
10192 node->label, node->next->label, secname);
10195 /* If the variable has a location at the last label
10196 it keeps its location until the end of function. */
10197 if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10199 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10201 varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10202 if (!current_function_decl)
10203 endname = text_end_label;
10204 else
10206 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10207 current_function_funcdef_no);
10208 endname = ggc_strdup (label_id);
10210 add_loc_descr_to_loc_list (&list,
10211 loc_descriptor (varloc,
10212 can_use_fb),
10213 node->label, endname, secname);
10216 /* Finally, add the location list to the DIE, and we are done. */
10217 add_AT_loc_list (die, attr, list);
10218 return;
10221 /* Try to get some constant RTL for this decl, and use that as the value of
10222 the location. */
10224 rtl = rtl_for_decl_location (decl);
10225 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10227 add_const_value_attribute (die, rtl);
10228 return;
10231 /* We couldn't get any rtl, and we had no >1 element location list, so try
10232 directly generating the location description from the tree. */
10233 descr = loc_descriptor_from_tree (decl);
10234 if (descr)
10236 add_AT_location_description (die, attr, descr);
10237 return;
10240 /* Lastly, if we have tried to generate the location otherwise, and it
10241 didn't work out (we wouldn't be here if we did), and we have a one entry
10242 location list, try generating a location from that. */
10243 if (loc_list && loc_list->first)
10245 node = loc_list->first;
10246 descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note),
10247 can_use_fb);
10248 if (descr)
10249 add_AT_location_description (die, attr, descr);
10253 /* If we don't have a copy of this variable in memory for some reason (such
10254 as a C++ member constant that doesn't have an out-of-line definition),
10255 we should tell the debugger about the constant value. */
10257 static void
10258 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10260 tree init = DECL_INITIAL (decl);
10261 tree type = TREE_TYPE (decl);
10262 rtx rtl;
10264 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10265 /* OK */;
10266 else
10267 return;
10269 rtl = rtl_for_decl_init (init, type);
10270 if (rtl)
10271 add_const_value_attribute (var_die, rtl);
10274 /* Generate a DW_AT_name attribute given some string value to be included as
10275 the value of the attribute. */
10277 static void
10278 add_name_attribute (dw_die_ref die, const char *name_string)
10280 if (name_string != NULL && *name_string != 0)
10282 if (demangle_name_func)
10283 name_string = (*demangle_name_func) (name_string);
10285 add_AT_string (die, DW_AT_name, name_string);
10289 /* Generate a DW_AT_comp_dir attribute for DIE. */
10291 static void
10292 add_comp_dir_attribute (dw_die_ref die)
10294 const char *wd = get_src_pwd ();
10295 if (wd != NULL)
10296 add_AT_string (die, DW_AT_comp_dir, wd);
10299 /* Given a tree node describing an array bound (either lower or upper) output
10300 a representation for that bound. */
10302 static void
10303 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10305 switch (TREE_CODE (bound))
10307 case ERROR_MARK:
10308 return;
10310 /* All fixed-bounds are represented by INTEGER_CST nodes. */
10311 case INTEGER_CST:
10312 if (! host_integerp (bound, 0)
10313 || (bound_attr == DW_AT_lower_bound
10314 && (((is_c_family () || is_java ()) && integer_zerop (bound))
10315 || (is_fortran () && integer_onep (bound)))))
10316 /* Use the default. */
10318 else
10319 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
10320 break;
10322 case CONVERT_EXPR:
10323 case NOP_EXPR:
10324 case NON_LVALUE_EXPR:
10325 case VIEW_CONVERT_EXPR:
10326 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10327 break;
10329 case SAVE_EXPR:
10330 break;
10332 case VAR_DECL:
10333 case PARM_DECL:
10334 case RESULT_DECL:
10336 dw_die_ref decl_die = lookup_decl_die (bound);
10338 /* ??? Can this happen, or should the variable have been bound
10339 first? Probably it can, since I imagine that we try to create
10340 the types of parameters in the order in which they exist in
10341 the list, and won't have created a forward reference to a
10342 later parameter. */
10343 if (decl_die != NULL)
10344 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10345 break;
10348 default:
10350 /* Otherwise try to create a stack operation procedure to
10351 evaluate the value of the array bound. */
10353 dw_die_ref ctx, decl_die;
10354 dw_loc_descr_ref loc;
10356 loc = loc_descriptor_from_tree (bound);
10357 if (loc == NULL)
10358 break;
10360 if (current_function_decl == 0)
10361 ctx = comp_unit_die;
10362 else
10363 ctx = lookup_decl_die (current_function_decl);
10365 decl_die = new_die (DW_TAG_variable, ctx, bound);
10366 add_AT_flag (decl_die, DW_AT_artificial, 1);
10367 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10368 add_AT_loc (decl_die, DW_AT_location, loc);
10370 add_AT_die_ref (subrange_die, bound_attr, decl_die);
10371 break;
10376 /* Note that the block of subscript information for an array type also
10377 includes information about the element type of type given array type. */
10379 static void
10380 add_subscript_info (dw_die_ref type_die, tree type)
10382 #ifndef MIPS_DEBUGGING_INFO
10383 unsigned dimension_number;
10384 #endif
10385 tree lower, upper;
10386 dw_die_ref subrange_die;
10388 /* The GNU compilers represent multidimensional array types as sequences of
10389 one dimensional array types whose element types are themselves array
10390 types. Here we squish that down, so that each multidimensional array
10391 type gets only one array_type DIE in the Dwarf debugging info. The draft
10392 Dwarf specification say that we are allowed to do this kind of
10393 compression in C (because there is no difference between an array or
10394 arrays and a multidimensional array in C) but for other source languages
10395 (e.g. Ada) we probably shouldn't do this. */
10397 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10398 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10399 We work around this by disabling this feature. See also
10400 gen_array_type_die. */
10401 #ifndef MIPS_DEBUGGING_INFO
10402 for (dimension_number = 0;
10403 TREE_CODE (type) == ARRAY_TYPE;
10404 type = TREE_TYPE (type), dimension_number++)
10405 #endif
10407 tree domain = TYPE_DOMAIN (type);
10409 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
10410 and (in GNU C only) variable bounds. Handle all three forms
10411 here. */
10412 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
10413 if (domain)
10415 /* We have an array type with specified bounds. */
10416 lower = TYPE_MIN_VALUE (domain);
10417 upper = TYPE_MAX_VALUE (domain);
10419 /* Define the index type. */
10420 if (TREE_TYPE (domain))
10422 /* ??? This is probably an Ada unnamed subrange type. Ignore the
10423 TREE_TYPE field. We can't emit debug info for this
10424 because it is an unnamed integral type. */
10425 if (TREE_CODE (domain) == INTEGER_TYPE
10426 && TYPE_NAME (domain) == NULL_TREE
10427 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10428 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
10430 else
10431 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10432 type_die);
10435 /* ??? If upper is NULL, the array has unspecified length,
10436 but it does have a lower bound. This happens with Fortran
10437 dimension arr(N:*)
10438 Since the debugger is definitely going to need to know N
10439 to produce useful results, go ahead and output the lower
10440 bound solo, and hope the debugger can cope. */
10442 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
10443 if (upper)
10444 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
10447 /* Otherwise we have an array type with an unspecified length. The
10448 DWARF-2 spec does not say how to handle this; let's just leave out the
10449 bounds. */
10453 static void
10454 add_byte_size_attribute (dw_die_ref die, tree tree_node)
10456 unsigned size;
10458 switch (TREE_CODE (tree_node))
10460 case ERROR_MARK:
10461 size = 0;
10462 break;
10463 case ENUMERAL_TYPE:
10464 case RECORD_TYPE:
10465 case UNION_TYPE:
10466 case QUAL_UNION_TYPE:
10467 size = int_size_in_bytes (tree_node);
10468 break;
10469 case FIELD_DECL:
10470 /* For a data member of a struct or union, the DW_AT_byte_size is
10471 generally given as the number of bytes normally allocated for an
10472 object of the *declared* type of the member itself. This is true
10473 even for bit-fields. */
10474 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10475 break;
10476 default:
10477 gcc_unreachable ();
10480 /* Note that `size' might be -1 when we get to this point. If it is, that
10481 indicates that the byte size of the entity in question is variable. We
10482 have no good way of expressing this fact in Dwarf at the present time,
10483 so just let the -1 pass on through. */
10484 add_AT_unsigned (die, DW_AT_byte_size, size);
10487 /* For a FIELD_DECL node which represents a bit-field, output an attribute
10488 which specifies the distance in bits from the highest order bit of the
10489 "containing object" for the bit-field to the highest order bit of the
10490 bit-field itself.
10492 For any given bit-field, the "containing object" is a hypothetical object
10493 (of some integral or enum type) within which the given bit-field lives. The
10494 type of this hypothetical "containing object" is always the same as the
10495 declared type of the individual bit-field itself. The determination of the
10496 exact location of the "containing object" for a bit-field is rather
10497 complicated. It's handled by the `field_byte_offset' function (above).
10499 Note that it is the size (in bytes) of the hypothetical "containing object"
10500 which will be given in the DW_AT_byte_size attribute for this bit-field.
10501 (See `byte_size_attribute' above). */
10503 static inline void
10504 add_bit_offset_attribute (dw_die_ref die, tree decl)
10506 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10507 tree type = DECL_BIT_FIELD_TYPE (decl);
10508 HOST_WIDE_INT bitpos_int;
10509 HOST_WIDE_INT highest_order_object_bit_offset;
10510 HOST_WIDE_INT highest_order_field_bit_offset;
10511 HOST_WIDE_INT unsigned bit_offset;
10513 /* Must be a field and a bit field. */
10514 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
10516 /* We can't yet handle bit-fields whose offsets are variable, so if we
10517 encounter such things, just return without generating any attribute
10518 whatsoever. Likewise for variable or too large size. */
10519 if (! host_integerp (bit_position (decl), 0)
10520 || ! host_integerp (DECL_SIZE (decl), 1))
10521 return;
10523 bitpos_int = int_bit_position (decl);
10525 /* Note that the bit offset is always the distance (in bits) from the
10526 highest-order bit of the "containing object" to the highest-order bit of
10527 the bit-field itself. Since the "high-order end" of any object or field
10528 is different on big-endian and little-endian machines, the computation
10529 below must take account of these differences. */
10530 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10531 highest_order_field_bit_offset = bitpos_int;
10533 if (! BYTES_BIG_ENDIAN)
10535 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
10536 highest_order_object_bit_offset += simple_type_size_in_bits (type);
10539 bit_offset
10540 = (! BYTES_BIG_ENDIAN
10541 ? highest_order_object_bit_offset - highest_order_field_bit_offset
10542 : highest_order_field_bit_offset - highest_order_object_bit_offset);
10544 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10547 /* For a FIELD_DECL node which represents a bit field, output an attribute
10548 which specifies the length in bits of the given field. */
10550 static inline void
10551 add_bit_size_attribute (dw_die_ref die, tree decl)
10553 /* Must be a field and a bit field. */
10554 gcc_assert (TREE_CODE (decl) == FIELD_DECL
10555 && DECL_BIT_FIELD_TYPE (decl));
10557 if (host_integerp (DECL_SIZE (decl), 1))
10558 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
10561 /* If the compiled language is ANSI C, then add a 'prototyped'
10562 attribute, if arg types are given for the parameters of a function. */
10564 static inline void
10565 add_prototyped_attribute (dw_die_ref die, tree func_type)
10567 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10568 && TYPE_ARG_TYPES (func_type) != NULL)
10569 add_AT_flag (die, DW_AT_prototyped, 1);
10572 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
10573 by looking in either the type declaration or object declaration
10574 equate table. */
10576 static inline void
10577 add_abstract_origin_attribute (dw_die_ref die, tree origin)
10579 dw_die_ref origin_die = NULL;
10581 if (TREE_CODE (origin) != FUNCTION_DECL)
10583 /* We may have gotten separated from the block for the inlined
10584 function, if we're in an exception handler or some such; make
10585 sure that the abstract function has been written out.
10587 Doing this for nested functions is wrong, however; functions are
10588 distinct units, and our context might not even be inline. */
10589 tree fn = origin;
10591 if (TYPE_P (fn))
10592 fn = TYPE_STUB_DECL (fn);
10594 fn = decl_function_context (fn);
10595 if (fn)
10596 dwarf2out_abstract_function (fn);
10599 if (DECL_P (origin))
10600 origin_die = lookup_decl_die (origin);
10601 else if (TYPE_P (origin))
10602 origin_die = lookup_type_die (origin);
10604 /* XXX: Functions that are never lowered don't always have correct block
10605 trees (in the case of java, they simply have no block tree, in some other
10606 languages). For these functions, there is nothing we can really do to
10607 output correct debug info for inlined functions in all cases. Rather
10608 than die, we'll just produce deficient debug info now, in that we will
10609 have variables without a proper abstract origin. In the future, when all
10610 functions are lowered, we should re-add a gcc_assert (origin_die)
10611 here. */
10613 if (origin_die)
10614 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
10617 /* We do not currently support the pure_virtual attribute. */
10619 static inline void
10620 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
10622 if (DECL_VINDEX (func_decl))
10624 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10626 if (host_integerp (DECL_VINDEX (func_decl), 0))
10627 add_AT_loc (die, DW_AT_vtable_elem_location,
10628 new_loc_descr (DW_OP_constu,
10629 tree_low_cst (DECL_VINDEX (func_decl), 0),
10630 0));
10632 /* GNU extension: Record what type this method came from originally. */
10633 if (debug_info_level > DINFO_LEVEL_TERSE)
10634 add_AT_die_ref (die, DW_AT_containing_type,
10635 lookup_type_die (DECL_CONTEXT (func_decl)));
10639 /* Add source coordinate attributes for the given decl. */
10641 static void
10642 add_src_coords_attributes (dw_die_ref die, tree decl)
10644 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
10645 unsigned file_index = lookup_filename (s.file);
10647 add_AT_unsigned (die, DW_AT_decl_file, file_index);
10648 add_AT_unsigned (die, DW_AT_decl_line, s.line);
10651 /* Add a DW_AT_name attribute and source coordinate attribute for the
10652 given decl, but only if it actually has a name. */
10654 static void
10655 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
10657 tree decl_name;
10659 decl_name = DECL_NAME (decl);
10660 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
10662 add_name_attribute (die, dwarf2_name (decl, 0));
10663 if (! DECL_ARTIFICIAL (decl))
10664 add_src_coords_attributes (die, decl);
10666 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
10667 && TREE_PUBLIC (decl)
10668 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
10669 && !DECL_ABSTRACT (decl))
10670 add_AT_string (die, DW_AT_MIPS_linkage_name,
10671 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
10674 #ifdef VMS_DEBUGGING_INFO
10675 /* Get the function's name, as described by its RTL. This may be different
10676 from the DECL_NAME name used in the source file. */
10677 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
10679 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
10680 XEXP (DECL_RTL (decl), 0));
10681 VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
10683 #endif
10686 /* Push a new declaration scope. */
10688 static void
10689 push_decl_scope (tree scope)
10691 VEC_safe_push (tree, gc, decl_scope_table, scope);
10694 /* Pop a declaration scope. */
10696 static inline void
10697 pop_decl_scope (void)
10699 VEC_pop (tree, decl_scope_table);
10702 /* Return the DIE for the scope that immediately contains this type.
10703 Non-named types get global scope. Named types nested in other
10704 types get their containing scope if it's open, or global scope
10705 otherwise. All other types (i.e. function-local named types) get
10706 the current active scope. */
10708 static dw_die_ref
10709 scope_die_for (tree t, dw_die_ref context_die)
10711 dw_die_ref scope_die = NULL;
10712 tree containing_scope;
10713 int i;
10715 /* Non-types always go in the current scope. */
10716 gcc_assert (TYPE_P (t));
10718 containing_scope = TYPE_CONTEXT (t);
10720 /* Use the containing namespace if it was passed in (for a declaration). */
10721 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
10723 if (context_die == lookup_decl_die (containing_scope))
10724 /* OK */;
10725 else
10726 containing_scope = NULL_TREE;
10729 /* Ignore function type "scopes" from the C frontend. They mean that
10730 a tagged type is local to a parmlist of a function declarator, but
10731 that isn't useful to DWARF. */
10732 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
10733 containing_scope = NULL_TREE;
10735 if (containing_scope == NULL_TREE)
10736 scope_die = comp_unit_die;
10737 else if (TYPE_P (containing_scope))
10739 /* For types, we can just look up the appropriate DIE. But
10740 first we check to see if we're in the middle of emitting it
10741 so we know where the new DIE should go. */
10742 for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
10743 if (VEC_index (tree, decl_scope_table, i) == containing_scope)
10744 break;
10746 if (i < 0)
10748 gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
10749 || TREE_ASM_WRITTEN (containing_scope));
10751 /* If none of the current dies are suitable, we get file scope. */
10752 scope_die = comp_unit_die;
10754 else
10755 scope_die = lookup_type_die (containing_scope);
10757 else
10758 scope_die = context_die;
10760 return scope_die;
10763 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
10765 static inline int
10766 local_scope_p (dw_die_ref context_die)
10768 for (; context_die; context_die = context_die->die_parent)
10769 if (context_die->die_tag == DW_TAG_inlined_subroutine
10770 || context_die->die_tag == DW_TAG_subprogram)
10771 return 1;
10773 return 0;
10776 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
10777 whether or not to treat a DIE in this context as a declaration. */
10779 static inline int
10780 class_or_namespace_scope_p (dw_die_ref context_die)
10782 return (context_die
10783 && (context_die->die_tag == DW_TAG_structure_type
10784 || context_die->die_tag == DW_TAG_union_type
10785 || context_die->die_tag == DW_TAG_namespace));
10788 /* Many forms of DIEs require a "type description" attribute. This
10789 routine locates the proper "type descriptor" die for the type given
10790 by 'type', and adds a DW_AT_type attribute below the given die. */
10792 static void
10793 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
10794 int decl_volatile, dw_die_ref context_die)
10796 enum tree_code code = TREE_CODE (type);
10797 dw_die_ref type_die = NULL;
10799 /* ??? If this type is an unnamed subrange type of an integral or
10800 floating-point type, use the inner type. This is because we have no
10801 support for unnamed types in base_type_die. This can happen if this is
10802 an Ada subrange type. Correct solution is emit a subrange type die. */
10803 if ((code == INTEGER_TYPE || code == REAL_TYPE)
10804 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
10805 type = TREE_TYPE (type), code = TREE_CODE (type);
10807 if (code == ERROR_MARK
10808 /* Handle a special case. For functions whose return type is void, we
10809 generate *no* type attribute. (Note that no object may have type
10810 `void', so this only applies to function return types). */
10811 || code == VOID_TYPE)
10812 return;
10814 type_die = modified_type_die (type,
10815 decl_const || TYPE_READONLY (type),
10816 decl_volatile || TYPE_VOLATILE (type),
10817 context_die);
10819 if (type_die != NULL)
10820 add_AT_die_ref (object_die, DW_AT_type, type_die);
10823 /* Given an object die, add the calling convention attribute for the
10824 function call type. */
10825 static void
10826 add_calling_convention_attribute (dw_die_ref subr_die, tree type)
10828 enum dwarf_calling_convention value = DW_CC_normal;
10830 value = targetm.dwarf_calling_convention (type);
10832 /* Only add the attribute if the backend requests it, and
10833 is not DW_CC_normal. */
10834 if (value && (value != DW_CC_normal))
10835 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
10838 /* Given a tree pointer to a struct, class, union, or enum type node, return
10839 a pointer to the (string) tag name for the given type, or zero if the type
10840 was declared without a tag. */
10842 static const char *
10843 type_tag (tree type)
10845 const char *name = 0;
10847 if (TYPE_NAME (type) != 0)
10849 tree t = 0;
10851 /* Find the IDENTIFIER_NODE for the type name. */
10852 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
10853 t = TYPE_NAME (type);
10855 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
10856 a TYPE_DECL node, regardless of whether or not a `typedef' was
10857 involved. */
10858 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10859 && ! DECL_IGNORED_P (TYPE_NAME (type)))
10860 t = DECL_NAME (TYPE_NAME (type));
10862 /* Now get the name as a string, or invent one. */
10863 if (t != 0)
10864 name = IDENTIFIER_POINTER (t);
10867 return (name == 0 || *name == '\0') ? 0 : name;
10870 /* Return the type associated with a data member, make a special check
10871 for bit field types. */
10873 static inline tree
10874 member_declared_type (tree member)
10876 return (DECL_BIT_FIELD_TYPE (member)
10877 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
10880 /* Get the decl's label, as described by its RTL. This may be different
10881 from the DECL_NAME name used in the source file. */
10883 #if 0
10884 static const char *
10885 decl_start_label (tree decl)
10887 rtx x;
10888 const char *fnname;
10890 x = DECL_RTL (decl);
10891 gcc_assert (MEM_P (x));
10893 x = XEXP (x, 0);
10894 gcc_assert (GET_CODE (x) == SYMBOL_REF);
10896 fnname = XSTR (x, 0);
10897 return fnname;
10899 #endif
10901 /* These routines generate the internal representation of the DIE's for
10902 the compilation unit. Debugging information is collected by walking
10903 the declaration trees passed in from dwarf2out_decl(). */
10905 static void
10906 gen_array_type_die (tree type, dw_die_ref context_die)
10908 dw_die_ref scope_die = scope_die_for (type, context_die);
10909 dw_die_ref array_die;
10910 tree element_type;
10912 /* ??? The SGI dwarf reader fails for array of array of enum types unless
10913 the inner array type comes before the outer array type. Thus we must
10914 call gen_type_die before we call new_die. See below also. */
10915 #ifdef MIPS_DEBUGGING_INFO
10916 gen_type_die (TREE_TYPE (type), context_die);
10917 #endif
10919 array_die = new_die (DW_TAG_array_type, scope_die, type);
10920 add_name_attribute (array_die, type_tag (type));
10921 equate_type_number_to_die (type, array_die);
10923 if (TREE_CODE (type) == VECTOR_TYPE)
10925 /* The frontend feeds us a representation for the vector as a struct
10926 containing an array. Pull out the array type. */
10927 type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
10928 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
10931 #if 0
10932 /* We default the array ordering. SDB will probably do
10933 the right things even if DW_AT_ordering is not present. It's not even
10934 an issue until we start to get into multidimensional arrays anyway. If
10935 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
10936 then we'll have to put the DW_AT_ordering attribute back in. (But if
10937 and when we find out that we need to put these in, we will only do so
10938 for multidimensional arrays. */
10939 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
10940 #endif
10942 #ifdef MIPS_DEBUGGING_INFO
10943 /* The SGI compilers handle arrays of unknown bound by setting
10944 AT_declaration and not emitting any subrange DIEs. */
10945 if (! TYPE_DOMAIN (type))
10946 add_AT_flag (array_die, DW_AT_declaration, 1);
10947 else
10948 #endif
10949 add_subscript_info (array_die, type);
10951 /* Add representation of the type of the elements of this array type. */
10952 element_type = TREE_TYPE (type);
10954 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10955 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
10956 We work around this by disabling this feature. See also
10957 add_subscript_info. */
10958 #ifndef MIPS_DEBUGGING_INFO
10959 while (TREE_CODE (element_type) == ARRAY_TYPE)
10960 element_type = TREE_TYPE (element_type);
10962 gen_type_die (element_type, context_die);
10963 #endif
10965 add_type_attribute (array_die, element_type, 0, 0, context_die);
10968 #if 0
10969 static void
10970 gen_entry_point_die (tree decl, dw_die_ref context_die)
10972 tree origin = decl_ultimate_origin (decl);
10973 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
10975 if (origin != NULL)
10976 add_abstract_origin_attribute (decl_die, origin);
10977 else
10979 add_name_and_src_coords_attributes (decl_die, decl);
10980 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
10981 0, 0, context_die);
10984 if (DECL_ABSTRACT (decl))
10985 equate_decl_number_to_die (decl, decl_die);
10986 else
10987 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
10989 #endif
10991 /* Walk through the list of incomplete types again, trying once more to
10992 emit full debugging info for them. */
10994 static void
10995 retry_incomplete_types (void)
10997 int i;
10999 for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11000 gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
11003 /* Generate a DIE to represent an inlined instance of an enumeration type. */
11005 static void
11006 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
11008 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
11010 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11011 be incomplete and such types are not marked. */
11012 add_abstract_origin_attribute (type_die, type);
11015 /* Generate a DIE to represent an inlined instance of a structure type. */
11017 static void
11018 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
11020 dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
11022 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11023 be incomplete and such types are not marked. */
11024 add_abstract_origin_attribute (type_die, type);
11027 /* Generate a DIE to represent an inlined instance of a union type. */
11029 static void
11030 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
11032 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
11034 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11035 be incomplete and such types are not marked. */
11036 add_abstract_origin_attribute (type_die, type);
11039 /* Generate a DIE to represent an enumeration type. Note that these DIEs
11040 include all of the information about the enumeration values also. Each
11041 enumerated type name/value is listed as a child of the enumerated type
11042 DIE. */
11044 static dw_die_ref
11045 gen_enumeration_type_die (tree type, dw_die_ref context_die)
11047 dw_die_ref type_die = lookup_type_die (type);
11049 if (type_die == NULL)
11051 type_die = new_die (DW_TAG_enumeration_type,
11052 scope_die_for (type, context_die), type);
11053 equate_type_number_to_die (type, type_die);
11054 add_name_attribute (type_die, type_tag (type));
11056 else if (! TYPE_SIZE (type))
11057 return type_die;
11058 else
11059 remove_AT (type_die, DW_AT_declaration);
11061 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
11062 given enum type is incomplete, do not generate the DW_AT_byte_size
11063 attribute or the DW_AT_element_list attribute. */
11064 if (TYPE_SIZE (type))
11066 tree link;
11068 TREE_ASM_WRITTEN (type) = 1;
11069 add_byte_size_attribute (type_die, type);
11070 if (TYPE_STUB_DECL (type) != NULL_TREE)
11071 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11073 /* If the first reference to this type was as the return type of an
11074 inline function, then it may not have a parent. Fix this now. */
11075 if (type_die->die_parent == NULL)
11076 add_child_die (scope_die_for (type, context_die), type_die);
11078 for (link = TYPE_VALUES (type);
11079 link != NULL; link = TREE_CHAIN (link))
11081 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
11082 tree value = TREE_VALUE (link);
11084 add_name_attribute (enum_die,
11085 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
11087 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
11088 /* DWARF2 does not provide a way of indicating whether or
11089 not enumeration constants are signed or unsigned. GDB
11090 always assumes the values are signed, so we output all
11091 values as if they were signed. That means that
11092 enumeration constants with very large unsigned values
11093 will appear to have negative values in the debugger. */
11094 add_AT_int (enum_die, DW_AT_const_value,
11095 tree_low_cst (value, tree_int_cst_sgn (value) > 0));
11098 else
11099 add_AT_flag (type_die, DW_AT_declaration, 1);
11101 return type_die;
11104 /* Generate a DIE to represent either a real live formal parameter decl or to
11105 represent just the type of some formal parameter position in some function
11106 type.
11108 Note that this routine is a bit unusual because its argument may be a
11109 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
11110 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
11111 node. If it's the former then this function is being called to output a
11112 DIE to represent a formal parameter object (or some inlining thereof). If
11113 it's the latter, then this function is only being called to output a
11114 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
11115 argument type of some subprogram type. */
11117 static dw_die_ref
11118 gen_formal_parameter_die (tree node, dw_die_ref context_die)
11120 dw_die_ref parm_die
11121 = new_die (DW_TAG_formal_parameter, context_die, node);
11122 tree origin;
11124 switch (TREE_CODE_CLASS (TREE_CODE (node)))
11126 case tcc_declaration:
11127 origin = decl_ultimate_origin (node);
11128 if (origin != NULL)
11129 add_abstract_origin_attribute (parm_die, origin);
11130 else
11132 add_name_and_src_coords_attributes (parm_die, node);
11133 add_type_attribute (parm_die, TREE_TYPE (node),
11134 TREE_READONLY (node),
11135 TREE_THIS_VOLATILE (node),
11136 context_die);
11137 if (DECL_ARTIFICIAL (node))
11138 add_AT_flag (parm_die, DW_AT_artificial, 1);
11141 equate_decl_number_to_die (node, parm_die);
11142 if (! DECL_ABSTRACT (node))
11143 add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
11145 break;
11147 case tcc_type:
11148 /* We were called with some kind of a ..._TYPE node. */
11149 add_type_attribute (parm_die, node, 0, 0, context_die);
11150 break;
11152 default:
11153 gcc_unreachable ();
11156 return parm_die;
11159 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11160 at the end of an (ANSI prototyped) formal parameters list. */
11162 static void
11163 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11165 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11168 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11169 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11170 parameters as specified in some function type specification (except for
11171 those which appear as part of a function *definition*). */
11173 static void
11174 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11176 tree link;
11177 tree formal_type = NULL;
11178 tree first_parm_type;
11179 tree arg;
11181 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11183 arg = DECL_ARGUMENTS (function_or_method_type);
11184 function_or_method_type = TREE_TYPE (function_or_method_type);
11186 else
11187 arg = NULL_TREE;
11189 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11191 /* Make our first pass over the list of formal parameter types and output a
11192 DW_TAG_formal_parameter DIE for each one. */
11193 for (link = first_parm_type; link; )
11195 dw_die_ref parm_die;
11197 formal_type = TREE_VALUE (link);
11198 if (formal_type == void_type_node)
11199 break;
11201 /* Output a (nameless) DIE to represent the formal parameter itself. */
11202 parm_die = gen_formal_parameter_die (formal_type, context_die);
11203 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11204 && link == first_parm_type)
11205 || (arg && DECL_ARTIFICIAL (arg)))
11206 add_AT_flag (parm_die, DW_AT_artificial, 1);
11208 link = TREE_CHAIN (link);
11209 if (arg)
11210 arg = TREE_CHAIN (arg);
11213 /* If this function type has an ellipsis, add a
11214 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
11215 if (formal_type != void_type_node)
11216 gen_unspecified_parameters_die (function_or_method_type, context_die);
11218 /* Make our second (and final) pass over the list of formal parameter types
11219 and output DIEs to represent those types (as necessary). */
11220 for (link = TYPE_ARG_TYPES (function_or_method_type);
11221 link && TREE_VALUE (link);
11222 link = TREE_CHAIN (link))
11223 gen_type_die (TREE_VALUE (link), context_die);
11226 /* We want to generate the DIE for TYPE so that we can generate the
11227 die for MEMBER, which has been defined; we will need to refer back
11228 to the member declaration nested within TYPE. If we're trying to
11229 generate minimal debug info for TYPE, processing TYPE won't do the
11230 trick; we need to attach the member declaration by hand. */
11232 static void
11233 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11235 gen_type_die (type, context_die);
11237 /* If we're trying to avoid duplicate debug info, we may not have
11238 emitted the member decl for this function. Emit it now. */
11239 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11240 && ! lookup_decl_die (member))
11242 dw_die_ref type_die;
11243 gcc_assert (!decl_ultimate_origin (member));
11245 push_decl_scope (type);
11246 type_die = lookup_type_die (type);
11247 if (TREE_CODE (member) == FUNCTION_DECL)
11248 gen_subprogram_die (member, type_die);
11249 else if (TREE_CODE (member) == FIELD_DECL)
11251 /* Ignore the nameless fields that are used to skip bits but handle
11252 C++ anonymous unions and structs. */
11253 if (DECL_NAME (member) != NULL_TREE
11254 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11255 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11257 gen_type_die (member_declared_type (member), type_die);
11258 gen_field_die (member, type_die);
11261 else
11262 gen_variable_die (member, type_die);
11264 pop_decl_scope ();
11268 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11269 may later generate inlined and/or out-of-line instances of. */
11271 static void
11272 dwarf2out_abstract_function (tree decl)
11274 dw_die_ref old_die;
11275 tree save_fn;
11276 tree context;
11277 int was_abstract = DECL_ABSTRACT (decl);
11279 /* Make sure we have the actual abstract inline, not a clone. */
11280 decl = DECL_ORIGIN (decl);
11282 old_die = lookup_decl_die (decl);
11283 if (old_die && get_AT (old_die, DW_AT_inline))
11284 /* We've already generated the abstract instance. */
11285 return;
11287 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11288 we don't get confused by DECL_ABSTRACT. */
11289 if (debug_info_level > DINFO_LEVEL_TERSE)
11291 context = decl_class_context (decl);
11292 if (context)
11293 gen_type_die_for_member
11294 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11297 /* Pretend we've just finished compiling this function. */
11298 save_fn = current_function_decl;
11299 current_function_decl = decl;
11301 set_decl_abstract_flags (decl, 1);
11302 dwarf2out_decl (decl);
11303 if (! was_abstract)
11304 set_decl_abstract_flags (decl, 0);
11306 current_function_decl = save_fn;
11309 /* Generate a DIE to represent a declared function (either file-scope or
11310 block-local). */
11312 static void
11313 gen_subprogram_die (tree decl, dw_die_ref context_die)
11315 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11316 tree origin = decl_ultimate_origin (decl);
11317 dw_die_ref subr_die;
11318 rtx fp_reg;
11319 tree fn_arg_types;
11320 tree outer_scope;
11321 dw_die_ref old_die = lookup_decl_die (decl);
11322 int declaration = (current_function_decl != decl
11323 || class_or_namespace_scope_p (context_die));
11325 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11326 started to generate the abstract instance of an inline, decided to output
11327 its containing class, and proceeded to emit the declaration of the inline
11328 from the member list for the class. If so, DECLARATION takes priority;
11329 we'll get back to the abstract instance when done with the class. */
11331 /* The class-scope declaration DIE must be the primary DIE. */
11332 if (origin && declaration && class_or_namespace_scope_p (context_die))
11334 origin = NULL;
11335 gcc_assert (!old_die);
11338 if (origin != NULL)
11340 gcc_assert (!declaration || local_scope_p (context_die));
11342 /* Fixup die_parent for the abstract instance of a nested
11343 inline function. */
11344 if (old_die && old_die->die_parent == NULL)
11345 add_child_die (context_die, old_die);
11347 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11348 add_abstract_origin_attribute (subr_die, origin);
11350 else if (old_die)
11352 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11353 unsigned file_index = lookup_filename (s.file);
11355 if (!get_AT_flag (old_die, DW_AT_declaration)
11356 /* We can have a normal definition following an inline one in the
11357 case of redefinition of GNU C extern inlines.
11358 It seems reasonable to use AT_specification in this case. */
11359 && !get_AT (old_die, DW_AT_inline))
11361 /* Detect and ignore this case, where we are trying to output
11362 something we have already output. */
11363 return;
11366 /* If the definition comes from the same place as the declaration,
11367 maybe use the old DIE. We always want the DIE for this function
11368 that has the *_pc attributes to be under comp_unit_die so the
11369 debugger can find it. We also need to do this for abstract
11370 instances of inlines, since the spec requires the out-of-line copy
11371 to have the same parent. For local class methods, this doesn't
11372 apply; we just use the old DIE. */
11373 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
11374 && (DECL_ARTIFICIAL (decl)
11375 || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
11376 && (get_AT_unsigned (old_die, DW_AT_decl_line)
11377 == (unsigned) s.line))))
11379 subr_die = old_die;
11381 /* Clear out the declaration attribute and the formal parameters.
11382 Do not remove all children, because it is possible that this
11383 declaration die was forced using force_decl_die(). In such
11384 cases die that forced declaration die (e.g. TAG_imported_module)
11385 is one of the children that we do not want to remove. */
11386 remove_AT (subr_die, DW_AT_declaration);
11387 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
11389 else
11391 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11392 add_AT_specification (subr_die, old_die);
11393 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11394 add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
11395 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11396 != (unsigned) s.line)
11397 add_AT_unsigned
11398 (subr_die, DW_AT_decl_line, s.line);
11401 else
11403 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11405 if (TREE_PUBLIC (decl))
11406 add_AT_flag (subr_die, DW_AT_external, 1);
11408 add_name_and_src_coords_attributes (subr_die, decl);
11409 if (debug_info_level > DINFO_LEVEL_TERSE)
11411 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11412 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11413 0, 0, context_die);
11416 add_pure_or_virtual_attribute (subr_die, decl);
11417 if (DECL_ARTIFICIAL (decl))
11418 add_AT_flag (subr_die, DW_AT_artificial, 1);
11420 if (TREE_PROTECTED (decl))
11421 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11422 else if (TREE_PRIVATE (decl))
11423 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
11426 if (declaration)
11428 if (!old_die || !get_AT (old_die, DW_AT_inline))
11430 add_AT_flag (subr_die, DW_AT_declaration, 1);
11432 /* The first time we see a member function, it is in the context of
11433 the class to which it belongs. We make sure of this by emitting
11434 the class first. The next time is the definition, which is
11435 handled above. The two may come from the same source text.
11437 Note that force_decl_die() forces function declaration die. It is
11438 later reused to represent definition. */
11439 equate_decl_number_to_die (decl, subr_die);
11442 else if (DECL_ABSTRACT (decl))
11444 if (DECL_DECLARED_INLINE_P (decl))
11446 if (cgraph_function_possibly_inlined_p (decl))
11447 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11448 else
11449 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
11451 else
11453 if (cgraph_function_possibly_inlined_p (decl))
11454 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11455 else
11456 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11459 equate_decl_number_to_die (decl, subr_die);
11461 else if (!DECL_EXTERNAL (decl))
11463 if (!old_die || !get_AT (old_die, DW_AT_inline))
11464 equate_decl_number_to_die (decl, subr_die);
11466 if (!flag_reorder_blocks_and_partition)
11468 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11469 current_function_funcdef_no);
11470 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11471 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11472 current_function_funcdef_no);
11473 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11475 add_pubname (decl, subr_die);
11476 add_arange (decl, subr_die);
11478 else
11479 { /* Do nothing for now; maybe need to duplicate die, one for
11480 hot section and ond for cold section, then use the hot/cold
11481 section begin/end labels to generate the aranges... */
11483 add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
11484 add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
11485 add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
11486 add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
11488 add_pubname (decl, subr_die);
11489 add_arange (decl, subr_die);
11490 add_arange (decl, subr_die);
11494 #ifdef MIPS_DEBUGGING_INFO
11495 /* Add a reference to the FDE for this routine. */
11496 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11497 #endif
11499 /* Define the "frame base" location for this routine. We use the
11500 frame pointer or stack pointer registers, since the RTL for local
11501 variables is relative to one of them. */
11502 if (frame_base_decl && lookup_decl_loc (frame_base_decl) != NULL)
11504 add_location_or_const_value_attribute (subr_die, frame_base_decl,
11505 DW_AT_frame_base);
11507 else
11509 fp_reg
11510 = frame_pointer_needed ? hard_frame_pointer_rtx : stack_pointer_rtx;
11511 add_AT_loc (subr_die, DW_AT_frame_base, reg_loc_descriptor (fp_reg));
11514 if (cfun->static_chain_decl)
11515 add_AT_location_description (subr_die, DW_AT_static_link,
11516 loc_descriptor_from_tree (cfun->static_chain_decl));
11519 /* Now output descriptions of the arguments for this function. This gets
11520 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
11521 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
11522 `...' at the end of the formal parameter list. In order to find out if
11523 there was a trailing ellipsis or not, we must instead look at the type
11524 associated with the FUNCTION_DECL. This will be a node of type
11525 FUNCTION_TYPE. If the chain of type nodes hanging off of this
11526 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
11527 an ellipsis at the end. */
11529 /* In the case where we are describing a mere function declaration, all we
11530 need to do here (and all we *can* do here) is to describe the *types* of
11531 its formal parameters. */
11532 if (debug_info_level <= DINFO_LEVEL_TERSE)
11534 else if (declaration)
11535 gen_formal_types_die (decl, subr_die);
11536 else
11538 /* Generate DIEs to represent all known formal parameters. */
11539 tree arg_decls = DECL_ARGUMENTS (decl);
11540 tree parm;
11542 /* When generating DIEs, generate the unspecified_parameters DIE
11543 instead if we come across the arg "__builtin_va_alist" */
11544 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
11545 if (TREE_CODE (parm) == PARM_DECL)
11547 if (DECL_NAME (parm)
11548 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
11549 "__builtin_va_alist"))
11550 gen_unspecified_parameters_die (parm, subr_die);
11551 else
11552 gen_decl_die (parm, subr_die);
11555 /* Decide whether we need an unspecified_parameters DIE at the end.
11556 There are 2 more cases to do this for: 1) the ansi ... declaration -
11557 this is detectable when the end of the arg list is not a
11558 void_type_node 2) an unprototyped function declaration (not a
11559 definition). This just means that we have no info about the
11560 parameters at all. */
11561 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
11562 if (fn_arg_types != NULL)
11564 /* This is the prototyped case, check for.... */
11565 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
11566 gen_unspecified_parameters_die (decl, subr_die);
11568 else if (DECL_INITIAL (decl) == NULL_TREE)
11569 gen_unspecified_parameters_die (decl, subr_die);
11572 /* Output Dwarf info for all of the stuff within the body of the function
11573 (if it has one - it may be just a declaration). */
11574 outer_scope = DECL_INITIAL (decl);
11576 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
11577 a function. This BLOCK actually represents the outermost binding contour
11578 for the function, i.e. the contour in which the function's formal
11579 parameters and labels get declared. Curiously, it appears that the front
11580 end doesn't actually put the PARM_DECL nodes for the current function onto
11581 the BLOCK_VARS list for this outer scope, but are strung off of the
11582 DECL_ARGUMENTS list for the function instead.
11584 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
11585 the LABEL_DECL nodes for the function however, and we output DWARF info
11586 for those in decls_for_scope. Just within the `outer_scope' there will be
11587 a BLOCK node representing the function's outermost pair of curly braces,
11588 and any blocks used for the base and member initializers of a C++
11589 constructor function. */
11590 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
11592 /* Emit a DW_TAG_variable DIE for a named return value. */
11593 if (DECL_NAME (DECL_RESULT (decl)))
11594 gen_decl_die (DECL_RESULT (decl), subr_die);
11596 current_function_has_inlines = 0;
11597 decls_for_scope (outer_scope, subr_die, 0);
11599 #if 0 && defined (MIPS_DEBUGGING_INFO)
11600 if (current_function_has_inlines)
11602 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
11603 if (! comp_unit_has_inlines)
11605 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
11606 comp_unit_has_inlines = 1;
11609 #endif
11611 /* Add the calling convention attribute if requested. */
11612 add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
11616 /* Generate a DIE to represent a declared data object. */
11618 static void
11619 gen_variable_die (tree decl, dw_die_ref context_die)
11621 tree origin = decl_ultimate_origin (decl);
11622 dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
11624 dw_die_ref old_die = lookup_decl_die (decl);
11625 int declaration = (DECL_EXTERNAL (decl)
11626 || class_or_namespace_scope_p (context_die));
11628 if (origin != NULL)
11629 add_abstract_origin_attribute (var_die, origin);
11631 /* Loop unrolling can create multiple blocks that refer to the same
11632 static variable, so we must test for the DW_AT_declaration flag.
11634 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
11635 copy decls and set the DECL_ABSTRACT flag on them instead of
11636 sharing them.
11638 ??? Duplicated blocks have been rewritten to use .debug_ranges.
11640 ??? The declare_in_namespace support causes us to get two DIEs for one
11641 variable, both of which are declarations. We want to avoid considering
11642 one to be a specification, so we must test that this DIE is not a
11643 declaration. */
11644 else if (old_die && TREE_STATIC (decl) && ! declaration
11645 && get_AT_flag (old_die, DW_AT_declaration) == 1)
11647 /* This is a definition of a C++ class level static. */
11648 add_AT_specification (var_die, old_die);
11649 if (DECL_NAME (decl))
11651 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11652 unsigned file_index = lookup_filename (s.file);
11654 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11655 add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
11657 if (get_AT_unsigned (old_die, DW_AT_decl_line)
11658 != (unsigned) s.line)
11660 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
11663 else
11665 add_name_and_src_coords_attributes (var_die, decl);
11666 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
11667 TREE_THIS_VOLATILE (decl), context_die);
11669 if (TREE_PUBLIC (decl))
11670 add_AT_flag (var_die, DW_AT_external, 1);
11672 if (DECL_ARTIFICIAL (decl))
11673 add_AT_flag (var_die, DW_AT_artificial, 1);
11675 if (TREE_PROTECTED (decl))
11676 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
11677 else if (TREE_PRIVATE (decl))
11678 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
11681 if (declaration)
11682 add_AT_flag (var_die, DW_AT_declaration, 1);
11684 if (DECL_ABSTRACT (decl) || declaration)
11685 equate_decl_number_to_die (decl, var_die);
11687 if (! declaration && ! DECL_ABSTRACT (decl))
11689 add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
11690 add_pubname (decl, var_die);
11692 else
11693 tree_add_const_value_attribute (var_die, decl);
11696 /* Generate a DIE to represent a label identifier. */
11698 static void
11699 gen_label_die (tree decl, dw_die_ref context_die)
11701 tree origin = decl_ultimate_origin (decl);
11702 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
11703 rtx insn;
11704 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11706 if (origin != NULL)
11707 add_abstract_origin_attribute (lbl_die, origin);
11708 else
11709 add_name_and_src_coords_attributes (lbl_die, decl);
11711 if (DECL_ABSTRACT (decl))
11712 equate_decl_number_to_die (decl, lbl_die);
11713 else
11715 insn = DECL_RTL_IF_SET (decl);
11717 /* Deleted labels are programmer specified labels which have been
11718 eliminated because of various optimizations. We still emit them
11719 here so that it is possible to put breakpoints on them. */
11720 if (insn
11721 && (LABEL_P (insn)
11722 || ((NOTE_P (insn)
11723 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
11725 /* When optimization is enabled (via -O) some parts of the compiler
11726 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
11727 represent source-level labels which were explicitly declared by
11728 the user. This really shouldn't be happening though, so catch
11729 it if it ever does happen. */
11730 gcc_assert (!INSN_DELETED_P (insn));
11732 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
11733 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
11738 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
11739 Add low_pc and high_pc attributes to the DIE for a block STMT. */
11741 static inline void
11742 add_high_low_attributes (tree stmt, dw_die_ref die)
11744 char label[MAX_ARTIFICIAL_LABEL_BYTES];
11746 if (BLOCK_FRAGMENT_CHAIN (stmt))
11748 tree chain;
11750 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
11752 chain = BLOCK_FRAGMENT_CHAIN (stmt);
11755 add_ranges (chain);
11756 chain = BLOCK_FRAGMENT_CHAIN (chain);
11758 while (chain);
11759 add_ranges (NULL);
11761 else
11763 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
11764 BLOCK_NUMBER (stmt));
11765 add_AT_lbl_id (die, DW_AT_low_pc, label);
11766 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
11767 BLOCK_NUMBER (stmt));
11768 add_AT_lbl_id (die, DW_AT_high_pc, label);
11772 /* Generate a DIE for a lexical block. */
11774 static void
11775 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
11777 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
11779 if (! BLOCK_ABSTRACT (stmt))
11780 add_high_low_attributes (stmt, stmt_die);
11782 decls_for_scope (stmt, stmt_die, depth);
11785 /* Generate a DIE for an inlined subprogram. */
11787 static void
11788 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
11790 tree decl = block_ultimate_origin (stmt);
11792 /* Emit info for the abstract instance first, if we haven't yet. We
11793 must emit this even if the block is abstract, otherwise when we
11794 emit the block below (or elsewhere), we may end up trying to emit
11795 a die whose origin die hasn't been emitted, and crashing. */
11796 dwarf2out_abstract_function (decl);
11798 if (! BLOCK_ABSTRACT (stmt))
11800 dw_die_ref subr_die
11801 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
11803 add_abstract_origin_attribute (subr_die, decl);
11804 add_high_low_attributes (stmt, subr_die);
11806 decls_for_scope (stmt, subr_die, depth);
11807 current_function_has_inlines = 1;
11809 else
11810 /* We may get here if we're the outer block of function A that was
11811 inlined into function B that was inlined into function C. When
11812 generating debugging info for C, dwarf2out_abstract_function(B)
11813 would mark all inlined blocks as abstract, including this one.
11814 So, we wouldn't (and shouldn't) expect labels to be generated
11815 for this one. Instead, just emit debugging info for
11816 declarations within the block. This is particularly important
11817 in the case of initializers of arguments passed from B to us:
11818 if they're statement expressions containing declarations, we
11819 wouldn't generate dies for their abstract variables, and then,
11820 when generating dies for the real variables, we'd die (pun
11821 intended :-) */
11822 gen_lexical_block_die (stmt, context_die, depth);
11825 /* Generate a DIE for a field in a record, or structure. */
11827 static void
11828 gen_field_die (tree decl, dw_die_ref context_die)
11830 dw_die_ref decl_die;
11832 if (TREE_TYPE (decl) == error_mark_node)
11833 return;
11835 decl_die = new_die (DW_TAG_member, context_die, decl);
11836 add_name_and_src_coords_attributes (decl_die, decl);
11837 add_type_attribute (decl_die, member_declared_type (decl),
11838 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
11839 context_die);
11841 if (DECL_BIT_FIELD_TYPE (decl))
11843 add_byte_size_attribute (decl_die, decl);
11844 add_bit_size_attribute (decl_die, decl);
11845 add_bit_offset_attribute (decl_die, decl);
11848 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
11849 add_data_member_location_attribute (decl_die, decl);
11851 if (DECL_ARTIFICIAL (decl))
11852 add_AT_flag (decl_die, DW_AT_artificial, 1);
11854 if (TREE_PROTECTED (decl))
11855 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
11856 else if (TREE_PRIVATE (decl))
11857 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
11859 /* Equate decl number to die, so that we can look up this decl later on. */
11860 equate_decl_number_to_die (decl, decl_die);
11863 #if 0
11864 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
11865 Use modified_type_die instead.
11866 We keep this code here just in case these types of DIEs may be needed to
11867 represent certain things in other languages (e.g. Pascal) someday. */
11869 static void
11870 gen_pointer_type_die (tree type, dw_die_ref context_die)
11872 dw_die_ref ptr_die
11873 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
11875 equate_type_number_to_die (type, ptr_die);
11876 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
11877 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
11880 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
11881 Use modified_type_die instead.
11882 We keep this code here just in case these types of DIEs may be needed to
11883 represent certain things in other languages (e.g. Pascal) someday. */
11885 static void
11886 gen_reference_type_die (tree type, dw_die_ref context_die)
11888 dw_die_ref ref_die
11889 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
11891 equate_type_number_to_die (type, ref_die);
11892 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
11893 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
11895 #endif
11897 /* Generate a DIE for a pointer to a member type. */
11899 static void
11900 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
11902 dw_die_ref ptr_die
11903 = new_die (DW_TAG_ptr_to_member_type,
11904 scope_die_for (type, context_die), type);
11906 equate_type_number_to_die (type, ptr_die);
11907 add_AT_die_ref (ptr_die, DW_AT_containing_type,
11908 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
11909 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
11912 /* Generate the DIE for the compilation unit. */
11914 static dw_die_ref
11915 gen_compile_unit_die (const char *filename)
11917 dw_die_ref die;
11918 char producer[250];
11919 const char *language_string = lang_hooks.name;
11920 int language;
11922 die = new_die (DW_TAG_compile_unit, NULL, NULL);
11924 if (filename)
11926 add_name_attribute (die, filename);
11927 /* Don't add cwd for <built-in>. */
11928 if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
11929 add_comp_dir_attribute (die);
11932 sprintf (producer, "%s %s", language_string, version_string);
11934 #ifdef MIPS_DEBUGGING_INFO
11935 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
11936 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
11937 not appear in the producer string, the debugger reaches the conclusion
11938 that the object file is stripped and has no debugging information.
11939 To get the MIPS/SGI debugger to believe that there is debugging
11940 information in the object file, we add a -g to the producer string. */
11941 if (debug_info_level > DINFO_LEVEL_TERSE)
11942 strcat (producer, " -g");
11943 #endif
11945 add_AT_string (die, DW_AT_producer, producer);
11947 if (strcmp (language_string, "GNU C++") == 0)
11948 language = DW_LANG_C_plus_plus;
11949 else if (strcmp (language_string, "GNU Ada") == 0)
11950 language = DW_LANG_Ada95;
11951 else if (strcmp (language_string, "GNU F77") == 0)
11952 language = DW_LANG_Fortran77;
11953 else if (strcmp (language_string, "GNU F95") == 0)
11954 language = DW_LANG_Fortran95;
11955 else if (strcmp (language_string, "GNU Pascal") == 0)
11956 language = DW_LANG_Pascal83;
11957 else if (strcmp (language_string, "GNU Java") == 0)
11958 language = DW_LANG_Java;
11959 else
11960 language = DW_LANG_C89;
11962 add_AT_unsigned (die, DW_AT_language, language);
11963 return die;
11966 /* Generate a DIE for a string type. */
11968 static void
11969 gen_string_type_die (tree type, dw_die_ref context_die)
11971 dw_die_ref type_die
11972 = new_die (DW_TAG_string_type, scope_die_for (type, context_die), type);
11974 equate_type_number_to_die (type, type_die);
11976 /* ??? Fudge the string length attribute for now.
11977 TODO: add string length info. */
11978 #if 0
11979 string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
11980 bound_representation (upper_bound, 0, 'u');
11981 #endif
11984 /* Generate the DIE for a base class. */
11986 static void
11987 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
11989 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
11991 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
11992 add_data_member_location_attribute (die, binfo);
11994 if (BINFO_VIRTUAL_P (binfo))
11995 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11997 if (access == access_public_node)
11998 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
11999 else if (access == access_protected_node)
12000 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
12003 /* Generate a DIE for a class member. */
12005 static void
12006 gen_member_die (tree type, dw_die_ref context_die)
12008 tree member;
12009 tree binfo = TYPE_BINFO (type);
12010 dw_die_ref child;
12012 /* If this is not an incomplete type, output descriptions of each of its
12013 members. Note that as we output the DIEs necessary to represent the
12014 members of this record or union type, we will also be trying to output
12015 DIEs to represent the *types* of those members. However the `type'
12016 function (above) will specifically avoid generating type DIEs for member
12017 types *within* the list of member DIEs for this (containing) type except
12018 for those types (of members) which are explicitly marked as also being
12019 members of this (containing) type themselves. The g++ front- end can
12020 force any given type to be treated as a member of some other (containing)
12021 type by setting the TYPE_CONTEXT of the given (member) type to point to
12022 the TREE node representing the appropriate (containing) type. */
12024 /* First output info about the base classes. */
12025 if (binfo)
12027 VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
12028 int i;
12029 tree base;
12031 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12032 gen_inheritance_die (base,
12033 (accesses ? VEC_index (tree, accesses, i)
12034 : access_public_node), context_die);
12037 /* Now output info about the data members and type members. */
12038 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
12040 /* If we thought we were generating minimal debug info for TYPE
12041 and then changed our minds, some of the member declarations
12042 may have already been defined. Don't define them again, but
12043 do put them in the right order. */
12045 child = lookup_decl_die (member);
12046 if (child)
12047 splice_child_die (context_die, child);
12048 else
12049 gen_decl_die (member, context_die);
12052 /* Now output info about the function members (if any). */
12053 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
12055 /* Don't include clones in the member list. */
12056 if (DECL_ABSTRACT_ORIGIN (member))
12057 continue;
12059 child = lookup_decl_die (member);
12060 if (child)
12061 splice_child_die (context_die, child);
12062 else
12063 gen_decl_die (member, context_die);
12067 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
12068 is set, we pretend that the type was never defined, so we only get the
12069 member DIEs needed by later specification DIEs. */
12071 static void
12072 gen_struct_or_union_type_die (tree type, dw_die_ref context_die)
12074 dw_die_ref type_die = lookup_type_die (type);
12075 dw_die_ref scope_die = 0;
12076 int nested = 0;
12077 int complete = (TYPE_SIZE (type)
12078 && (! TYPE_STUB_DECL (type)
12079 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
12080 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
12082 if (type_die && ! complete)
12083 return;
12085 if (TYPE_CONTEXT (type) != NULL_TREE
12086 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12087 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
12088 nested = 1;
12090 scope_die = scope_die_for (type, context_die);
12092 if (! type_die || (nested && scope_die == comp_unit_die))
12093 /* First occurrence of type or toplevel definition of nested class. */
12095 dw_die_ref old_die = type_die;
12097 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
12098 ? DW_TAG_structure_type : DW_TAG_union_type,
12099 scope_die, type);
12100 equate_type_number_to_die (type, type_die);
12101 if (old_die)
12102 add_AT_specification (type_die, old_die);
12103 else
12104 add_name_attribute (type_die, type_tag (type));
12106 else
12107 remove_AT (type_die, DW_AT_declaration);
12109 /* If this type has been completed, then give it a byte_size attribute and
12110 then give a list of members. */
12111 if (complete && !ns_decl)
12113 /* Prevent infinite recursion in cases where the type of some member of
12114 this type is expressed in terms of this type itself. */
12115 TREE_ASM_WRITTEN (type) = 1;
12116 add_byte_size_attribute (type_die, type);
12117 if (TYPE_STUB_DECL (type) != NULL_TREE)
12118 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12120 /* If the first reference to this type was as the return type of an
12121 inline function, then it may not have a parent. Fix this now. */
12122 if (type_die->die_parent == NULL)
12123 add_child_die (scope_die, type_die);
12125 push_decl_scope (type);
12126 gen_member_die (type, type_die);
12127 pop_decl_scope ();
12129 /* GNU extension: Record what type our vtable lives in. */
12130 if (TYPE_VFIELD (type))
12132 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
12134 gen_type_die (vtype, context_die);
12135 add_AT_die_ref (type_die, DW_AT_containing_type,
12136 lookup_type_die (vtype));
12139 else
12141 add_AT_flag (type_die, DW_AT_declaration, 1);
12143 /* We don't need to do this for function-local types. */
12144 if (TYPE_STUB_DECL (type)
12145 && ! decl_function_context (TYPE_STUB_DECL (type)))
12146 VEC_safe_push (tree, gc, incomplete_types, type);
12150 /* Generate a DIE for a subroutine _type_. */
12152 static void
12153 gen_subroutine_type_die (tree type, dw_die_ref context_die)
12155 tree return_type = TREE_TYPE (type);
12156 dw_die_ref subr_die
12157 = new_die (DW_TAG_subroutine_type,
12158 scope_die_for (type, context_die), type);
12160 equate_type_number_to_die (type, subr_die);
12161 add_prototyped_attribute (subr_die, type);
12162 add_type_attribute (subr_die, return_type, 0, 0, context_die);
12163 gen_formal_types_die (type, subr_die);
12166 /* Generate a DIE for a type definition. */
12168 static void
12169 gen_typedef_die (tree decl, dw_die_ref context_die)
12171 dw_die_ref type_die;
12172 tree origin;
12174 if (TREE_ASM_WRITTEN (decl))
12175 return;
12177 TREE_ASM_WRITTEN (decl) = 1;
12178 type_die = new_die (DW_TAG_typedef, context_die, decl);
12179 origin = decl_ultimate_origin (decl);
12180 if (origin != NULL)
12181 add_abstract_origin_attribute (type_die, origin);
12182 else
12184 tree type;
12186 add_name_and_src_coords_attributes (type_die, decl);
12187 if (DECL_ORIGINAL_TYPE (decl))
12189 type = DECL_ORIGINAL_TYPE (decl);
12191 gcc_assert (type != TREE_TYPE (decl));
12192 equate_type_number_to_die (TREE_TYPE (decl), type_die);
12194 else
12195 type = TREE_TYPE (decl);
12197 add_type_attribute (type_die, type, TREE_READONLY (decl),
12198 TREE_THIS_VOLATILE (decl), context_die);
12201 if (DECL_ABSTRACT (decl))
12202 equate_decl_number_to_die (decl, type_die);
12205 /* Generate a type description DIE. */
12207 static void
12208 gen_type_die (tree type, dw_die_ref context_die)
12210 int need_pop;
12212 if (type == NULL_TREE || type == error_mark_node)
12213 return;
12215 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12216 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
12218 if (TREE_ASM_WRITTEN (type))
12219 return;
12221 /* Prevent broken recursion; we can't hand off to the same type. */
12222 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
12224 TREE_ASM_WRITTEN (type) = 1;
12225 gen_decl_die (TYPE_NAME (type), context_die);
12226 return;
12229 /* We are going to output a DIE to represent the unqualified version
12230 of this type (i.e. without any const or volatile qualifiers) so
12231 get the main variant (i.e. the unqualified version) of this type
12232 now. (Vectors are special because the debugging info is in the
12233 cloned type itself). */
12234 if (TREE_CODE (type) != VECTOR_TYPE)
12235 type = type_main_variant (type);
12237 if (TREE_ASM_WRITTEN (type))
12238 return;
12240 switch (TREE_CODE (type))
12242 case ERROR_MARK:
12243 break;
12245 case POINTER_TYPE:
12246 case REFERENCE_TYPE:
12247 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
12248 ensures that the gen_type_die recursion will terminate even if the
12249 type is recursive. Recursive types are possible in Ada. */
12250 /* ??? We could perhaps do this for all types before the switch
12251 statement. */
12252 TREE_ASM_WRITTEN (type) = 1;
12254 /* For these types, all that is required is that we output a DIE (or a
12255 set of DIEs) to represent the "basis" type. */
12256 gen_type_die (TREE_TYPE (type), context_die);
12257 break;
12259 case OFFSET_TYPE:
12260 /* This code is used for C++ pointer-to-data-member types.
12261 Output a description of the relevant class type. */
12262 gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
12264 /* Output a description of the type of the object pointed to. */
12265 gen_type_die (TREE_TYPE (type), context_die);
12267 /* Now output a DIE to represent this pointer-to-data-member type
12268 itself. */
12269 gen_ptr_to_mbr_type_die (type, context_die);
12270 break;
12272 case FUNCTION_TYPE:
12273 /* Force out return type (in case it wasn't forced out already). */
12274 gen_type_die (TREE_TYPE (type), context_die);
12275 gen_subroutine_type_die (type, context_die);
12276 break;
12278 case METHOD_TYPE:
12279 /* Force out return type (in case it wasn't forced out already). */
12280 gen_type_die (TREE_TYPE (type), context_die);
12281 gen_subroutine_type_die (type, context_die);
12282 break;
12284 case ARRAY_TYPE:
12285 if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
12287 gen_type_die (TREE_TYPE (type), context_die);
12288 gen_string_type_die (type, context_die);
12290 else
12291 gen_array_type_die (type, context_die);
12292 break;
12294 case VECTOR_TYPE:
12295 gen_array_type_die (type, context_die);
12296 break;
12298 case ENUMERAL_TYPE:
12299 case RECORD_TYPE:
12300 case UNION_TYPE:
12301 case QUAL_UNION_TYPE:
12302 /* If this is a nested type whose containing class hasn't been written
12303 out yet, writing it out will cover this one, too. This does not apply
12304 to instantiations of member class templates; they need to be added to
12305 the containing class as they are generated. FIXME: This hurts the
12306 idea of combining type decls from multiple TUs, since we can't predict
12307 what set of template instantiations we'll get. */
12308 if (TYPE_CONTEXT (type)
12309 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12310 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
12312 gen_type_die (TYPE_CONTEXT (type), context_die);
12314 if (TREE_ASM_WRITTEN (type))
12315 return;
12317 /* If that failed, attach ourselves to the stub. */
12318 push_decl_scope (TYPE_CONTEXT (type));
12319 context_die = lookup_type_die (TYPE_CONTEXT (type));
12320 need_pop = 1;
12322 else
12324 declare_in_namespace (type, context_die);
12325 need_pop = 0;
12328 if (TREE_CODE (type) == ENUMERAL_TYPE)
12329 gen_enumeration_type_die (type, context_die);
12330 else
12331 gen_struct_or_union_type_die (type, context_die);
12333 if (need_pop)
12334 pop_decl_scope ();
12336 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
12337 it up if it is ever completed. gen_*_type_die will set it for us
12338 when appropriate. */
12339 return;
12341 case VOID_TYPE:
12342 case INTEGER_TYPE:
12343 case REAL_TYPE:
12344 case COMPLEX_TYPE:
12345 case BOOLEAN_TYPE:
12346 case CHAR_TYPE:
12347 /* No DIEs needed for fundamental types. */
12348 break;
12350 case LANG_TYPE:
12351 /* No Dwarf representation currently defined. */
12352 break;
12354 default:
12355 gcc_unreachable ();
12358 TREE_ASM_WRITTEN (type) = 1;
12361 /* Generate a DIE for a tagged type instantiation. */
12363 static void
12364 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
12366 if (type == NULL_TREE || type == error_mark_node)
12367 return;
12369 /* We are going to output a DIE to represent the unqualified version of
12370 this type (i.e. without any const or volatile qualifiers) so make sure
12371 that we have the main variant (i.e. the unqualified version) of this
12372 type now. */
12373 gcc_assert (type == type_main_variant (type));
12375 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
12376 an instance of an unresolved type. */
12378 switch (TREE_CODE (type))
12380 case ERROR_MARK:
12381 break;
12383 case ENUMERAL_TYPE:
12384 gen_inlined_enumeration_type_die (type, context_die);
12385 break;
12387 case RECORD_TYPE:
12388 gen_inlined_structure_type_die (type, context_die);
12389 break;
12391 case UNION_TYPE:
12392 case QUAL_UNION_TYPE:
12393 gen_inlined_union_type_die (type, context_die);
12394 break;
12396 default:
12397 gcc_unreachable ();
12401 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12402 things which are local to the given block. */
12404 static void
12405 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
12407 int must_output_die = 0;
12408 tree origin;
12409 tree decl;
12410 enum tree_code origin_code;
12412 /* Ignore blocks that are NULL. */
12413 if (stmt == NULL_TREE)
12414 return;
12416 /* If the block is one fragment of a non-contiguous block, do not
12417 process the variables, since they will have been done by the
12418 origin block. Do process subblocks. */
12419 if (BLOCK_FRAGMENT_ORIGIN (stmt))
12421 tree sub;
12423 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
12424 gen_block_die (sub, context_die, depth + 1);
12426 return;
12429 /* Determine the "ultimate origin" of this block. This block may be an
12430 inlined instance of an inlined instance of inline function, so we have
12431 to trace all of the way back through the origin chain to find out what
12432 sort of node actually served as the original seed for the creation of
12433 the current block. */
12434 origin = block_ultimate_origin (stmt);
12435 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
12437 /* Determine if we need to output any Dwarf DIEs at all to represent this
12438 block. */
12439 if (origin_code == FUNCTION_DECL)
12440 /* The outer scopes for inlinings *must* always be represented. We
12441 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
12442 must_output_die = 1;
12443 else
12445 /* In the case where the current block represents an inlining of the
12446 "body block" of an inline function, we must *NOT* output any DIE for
12447 this block because we have already output a DIE to represent the whole
12448 inlined function scope and the "body block" of any function doesn't
12449 really represent a different scope according to ANSI C rules. So we
12450 check here to make sure that this block does not represent a "body
12451 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
12452 if (! is_body_block (origin ? origin : stmt))
12454 /* Determine if this block directly contains any "significant"
12455 local declarations which we will need to output DIEs for. */
12456 if (debug_info_level > DINFO_LEVEL_TERSE)
12457 /* We are not in terse mode so *any* local declaration counts
12458 as being a "significant" one. */
12459 must_output_die = (BLOCK_VARS (stmt) != NULL
12460 && (TREE_USED (stmt)
12461 || TREE_ASM_WRITTEN (stmt)
12462 || BLOCK_ABSTRACT (stmt)));
12463 else
12464 /* We are in terse mode, so only local (nested) function
12465 definitions count as "significant" local declarations. */
12466 for (decl = BLOCK_VARS (stmt);
12467 decl != NULL; decl = TREE_CHAIN (decl))
12468 if (TREE_CODE (decl) == FUNCTION_DECL
12469 && DECL_INITIAL (decl))
12471 must_output_die = 1;
12472 break;
12477 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
12478 DIE for any block which contains no significant local declarations at
12479 all. Rather, in such cases we just call `decls_for_scope' so that any
12480 needed Dwarf info for any sub-blocks will get properly generated. Note
12481 that in terse mode, our definition of what constitutes a "significant"
12482 local declaration gets restricted to include only inlined function
12483 instances and local (nested) function definitions. */
12484 if (must_output_die)
12486 if (origin_code == FUNCTION_DECL)
12487 gen_inlined_subroutine_die (stmt, context_die, depth);
12488 else
12489 gen_lexical_block_die (stmt, context_die, depth);
12491 else
12492 decls_for_scope (stmt, context_die, depth);
12495 /* Generate all of the decls declared within a given scope and (recursively)
12496 all of its sub-blocks. */
12498 static void
12499 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
12501 tree decl;
12502 tree subblocks;
12504 /* Ignore NULL blocks. */
12505 if (stmt == NULL_TREE)
12506 return;
12508 if (TREE_USED (stmt))
12510 /* Output the DIEs to represent all of the data objects and typedefs
12511 declared directly within this block but not within any nested
12512 sub-blocks. Also, nested function and tag DIEs have been
12513 generated with a parent of NULL; fix that up now. */
12514 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
12516 dw_die_ref die;
12518 if (TREE_CODE (decl) == FUNCTION_DECL)
12519 die = lookup_decl_die (decl);
12520 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
12521 die = lookup_type_die (TREE_TYPE (decl));
12522 else
12523 die = NULL;
12525 if (die != NULL && die->die_parent == NULL)
12526 add_child_die (context_die, die);
12527 else
12528 gen_decl_die (decl, context_die);
12532 /* If we're at -g1, we're not interested in subblocks. */
12533 if (debug_info_level <= DINFO_LEVEL_TERSE)
12534 return;
12536 /* Output the DIEs to represent all sub-blocks (and the items declared
12537 therein) of this block. */
12538 for (subblocks = BLOCK_SUBBLOCKS (stmt);
12539 subblocks != NULL;
12540 subblocks = BLOCK_CHAIN (subblocks))
12541 gen_block_die (subblocks, context_die, depth + 1);
12544 /* Is this a typedef we can avoid emitting? */
12546 static inline int
12547 is_redundant_typedef (tree decl)
12549 if (TYPE_DECL_IS_STUB (decl))
12550 return 1;
12552 if (DECL_ARTIFICIAL (decl)
12553 && DECL_CONTEXT (decl)
12554 && is_tagged_type (DECL_CONTEXT (decl))
12555 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
12556 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
12557 /* Also ignore the artificial member typedef for the class name. */
12558 return 1;
12560 return 0;
12563 /* Returns the DIE for decl. A DIE will always be returned. */
12565 static dw_die_ref
12566 force_decl_die (tree decl)
12568 dw_die_ref decl_die;
12569 unsigned saved_external_flag;
12570 tree save_fn = NULL_TREE;
12571 decl_die = lookup_decl_die (decl);
12572 if (!decl_die)
12574 dw_die_ref context_die;
12575 tree decl_context = DECL_CONTEXT (decl);
12576 if (decl_context)
12578 /* Find die that represents this context. */
12579 if (TYPE_P (decl_context))
12580 context_die = force_type_die (decl_context);
12581 else
12582 context_die = force_decl_die (decl_context);
12584 else
12585 context_die = comp_unit_die;
12587 switch (TREE_CODE (decl))
12589 case FUNCTION_DECL:
12590 /* Clear current_function_decl, so that gen_subprogram_die thinks
12591 that this is a declaration. At this point, we just want to force
12592 declaration die. */
12593 save_fn = current_function_decl;
12594 current_function_decl = NULL_TREE;
12595 gen_subprogram_die (decl, context_die);
12596 current_function_decl = save_fn;
12597 break;
12599 case VAR_DECL:
12600 /* Set external flag to force declaration die. Restore it after
12601 gen_decl_die() call. */
12602 saved_external_flag = DECL_EXTERNAL (decl);
12603 DECL_EXTERNAL (decl) = 1;
12604 gen_decl_die (decl, context_die);
12605 DECL_EXTERNAL (decl) = saved_external_flag;
12606 break;
12608 case NAMESPACE_DECL:
12609 dwarf2out_decl (decl);
12610 break;
12612 default:
12613 gcc_unreachable ();
12616 /* We should be able to find the DIE now. */
12617 if (!decl_die)
12618 decl_die = lookup_decl_die (decl);
12619 gcc_assert (decl_die);
12622 return decl_die;
12625 /* Returns the DIE for TYPE. A DIE is always returned. */
12627 static dw_die_ref
12628 force_type_die (tree type)
12630 dw_die_ref type_die;
12632 type_die = lookup_type_die (type);
12633 if (!type_die)
12635 dw_die_ref context_die;
12636 if (TYPE_CONTEXT (type))
12637 if (TYPE_P (TYPE_CONTEXT (type)))
12638 context_die = force_type_die (TYPE_CONTEXT (type));
12639 else
12640 context_die = force_decl_die (TYPE_CONTEXT (type));
12641 else
12642 context_die = comp_unit_die;
12644 gen_type_die (type, context_die);
12645 type_die = lookup_type_die (type);
12646 gcc_assert (type_die);
12648 return type_die;
12651 /* Force out any required namespaces to be able to output DECL,
12652 and return the new context_die for it, if it's changed. */
12654 static dw_die_ref
12655 setup_namespace_context (tree thing, dw_die_ref context_die)
12657 tree context = (DECL_P (thing)
12658 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
12659 if (context && TREE_CODE (context) == NAMESPACE_DECL)
12660 /* Force out the namespace. */
12661 context_die = force_decl_die (context);
12663 return context_die;
12666 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
12667 type) within its namespace, if appropriate.
12669 For compatibility with older debuggers, namespace DIEs only contain
12670 declarations; all definitions are emitted at CU scope. */
12672 static void
12673 declare_in_namespace (tree thing, dw_die_ref context_die)
12675 dw_die_ref ns_context;
12677 if (debug_info_level <= DINFO_LEVEL_TERSE)
12678 return;
12680 /* If this decl is from an inlined function, then don't try to emit it in its
12681 namespace, as we will get confused. It would have already been emitted
12682 when the abstract instance of the inline function was emitted anyways. */
12683 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
12684 return;
12686 ns_context = setup_namespace_context (thing, context_die);
12688 if (ns_context != context_die)
12690 if (DECL_P (thing))
12691 gen_decl_die (thing, ns_context);
12692 else
12693 gen_type_die (thing, ns_context);
12697 /* Generate a DIE for a namespace or namespace alias. */
12699 static void
12700 gen_namespace_die (tree decl)
12702 dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
12704 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
12705 they are an alias of. */
12706 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
12708 /* Output a real namespace. */
12709 dw_die_ref namespace_die
12710 = new_die (DW_TAG_namespace, context_die, decl);
12711 add_name_and_src_coords_attributes (namespace_die, decl);
12712 equate_decl_number_to_die (decl, namespace_die);
12714 else
12716 /* Output a namespace alias. */
12718 /* Force out the namespace we are an alias of, if necessary. */
12719 dw_die_ref origin_die
12720 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
12722 /* Now create the namespace alias DIE. */
12723 dw_die_ref namespace_die
12724 = new_die (DW_TAG_imported_declaration, context_die, decl);
12725 add_name_and_src_coords_attributes (namespace_die, decl);
12726 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
12727 equate_decl_number_to_die (decl, namespace_die);
12731 /* Generate Dwarf debug information for a decl described by DECL. */
12733 static void
12734 gen_decl_die (tree decl, dw_die_ref context_die)
12736 tree origin;
12738 if (DECL_P (decl) && DECL_IGNORED_P (decl))
12739 return;
12741 switch (TREE_CODE (decl))
12743 case ERROR_MARK:
12744 break;
12746 case CONST_DECL:
12747 /* The individual enumerators of an enum type get output when we output
12748 the Dwarf representation of the relevant enum type itself. */
12749 break;
12751 case FUNCTION_DECL:
12752 /* Don't output any DIEs to represent mere function declarations,
12753 unless they are class members or explicit block externs. */
12754 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
12755 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
12756 break;
12758 #if 0
12759 /* FIXME */
12760 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
12761 on local redeclarations of global functions. That seems broken. */
12762 if (current_function_decl != decl)
12763 /* This is only a declaration. */;
12764 #endif
12766 /* If we're emitting a clone, emit info for the abstract instance. */
12767 if (DECL_ORIGIN (decl) != decl)
12768 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
12770 /* If we're emitting an out-of-line copy of an inline function,
12771 emit info for the abstract instance and set up to refer to it. */
12772 else if (cgraph_function_possibly_inlined_p (decl)
12773 && ! DECL_ABSTRACT (decl)
12774 && ! class_or_namespace_scope_p (context_die)
12775 /* dwarf2out_abstract_function won't emit a die if this is just
12776 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
12777 that case, because that works only if we have a die. */
12778 && DECL_INITIAL (decl) != NULL_TREE)
12780 dwarf2out_abstract_function (decl);
12781 set_decl_origin_self (decl);
12784 /* Otherwise we're emitting the primary DIE for this decl. */
12785 else if (debug_info_level > DINFO_LEVEL_TERSE)
12787 /* Before we describe the FUNCTION_DECL itself, make sure that we
12788 have described its return type. */
12789 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
12791 /* And its virtual context. */
12792 if (DECL_VINDEX (decl) != NULL_TREE)
12793 gen_type_die (DECL_CONTEXT (decl), context_die);
12795 /* And its containing type. */
12796 origin = decl_class_context (decl);
12797 if (origin != NULL_TREE)
12798 gen_type_die_for_member (origin, decl, context_die);
12800 /* And its containing namespace. */
12801 declare_in_namespace (decl, context_die);
12804 /* Now output a DIE to represent the function itself. */
12805 gen_subprogram_die (decl, context_die);
12806 break;
12808 case TYPE_DECL:
12809 /* If we are in terse mode, don't generate any DIEs to represent any
12810 actual typedefs. */
12811 if (debug_info_level <= DINFO_LEVEL_TERSE)
12812 break;
12814 /* In the special case of a TYPE_DECL node representing the declaration
12815 of some type tag, if the given TYPE_DECL is marked as having been
12816 instantiated from some other (original) TYPE_DECL node (e.g. one which
12817 was generated within the original definition of an inline function) we
12818 have to generate a special (abbreviated) DW_TAG_structure_type,
12819 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
12820 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
12822 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
12823 break;
12826 if (is_redundant_typedef (decl))
12827 gen_type_die (TREE_TYPE (decl), context_die);
12828 else
12829 /* Output a DIE to represent the typedef itself. */
12830 gen_typedef_die (decl, context_die);
12831 break;
12833 case LABEL_DECL:
12834 if (debug_info_level >= DINFO_LEVEL_NORMAL)
12835 gen_label_die (decl, context_die);
12836 break;
12838 case VAR_DECL:
12839 case RESULT_DECL:
12840 /* If we are in terse mode, don't generate any DIEs to represent any
12841 variable declarations or definitions. */
12842 if (debug_info_level <= DINFO_LEVEL_TERSE)
12843 break;
12845 /* Output any DIEs that are needed to specify the type of this data
12846 object. */
12847 gen_type_die (TREE_TYPE (decl), context_die);
12849 /* And its containing type. */
12850 origin = decl_class_context (decl);
12851 if (origin != NULL_TREE)
12852 gen_type_die_for_member (origin, decl, context_die);
12854 /* And its containing namespace. */
12855 declare_in_namespace (decl, context_die);
12857 /* Now output the DIE to represent the data object itself. This gets
12858 complicated because of the possibility that the VAR_DECL really
12859 represents an inlined instance of a formal parameter for an inline
12860 function. */
12861 origin = decl_ultimate_origin (decl);
12862 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
12863 gen_formal_parameter_die (decl, context_die);
12864 else
12865 gen_variable_die (decl, context_die);
12866 break;
12868 case FIELD_DECL:
12869 /* Ignore the nameless fields that are used to skip bits but handle C++
12870 anonymous unions and structs. */
12871 if (DECL_NAME (decl) != NULL_TREE
12872 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
12873 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
12875 gen_type_die (member_declared_type (decl), context_die);
12876 gen_field_die (decl, context_die);
12878 break;
12880 case PARM_DECL:
12881 gen_type_die (TREE_TYPE (decl), context_die);
12882 gen_formal_parameter_die (decl, context_die);
12883 break;
12885 case NAMESPACE_DECL:
12886 gen_namespace_die (decl);
12887 break;
12889 default:
12890 /* Probably some frontend-internal decl. Assume we don't care. */
12891 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
12892 break;
12896 /* Add Ada "use" clause information for SGI Workshop debugger. */
12898 void
12899 dwarf2out_add_library_unit_info (const char *filename, const char *context_list)
12901 unsigned int file_index;
12903 if (filename != NULL)
12905 dw_die_ref unit_die = new_die (DW_TAG_module, comp_unit_die, NULL);
12906 tree context_list_decl
12907 = build_decl (LABEL_DECL, get_identifier (context_list),
12908 void_type_node);
12910 TREE_PUBLIC (context_list_decl) = TRUE;
12911 add_name_attribute (unit_die, context_list);
12912 file_index = lookup_filename (filename);
12913 add_AT_unsigned (unit_die, DW_AT_decl_file, file_index);
12914 add_pubname (context_list_decl, unit_die);
12918 /* Output debug information for global decl DECL. Called from toplev.c after
12919 compilation proper has finished. */
12921 static void
12922 dwarf2out_global_decl (tree decl)
12924 /* Output DWARF2 information for file-scope tentative data object
12925 declarations, file-scope (extern) function declarations (which had no
12926 corresponding body) and file-scope tagged type declarations and
12927 definitions which have not yet been forced out. */
12928 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
12929 dwarf2out_decl (decl);
12932 /* Output debug information for type decl DECL. Called from toplev.c
12933 and from language front ends (to record built-in types). */
12934 static void
12935 dwarf2out_type_decl (tree decl, int local)
12937 if (!local)
12938 dwarf2out_decl (decl);
12941 /* Output debug information for imported module or decl. */
12943 static void
12944 dwarf2out_imported_module_or_decl (tree decl, tree context)
12946 dw_die_ref imported_die, at_import_die;
12947 dw_die_ref scope_die;
12948 unsigned file_index;
12949 expanded_location xloc;
12951 if (debug_info_level <= DINFO_LEVEL_TERSE)
12952 return;
12954 gcc_assert (decl);
12956 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
12957 We need decl DIE for reference and scope die. First, get DIE for the decl
12958 itself. */
12960 /* Get the scope die for decl context. Use comp_unit_die for global module
12961 or decl. If die is not found for non globals, force new die. */
12962 if (!context)
12963 scope_die = comp_unit_die;
12964 else if (TYPE_P (context))
12965 scope_die = force_type_die (context);
12966 else
12967 scope_die = force_decl_die (context);
12969 /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE. */
12970 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
12971 at_import_die = force_type_die (TREE_TYPE (decl));
12972 else
12974 at_import_die = lookup_decl_die (decl);
12975 if (!at_import_die)
12977 /* If we're trying to avoid duplicate debug info, we may not have
12978 emitted the member decl for this field. Emit it now. */
12979 if (TREE_CODE (decl) == FIELD_DECL)
12981 tree type = DECL_CONTEXT (decl);
12982 dw_die_ref type_context_die;
12984 if (TYPE_CONTEXT (type))
12985 if (TYPE_P (TYPE_CONTEXT (type)))
12986 type_context_die = force_type_die (TYPE_CONTEXT (type));
12987 else
12988 type_context_die = force_decl_die (TYPE_CONTEXT (type));
12989 else
12990 type_context_die = comp_unit_die;
12991 gen_type_die_for_member (type, decl, type_context_die);
12993 at_import_die = force_decl_die (decl);
12997 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
12998 if (TREE_CODE (decl) == NAMESPACE_DECL)
12999 imported_die = new_die (DW_TAG_imported_module, scope_die, context);
13000 else
13001 imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
13003 xloc = expand_location (input_location);
13004 file_index = lookup_filename (xloc.file);
13005 add_AT_unsigned (imported_die, DW_AT_decl_file, file_index);
13006 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
13007 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
13010 /* Write the debugging output for DECL. */
13012 void
13013 dwarf2out_decl (tree decl)
13015 dw_die_ref context_die = comp_unit_die;
13017 switch (TREE_CODE (decl))
13019 case ERROR_MARK:
13020 return;
13022 case FUNCTION_DECL:
13023 /* What we would really like to do here is to filter out all mere
13024 file-scope declarations of file-scope functions which are never
13025 referenced later within this translation unit (and keep all of ones
13026 that *are* referenced later on) but we aren't clairvoyant, so we have
13027 no idea which functions will be referenced in the future (i.e. later
13028 on within the current translation unit). So here we just ignore all
13029 file-scope function declarations which are not also definitions. If
13030 and when the debugger needs to know something about these functions,
13031 it will have to hunt around and find the DWARF information associated
13032 with the definition of the function.
13034 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
13035 nodes represent definitions and which ones represent mere
13036 declarations. We have to check DECL_INITIAL instead. That's because
13037 the C front-end supports some weird semantics for "extern inline"
13038 function definitions. These can get inlined within the current
13039 translation unit (an thus, we need to generate Dwarf info for their
13040 abstract instances so that the Dwarf info for the concrete inlined
13041 instances can have something to refer to) but the compiler never
13042 generates any out-of-lines instances of such things (despite the fact
13043 that they *are* definitions).
13045 The important point is that the C front-end marks these "extern
13046 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
13047 them anyway. Note that the C++ front-end also plays some similar games
13048 for inline function definitions appearing within include files which
13049 also contain `#pragma interface' pragmas. */
13050 if (DECL_INITIAL (decl) == NULL_TREE)
13051 return;
13053 /* If we're a nested function, initially use a parent of NULL; if we're
13054 a plain function, this will be fixed up in decls_for_scope. If
13055 we're a method, it will be ignored, since we already have a DIE. */
13056 if (decl_function_context (decl)
13057 /* But if we're in terse mode, we don't care about scope. */
13058 && debug_info_level > DINFO_LEVEL_TERSE)
13059 context_die = NULL;
13060 break;
13062 case VAR_DECL:
13063 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
13064 declaration and if the declaration was never even referenced from
13065 within this entire compilation unit. We suppress these DIEs in
13066 order to save space in the .debug section (by eliminating entries
13067 which are probably useless). Note that we must not suppress
13068 block-local extern declarations (whether used or not) because that
13069 would screw-up the debugger's name lookup mechanism and cause it to
13070 miss things which really ought to be in scope at a given point. */
13071 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
13072 return;
13074 /* If we are in terse mode, don't generate any DIEs to represent any
13075 variable declarations or definitions. */
13076 if (debug_info_level <= DINFO_LEVEL_TERSE)
13077 return;
13078 break;
13080 case NAMESPACE_DECL:
13081 if (debug_info_level <= DINFO_LEVEL_TERSE)
13082 return;
13083 if (lookup_decl_die (decl) != NULL)
13084 return;
13085 break;
13087 case TYPE_DECL:
13088 /* Don't emit stubs for types unless they are needed by other DIEs. */
13089 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
13090 return;
13092 /* Don't bother trying to generate any DIEs to represent any of the
13093 normal built-in types for the language we are compiling. */
13094 if (DECL_IS_BUILTIN (decl))
13096 /* OK, we need to generate one for `bool' so GDB knows what type
13097 comparisons have. */
13098 if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
13099 == DW_LANG_C_plus_plus)
13100 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
13101 && ! DECL_IGNORED_P (decl))
13102 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
13104 return;
13107 /* If we are in terse mode, don't generate any DIEs for types. */
13108 if (debug_info_level <= DINFO_LEVEL_TERSE)
13109 return;
13111 /* If we're a function-scope tag, initially use a parent of NULL;
13112 this will be fixed up in decls_for_scope. */
13113 if (decl_function_context (decl))
13114 context_die = NULL;
13116 break;
13118 default:
13119 return;
13122 gen_decl_die (decl, context_die);
13125 /* Output a marker (i.e. a label) for the beginning of the generated code for
13126 a lexical block. */
13128 static void
13129 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13130 unsigned int blocknum)
13132 current_function_section (current_function_decl);
13133 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
13136 /* Output a marker (i.e. a label) for the end of the generated code for a
13137 lexical block. */
13139 static void
13140 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
13142 current_function_section (current_function_decl);
13143 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
13146 /* Returns nonzero if it is appropriate not to emit any debugging
13147 information for BLOCK, because it doesn't contain any instructions.
13149 Don't allow this for blocks with nested functions or local classes
13150 as we would end up with orphans, and in the presence of scheduling
13151 we may end up calling them anyway. */
13153 static bool
13154 dwarf2out_ignore_block (tree block)
13156 tree decl;
13158 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
13159 if (TREE_CODE (decl) == FUNCTION_DECL
13160 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
13161 return 0;
13163 return 1;
13166 /* Lookup FILE_NAME (in the list of filenames that we know about here in
13167 dwarf2out.c) and return its "index". The index of each (known) filename is
13168 just a unique number which is associated with only that one filename. We
13169 need such numbers for the sake of generating labels (in the .debug_sfnames
13170 section) and references to those files numbers (in the .debug_srcinfo
13171 and.debug_macinfo sections). If the filename given as an argument is not
13172 found in our current list, add it to the list and assign it the next
13173 available unique index number. In order to speed up searches, we remember
13174 the index of the filename was looked up last. This handles the majority of
13175 all searches. */
13177 static unsigned
13178 lookup_filename (const char *file_name)
13180 size_t i, n;
13181 char *save_file_name;
13183 /* Check to see if the file name that was searched on the previous
13184 call matches this file name. If so, return the index. */
13185 if (file_table_last_lookup_index != 0)
13187 const char *last
13188 = VARRAY_CHAR_PTR (file_table, file_table_last_lookup_index);
13189 if (strcmp (file_name, last) == 0)
13190 return file_table_last_lookup_index;
13193 /* Didn't match the previous lookup, search the table. */
13194 n = VARRAY_ACTIVE_SIZE (file_table);
13195 for (i = 1; i < n; i++)
13196 if (strcmp (file_name, VARRAY_CHAR_PTR (file_table, i)) == 0)
13198 file_table_last_lookup_index = i;
13199 return i;
13202 /* Add the new entry to the end of the filename table. */
13203 file_table_last_lookup_index = n;
13204 save_file_name = (char *) ggc_strdup (file_name);
13205 VARRAY_PUSH_CHAR_PTR (file_table, save_file_name);
13206 VARRAY_PUSH_UINT (file_table_emitted, 0);
13208 /* If the assembler is emitting the file table, and we aren't eliminating
13209 unused debug types, then we must emit .file here. If we are eliminating
13210 unused debug types, then this will be done by the maybe_emit_file call in
13211 prune_unused_types_walk_attribs. */
13213 if (DWARF2_ASM_LINE_DEBUG_INFO && ! flag_eliminate_unused_debug_types)
13214 maybe_emit_file (i);
13216 return i;
13219 static int
13220 maybe_emit_file (int fileno)
13222 if (DWARF2_ASM_LINE_DEBUG_INFO && fileno > 0)
13224 if (!VARRAY_UINT (file_table_emitted, fileno))
13226 VARRAY_UINT (file_table_emitted, fileno) = ++emitcount;
13227 fprintf (asm_out_file, "\t.file %u ",
13228 VARRAY_UINT (file_table_emitted, fileno));
13229 output_quoted_string (asm_out_file,
13230 VARRAY_CHAR_PTR (file_table, fileno));
13231 fputc ('\n', asm_out_file);
13233 return VARRAY_UINT (file_table_emitted, fileno);
13235 else
13236 return fileno;
13239 static void
13240 init_file_table (void)
13242 /* Allocate the initial hunk of the file_table. */
13243 VARRAY_CHAR_PTR_INIT (file_table, 64, "file_table");
13244 VARRAY_UINT_INIT (file_table_emitted, 64, "file_table_emitted");
13246 /* Skip the first entry - file numbers begin at 1. */
13247 VARRAY_PUSH_CHAR_PTR (file_table, NULL);
13248 VARRAY_PUSH_UINT (file_table_emitted, 0);
13249 file_table_last_lookup_index = 0;
13252 /* Called by the final INSN scan whenever we see a var location. We
13253 use it to drop labels in the right places, and throw the location in
13254 our lookup table. */
13256 static void
13257 dwarf2out_var_location (rtx loc_note)
13259 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13260 struct var_loc_node *newloc;
13261 rtx prev_insn;
13262 static rtx last_insn;
13263 static const char *last_label;
13264 tree decl;
13266 if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13267 return;
13268 prev_insn = PREV_INSN (loc_note);
13270 newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13271 /* If the insn we processed last time is the previous insn
13272 and it is also a var location note, use the label we emitted
13273 last time. */
13274 if (last_insn != NULL_RTX
13275 && last_insn == prev_insn
13276 && NOTE_P (prev_insn)
13277 && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13279 newloc->label = last_label;
13281 else
13283 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13284 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13285 loclabel_num++;
13286 newloc->label = ggc_strdup (loclabel);
13288 newloc->var_loc_note = loc_note;
13289 newloc->next = NULL;
13291 if (cfun
13292 && (last_text_section == in_unlikely_executed_text
13293 || (last_text_section == in_named
13294 && last_text_section_name == cfun->unlikely_text_section_name)))
13295 newloc->section_label = cfun->cold_section_label;
13296 else
13297 newloc->section_label = text_section_label;
13299 last_insn = loc_note;
13300 last_label = newloc->label;
13301 decl = NOTE_VAR_LOCATION_DECL (loc_note);
13302 if (DECL_DEBUG_EXPR_IS_FROM (decl) && DECL_DEBUG_EXPR (decl)
13303 && DECL_P (DECL_DEBUG_EXPR (decl)))
13304 decl = DECL_DEBUG_EXPR (decl);
13305 add_var_loc_to_decl (decl, newloc);
13308 /* We need to reset the locations at the beginning of each
13309 function. We can't do this in the end_function hook, because the
13310 declarations that use the locations won't have been outputted when
13311 that hook is called. */
13313 static void
13314 dwarf2out_begin_function (tree unused ATTRIBUTE_UNUSED)
13316 htab_empty (decl_loc_table);
13319 /* Output a label to mark the beginning of a source code line entry
13320 and record information relating to this source line, in
13321 'line_info_table' for later output of the .debug_line section. */
13323 static void
13324 dwarf2out_source_line (unsigned int line, const char *filename)
13326 if (debug_info_level >= DINFO_LEVEL_NORMAL
13327 && line != 0)
13329 current_function_section (current_function_decl);
13331 /* If requested, emit something human-readable. */
13332 if (flag_debug_asm)
13333 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13334 filename, line);
13336 if (DWARF2_ASM_LINE_DEBUG_INFO)
13338 unsigned file_num = lookup_filename (filename);
13340 file_num = maybe_emit_file (file_num);
13342 /* Emit the .loc directive understood by GNU as. */
13343 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
13345 /* Indicate that line number info exists. */
13346 line_info_table_in_use++;
13348 /* Indicate that multiple line number tables exist. */
13349 if (DECL_SECTION_NAME (current_function_decl))
13350 separate_line_info_table_in_use++;
13352 else if (DECL_SECTION_NAME (current_function_decl))
13354 dw_separate_line_info_ref line_info;
13355 targetm.asm_out.internal_label (asm_out_file, SEPARATE_LINE_CODE_LABEL,
13356 separate_line_info_table_in_use);
13358 /* Expand the line info table if necessary. */
13359 if (separate_line_info_table_in_use
13360 == separate_line_info_table_allocated)
13362 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13363 separate_line_info_table
13364 = ggc_realloc (separate_line_info_table,
13365 separate_line_info_table_allocated
13366 * sizeof (dw_separate_line_info_entry));
13367 memset (separate_line_info_table
13368 + separate_line_info_table_in_use,
13370 (LINE_INFO_TABLE_INCREMENT
13371 * sizeof (dw_separate_line_info_entry)));
13374 /* Add the new entry at the end of the line_info_table. */
13375 line_info
13376 = &separate_line_info_table[separate_line_info_table_in_use++];
13377 line_info->dw_file_num = lookup_filename (filename);
13378 line_info->dw_line_num = line;
13379 line_info->function = current_function_funcdef_no;
13381 else
13383 dw_line_info_ref line_info;
13385 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
13386 line_info_table_in_use);
13388 /* Expand the line info table if necessary. */
13389 if (line_info_table_in_use == line_info_table_allocated)
13391 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13392 line_info_table
13393 = ggc_realloc (line_info_table,
13394 (line_info_table_allocated
13395 * sizeof (dw_line_info_entry)));
13396 memset (line_info_table + line_info_table_in_use, 0,
13397 LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
13400 /* Add the new entry at the end of the line_info_table. */
13401 line_info = &line_info_table[line_info_table_in_use++];
13402 line_info->dw_file_num = lookup_filename (filename);
13403 line_info->dw_line_num = line;
13408 /* Record the beginning of a new source file. */
13410 static void
13411 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
13413 if (flag_eliminate_dwarf2_dups)
13415 /* Record the beginning of the file for break_out_includes. */
13416 dw_die_ref bincl_die;
13418 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
13419 add_AT_string (bincl_die, DW_AT_name, filename);
13422 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13424 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13425 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
13426 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13427 lineno);
13428 maybe_emit_file (lookup_filename (filename));
13429 dw2_asm_output_data_uleb128 (lookup_filename (filename),
13430 "Filename we just started");
13434 /* Record the end of a source file. */
13436 static void
13437 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
13439 if (flag_eliminate_dwarf2_dups)
13440 /* Record the end of the file for break_out_includes. */
13441 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
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_end_file, "End file");
13450 /* Called from debug_define in toplev.c. The `buffer' parameter contains
13451 the tail part of the directive line, i.e. the part which is past the
13452 initial whitespace, #, whitespace, directive-name, whitespace part. */
13454 static void
13455 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13456 const char *buffer ATTRIBUTE_UNUSED)
13458 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13460 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13461 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
13462 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13463 dw2_asm_output_nstring (buffer, -1, "The macro");
13467 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
13468 the tail part of the directive line, i.e. the part which is past the
13469 initial whitespace, #, whitespace, directive-name, whitespace part. */
13471 static void
13472 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13473 const char *buffer ATTRIBUTE_UNUSED)
13475 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13477 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13478 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
13479 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13480 dw2_asm_output_nstring (buffer, -1, "The macro");
13484 /* Set up for Dwarf output at the start of compilation. */
13486 static void
13487 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
13489 init_file_table ();
13491 /* Allocate the decl_die_table. */
13492 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13493 decl_die_table_eq, NULL);
13495 /* Allocate the decl_loc_table. */
13496 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13497 decl_loc_table_eq, NULL);
13499 /* Allocate the initial hunk of the decl_scope_table. */
13500 decl_scope_table = VEC_alloc (tree, gc, 256);
13502 /* Allocate the initial hunk of the abbrev_die_table. */
13503 abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
13504 * sizeof (dw_die_ref));
13505 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
13506 /* Zero-th entry is allocated, but unused. */
13507 abbrev_die_table_in_use = 1;
13509 /* Allocate the initial hunk of the line_info_table. */
13510 line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
13511 * sizeof (dw_line_info_entry));
13512 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
13514 /* Zero-th entry is allocated, but unused. */
13515 line_info_table_in_use = 1;
13517 /* Generate the initial DIE for the .debug section. Note that the (string)
13518 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
13519 will (typically) be a relative pathname and that this pathname should be
13520 taken as being relative to the directory from which the compiler was
13521 invoked when the given (base) source file was compiled. We will fill
13522 in this value in dwarf2out_finish. */
13523 comp_unit_die = gen_compile_unit_die (NULL);
13525 incomplete_types = VEC_alloc (tree, gc, 64);
13527 used_rtx_array = VEC_alloc (rtx, gc, 32);
13529 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
13530 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
13531 DEBUG_ABBREV_SECTION_LABEL, 0);
13532 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
13533 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
13534 COLD_TEXT_SECTION_LABEL, 0);
13535 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
13537 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
13538 DEBUG_INFO_SECTION_LABEL, 0);
13539 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
13540 DEBUG_LINE_SECTION_LABEL, 0);
13541 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
13542 DEBUG_RANGES_SECTION_LABEL, 0);
13543 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13544 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
13545 named_section_flags (DEBUG_INFO_SECTION, SECTION_DEBUG);
13546 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
13547 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13548 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
13550 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13552 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13553 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
13554 DEBUG_MACINFO_SECTION_LABEL, 0);
13555 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
13558 text_section ();
13559 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
13560 if (flag_reorder_blocks_and_partition)
13562 unlikely_text_section ();
13563 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
13567 /* A helper function for dwarf2out_finish called through
13568 ht_forall. Emit one queued .debug_str string. */
13570 static int
13571 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
13573 struct indirect_string_node *node = (struct indirect_string_node *) *h;
13575 if (node->form == DW_FORM_strp)
13577 named_section_flags (DEBUG_STR_SECTION, DEBUG_STR_SECTION_FLAGS);
13578 ASM_OUTPUT_LABEL (asm_out_file, node->label);
13579 assemble_string (node->str, strlen (node->str) + 1);
13582 return 1;
13587 /* Clear the marks for a die and its children.
13588 Be cool if the mark isn't set. */
13590 static void
13591 prune_unmark_dies (dw_die_ref die)
13593 dw_die_ref c;
13594 die->die_mark = 0;
13595 for (c = die->die_child; c; c = c->die_sib)
13596 prune_unmark_dies (c);
13600 /* Given DIE that we're marking as used, find any other dies
13601 it references as attributes and mark them as used. */
13603 static void
13604 prune_unused_types_walk_attribs (dw_die_ref die)
13606 dw_attr_ref a;
13608 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
13610 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
13612 /* A reference to another DIE.
13613 Make sure that it will get emitted. */
13614 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
13616 else if (a->dw_attr == DW_AT_decl_file)
13618 /* A reference to a file. Make sure the file name is emitted. */
13619 a->dw_attr_val.v.val_unsigned =
13620 maybe_emit_file (a->dw_attr_val.v.val_unsigned);
13626 /* Mark DIE as being used. If DOKIDS is true, then walk down
13627 to DIE's children. */
13629 static void
13630 prune_unused_types_mark (dw_die_ref die, int dokids)
13632 dw_die_ref c;
13634 if (die->die_mark == 0)
13636 /* We haven't done this node yet. Mark it as used. */
13637 die->die_mark = 1;
13639 /* We also have to mark its parents as used.
13640 (But we don't want to mark our parents' kids due to this.) */
13641 if (die->die_parent)
13642 prune_unused_types_mark (die->die_parent, 0);
13644 /* Mark any referenced nodes. */
13645 prune_unused_types_walk_attribs (die);
13647 /* If this node is a specification,
13648 also mark the definition, if it exists. */
13649 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
13650 prune_unused_types_mark (die->die_definition, 1);
13653 if (dokids && die->die_mark != 2)
13655 /* We need to walk the children, but haven't done so yet.
13656 Remember that we've walked the kids. */
13657 die->die_mark = 2;
13659 /* Walk them. */
13660 for (c = die->die_child; c; c = c->die_sib)
13662 /* If this is an array type, we need to make sure our
13663 kids get marked, even if they're types. */
13664 if (die->die_tag == DW_TAG_array_type)
13665 prune_unused_types_mark (c, 1);
13666 else
13667 prune_unused_types_walk (c);
13673 /* Walk the tree DIE and mark types that we actually use. */
13675 static void
13676 prune_unused_types_walk (dw_die_ref die)
13678 dw_die_ref c;
13680 /* Don't do anything if this node is already marked. */
13681 if (die->die_mark)
13682 return;
13684 switch (die->die_tag) {
13685 case DW_TAG_const_type:
13686 case DW_TAG_packed_type:
13687 case DW_TAG_pointer_type:
13688 case DW_TAG_reference_type:
13689 case DW_TAG_volatile_type:
13690 case DW_TAG_typedef:
13691 case DW_TAG_array_type:
13692 case DW_TAG_structure_type:
13693 case DW_TAG_union_type:
13694 case DW_TAG_class_type:
13695 case DW_TAG_friend:
13696 case DW_TAG_variant_part:
13697 case DW_TAG_enumeration_type:
13698 case DW_TAG_subroutine_type:
13699 case DW_TAG_string_type:
13700 case DW_TAG_set_type:
13701 case DW_TAG_subrange_type:
13702 case DW_TAG_ptr_to_member_type:
13703 case DW_TAG_file_type:
13704 /* It's a type node --- don't mark it. */
13705 return;
13707 default:
13708 /* Mark everything else. */
13709 break;
13712 die->die_mark = 1;
13714 /* Now, mark any dies referenced from here. */
13715 prune_unused_types_walk_attribs (die);
13717 /* Mark children. */
13718 for (c = die->die_child; c; c = c->die_sib)
13719 prune_unused_types_walk (c);
13723 /* Remove from the tree DIE any dies that aren't marked. */
13725 static void
13726 prune_unused_types_prune (dw_die_ref die)
13728 dw_die_ref c, p, n;
13730 gcc_assert (die->die_mark);
13732 p = NULL;
13733 for (c = die->die_child; c; c = n)
13735 n = c->die_sib;
13736 if (c->die_mark)
13738 prune_unused_types_prune (c);
13739 p = c;
13741 else
13743 if (p)
13744 p->die_sib = n;
13745 else
13746 die->die_child = n;
13747 free_die (c);
13753 /* Remove dies representing declarations that we never use. */
13755 static void
13756 prune_unused_types (void)
13758 unsigned int i;
13759 limbo_die_node *node;
13761 /* Clear all the marks. */
13762 prune_unmark_dies (comp_unit_die);
13763 for (node = limbo_die_list; node; node = node->next)
13764 prune_unmark_dies (node->die);
13766 /* Set the mark on nodes that are actually used. */
13767 prune_unused_types_walk (comp_unit_die);
13768 for (node = limbo_die_list; node; node = node->next)
13769 prune_unused_types_walk (node->die);
13771 /* Also set the mark on nodes referenced from the
13772 pubname_table or arange_table. */
13773 for (i = 0; i < pubname_table_in_use; i++)
13774 prune_unused_types_mark (pubname_table[i].die, 1);
13775 for (i = 0; i < arange_table_in_use; i++)
13776 prune_unused_types_mark (arange_table[i], 1);
13778 /* Get rid of nodes that aren't marked. */
13779 prune_unused_types_prune (comp_unit_die);
13780 for (node = limbo_die_list; node; node = node->next)
13781 prune_unused_types_prune (node->die);
13783 /* Leave the marks clear. */
13784 prune_unmark_dies (comp_unit_die);
13785 for (node = limbo_die_list; node; node = node->next)
13786 prune_unmark_dies (node->die);
13789 /* Output stuff that dwarf requires at the end of every file,
13790 and generate the DWARF-2 debugging info. */
13792 static void
13793 dwarf2out_finish (const char *filename)
13795 limbo_die_node *node, *next_node;
13796 dw_die_ref die = 0;
13798 /* Add the name for the main input file now. We delayed this from
13799 dwarf2out_init to avoid complications with PCH. */
13800 add_name_attribute (comp_unit_die, filename);
13801 if (filename[0] != DIR_SEPARATOR)
13802 add_comp_dir_attribute (comp_unit_die);
13803 else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
13805 size_t i;
13806 for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
13807 if (VARRAY_CHAR_PTR (file_table, i)[0] != DIR_SEPARATOR
13808 /* Don't add cwd for <built-in>. */
13809 && VARRAY_CHAR_PTR (file_table, i)[0] != '<')
13811 add_comp_dir_attribute (comp_unit_die);
13812 break;
13816 /* Traverse the limbo die list, and add parent/child links. The only
13817 dies without parents that should be here are concrete instances of
13818 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
13819 For concrete instances, we can get the parent die from the abstract
13820 instance. */
13821 for (node = limbo_die_list; node; node = next_node)
13823 next_node = node->next;
13824 die = node->die;
13826 if (die->die_parent == NULL)
13828 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
13830 if (origin)
13831 add_child_die (origin->die_parent, die);
13832 else if (die == comp_unit_die)
13834 else if (errorcount > 0 || sorrycount > 0)
13835 /* It's OK to be confused by errors in the input. */
13836 add_child_die (comp_unit_die, die);
13837 else
13839 /* In certain situations, the lexical block containing a
13840 nested function can be optimized away, which results
13841 in the nested function die being orphaned. Likewise
13842 with the return type of that nested function. Force
13843 this to be a child of the containing function.
13845 It may happen that even the containing function got fully
13846 inlined and optimized out. In that case we are lost and
13847 assign the empty child. This should not be big issue as
13848 the function is likely unreachable too. */
13849 tree context = NULL_TREE;
13851 gcc_assert (node->created_for);
13853 if (DECL_P (node->created_for))
13854 context = DECL_CONTEXT (node->created_for);
13855 else if (TYPE_P (node->created_for))
13856 context = TYPE_CONTEXT (node->created_for);
13858 gcc_assert (context && TREE_CODE (context) == FUNCTION_DECL);
13860 origin = lookup_decl_die (context);
13861 if (origin)
13862 add_child_die (origin, die);
13863 else
13864 add_child_die (comp_unit_die, die);
13869 limbo_die_list = NULL;
13871 /* Walk through the list of incomplete types again, trying once more to
13872 emit full debugging info for them. */
13873 retry_incomplete_types ();
13875 /* We need to reverse all the dies before break_out_includes, or
13876 we'll see the end of an include file before the beginning. */
13877 reverse_all_dies (comp_unit_die);
13879 if (flag_eliminate_unused_debug_types)
13880 prune_unused_types ();
13882 /* Generate separate CUs for each of the include files we've seen.
13883 They will go into limbo_die_list. */
13884 if (flag_eliminate_dwarf2_dups)
13885 break_out_includes (comp_unit_die);
13887 /* Traverse the DIE's and add add sibling attributes to those DIE's
13888 that have children. */
13889 add_sibling_attributes (comp_unit_die);
13890 for (node = limbo_die_list; node; node = node->next)
13891 add_sibling_attributes (node->die);
13893 /* Output a terminator label for the .text section. */
13894 text_section ();
13895 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
13896 if (flag_reorder_blocks_and_partition)
13898 unlikely_text_section ();
13899 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
13902 /* Output the source line correspondence table. We must do this
13903 even if there is no line information. Otherwise, on an empty
13904 translation unit, we will generate a present, but empty,
13905 .debug_info section. IRIX 6.5 `nm' will then complain when
13906 examining the file. */
13907 if (! DWARF2_ASM_LINE_DEBUG_INFO)
13909 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13910 output_line_info ();
13913 /* Output location list section if necessary. */
13914 if (have_location_lists)
13916 /* Output the location lists info. */
13917 named_section_flags (DEBUG_LOC_SECTION, SECTION_DEBUG);
13918 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
13919 DEBUG_LOC_SECTION_LABEL, 0);
13920 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
13921 output_location_lists (die);
13922 have_location_lists = 0;
13925 /* We can only use the low/high_pc attributes if all of the code was
13926 in .text. */
13927 if (separate_line_info_table_in_use == 0)
13929 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
13930 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
13933 /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
13934 "base address". Use zero so that these addresses become absolute. */
13935 else if (have_location_lists || ranges_table_in_use)
13936 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
13938 if (debug_info_level >= DINFO_LEVEL_NORMAL)
13939 add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
13940 debug_line_section_label);
13942 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13943 add_AT_lbl_offset (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
13945 /* Output all of the compilation units. We put the main one last so that
13946 the offsets are available to output_pubnames. */
13947 for (node = limbo_die_list; node; node = node->next)
13948 output_comp_unit (node->die, 0);
13950 output_comp_unit (comp_unit_die, 0);
13952 /* Output the abbreviation table. */
13953 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13954 output_abbrev_section ();
13956 /* Output public names table if necessary. */
13957 if (pubname_table_in_use)
13959 named_section_flags (DEBUG_PUBNAMES_SECTION, SECTION_DEBUG);
13960 output_pubnames ();
13963 /* Output the address range information. We only put functions in the arange
13964 table, so don't write it out if we don't have any. */
13965 if (fde_table_in_use)
13967 named_section_flags (DEBUG_ARANGES_SECTION, SECTION_DEBUG);
13968 output_aranges ();
13971 /* Output ranges section if necessary. */
13972 if (ranges_table_in_use)
13974 named_section_flags (DEBUG_RANGES_SECTION, SECTION_DEBUG);
13975 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
13976 output_ranges ();
13979 /* Have to end the macro section. */
13980 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13982 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13983 dw2_asm_output_data (1, 0, "End compilation unit");
13986 /* If we emitted any DW_FORM_strp form attribute, output the string
13987 table too. */
13988 if (debug_str_hash)
13989 htab_traverse (debug_str_hash, output_indirect_string, NULL);
13991 #else
13993 /* This should never be used, but its address is needed for comparisons. */
13994 const struct gcc_debug_hooks dwarf2_debug_hooks;
13996 #endif /* DWARF2_DEBUGGING_INFO */
13998 #include "gt-dwarf2out.h"