Daily bump.
[official-gcc.git] / gcc / dwarf2out.c
blob526766b3e5fc5a8c0867a1d58b406840e205eab5
1 /* Output Dwarf2 format symbol table information from the GNU C compiler.
2 Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 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 "tree.h"
40 #include "flags.h"
41 #include "rtl.h"
42 #include "hard-reg-set.h"
43 #include "regs.h"
44 #include "insn-config.h"
45 #include "reload.h"
46 #include "function.h"
47 #include "output.h"
48 #include "expr.h"
49 #include "libfuncs.h"
50 #include "except.h"
51 #include "dwarf2.h"
52 #include "dwarf2out.h"
53 #include "dwarf2asm.h"
54 #include "toplev.h"
55 #include "varray.h"
56 #include "ggc.h"
57 #include "md5.h"
58 #include "tm_p.h"
59 #include "diagnostic.h"
60 #include "debug.h"
61 #include "target.h"
62 #include "langhooks.h"
63 #include "hashtable.h"
65 #ifdef DWARF2_DEBUGGING_INFO
66 static void dwarf2out_source_line PARAMS ((unsigned int, const char *));
67 #endif
69 /* DWARF2 Abbreviation Glossary:
70 CFA = Canonical Frame Address
71 a fixed address on the stack which identifies a call frame.
72 We define it to be the value of SP just before the call insn.
73 The CFA register and offset, which may change during the course
74 of the function, are used to calculate its value at runtime.
75 CFI = Call Frame Instruction
76 an instruction for the DWARF2 abstract machine
77 CIE = Common Information Entry
78 information describing information common to one or more FDEs
79 DIE = Debugging Information Entry
80 FDE = Frame Description Entry
81 information describing the stack call frame, in particular,
82 how to restore registers
84 DW_CFA_... = DWARF2 CFA call frame instruction
85 DW_TAG_... = DWARF2 DIE tag */
87 /* Decide whether we want to emit frame unwind information for the current
88 translation unit. */
90 int
91 dwarf2out_do_frame ()
93 return (write_symbols == DWARF2_DEBUG
94 || write_symbols == VMS_AND_DWARF2_DEBUG
95 #ifdef DWARF2_FRAME_INFO
96 || DWARF2_FRAME_INFO
97 #endif
98 #ifdef DWARF2_UNWIND_INFO
99 || flag_unwind_tables
100 || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)
101 #endif
105 /* The number of the current function definition for which debugging
106 information is being generated. These numbers range from 1 up to the
107 maximum number of function definitions contained within the current
108 compilation unit. These numbers are used to create unique label id's
109 unique to each function definition. */
110 unsigned current_funcdef_number = 0;
112 /* The size of the target's pointer type. */
113 #ifndef PTR_SIZE
114 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
115 #endif
117 /* Default version of targetm.eh_frame_section. Note this must appear
118 outside the DWARF2_DEBUGGING_INFO || DWARF2_UNWIND_INFO macro
119 guards. */
121 void
122 default_eh_frame_section ()
124 #ifdef EH_FRAME_SECTION_NAME
125 named_section_flags (EH_FRAME_SECTION_NAME, SECTION_WRITE);
126 #else
127 tree label = get_file_function_name ('F');
129 data_section ();
130 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
131 ASM_GLOBALIZE_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
132 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
133 #endif
136 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
138 /* How to start an assembler comment. */
139 #ifndef ASM_COMMENT_START
140 #define ASM_COMMENT_START ";#"
141 #endif
143 typedef struct dw_cfi_struct *dw_cfi_ref;
144 typedef struct dw_fde_struct *dw_fde_ref;
145 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
147 /* Call frames are described using a sequence of Call Frame
148 Information instructions. The register number, offset
149 and address fields are provided as possible operands;
150 their use is selected by the opcode field. */
152 typedef union dw_cfi_oprnd_struct
154 unsigned long dw_cfi_reg_num;
155 long int dw_cfi_offset;
156 const char *dw_cfi_addr;
157 struct dw_loc_descr_struct *dw_cfi_loc;
159 dw_cfi_oprnd;
161 typedef struct dw_cfi_struct
163 dw_cfi_ref dw_cfi_next;
164 enum dwarf_call_frame_info dw_cfi_opc;
165 dw_cfi_oprnd dw_cfi_oprnd1;
166 dw_cfi_oprnd dw_cfi_oprnd2;
168 dw_cfi_node;
170 /* This is how we define the location of the CFA. We use to handle it
171 as REG + OFFSET all the time, but now it can be more complex.
172 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
173 Instead of passing around REG and OFFSET, we pass a copy
174 of this structure. */
175 typedef struct cfa_loc
177 unsigned long reg;
178 long offset;
179 long base_offset;
180 int indirect; /* 1 if CFA is accessed via a dereference. */
181 } dw_cfa_location;
183 /* All call frame descriptions (FDE's) in the GCC generated DWARF
184 refer to a single Common Information Entry (CIE), defined at
185 the beginning of the .debug_frame section. This use of a single
186 CIE obviates the need to keep track of multiple CIE's
187 in the DWARF generation routines below. */
189 typedef struct dw_fde_struct
191 const char *dw_fde_begin;
192 const char *dw_fde_current_label;
193 const char *dw_fde_end;
194 dw_cfi_ref dw_fde_cfi;
195 unsigned funcdef_number;
196 unsigned nothrow : 1;
197 unsigned uses_eh_lsda : 1;
199 dw_fde_node;
201 /* Maximum size (in bytes) of an artificially generated label. */
202 #define MAX_ARTIFICIAL_LABEL_BYTES 30
204 /* The size of addresses as they appear in the Dwarf 2 data.
205 Some architectures use word addresses to refer to code locations,
206 but Dwarf 2 info always uses byte addresses. On such machines,
207 Dwarf 2 addresses need to be larger than the architecture's
208 pointers. */
209 #ifndef DWARF2_ADDR_SIZE
210 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
211 #endif
213 /* The size in bytes of a DWARF field indicating an offset or length
214 relative to a debug info section, specified to be 4 bytes in the
215 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
216 as PTR_SIZE. */
218 #ifndef DWARF_OFFSET_SIZE
219 #define DWARF_OFFSET_SIZE 4
220 #endif
222 #define DWARF_VERSION 2
224 /* Round SIZE up to the nearest BOUNDARY. */
225 #define DWARF_ROUND(SIZE,BOUNDARY) \
226 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
228 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
229 #ifndef DWARF_CIE_DATA_ALIGNMENT
230 #ifdef STACK_GROWS_DOWNWARD
231 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
232 #else
233 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
234 #endif
235 #endif
237 /* A pointer to the base of a table that contains frame description
238 information for each routine. */
239 static dw_fde_ref fde_table;
241 /* Number of elements currently allocated for fde_table. */
242 static unsigned fde_table_allocated;
244 /* Number of elements in fde_table currently in use. */
245 static unsigned fde_table_in_use;
247 /* Size (in elements) of increments by which we may expand the
248 fde_table. */
249 #define FDE_TABLE_INCREMENT 256
251 /* A list of call frame insns for the CIE. */
252 static dw_cfi_ref cie_cfi_head;
254 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
255 attribute that accelerates the lookup of the FDE associated
256 with the subprogram. This variable holds the table index of the FDE
257 associated with the current function (body) definition. */
258 static unsigned current_funcdef_fde;
260 struct ht *debug_str_hash;
262 struct indirect_string_node
264 struct ht_identifier id;
265 unsigned int refcount;
266 unsigned int form;
267 char *label;
270 /* Forward declarations for functions defined in this file. */
272 static char *stripattributes PARAMS ((const char *));
273 static const char *dwarf_cfi_name PARAMS ((unsigned));
274 static dw_cfi_ref new_cfi PARAMS ((void));
275 static void add_cfi PARAMS ((dw_cfi_ref *, dw_cfi_ref));
276 static void add_fde_cfi PARAMS ((const char *, dw_cfi_ref));
277 static void lookup_cfa_1 PARAMS ((dw_cfi_ref,
278 dw_cfa_location *));
279 static void lookup_cfa PARAMS ((dw_cfa_location *));
280 static void reg_save PARAMS ((const char *, unsigned,
281 unsigned, long));
282 static void initial_return_save PARAMS ((rtx));
283 static long stack_adjust_offset PARAMS ((rtx));
284 static void output_cfi PARAMS ((dw_cfi_ref, dw_fde_ref, int));
285 static void output_call_frame_info PARAMS ((int));
286 static void dwarf2out_stack_adjust PARAMS ((rtx));
287 static void queue_reg_save PARAMS ((const char *, rtx, long));
288 static void flush_queued_reg_saves PARAMS ((void));
289 static bool clobbers_queued_reg_save PARAMS ((rtx));
290 static void dwarf2out_frame_debug_expr PARAMS ((rtx, const char *));
292 /* Support for complex CFA locations. */
293 static void output_cfa_loc PARAMS ((dw_cfi_ref));
294 static void get_cfa_from_loc_descr PARAMS ((dw_cfa_location *,
295 struct dw_loc_descr_struct *));
296 static struct dw_loc_descr_struct *build_cfa_loc
297 PARAMS ((dw_cfa_location *));
298 static void def_cfa_1 PARAMS ((const char *,
299 dw_cfa_location *));
301 /* How to start an assembler comment. */
302 #ifndef ASM_COMMENT_START
303 #define ASM_COMMENT_START ";#"
304 #endif
306 /* Data and reference forms for relocatable data. */
307 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
308 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
310 /* Pseudo-op for defining a new section. */
311 #ifndef SECTION_ASM_OP
312 #define SECTION_ASM_OP "\t.section\t"
313 #endif
315 #ifndef DEBUG_FRAME_SECTION
316 #define DEBUG_FRAME_SECTION ".debug_frame"
317 #endif
319 #ifndef FUNC_BEGIN_LABEL
320 #define FUNC_BEGIN_LABEL "LFB"
321 #endif
323 #ifndef FUNC_END_LABEL
324 #define FUNC_END_LABEL "LFE"
325 #endif
327 #define FRAME_BEGIN_LABEL "Lframe"
328 #define CIE_AFTER_SIZE_LABEL "LSCIE"
329 #define CIE_END_LABEL "LECIE"
330 #define CIE_LENGTH_LABEL "LLCIE"
331 #define FDE_LABEL "LSFDE"
332 #define FDE_AFTER_SIZE_LABEL "LASFDE"
333 #define FDE_END_LABEL "LEFDE"
334 #define FDE_LENGTH_LABEL "LLFDE"
335 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
336 #define LINE_NUMBER_END_LABEL "LELT"
337 #define LN_PROLOG_AS_LABEL "LASLTP"
338 #define LN_PROLOG_END_LABEL "LELTP"
339 #define DIE_LABEL_PREFIX "DW"
341 /* Definitions of defaults for various types of primitive assembly language
342 output operations. These may be overridden from within the tm.h file,
343 but typically, that is unnecessary. */
345 #ifdef SET_ASM_OP
346 #ifndef ASM_OUTPUT_DEFINE_LABEL_DIFFERENCE_SYMBOL
347 #define ASM_OUTPUT_DEFINE_LABEL_DIFFERENCE_SYMBOL(FILE, SY, HI, LO) \
348 do { \
349 fprintf (FILE, "%s", SET_ASM_OP); \
350 assemble_name (FILE, SY); \
351 fputc (',', FILE); \
352 assemble_name (FILE, HI); \
353 fputc ('-', FILE); \
354 assemble_name (FILE, LO); \
355 } while (0)
356 #endif
357 #endif
359 /* The DWARF 2 CFA column which tracks the return address. Normally this
360 is the column for PC, or the first column after all of the hard
361 registers. */
362 #ifndef DWARF_FRAME_RETURN_COLUMN
363 #ifdef PC_REGNUM
364 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
365 #else
366 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
367 #endif
368 #endif
370 /* The mapping from gcc register number to DWARF 2 CFA column number. By
371 default, we just provide columns for all registers. */
372 #ifndef DWARF_FRAME_REGNUM
373 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
374 #endif
376 /* The offset from the incoming value of %sp to the top of the stack frame
377 for the current function. */
378 #ifndef INCOMING_FRAME_SP_OFFSET
379 #define INCOMING_FRAME_SP_OFFSET 0
380 #endif
382 /* Hook used by __throw. */
385 expand_builtin_dwarf_fp_regnum ()
387 return GEN_INT (DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM));
390 /* Return a pointer to a copy of the section string name S with all
391 attributes stripped off, and an asterisk prepended (for assemble_name). */
393 static inline char *
394 stripattributes (s)
395 const char *s;
397 char *stripped = xmalloc (strlen (s) + 2);
398 char *p = stripped;
400 *p++ = '*';
402 while (*s && *s != ',')
403 *p++ = *s++;
405 *p = '\0';
406 return stripped;
409 /* Generate code to initialize the register size table. */
411 void
412 expand_builtin_init_dwarf_reg_sizes (address)
413 tree address;
415 int i;
416 enum machine_mode mode = TYPE_MODE (char_type_node);
417 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, 0);
418 rtx mem = gen_rtx_MEM (BLKmode, addr);
420 for (i = 0; i < DWARF_FRAME_REGISTERS; i++)
422 HOST_WIDE_INT offset = DWARF_FRAME_REGNUM (i) * GET_MODE_SIZE (mode);
423 HOST_WIDE_INT size = GET_MODE_SIZE (reg_raw_mode[i]);
425 if (offset < 0)
426 continue;
428 emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
432 /* Convert a DWARF call frame info. operation to its string name */
434 static const char *
435 dwarf_cfi_name (cfi_opc)
436 unsigned cfi_opc;
438 switch (cfi_opc)
440 case DW_CFA_advance_loc:
441 return "DW_CFA_advance_loc";
442 case DW_CFA_offset:
443 return "DW_CFA_offset";
444 case DW_CFA_restore:
445 return "DW_CFA_restore";
446 case DW_CFA_nop:
447 return "DW_CFA_nop";
448 case DW_CFA_set_loc:
449 return "DW_CFA_set_loc";
450 case DW_CFA_advance_loc1:
451 return "DW_CFA_advance_loc1";
452 case DW_CFA_advance_loc2:
453 return "DW_CFA_advance_loc2";
454 case DW_CFA_advance_loc4:
455 return "DW_CFA_advance_loc4";
456 case DW_CFA_offset_extended:
457 return "DW_CFA_offset_extended";
458 case DW_CFA_restore_extended:
459 return "DW_CFA_restore_extended";
460 case DW_CFA_undefined:
461 return "DW_CFA_undefined";
462 case DW_CFA_same_value:
463 return "DW_CFA_same_value";
464 case DW_CFA_register:
465 return "DW_CFA_register";
466 case DW_CFA_remember_state:
467 return "DW_CFA_remember_state";
468 case DW_CFA_restore_state:
469 return "DW_CFA_restore_state";
470 case DW_CFA_def_cfa:
471 return "DW_CFA_def_cfa";
472 case DW_CFA_def_cfa_register:
473 return "DW_CFA_def_cfa_register";
474 case DW_CFA_def_cfa_offset:
475 return "DW_CFA_def_cfa_offset";
477 /* DWARF 3 */
478 case DW_CFA_def_cfa_expression:
479 return "DW_CFA_def_cfa_expression";
480 case DW_CFA_expression:
481 return "DW_CFA_expression";
482 case DW_CFA_offset_extended_sf:
483 return "DW_CFA_offset_extended_sf";
484 case DW_CFA_def_cfa_sf:
485 return "DW_CFA_def_cfa_sf";
486 case DW_CFA_def_cfa_offset_sf:
487 return "DW_CFA_def_cfa_offset_sf";
489 /* SGI/MIPS specific */
490 case DW_CFA_MIPS_advance_loc8:
491 return "DW_CFA_MIPS_advance_loc8";
493 /* GNU extensions */
494 case DW_CFA_GNU_window_save:
495 return "DW_CFA_GNU_window_save";
496 case DW_CFA_GNU_args_size:
497 return "DW_CFA_GNU_args_size";
498 case DW_CFA_GNU_negative_offset_extended:
499 return "DW_CFA_GNU_negative_offset_extended";
501 default:
502 return "DW_CFA_<unknown>";
506 /* Return a pointer to a newly allocated Call Frame Instruction. */
508 static inline dw_cfi_ref
509 new_cfi ()
511 dw_cfi_ref cfi = (dw_cfi_ref) xmalloc (sizeof (dw_cfi_node));
513 cfi->dw_cfi_next = NULL;
514 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
515 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
517 return cfi;
520 /* Add a Call Frame Instruction to list of instructions. */
522 static inline void
523 add_cfi (list_head, cfi)
524 dw_cfi_ref *list_head;
525 dw_cfi_ref cfi;
527 dw_cfi_ref *p;
529 /* Find the end of the chain. */
530 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
533 *p = cfi;
536 /* Generate a new label for the CFI info to refer to. */
538 char *
539 dwarf2out_cfi_label ()
541 static char label[20];
542 static unsigned long label_num = 0;
544 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", label_num++);
545 ASM_OUTPUT_LABEL (asm_out_file, label);
546 return label;
549 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
550 or to the CIE if LABEL is NULL. */
552 static void
553 add_fde_cfi (label, cfi)
554 const char *label;
555 dw_cfi_ref cfi;
557 if (label)
559 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
561 if (*label == 0)
562 label = dwarf2out_cfi_label ();
564 if (fde->dw_fde_current_label == NULL
565 || strcmp (label, fde->dw_fde_current_label) != 0)
567 dw_cfi_ref xcfi;
569 fde->dw_fde_current_label = label = xstrdup (label);
571 /* Set the location counter to the new label. */
572 xcfi = new_cfi ();
573 xcfi->dw_cfi_opc = DW_CFA_advance_loc4;
574 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
575 add_cfi (&fde->dw_fde_cfi, xcfi);
578 add_cfi (&fde->dw_fde_cfi, cfi);
581 else
582 add_cfi (&cie_cfi_head, cfi);
585 /* Subroutine of lookup_cfa. */
587 static inline void
588 lookup_cfa_1 (cfi, loc)
589 dw_cfi_ref cfi;
590 dw_cfa_location *loc;
592 switch (cfi->dw_cfi_opc)
594 case DW_CFA_def_cfa_offset:
595 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
596 break;
597 case DW_CFA_def_cfa_register:
598 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
599 break;
600 case DW_CFA_def_cfa:
601 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
602 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
603 break;
604 case DW_CFA_def_cfa_expression:
605 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
606 break;
607 default:
608 break;
612 /* Find the previous value for the CFA. */
614 static void
615 lookup_cfa (loc)
616 dw_cfa_location *loc;
618 dw_cfi_ref cfi;
620 loc->reg = (unsigned long) -1;
621 loc->offset = 0;
622 loc->indirect = 0;
623 loc->base_offset = 0;
625 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
626 lookup_cfa_1 (cfi, loc);
628 if (fde_table_in_use)
630 dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
631 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
632 lookup_cfa_1 (cfi, loc);
636 /* The current rule for calculating the DWARF2 canonical frame address. */
637 static dw_cfa_location cfa;
639 /* The register used for saving registers to the stack, and its offset
640 from the CFA. */
641 static dw_cfa_location cfa_store;
643 /* The running total of the size of arguments pushed onto the stack. */
644 static long args_size;
646 /* The last args_size we actually output. */
647 static long old_args_size;
649 /* Entry point to update the canonical frame address (CFA).
650 LABEL is passed to add_fde_cfi. The value of CFA is now to be
651 calculated from REG+OFFSET. */
653 void
654 dwarf2out_def_cfa (label, reg, offset)
655 const char *label;
656 unsigned reg;
657 long offset;
659 dw_cfa_location loc;
660 loc.indirect = 0;
661 loc.base_offset = 0;
662 loc.reg = reg;
663 loc.offset = offset;
664 def_cfa_1 (label, &loc);
667 /* This routine does the actual work. The CFA is now calculated from
668 the dw_cfa_location structure. */
670 static void
671 def_cfa_1 (label, loc_p)
672 const char *label;
673 dw_cfa_location *loc_p;
675 dw_cfi_ref cfi;
676 dw_cfa_location old_cfa, loc;
678 cfa = *loc_p;
679 loc = *loc_p;
681 if (cfa_store.reg == loc.reg && loc.indirect == 0)
682 cfa_store.offset = loc.offset;
684 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
685 lookup_cfa (&old_cfa);
687 /* If nothing changed, no need to issue any call frame instructions. */
688 if (loc.reg == old_cfa.reg && loc.offset == old_cfa.offset
689 && loc.indirect == old_cfa.indirect
690 && (loc.indirect == 0 || loc.base_offset == old_cfa.base_offset))
691 return;
693 cfi = new_cfi ();
695 if (loc.reg == old_cfa.reg && !loc.indirect)
697 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction,
698 indicating the CFA register did not change but the offset
699 did. */
700 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
701 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
704 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
705 else if (loc.offset == old_cfa.offset && old_cfa.reg != (unsigned long) -1
706 && !loc.indirect)
708 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
709 indicating the CFA register has changed to <register> but the
710 offset has not changed. */
711 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
712 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
714 #endif
716 else if (loc.indirect == 0)
718 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
719 indicating the CFA register has changed to <register> with
720 the specified offset. */
721 cfi->dw_cfi_opc = DW_CFA_def_cfa;
722 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
723 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
725 else
727 /* Construct a DW_CFA_def_cfa_expression instruction to
728 calculate the CFA using a full location expression since no
729 register-offset pair is available. */
730 struct dw_loc_descr_struct *loc_list;
732 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
733 loc_list = build_cfa_loc (&loc);
734 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
737 add_fde_cfi (label, cfi);
740 /* Add the CFI for saving a register. REG is the CFA column number.
741 LABEL is passed to add_fde_cfi.
742 If SREG is -1, the register is saved at OFFSET from the CFA;
743 otherwise it is saved in SREG. */
745 static void
746 reg_save (label, reg, sreg, offset)
747 const char *label;
748 unsigned reg;
749 unsigned sreg;
750 long offset;
752 dw_cfi_ref cfi = new_cfi ();
754 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
756 /* The following comparison is correct. -1 is used to indicate that
757 the value isn't a register number. */
758 if (sreg == (unsigned int) -1)
760 if (reg & ~0x3f)
761 /* The register number won't fit in 6 bits, so we have to use
762 the long form. */
763 cfi->dw_cfi_opc = DW_CFA_offset_extended;
764 else
765 cfi->dw_cfi_opc = DW_CFA_offset;
767 #ifdef ENABLE_CHECKING
769 /* If we get an offset that is not a multiple of
770 DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
771 definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
772 description. */
773 long check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
775 if (check_offset * DWARF_CIE_DATA_ALIGNMENT != offset)
776 abort ();
778 #endif
779 offset /= DWARF_CIE_DATA_ALIGNMENT;
780 if (offset < 0)
781 cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
783 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
785 else if (sreg == reg)
786 /* We could emit a DW_CFA_same_value in this case, but don't bother. */
787 return;
788 else
790 cfi->dw_cfi_opc = DW_CFA_register;
791 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
794 add_fde_cfi (label, cfi);
797 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
798 This CFI tells the unwinder that it needs to restore the window registers
799 from the previous frame's window save area.
801 ??? Perhaps we should note in the CIE where windows are saved (instead of
802 assuming 0(cfa)) and what registers are in the window. */
804 void
805 dwarf2out_window_save (label)
806 const char *label;
808 dw_cfi_ref cfi = new_cfi ();
810 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
811 add_fde_cfi (label, cfi);
814 /* Add a CFI to update the running total of the size of arguments
815 pushed onto the stack. */
817 void
818 dwarf2out_args_size (label, size)
819 const char *label;
820 long size;
822 dw_cfi_ref cfi;
824 if (size == old_args_size)
825 return;
827 old_args_size = size;
829 cfi = new_cfi ();
830 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
831 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
832 add_fde_cfi (label, cfi);
835 /* Entry point for saving a register to the stack. REG is the GCC register
836 number. LABEL and OFFSET are passed to reg_save. */
838 void
839 dwarf2out_reg_save (label, reg, offset)
840 const char *label;
841 unsigned reg;
842 long offset;
844 reg_save (label, DWARF_FRAME_REGNUM (reg), -1, offset);
847 /* Entry point for saving the return address in the stack.
848 LABEL and OFFSET are passed to reg_save. */
850 void
851 dwarf2out_return_save (label, offset)
852 const char *label;
853 long offset;
855 reg_save (label, DWARF_FRAME_RETURN_COLUMN, -1, offset);
858 /* Entry point for saving the return address in a register.
859 LABEL and SREG are passed to reg_save. */
861 void
862 dwarf2out_return_reg (label, sreg)
863 const char *label;
864 unsigned sreg;
866 reg_save (label, DWARF_FRAME_RETURN_COLUMN, sreg, 0);
869 /* Record the initial position of the return address. RTL is
870 INCOMING_RETURN_ADDR_RTX. */
872 static void
873 initial_return_save (rtl)
874 rtx rtl;
876 unsigned int reg = (unsigned int) -1;
877 HOST_WIDE_INT offset = 0;
879 switch (GET_CODE (rtl))
881 case REG:
882 /* RA is in a register. */
883 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
884 break;
886 case MEM:
887 /* RA is on the stack. */
888 rtl = XEXP (rtl, 0);
889 switch (GET_CODE (rtl))
891 case REG:
892 if (REGNO (rtl) != STACK_POINTER_REGNUM)
893 abort ();
894 offset = 0;
895 break;
897 case PLUS:
898 if (REGNO (XEXP (rtl, 0)) != STACK_POINTER_REGNUM)
899 abort ();
900 offset = INTVAL (XEXP (rtl, 1));
901 break;
903 case MINUS:
904 if (REGNO (XEXP (rtl, 0)) != STACK_POINTER_REGNUM)
905 abort ();
906 offset = -INTVAL (XEXP (rtl, 1));
907 break;
909 default:
910 abort ();
913 break;
915 case PLUS:
916 /* The return address is at some offset from any value we can
917 actually load. For instance, on the SPARC it is in %i7+8. Just
918 ignore the offset for now; it doesn't matter for unwinding frames. */
919 if (GET_CODE (XEXP (rtl, 1)) != CONST_INT)
920 abort ();
921 initial_return_save (XEXP (rtl, 0));
922 return;
924 default:
925 abort ();
928 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
931 /* Given a SET, calculate the amount of stack adjustment it
932 contains. */
934 static long
935 stack_adjust_offset (pattern)
936 rtx pattern;
938 rtx src = SET_SRC (pattern);
939 rtx dest = SET_DEST (pattern);
940 HOST_WIDE_INT offset = 0;
941 enum rtx_code code;
943 if (dest == stack_pointer_rtx)
945 /* (set (reg sp) (plus (reg sp) (const_int))) */
946 code = GET_CODE (src);
947 if (! (code == PLUS || code == MINUS)
948 || XEXP (src, 0) != stack_pointer_rtx
949 || GET_CODE (XEXP (src, 1)) != CONST_INT)
950 return 0;
952 offset = INTVAL (XEXP (src, 1));
954 else if (GET_CODE (dest) == MEM)
956 /* (set (mem (pre_dec (reg sp))) (foo)) */
957 src = XEXP (dest, 0);
958 code = GET_CODE (src);
960 if ((code != PRE_DEC && code != PRE_INC && code != PRE_MODIFY)
961 || XEXP (src, 0) != stack_pointer_rtx)
962 return 0;
964 if (code == PRE_MODIFY)
966 rtx val = XEXP (XEXP (src, 1), 1);
968 /* We handle only adjustments by constant amount. */
969 if (GET_CODE (XEXP (src, 1)) != PLUS ||
970 GET_CODE (val) != CONST_INT)
971 abort ();
973 offset = -INTVAL (val);
975 else
976 offset = GET_MODE_SIZE (GET_MODE (dest));
978 else
979 return 0;
981 if (code == PLUS || code == PRE_INC)
982 offset = -offset;
984 return offset;
987 /* Check INSN to see if it looks like a push or a stack adjustment, and
988 make a note of it if it does. EH uses this information to find out how
989 much extra space it needs to pop off the stack. */
991 static void
992 dwarf2out_stack_adjust (insn)
993 rtx insn;
995 HOST_WIDE_INT offset;
996 const char *label;
997 int i;
999 if (!flag_asynchronous_unwind_tables && GET_CODE (insn) == CALL_INSN)
1001 /* Extract the size of the args from the CALL rtx itself. */
1002 insn = PATTERN (insn);
1003 if (GET_CODE (insn) == PARALLEL)
1004 insn = XVECEXP (insn, 0, 0);
1005 if (GET_CODE (insn) == SET)
1006 insn = SET_SRC (insn);
1007 if (GET_CODE (insn) != CALL)
1008 abort ();
1010 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1011 return;
1014 /* If only calls can throw, and we have a frame pointer,
1015 save up adjustments until we see the CALL_INSN. */
1016 else if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1017 return;
1019 if (GET_CODE (insn) == BARRIER)
1021 /* When we see a BARRIER, we know to reset args_size to 0. Usually
1022 the compiler will have already emitted a stack adjustment, but
1023 doesn't bother for calls to noreturn functions. */
1024 #ifdef STACK_GROWS_DOWNWARD
1025 offset = -args_size;
1026 #else
1027 offset = args_size;
1028 #endif
1030 else if (GET_CODE (PATTERN (insn)) == SET)
1031 offset = stack_adjust_offset (PATTERN (insn));
1032 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1033 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1035 /* There may be stack adjustments inside compound insns. Search
1036 for them. */
1037 for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1038 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1039 offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1041 else
1042 return;
1044 if (offset == 0)
1045 return;
1047 if (cfa.reg == STACK_POINTER_REGNUM)
1048 cfa.offset += offset;
1050 #ifndef STACK_GROWS_DOWNWARD
1051 offset = -offset;
1052 #endif
1054 args_size += offset;
1055 if (args_size < 0)
1056 args_size = 0;
1058 label = dwarf2out_cfi_label ();
1059 def_cfa_1 (label, &cfa);
1060 dwarf2out_args_size (label, args_size);
1063 /* We delay emitting a register save until either (a) we reach the end
1064 of the prologue or (b) the register is clobbered. This clusters
1065 register saves so that there are fewer pc advances. */
1067 struct queued_reg_save
1069 struct queued_reg_save *next;
1070 rtx reg;
1071 long cfa_offset;
1074 static struct queued_reg_save *queued_reg_saves;
1075 static const char *last_reg_save_label;
1077 static void
1078 queue_reg_save (label, reg, offset)
1079 const char *label;
1080 rtx reg;
1081 long offset;
1083 struct queued_reg_save *q = (struct queued_reg_save *) xmalloc (sizeof (*q));
1085 q->next = queued_reg_saves;
1086 q->reg = reg;
1087 q->cfa_offset = offset;
1088 queued_reg_saves = q;
1090 last_reg_save_label = label;
1093 static void
1094 flush_queued_reg_saves ()
1096 struct queued_reg_save *q, *next;
1098 for (q = queued_reg_saves; q ; q = next)
1100 dwarf2out_reg_save (last_reg_save_label, REGNO (q->reg), q->cfa_offset);
1101 next = q->next;
1102 free (q);
1105 queued_reg_saves = NULL;
1106 last_reg_save_label = NULL;
1109 static bool
1110 clobbers_queued_reg_save (insn)
1111 rtx insn;
1113 struct queued_reg_save *q;
1115 for (q = queued_reg_saves; q ; q = q->next)
1116 if (modified_in_p (q->reg, insn))
1117 return true;
1119 return false;
1123 /* A temporary register holding an integral value used in adjusting SP
1124 or setting up the store_reg. The "offset" field holds the integer
1125 value, not an offset. */
1126 static dw_cfa_location cfa_temp;
1128 /* Record call frame debugging information for an expression EXPR,
1129 which either sets SP or FP (adjusting how we calculate the frame
1130 address) or saves a register to the stack. LABEL indicates the
1131 address of EXPR.
1133 This function encodes a state machine mapping rtxes to actions on
1134 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1135 users need not read the source code.
1137 The High-Level Picture
1139 Changes in the register we use to calculate the CFA: Currently we
1140 assume that if you copy the CFA register into another register, we
1141 should take the other one as the new CFA register; this seems to
1142 work pretty well. If it's wrong for some target, it's simple
1143 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1145 Changes in the register we use for saving registers to the stack:
1146 This is usually SP, but not always. Again, we deduce that if you
1147 copy SP into another register (and SP is not the CFA register),
1148 then the new register is the one we will be using for register
1149 saves. This also seems to work.
1151 Register saves: There's not much guesswork about this one; if
1152 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1153 register save, and the register used to calculate the destination
1154 had better be the one we think we're using for this purpose.
1156 Except: If the register being saved is the CFA register, and the
1157 offset is non-zero, we are saving the CFA, so we assume we have to
1158 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1159 the intent is to save the value of SP from the previous frame.
1161 Invariants / Summaries of Rules
1163 cfa current rule for calculating the CFA. It usually
1164 consists of a register and an offset.
1165 cfa_store register used by prologue code to save things to the stack
1166 cfa_store.offset is the offset from the value of
1167 cfa_store.reg to the actual CFA
1168 cfa_temp register holding an integral value. cfa_temp.offset
1169 stores the value, which will be used to adjust the
1170 stack pointer. cfa_temp is also used like cfa_store,
1171 to track stores to the stack via fp or a temp reg.
1173 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1174 with cfa.reg as the first operand changes the cfa.reg and its
1175 cfa.offset. Rule 1 and 4 also set cfa_temp.reg and
1176 cfa_temp.offset.
1178 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1179 expression yielding a constant. This sets cfa_temp.reg
1180 and cfa_temp.offset.
1182 Rule 5: Create a new register cfa_store used to save items to the
1183 stack.
1185 Rules 10-14: Save a register to the stack. Define offset as the
1186 difference of the original location and cfa_store's
1187 location (or cfa_temp's location if cfa_temp is used).
1189 The Rules
1191 "{a,b}" indicates a choice of a xor b.
1192 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1194 Rule 1:
1195 (set <reg1> <reg2>:cfa.reg)
1196 effects: cfa.reg = <reg1>
1197 cfa.offset unchanged
1198 cfa_temp.reg = <reg1>
1199 cfa_temp.offset = cfa.offset
1201 Rule 2:
1202 (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1203 {<const_int>,<reg>:cfa_temp.reg}))
1204 effects: cfa.reg = sp if fp used
1205 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1206 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1207 if cfa_store.reg==sp
1209 Rule 3:
1210 (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1211 effects: cfa.reg = fp
1212 cfa_offset += +/- <const_int>
1214 Rule 4:
1215 (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1216 constraints: <reg1> != fp
1217 <reg1> != sp
1218 effects: cfa.reg = <reg1>
1219 cfa_temp.reg = <reg1>
1220 cfa_temp.offset = cfa.offset
1222 Rule 5:
1223 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1224 constraints: <reg1> != fp
1225 <reg1> != sp
1226 effects: cfa_store.reg = <reg1>
1227 cfa_store.offset = cfa.offset - cfa_temp.offset
1229 Rule 6:
1230 (set <reg> <const_int>)
1231 effects: cfa_temp.reg = <reg>
1232 cfa_temp.offset = <const_int>
1234 Rule 7:
1235 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1236 effects: cfa_temp.reg = <reg1>
1237 cfa_temp.offset |= <const_int>
1239 Rule 8:
1240 (set <reg> (high <exp>))
1241 effects: none
1243 Rule 9:
1244 (set <reg> (lo_sum <exp> <const_int>))
1245 effects: cfa_temp.reg = <reg>
1246 cfa_temp.offset = <const_int>
1248 Rule 10:
1249 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1250 effects: cfa_store.offset -= <const_int>
1251 cfa.offset = cfa_store.offset if cfa.reg == sp
1252 cfa.reg = sp
1253 cfa.base_offset = -cfa_store.offset
1255 Rule 11:
1256 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1257 effects: cfa_store.offset += -/+ mode_size(mem)
1258 cfa.offset = cfa_store.offset if cfa.reg == sp
1259 cfa.reg = sp
1260 cfa.base_offset = -cfa_store.offset
1262 Rule 12:
1263 (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1265 <reg2>)
1266 effects: cfa.reg = <reg1>
1267 cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1269 Rule 13:
1270 (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1271 effects: cfa.reg = <reg1>
1272 cfa.base_offset = -{cfa_store,cfa_temp}.offset
1274 Rule 14:
1275 (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1276 effects: cfa.reg = <reg1>
1277 cfa.base_offset = -cfa_temp.offset
1278 cfa_temp.offset -= mode_size(mem) */
1280 static void
1281 dwarf2out_frame_debug_expr (expr, label)
1282 rtx expr;
1283 const char *label;
1285 rtx src, dest;
1286 HOST_WIDE_INT offset;
1288 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1289 the PARALLEL independently. The first element is always processed if
1290 it is a SET. This is for backward compatibility. Other elements
1291 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1292 flag is set in them. */
1293 if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1295 int par_index;
1296 int limit = XVECLEN (expr, 0);
1298 for (par_index = 0; par_index < limit; par_index++)
1299 if (GET_CODE (XVECEXP (expr, 0, par_index)) == SET
1300 && (RTX_FRAME_RELATED_P (XVECEXP (expr, 0, par_index))
1301 || par_index == 0))
1302 dwarf2out_frame_debug_expr (XVECEXP (expr, 0, par_index), label);
1304 return;
1307 if (GET_CODE (expr) != SET)
1308 abort ();
1310 src = SET_SRC (expr);
1311 dest = SET_DEST (expr);
1313 switch (GET_CODE (dest))
1315 case REG:
1316 /* Rule 1 */
1317 /* Update the CFA rule wrt SP or FP. Make sure src is
1318 relative to the current CFA register. */
1319 switch (GET_CODE (src))
1321 /* Setting FP from SP. */
1322 case REG:
1323 if (cfa.reg == (unsigned) REGNO (src))
1324 /* OK. */
1326 else
1327 abort ();
1329 /* We used to require that dest be either SP or FP, but the
1330 ARM copies SP to a temporary register, and from there to
1331 FP. So we just rely on the backends to only set
1332 RTX_FRAME_RELATED_P on appropriate insns. */
1333 cfa.reg = REGNO (dest);
1334 cfa_temp.reg = cfa.reg;
1335 cfa_temp.offset = cfa.offset;
1336 break;
1338 case PLUS:
1339 case MINUS:
1340 case LO_SUM:
1341 if (dest == stack_pointer_rtx)
1343 /* Rule 2 */
1344 /* Adjusting SP. */
1345 switch (GET_CODE (XEXP (src, 1)))
1347 case CONST_INT:
1348 offset = INTVAL (XEXP (src, 1));
1349 break;
1350 case REG:
1351 if ((unsigned) REGNO (XEXP (src, 1)) != cfa_temp.reg)
1352 abort ();
1353 offset = cfa_temp.offset;
1354 break;
1355 default:
1356 abort ();
1359 if (XEXP (src, 0) == hard_frame_pointer_rtx)
1361 /* Restoring SP from FP in the epilogue. */
1362 if (cfa.reg != (unsigned) HARD_FRAME_POINTER_REGNUM)
1363 abort ();
1364 cfa.reg = STACK_POINTER_REGNUM;
1366 else if (GET_CODE (src) == LO_SUM)
1367 /* Assume we've set the source reg of the LO_SUM from sp. */
1369 else if (XEXP (src, 0) != stack_pointer_rtx)
1370 abort ();
1372 if (GET_CODE (src) != MINUS)
1373 offset = -offset;
1374 if (cfa.reg == STACK_POINTER_REGNUM)
1375 cfa.offset += offset;
1376 if (cfa_store.reg == STACK_POINTER_REGNUM)
1377 cfa_store.offset += offset;
1379 else if (dest == hard_frame_pointer_rtx)
1381 /* Rule 3 */
1382 /* Either setting the FP from an offset of the SP,
1383 or adjusting the FP */
1384 if (! frame_pointer_needed)
1385 abort ();
1387 if (GET_CODE (XEXP (src, 0)) == REG
1388 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1389 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1391 offset = INTVAL (XEXP (src, 1));
1392 if (GET_CODE (src) != MINUS)
1393 offset = -offset;
1394 cfa.offset += offset;
1395 cfa.reg = HARD_FRAME_POINTER_REGNUM;
1397 else
1398 abort ();
1400 else
1402 if (GET_CODE (src) == MINUS)
1403 abort ();
1405 /* Rule 4 */
1406 if (GET_CODE (XEXP (src, 0)) == REG
1407 && REGNO (XEXP (src, 0)) == cfa.reg
1408 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1410 /* Setting a temporary CFA register that will be copied
1411 into the FP later on. */
1412 offset = - INTVAL (XEXP (src, 1));
1413 cfa.offset += offset;
1414 cfa.reg = REGNO (dest);
1415 /* Or used to save regs to the stack. */
1416 cfa_temp.reg = cfa.reg;
1417 cfa_temp.offset = cfa.offset;
1420 /* Rule 5 */
1421 else if (GET_CODE (XEXP (src, 0)) == REG
1422 && REGNO (XEXP (src, 0)) == cfa_temp.reg
1423 && XEXP (src, 1) == stack_pointer_rtx)
1425 /* Setting a scratch register that we will use instead
1426 of SP for saving registers to the stack. */
1427 if (cfa.reg != STACK_POINTER_REGNUM)
1428 abort ();
1429 cfa_store.reg = REGNO (dest);
1430 cfa_store.offset = cfa.offset - cfa_temp.offset;
1433 /* Rule 9 */
1434 else if (GET_CODE (src) == LO_SUM
1435 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1437 cfa_temp.reg = REGNO (dest);
1438 cfa_temp.offset = INTVAL (XEXP (src, 1));
1440 else
1441 abort ();
1443 break;
1445 /* Rule 6 */
1446 case CONST_INT:
1447 cfa_temp.reg = REGNO (dest);
1448 cfa_temp.offset = INTVAL (src);
1449 break;
1451 /* Rule 7 */
1452 case IOR:
1453 if (GET_CODE (XEXP (src, 0)) != REG
1454 || (unsigned) REGNO (XEXP (src, 0)) != cfa_temp.reg
1455 || GET_CODE (XEXP (src, 1)) != CONST_INT)
1456 abort ();
1458 if ((unsigned) REGNO (dest) != cfa_temp.reg)
1459 cfa_temp.reg = REGNO (dest);
1460 cfa_temp.offset |= INTVAL (XEXP (src, 1));
1461 break;
1463 /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1464 which will fill in all of the bits. */
1465 /* Rule 8 */
1466 case HIGH:
1467 break;
1469 default:
1470 abort ();
1473 def_cfa_1 (label, &cfa);
1474 break;
1476 case MEM:
1477 if (GET_CODE (src) != REG)
1478 abort ();
1480 /* Saving a register to the stack. Make sure dest is relative to the
1481 CFA register. */
1482 switch (GET_CODE (XEXP (dest, 0)))
1484 /* Rule 10 */
1485 /* With a push. */
1486 case PRE_MODIFY:
1487 /* We can't handle variable size modifications. */
1488 if (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1)) != CONST_INT)
1489 abort ();
1490 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1492 if (REGNO (XEXP (XEXP (dest, 0), 0)) != STACK_POINTER_REGNUM
1493 || cfa_store.reg != STACK_POINTER_REGNUM)
1494 abort ();
1496 cfa_store.offset += offset;
1497 if (cfa.reg == STACK_POINTER_REGNUM)
1498 cfa.offset = cfa_store.offset;
1500 offset = -cfa_store.offset;
1501 break;
1503 /* Rule 11 */
1504 case PRE_INC:
1505 case PRE_DEC:
1506 offset = GET_MODE_SIZE (GET_MODE (dest));
1507 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1508 offset = -offset;
1510 if (REGNO (XEXP (XEXP (dest, 0), 0)) != STACK_POINTER_REGNUM
1511 || cfa_store.reg != STACK_POINTER_REGNUM)
1512 abort ();
1514 cfa_store.offset += offset;
1515 if (cfa.reg == STACK_POINTER_REGNUM)
1516 cfa.offset = cfa_store.offset;
1518 offset = -cfa_store.offset;
1519 break;
1521 /* Rule 12 */
1522 /* With an offset. */
1523 case PLUS:
1524 case MINUS:
1525 case LO_SUM:
1526 if (GET_CODE (XEXP (XEXP (dest, 0), 1)) != CONST_INT)
1527 abort ();
1528 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1529 if (GET_CODE (XEXP (dest, 0)) == MINUS)
1530 offset = -offset;
1532 if (cfa_store.reg == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)))
1533 offset -= cfa_store.offset;
1534 else if (cfa_temp.reg == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)))
1535 offset -= cfa_temp.offset;
1536 else
1537 abort ();
1538 break;
1540 /* Rule 13 */
1541 /* Without an offset. */
1542 case REG:
1543 if (cfa_store.reg == (unsigned) REGNO (XEXP (dest, 0)))
1544 offset = -cfa_store.offset;
1545 else if (cfa_temp.reg == (unsigned) REGNO (XEXP (dest, 0)))
1546 offset = -cfa_temp.offset;
1547 else
1548 abort ();
1549 break;
1551 /* Rule 14 */
1552 case POST_INC:
1553 if (cfa_temp.reg != (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)))
1554 abort ();
1555 offset = -cfa_temp.offset;
1556 cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1557 break;
1559 default:
1560 abort ();
1563 if (REGNO (src) != STACK_POINTER_REGNUM
1564 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1565 && (unsigned) REGNO (src) == cfa.reg)
1567 /* We're storing the current CFA reg into the stack. */
1569 if (cfa.offset == 0)
1571 /* If the source register is exactly the CFA, assume
1572 we're saving SP like any other register; this happens
1573 on the ARM. */
1574 def_cfa_1 (label, &cfa);
1575 queue_reg_save (label, stack_pointer_rtx, offset);
1576 break;
1578 else
1580 /* Otherwise, we'll need to look in the stack to
1581 calculate the CFA. */
1582 rtx x = XEXP (dest, 0);
1584 if (GET_CODE (x) != REG)
1585 x = XEXP (x, 0);
1586 if (GET_CODE (x) != REG)
1587 abort ();
1589 cfa.reg = REGNO (x);
1590 cfa.base_offset = offset;
1591 cfa.indirect = 1;
1592 def_cfa_1 (label, &cfa);
1593 break;
1597 def_cfa_1 (label, &cfa);
1598 queue_reg_save (label, src, offset);
1599 break;
1601 default:
1602 abort ();
1606 /* Record call frame debugging information for INSN, which either
1607 sets SP or FP (adjusting how we calculate the frame address) or saves a
1608 register to the stack. If INSN is NULL_RTX, initialize our state. */
1610 void
1611 dwarf2out_frame_debug (insn)
1612 rtx insn;
1614 const char *label;
1615 rtx src;
1617 if (insn == NULL_RTX)
1619 /* Flush any queued register saves. */
1620 flush_queued_reg_saves ();
1622 /* Set up state for generating call frame debug info. */
1623 lookup_cfa (&cfa);
1624 if (cfa.reg != (unsigned long) DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM))
1625 abort ();
1627 cfa.reg = STACK_POINTER_REGNUM;
1628 cfa_store = cfa;
1629 cfa_temp.reg = -1;
1630 cfa_temp.offset = 0;
1631 return;
1634 if (GET_CODE (insn) != INSN || clobbers_queued_reg_save (insn))
1635 flush_queued_reg_saves ();
1637 if (! RTX_FRAME_RELATED_P (insn))
1639 if (!ACCUMULATE_OUTGOING_ARGS)
1640 dwarf2out_stack_adjust (insn);
1642 return;
1645 label = dwarf2out_cfi_label ();
1646 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1647 if (src)
1648 insn = XEXP (src, 0);
1649 else
1650 insn = PATTERN (insn);
1652 dwarf2out_frame_debug_expr (insn, label);
1655 /* Output a Call Frame Information opcode and its operand(s). */
1657 static void
1658 output_cfi (cfi, fde, for_eh)
1659 dw_cfi_ref cfi;
1660 dw_fde_ref fde;
1661 int for_eh;
1663 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
1664 dw2_asm_output_data (1, (cfi->dw_cfi_opc
1665 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
1666 "DW_CFA_advance_loc 0x%lx",
1667 cfi->dw_cfi_oprnd1.dw_cfi_offset);
1668 else if (cfi->dw_cfi_opc == DW_CFA_offset)
1670 dw2_asm_output_data (1, (cfi->dw_cfi_opc
1671 | (cfi->dw_cfi_oprnd1.dw_cfi_reg_num & 0x3f)),
1672 "DW_CFA_offset, column 0x%lx",
1673 cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
1674 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1676 else if (cfi->dw_cfi_opc == DW_CFA_restore)
1677 dw2_asm_output_data (1, (cfi->dw_cfi_opc
1678 | (cfi->dw_cfi_oprnd1.dw_cfi_reg_num & 0x3f)),
1679 "DW_CFA_restore, column 0x%lx",
1680 cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
1681 else
1683 dw2_asm_output_data (1, cfi->dw_cfi_opc,
1684 "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
1686 switch (cfi->dw_cfi_opc)
1688 case DW_CFA_set_loc:
1689 if (for_eh)
1690 dw2_asm_output_encoded_addr_rtx (
1691 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
1692 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
1693 NULL);
1694 else
1695 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
1696 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
1697 break;
1699 case DW_CFA_advance_loc1:
1700 dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1701 fde->dw_fde_current_label, NULL);
1702 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1703 break;
1705 case DW_CFA_advance_loc2:
1706 dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1707 fde->dw_fde_current_label, NULL);
1708 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1709 break;
1711 case DW_CFA_advance_loc4:
1712 dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1713 fde->dw_fde_current_label, NULL);
1714 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1715 break;
1717 case DW_CFA_MIPS_advance_loc8:
1718 dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1719 fde->dw_fde_current_label, NULL);
1720 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1721 break;
1723 case DW_CFA_offset_extended:
1724 case DW_CFA_def_cfa:
1725 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_reg_num,
1726 NULL);
1727 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1728 break;
1730 case DW_CFA_offset_extended_sf:
1731 case DW_CFA_def_cfa_sf:
1732 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_reg_num,
1733 NULL);
1734 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1735 break;
1737 case DW_CFA_restore_extended:
1738 case DW_CFA_undefined:
1739 case DW_CFA_same_value:
1740 case DW_CFA_def_cfa_register:
1741 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_reg_num,
1742 NULL);
1743 break;
1745 case DW_CFA_register:
1746 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_reg_num,
1747 NULL);
1748 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_reg_num,
1749 NULL);
1750 break;
1752 case DW_CFA_def_cfa_offset:
1753 case DW_CFA_GNU_args_size:
1754 dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
1755 break;
1757 case DW_CFA_def_cfa_offset_sf:
1758 dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
1759 break;
1761 case DW_CFA_GNU_window_save:
1762 break;
1764 case DW_CFA_def_cfa_expression:
1765 case DW_CFA_expression:
1766 output_cfa_loc (cfi);
1767 break;
1769 case DW_CFA_GNU_negative_offset_extended:
1770 /* Obsoleted by DW_CFA_offset_extended_sf. */
1771 abort ();
1773 default:
1774 break;
1779 /* Output the call frame information used to used to record information
1780 that relates to calculating the frame pointer, and records the
1781 location of saved registers. */
1783 static void
1784 output_call_frame_info (for_eh)
1785 int for_eh;
1787 unsigned int i;
1788 dw_fde_ref fde;
1789 dw_cfi_ref cfi;
1790 char l1[20], l2[20], section_start_label[20];
1791 int any_lsda_needed = 0;
1792 char augmentation[6];
1793 int augmentation_size;
1794 int fde_encoding = DW_EH_PE_absptr;
1795 int per_encoding = DW_EH_PE_absptr;
1796 int lsda_encoding = DW_EH_PE_absptr;
1798 /* If we don't have any functions we'll want to unwind out of, don't emit any
1799 EH unwind information. */
1800 if (for_eh)
1802 int any_eh_needed = flag_asynchronous_unwind_tables;
1804 for (i = 0; i < fde_table_in_use; i++)
1805 if (fde_table[i].uses_eh_lsda)
1806 any_eh_needed = any_lsda_needed = 1;
1807 else if (! fde_table[i].nothrow)
1808 any_eh_needed = 1;
1810 if (! any_eh_needed)
1811 return;
1814 /* We're going to be generating comments, so turn on app. */
1815 if (flag_debug_asm)
1816 app_enable ();
1818 if (for_eh)
1819 (*targetm.asm_out.eh_frame_section) ();
1820 else
1821 named_section_flags (DEBUG_FRAME_SECTION, SECTION_DEBUG);
1823 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
1824 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
1826 /* Output the CIE. */
1827 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
1828 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
1829 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
1830 "Length of Common Information Entry");
1831 ASM_OUTPUT_LABEL (asm_out_file, l1);
1833 /* Now that the CIE pointer is PC-relative for EH,
1834 use 0 to identify the CIE. */
1835 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
1836 (for_eh ? 0 : DW_CIE_ID),
1837 "CIE Identifier Tag");
1839 dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
1841 augmentation[0] = 0;
1842 augmentation_size = 0;
1843 if (for_eh)
1845 char *p;
1847 /* Augmentation:
1848 z Indicates that a uleb128 is present to size the
1849 augmentation section.
1850 L Indicates the encoding (and thus presence) of
1851 an LSDA pointer in the FDE augmentation.
1852 R Indicates a non-default pointer encoding for
1853 FDE code pointers.
1854 P Indicates the presence of an encoding + language
1855 personality routine in the CIE augmentation. */
1857 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
1858 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
1859 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1861 p = augmentation + 1;
1862 if (eh_personality_libfunc)
1864 *p++ = 'P';
1865 augmentation_size += 1 + size_of_encoded_value (per_encoding);
1867 if (any_lsda_needed)
1869 *p++ = 'L';
1870 augmentation_size += 1;
1872 if (fde_encoding != DW_EH_PE_absptr)
1874 *p++ = 'R';
1875 augmentation_size += 1;
1877 if (p > augmentation + 1)
1879 augmentation[0] = 'z';
1880 *p = '\0';
1883 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
1884 if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
1886 int offset = ( 4 /* Length */
1887 + 4 /* CIE Id */
1888 + 1 /* CIE version */
1889 + strlen (augmentation) + 1 /* Augmentation */
1890 + size_of_uleb128 (1) /* Code alignment */
1891 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
1892 + 1 /* RA column */
1893 + 1 /* Augmentation size */
1894 + 1 /* Personality encoding */ );
1895 int pad = -offset & (PTR_SIZE - 1);
1897 augmentation_size += pad;
1899 /* Augmentations should be small, so there's scarce need to
1900 iterate for a solution. Die if we exceed one uleb128 byte. */
1901 if (size_of_uleb128 (augmentation_size) != 1)
1902 abort ();
1906 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
1907 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
1908 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
1909 "CIE Data Alignment Factor");
1910 dw2_asm_output_data (1, DWARF_FRAME_RETURN_COLUMN, "CIE RA Column");
1912 if (augmentation[0])
1914 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
1915 if (eh_personality_libfunc)
1917 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
1918 eh_data_format_name (per_encoding));
1919 dw2_asm_output_encoded_addr_rtx (per_encoding,
1920 eh_personality_libfunc, NULL);
1923 if (any_lsda_needed)
1924 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
1925 eh_data_format_name (lsda_encoding));
1927 if (fde_encoding != DW_EH_PE_absptr)
1928 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
1929 eh_data_format_name (fde_encoding));
1932 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
1933 output_cfi (cfi, NULL, for_eh);
1935 /* Pad the CIE out to an address sized boundary. */
1936 ASM_OUTPUT_ALIGN (asm_out_file,
1937 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
1938 ASM_OUTPUT_LABEL (asm_out_file, l2);
1940 /* Loop through all of the FDE's. */
1941 for (i = 0; i < fde_table_in_use; i++)
1943 fde = &fde_table[i];
1945 /* Don't emit EH unwind info for leaf functions that don't need it. */
1946 if (for_eh && fde->nothrow && ! fde->uses_eh_lsda)
1947 continue;
1949 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, FDE_LABEL, for_eh + i * 2);
1950 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
1951 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
1952 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
1953 "FDE Length");
1954 ASM_OUTPUT_LABEL (asm_out_file, l1);
1956 if (for_eh)
1957 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
1958 else
1959 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
1960 "FDE CIE offset");
1962 if (for_eh)
1964 dw2_asm_output_encoded_addr_rtx (fde_encoding,
1965 gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin),
1966 "FDE initial location");
1967 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
1968 fde->dw_fde_end, fde->dw_fde_begin,
1969 "FDE address range");
1971 else
1973 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
1974 "FDE initial location");
1975 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
1976 fde->dw_fde_end, fde->dw_fde_begin,
1977 "FDE address range");
1980 if (augmentation[0])
1982 if (any_lsda_needed)
1984 int size = size_of_encoded_value (lsda_encoding);
1986 if (lsda_encoding == DW_EH_PE_aligned)
1988 int offset = ( 4 /* Length */
1989 + 4 /* CIE offset */
1990 + 2 * size_of_encoded_value (fde_encoding)
1991 + 1 /* Augmentation size */ );
1992 int pad = -offset & (PTR_SIZE - 1);
1994 size += pad;
1995 if (size_of_uleb128 (size) != 1)
1996 abort ();
1999 dw2_asm_output_data_uleb128 (size, "Augmentation size");
2001 if (fde->uses_eh_lsda)
2003 ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2004 fde->funcdef_number);
2005 dw2_asm_output_encoded_addr_rtx (
2006 lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2007 "Language Specific Data Area");
2009 else
2011 if (lsda_encoding == DW_EH_PE_aligned)
2012 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2013 dw2_asm_output_data
2014 (size_of_encoded_value (lsda_encoding), 0,
2015 "Language Specific Data Area (none)");
2018 else
2019 dw2_asm_output_data_uleb128 (0, "Augmentation size");
2022 /* Loop through the Call Frame Instructions associated with
2023 this FDE. */
2024 fde->dw_fde_current_label = fde->dw_fde_begin;
2025 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2026 output_cfi (cfi, fde, for_eh);
2028 /* Pad the FDE out to an address sized boundary. */
2029 ASM_OUTPUT_ALIGN (asm_out_file,
2030 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2031 ASM_OUTPUT_LABEL (asm_out_file, l2);
2034 #ifndef EH_FRAME_SECTION_NAME
2035 if (for_eh)
2036 dw2_asm_output_data (4, 0, "End of Table");
2037 #endif
2038 #ifdef MIPS_DEBUGGING_INFO
2039 /* Work around Irix 6 assembler bug whereby labels at the end of a section
2040 get a value of 0. Putting .align 0 after the label fixes it. */
2041 ASM_OUTPUT_ALIGN (asm_out_file, 0);
2042 #endif
2044 /* Turn off app to make assembly quicker. */
2045 if (flag_debug_asm)
2046 app_disable ();
2049 /* Output a marker (i.e. a label) for the beginning of a function, before
2050 the prologue. */
2052 void
2053 dwarf2out_begin_prologue (line, file)
2054 unsigned int line ATTRIBUTE_UNUSED;
2055 const char *file ATTRIBUTE_UNUSED;
2057 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2058 dw_fde_ref fde;
2060 current_function_func_begin_label = 0;
2062 #ifdef IA64_UNWIND_INFO
2063 /* ??? current_function_func_begin_label is also used by except.c
2064 for call-site information. We must emit this label if it might
2065 be used. */
2066 if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2067 && ! dwarf2out_do_frame ())
2068 return;
2069 #else
2070 if (! dwarf2out_do_frame ())
2071 return;
2072 #endif
2074 current_funcdef_number++;
2075 function_section (current_function_decl);
2076 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2077 current_funcdef_number);
2078 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2079 current_funcdef_number);
2080 current_function_func_begin_label = get_identifier (label);
2082 #ifdef IA64_UNWIND_INFO
2083 /* We can elide the fde allocation if we're not emitting debug info. */
2084 if (! dwarf2out_do_frame ())
2085 return;
2086 #endif
2088 /* Expand the fde table if necessary. */
2089 if (fde_table_in_use == fde_table_allocated)
2091 fde_table_allocated += FDE_TABLE_INCREMENT;
2092 fde_table
2093 = (dw_fde_ref) xrealloc (fde_table,
2094 fde_table_allocated * sizeof (dw_fde_node));
2097 /* Record the FDE associated with this function. */
2098 current_funcdef_fde = fde_table_in_use;
2100 /* Add the new FDE at the end of the fde_table. */
2101 fde = &fde_table[fde_table_in_use++];
2102 fde->dw_fde_begin = xstrdup (label);
2103 fde->dw_fde_current_label = NULL;
2104 fde->dw_fde_end = NULL;
2105 fde->dw_fde_cfi = NULL;
2106 fde->funcdef_number = current_funcdef_number;
2107 fde->nothrow = current_function_nothrow;
2108 fde->uses_eh_lsda = cfun->uses_eh_lsda;
2110 args_size = old_args_size = 0;
2112 /* We only want to output line number information for the genuine dwarf2
2113 prologue case, not the eh frame case. */
2114 #ifdef DWARF2_DEBUGGING_INFO
2115 if (file)
2116 dwarf2out_source_line (line, file);
2117 #endif
2120 /* Output a marker (i.e. a label) for the absolute end of the generated code
2121 for a function definition. This gets called *after* the epilogue code has
2122 been generated. */
2124 void
2125 dwarf2out_end_epilogue ()
2127 dw_fde_ref fde;
2128 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2130 /* Output a label to mark the endpoint of the code generated for this
2131 function. */
2132 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL, current_funcdef_number);
2133 ASM_OUTPUT_LABEL (asm_out_file, label);
2134 fde = &fde_table[fde_table_in_use - 1];
2135 fde->dw_fde_end = xstrdup (label);
2138 void
2139 dwarf2out_frame_init ()
2141 /* Allocate the initial hunk of the fde_table. */
2142 fde_table = (dw_fde_ref) xcalloc (FDE_TABLE_INCREMENT, sizeof (dw_fde_node));
2143 fde_table_allocated = FDE_TABLE_INCREMENT;
2144 fde_table_in_use = 0;
2146 /* Generate the CFA instructions common to all FDE's. Do it now for the
2147 sake of lookup_cfa. */
2149 #ifdef DWARF2_UNWIND_INFO
2150 /* On entry, the Canonical Frame Address is at SP. */
2151 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2152 initial_return_save (INCOMING_RETURN_ADDR_RTX);
2153 #endif
2156 void
2157 dwarf2out_frame_finish ()
2159 /* Output call frame information. */
2160 if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
2161 output_call_frame_info (0);
2163 if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2164 output_call_frame_info (1);
2167 /* And now, the subset of the debugging information support code necessary
2168 for emitting location expressions. */
2170 typedef struct dw_val_struct *dw_val_ref;
2171 typedef struct die_struct *dw_die_ref;
2172 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2173 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2175 /* Each DIE may have a series of attribute/value pairs. Values
2176 can take on several forms. The forms that are used in this
2177 implementation are listed below. */
2179 typedef enum
2181 dw_val_class_addr,
2182 dw_val_class_offset,
2183 dw_val_class_loc,
2184 dw_val_class_loc_list,
2185 dw_val_class_range_list,
2186 dw_val_class_const,
2187 dw_val_class_unsigned_const,
2188 dw_val_class_long_long,
2189 dw_val_class_float,
2190 dw_val_class_flag,
2191 dw_val_class_die_ref,
2192 dw_val_class_fde_ref,
2193 dw_val_class_lbl_id,
2194 dw_val_class_lbl_offset,
2195 dw_val_class_str
2197 dw_val_class;
2199 /* Describe a double word constant value. */
2200 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
2202 typedef struct dw_long_long_struct
2204 unsigned long hi;
2205 unsigned long low;
2207 dw_long_long_const;
2209 /* Describe a floating point constant value. */
2211 typedef struct dw_fp_struct
2213 long *array;
2214 unsigned length;
2216 dw_float_const;
2218 /* The dw_val_node describes an attribute's value, as it is
2219 represented internally. */
2221 typedef struct dw_val_struct
2223 dw_val_class val_class;
2224 union
2226 rtx val_addr;
2227 long unsigned val_offset;
2228 dw_loc_list_ref val_loc_list;
2229 dw_loc_descr_ref val_loc;
2230 long int val_int;
2231 long unsigned val_unsigned;
2232 dw_long_long_const val_long_long;
2233 dw_float_const val_float;
2234 struct
2236 dw_die_ref die;
2237 int external;
2238 } val_die_ref;
2239 unsigned val_fde_index;
2240 struct indirect_string_node *val_str;
2241 char *val_lbl_id;
2242 unsigned char val_flag;
2246 dw_val_node;
2248 /* Locations in memory are described using a sequence of stack machine
2249 operations. */
2251 typedef struct dw_loc_descr_struct
2253 dw_loc_descr_ref dw_loc_next;
2254 enum dwarf_location_atom dw_loc_opc;
2255 dw_val_node dw_loc_oprnd1;
2256 dw_val_node dw_loc_oprnd2;
2257 int dw_loc_addr;
2259 dw_loc_descr_node;
2261 /* Location lists are ranges + location descriptions for that range,
2262 so you can track variables that are in different places over
2263 their entire life. */
2264 typedef struct dw_loc_list_struct
2266 dw_loc_list_ref dw_loc_next;
2267 const char *begin; /* Label for begin address of range */
2268 const char *end; /* Label for end address of range */
2269 char *ll_symbol; /* Label for beginning of location list.
2270 Only on head of list */
2271 const char *section; /* Section this loclist is relative to */
2272 dw_loc_descr_ref expr;
2273 } dw_loc_list_node;
2275 static const char *dwarf_stack_op_name PARAMS ((unsigned));
2276 static dw_loc_descr_ref new_loc_descr PARAMS ((enum dwarf_location_atom,
2277 unsigned long,
2278 unsigned long));
2279 static void add_loc_descr PARAMS ((dw_loc_descr_ref *,
2280 dw_loc_descr_ref));
2281 static unsigned long size_of_loc_descr PARAMS ((dw_loc_descr_ref));
2282 static unsigned long size_of_locs PARAMS ((dw_loc_descr_ref));
2283 static void output_loc_operands PARAMS ((dw_loc_descr_ref));
2284 static void output_loc_sequence PARAMS ((dw_loc_descr_ref));
2286 /* Convert a DWARF stack opcode into its string name. */
2288 static const char *
2289 dwarf_stack_op_name (op)
2290 unsigned op;
2292 switch (op)
2294 case DW_OP_addr:
2295 return "DW_OP_addr";
2296 case DW_OP_deref:
2297 return "DW_OP_deref";
2298 case DW_OP_const1u:
2299 return "DW_OP_const1u";
2300 case DW_OP_const1s:
2301 return "DW_OP_const1s";
2302 case DW_OP_const2u:
2303 return "DW_OP_const2u";
2304 case DW_OP_const2s:
2305 return "DW_OP_const2s";
2306 case DW_OP_const4u:
2307 return "DW_OP_const4u";
2308 case DW_OP_const4s:
2309 return "DW_OP_const4s";
2310 case DW_OP_const8u:
2311 return "DW_OP_const8u";
2312 case DW_OP_const8s:
2313 return "DW_OP_const8s";
2314 case DW_OP_constu:
2315 return "DW_OP_constu";
2316 case DW_OP_consts:
2317 return "DW_OP_consts";
2318 case DW_OP_dup:
2319 return "DW_OP_dup";
2320 case DW_OP_drop:
2321 return "DW_OP_drop";
2322 case DW_OP_over:
2323 return "DW_OP_over";
2324 case DW_OP_pick:
2325 return "DW_OP_pick";
2326 case DW_OP_swap:
2327 return "DW_OP_swap";
2328 case DW_OP_rot:
2329 return "DW_OP_rot";
2330 case DW_OP_xderef:
2331 return "DW_OP_xderef";
2332 case DW_OP_abs:
2333 return "DW_OP_abs";
2334 case DW_OP_and:
2335 return "DW_OP_and";
2336 case DW_OP_div:
2337 return "DW_OP_div";
2338 case DW_OP_minus:
2339 return "DW_OP_minus";
2340 case DW_OP_mod:
2341 return "DW_OP_mod";
2342 case DW_OP_mul:
2343 return "DW_OP_mul";
2344 case DW_OP_neg:
2345 return "DW_OP_neg";
2346 case DW_OP_not:
2347 return "DW_OP_not";
2348 case DW_OP_or:
2349 return "DW_OP_or";
2350 case DW_OP_plus:
2351 return "DW_OP_plus";
2352 case DW_OP_plus_uconst:
2353 return "DW_OP_plus_uconst";
2354 case DW_OP_shl:
2355 return "DW_OP_shl";
2356 case DW_OP_shr:
2357 return "DW_OP_shr";
2358 case DW_OP_shra:
2359 return "DW_OP_shra";
2360 case DW_OP_xor:
2361 return "DW_OP_xor";
2362 case DW_OP_bra:
2363 return "DW_OP_bra";
2364 case DW_OP_eq:
2365 return "DW_OP_eq";
2366 case DW_OP_ge:
2367 return "DW_OP_ge";
2368 case DW_OP_gt:
2369 return "DW_OP_gt";
2370 case DW_OP_le:
2371 return "DW_OP_le";
2372 case DW_OP_lt:
2373 return "DW_OP_lt";
2374 case DW_OP_ne:
2375 return "DW_OP_ne";
2376 case DW_OP_skip:
2377 return "DW_OP_skip";
2378 case DW_OP_lit0:
2379 return "DW_OP_lit0";
2380 case DW_OP_lit1:
2381 return "DW_OP_lit1";
2382 case DW_OP_lit2:
2383 return "DW_OP_lit2";
2384 case DW_OP_lit3:
2385 return "DW_OP_lit3";
2386 case DW_OP_lit4:
2387 return "DW_OP_lit4";
2388 case DW_OP_lit5:
2389 return "DW_OP_lit5";
2390 case DW_OP_lit6:
2391 return "DW_OP_lit6";
2392 case DW_OP_lit7:
2393 return "DW_OP_lit7";
2394 case DW_OP_lit8:
2395 return "DW_OP_lit8";
2396 case DW_OP_lit9:
2397 return "DW_OP_lit9";
2398 case DW_OP_lit10:
2399 return "DW_OP_lit10";
2400 case DW_OP_lit11:
2401 return "DW_OP_lit11";
2402 case DW_OP_lit12:
2403 return "DW_OP_lit12";
2404 case DW_OP_lit13:
2405 return "DW_OP_lit13";
2406 case DW_OP_lit14:
2407 return "DW_OP_lit14";
2408 case DW_OP_lit15:
2409 return "DW_OP_lit15";
2410 case DW_OP_lit16:
2411 return "DW_OP_lit16";
2412 case DW_OP_lit17:
2413 return "DW_OP_lit17";
2414 case DW_OP_lit18:
2415 return "DW_OP_lit18";
2416 case DW_OP_lit19:
2417 return "DW_OP_lit19";
2418 case DW_OP_lit20:
2419 return "DW_OP_lit20";
2420 case DW_OP_lit21:
2421 return "DW_OP_lit21";
2422 case DW_OP_lit22:
2423 return "DW_OP_lit22";
2424 case DW_OP_lit23:
2425 return "DW_OP_lit23";
2426 case DW_OP_lit24:
2427 return "DW_OP_lit24";
2428 case DW_OP_lit25:
2429 return "DW_OP_lit25";
2430 case DW_OP_lit26:
2431 return "DW_OP_lit26";
2432 case DW_OP_lit27:
2433 return "DW_OP_lit27";
2434 case DW_OP_lit28:
2435 return "DW_OP_lit28";
2436 case DW_OP_lit29:
2437 return "DW_OP_lit29";
2438 case DW_OP_lit30:
2439 return "DW_OP_lit30";
2440 case DW_OP_lit31:
2441 return "DW_OP_lit31";
2442 case DW_OP_reg0:
2443 return "DW_OP_reg0";
2444 case DW_OP_reg1:
2445 return "DW_OP_reg1";
2446 case DW_OP_reg2:
2447 return "DW_OP_reg2";
2448 case DW_OP_reg3:
2449 return "DW_OP_reg3";
2450 case DW_OP_reg4:
2451 return "DW_OP_reg4";
2452 case DW_OP_reg5:
2453 return "DW_OP_reg5";
2454 case DW_OP_reg6:
2455 return "DW_OP_reg6";
2456 case DW_OP_reg7:
2457 return "DW_OP_reg7";
2458 case DW_OP_reg8:
2459 return "DW_OP_reg8";
2460 case DW_OP_reg9:
2461 return "DW_OP_reg9";
2462 case DW_OP_reg10:
2463 return "DW_OP_reg10";
2464 case DW_OP_reg11:
2465 return "DW_OP_reg11";
2466 case DW_OP_reg12:
2467 return "DW_OP_reg12";
2468 case DW_OP_reg13:
2469 return "DW_OP_reg13";
2470 case DW_OP_reg14:
2471 return "DW_OP_reg14";
2472 case DW_OP_reg15:
2473 return "DW_OP_reg15";
2474 case DW_OP_reg16:
2475 return "DW_OP_reg16";
2476 case DW_OP_reg17:
2477 return "DW_OP_reg17";
2478 case DW_OP_reg18:
2479 return "DW_OP_reg18";
2480 case DW_OP_reg19:
2481 return "DW_OP_reg19";
2482 case DW_OP_reg20:
2483 return "DW_OP_reg20";
2484 case DW_OP_reg21:
2485 return "DW_OP_reg21";
2486 case DW_OP_reg22:
2487 return "DW_OP_reg22";
2488 case DW_OP_reg23:
2489 return "DW_OP_reg23";
2490 case DW_OP_reg24:
2491 return "DW_OP_reg24";
2492 case DW_OP_reg25:
2493 return "DW_OP_reg25";
2494 case DW_OP_reg26:
2495 return "DW_OP_reg26";
2496 case DW_OP_reg27:
2497 return "DW_OP_reg27";
2498 case DW_OP_reg28:
2499 return "DW_OP_reg28";
2500 case DW_OP_reg29:
2501 return "DW_OP_reg29";
2502 case DW_OP_reg30:
2503 return "DW_OP_reg30";
2504 case DW_OP_reg31:
2505 return "DW_OP_reg31";
2506 case DW_OP_breg0:
2507 return "DW_OP_breg0";
2508 case DW_OP_breg1:
2509 return "DW_OP_breg1";
2510 case DW_OP_breg2:
2511 return "DW_OP_breg2";
2512 case DW_OP_breg3:
2513 return "DW_OP_breg3";
2514 case DW_OP_breg4:
2515 return "DW_OP_breg4";
2516 case DW_OP_breg5:
2517 return "DW_OP_breg5";
2518 case DW_OP_breg6:
2519 return "DW_OP_breg6";
2520 case DW_OP_breg7:
2521 return "DW_OP_breg7";
2522 case DW_OP_breg8:
2523 return "DW_OP_breg8";
2524 case DW_OP_breg9:
2525 return "DW_OP_breg9";
2526 case DW_OP_breg10:
2527 return "DW_OP_breg10";
2528 case DW_OP_breg11:
2529 return "DW_OP_breg11";
2530 case DW_OP_breg12:
2531 return "DW_OP_breg12";
2532 case DW_OP_breg13:
2533 return "DW_OP_breg13";
2534 case DW_OP_breg14:
2535 return "DW_OP_breg14";
2536 case DW_OP_breg15:
2537 return "DW_OP_breg15";
2538 case DW_OP_breg16:
2539 return "DW_OP_breg16";
2540 case DW_OP_breg17:
2541 return "DW_OP_breg17";
2542 case DW_OP_breg18:
2543 return "DW_OP_breg18";
2544 case DW_OP_breg19:
2545 return "DW_OP_breg19";
2546 case DW_OP_breg20:
2547 return "DW_OP_breg20";
2548 case DW_OP_breg21:
2549 return "DW_OP_breg21";
2550 case DW_OP_breg22:
2551 return "DW_OP_breg22";
2552 case DW_OP_breg23:
2553 return "DW_OP_breg23";
2554 case DW_OP_breg24:
2555 return "DW_OP_breg24";
2556 case DW_OP_breg25:
2557 return "DW_OP_breg25";
2558 case DW_OP_breg26:
2559 return "DW_OP_breg26";
2560 case DW_OP_breg27:
2561 return "DW_OP_breg27";
2562 case DW_OP_breg28:
2563 return "DW_OP_breg28";
2564 case DW_OP_breg29:
2565 return "DW_OP_breg29";
2566 case DW_OP_breg30:
2567 return "DW_OP_breg30";
2568 case DW_OP_breg31:
2569 return "DW_OP_breg31";
2570 case DW_OP_regx:
2571 return "DW_OP_regx";
2572 case DW_OP_fbreg:
2573 return "DW_OP_fbreg";
2574 case DW_OP_bregx:
2575 return "DW_OP_bregx";
2576 case DW_OP_piece:
2577 return "DW_OP_piece";
2578 case DW_OP_deref_size:
2579 return "DW_OP_deref_size";
2580 case DW_OP_xderef_size:
2581 return "DW_OP_xderef_size";
2582 case DW_OP_nop:
2583 return "DW_OP_nop";
2584 default:
2585 return "OP_<unknown>";
2589 /* Return a pointer to a newly allocated location description. Location
2590 descriptions are simple expression terms that can be strung
2591 together to form more complicated location (address) descriptions. */
2593 static inline dw_loc_descr_ref
2594 new_loc_descr (op, oprnd1, oprnd2)
2595 enum dwarf_location_atom op;
2596 unsigned long oprnd1;
2597 unsigned long oprnd2;
2599 /* Use xcalloc here so we clear out all of the long_long constant in
2600 the union. */
2601 dw_loc_descr_ref descr
2602 = (dw_loc_descr_ref) xcalloc (1, sizeof (dw_loc_descr_node));
2604 descr->dw_loc_opc = op;
2605 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
2606 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
2607 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
2608 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
2610 return descr;
2614 /* Add a location description term to a location description expression. */
2616 static inline void
2617 add_loc_descr (list_head, descr)
2618 dw_loc_descr_ref *list_head;
2619 dw_loc_descr_ref descr;
2621 dw_loc_descr_ref *d;
2623 /* Find the end of the chain. */
2624 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
2627 *d = descr;
2630 /* Return the size of a location descriptor. */
2632 static unsigned long
2633 size_of_loc_descr (loc)
2634 dw_loc_descr_ref loc;
2636 unsigned long size = 1;
2638 switch (loc->dw_loc_opc)
2640 case DW_OP_addr:
2641 size += DWARF2_ADDR_SIZE;
2642 break;
2643 case DW_OP_const1u:
2644 case DW_OP_const1s:
2645 size += 1;
2646 break;
2647 case DW_OP_const2u:
2648 case DW_OP_const2s:
2649 size += 2;
2650 break;
2651 case DW_OP_const4u:
2652 case DW_OP_const4s:
2653 size += 4;
2654 break;
2655 case DW_OP_const8u:
2656 case DW_OP_const8s:
2657 size += 8;
2658 break;
2659 case DW_OP_constu:
2660 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2661 break;
2662 case DW_OP_consts:
2663 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2664 break;
2665 case DW_OP_pick:
2666 size += 1;
2667 break;
2668 case DW_OP_plus_uconst:
2669 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2670 break;
2671 case DW_OP_skip:
2672 case DW_OP_bra:
2673 size += 2;
2674 break;
2675 case DW_OP_breg0:
2676 case DW_OP_breg1:
2677 case DW_OP_breg2:
2678 case DW_OP_breg3:
2679 case DW_OP_breg4:
2680 case DW_OP_breg5:
2681 case DW_OP_breg6:
2682 case DW_OP_breg7:
2683 case DW_OP_breg8:
2684 case DW_OP_breg9:
2685 case DW_OP_breg10:
2686 case DW_OP_breg11:
2687 case DW_OP_breg12:
2688 case DW_OP_breg13:
2689 case DW_OP_breg14:
2690 case DW_OP_breg15:
2691 case DW_OP_breg16:
2692 case DW_OP_breg17:
2693 case DW_OP_breg18:
2694 case DW_OP_breg19:
2695 case DW_OP_breg20:
2696 case DW_OP_breg21:
2697 case DW_OP_breg22:
2698 case DW_OP_breg23:
2699 case DW_OP_breg24:
2700 case DW_OP_breg25:
2701 case DW_OP_breg26:
2702 case DW_OP_breg27:
2703 case DW_OP_breg28:
2704 case DW_OP_breg29:
2705 case DW_OP_breg30:
2706 case DW_OP_breg31:
2707 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2708 break;
2709 case DW_OP_regx:
2710 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2711 break;
2712 case DW_OP_fbreg:
2713 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2714 break;
2715 case DW_OP_bregx:
2716 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2717 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
2718 break;
2719 case DW_OP_piece:
2720 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2721 break;
2722 case DW_OP_deref_size:
2723 case DW_OP_xderef_size:
2724 size += 1;
2725 break;
2726 default:
2727 break;
2730 return size;
2733 /* Return the size of a series of location descriptors. */
2735 static unsigned long
2736 size_of_locs (loc)
2737 dw_loc_descr_ref loc;
2739 unsigned long size;
2741 for (size = 0; loc != NULL; loc = loc->dw_loc_next)
2743 loc->dw_loc_addr = size;
2744 size += size_of_loc_descr (loc);
2747 return size;
2750 /* Output location description stack opcode's operands (if any). */
2752 static void
2753 output_loc_operands (loc)
2754 dw_loc_descr_ref loc;
2756 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2757 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2759 switch (loc->dw_loc_opc)
2761 #ifdef DWARF2_DEBUGGING_INFO
2762 case DW_OP_addr:
2763 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2764 break;
2765 case DW_OP_const2u:
2766 case DW_OP_const2s:
2767 dw2_asm_output_data (2, val1->v.val_int, NULL);
2768 break;
2769 case DW_OP_const4u:
2770 case DW_OP_const4s:
2771 dw2_asm_output_data (4, val1->v.val_int, NULL);
2772 break;
2773 case DW_OP_const8u:
2774 case DW_OP_const8s:
2775 if (HOST_BITS_PER_LONG < 64)
2776 abort ();
2777 dw2_asm_output_data (8, val1->v.val_int, NULL);
2778 break;
2779 case DW_OP_skip:
2780 case DW_OP_bra:
2782 int offset;
2784 if (val1->val_class == dw_val_class_loc)
2785 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2786 else
2787 abort ();
2789 dw2_asm_output_data (2, offset, NULL);
2791 break;
2792 #else
2793 case DW_OP_addr:
2794 case DW_OP_const2u:
2795 case DW_OP_const2s:
2796 case DW_OP_const4u:
2797 case DW_OP_const4s:
2798 case DW_OP_const8u:
2799 case DW_OP_const8s:
2800 case DW_OP_skip:
2801 case DW_OP_bra:
2802 /* We currently don't make any attempt to make sure these are
2803 aligned properly like we do for the main unwind info, so
2804 don't support emitting things larger than a byte if we're
2805 only doing unwinding. */
2806 abort ();
2807 #endif
2808 case DW_OP_const1u:
2809 case DW_OP_const1s:
2810 dw2_asm_output_data (1, val1->v.val_int, NULL);
2811 break;
2812 case DW_OP_constu:
2813 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2814 break;
2815 case DW_OP_consts:
2816 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2817 break;
2818 case DW_OP_pick:
2819 dw2_asm_output_data (1, val1->v.val_int, NULL);
2820 break;
2821 case DW_OP_plus_uconst:
2822 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2823 break;
2824 case DW_OP_breg0:
2825 case DW_OP_breg1:
2826 case DW_OP_breg2:
2827 case DW_OP_breg3:
2828 case DW_OP_breg4:
2829 case DW_OP_breg5:
2830 case DW_OP_breg6:
2831 case DW_OP_breg7:
2832 case DW_OP_breg8:
2833 case DW_OP_breg9:
2834 case DW_OP_breg10:
2835 case DW_OP_breg11:
2836 case DW_OP_breg12:
2837 case DW_OP_breg13:
2838 case DW_OP_breg14:
2839 case DW_OP_breg15:
2840 case DW_OP_breg16:
2841 case DW_OP_breg17:
2842 case DW_OP_breg18:
2843 case DW_OP_breg19:
2844 case DW_OP_breg20:
2845 case DW_OP_breg21:
2846 case DW_OP_breg22:
2847 case DW_OP_breg23:
2848 case DW_OP_breg24:
2849 case DW_OP_breg25:
2850 case DW_OP_breg26:
2851 case DW_OP_breg27:
2852 case DW_OP_breg28:
2853 case DW_OP_breg29:
2854 case DW_OP_breg30:
2855 case DW_OP_breg31:
2856 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2857 break;
2858 case DW_OP_regx:
2859 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2860 break;
2861 case DW_OP_fbreg:
2862 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2863 break;
2864 case DW_OP_bregx:
2865 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2866 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2867 break;
2868 case DW_OP_piece:
2869 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2870 break;
2871 case DW_OP_deref_size:
2872 case DW_OP_xderef_size:
2873 dw2_asm_output_data (1, val1->v.val_int, NULL);
2874 break;
2875 default:
2876 /* Other codes have no operands. */
2877 break;
2881 /* Output a sequence of location operations. */
2883 static void
2884 output_loc_sequence (loc)
2885 dw_loc_descr_ref loc;
2887 for (; loc != NULL; loc = loc->dw_loc_next)
2889 /* Output the opcode. */
2890 dw2_asm_output_data (1, loc->dw_loc_opc,
2891 "%s", dwarf_stack_op_name (loc->dw_loc_opc));
2893 /* Output the operand(s) (if any). */
2894 output_loc_operands (loc);
2898 /* This routine will generate the correct assembly data for a location
2899 description based on a cfi entry with a complex address. */
2901 static void
2902 output_cfa_loc (cfi)
2903 dw_cfi_ref cfi;
2905 dw_loc_descr_ref loc;
2906 unsigned long size;
2908 /* Output the size of the block. */
2909 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
2910 size = size_of_locs (loc);
2911 dw2_asm_output_data_uleb128 (size, NULL);
2913 /* Now output the operations themselves. */
2914 output_loc_sequence (loc);
2917 /* This function builds a dwarf location descriptor sequence from
2918 a dw_cfa_location. */
2920 static struct dw_loc_descr_struct *
2921 build_cfa_loc (cfa)
2922 dw_cfa_location *cfa;
2924 struct dw_loc_descr_struct *head, *tmp;
2926 if (cfa->indirect == 0)
2927 abort ();
2929 if (cfa->base_offset)
2931 if (cfa->reg <= 31)
2932 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
2933 else
2934 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
2936 else if (cfa->reg <= 31)
2937 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
2938 else
2939 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
2941 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2942 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2943 add_loc_descr (&head, tmp);
2944 if (cfa->offset != 0)
2946 tmp = new_loc_descr (DW_OP_plus_uconst, cfa->offset, 0);
2947 add_loc_descr (&head, tmp);
2950 return head;
2953 /* This function fills in aa dw_cfa_location structure from a dwarf location
2954 descriptor sequence. */
2956 static void
2957 get_cfa_from_loc_descr (cfa, loc)
2958 dw_cfa_location *cfa;
2959 struct dw_loc_descr_struct *loc;
2961 struct dw_loc_descr_struct *ptr;
2962 cfa->offset = 0;
2963 cfa->base_offset = 0;
2964 cfa->indirect = 0;
2965 cfa->reg = -1;
2967 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
2969 enum dwarf_location_atom op = ptr->dw_loc_opc;
2971 switch (op)
2973 case DW_OP_reg0:
2974 case DW_OP_reg1:
2975 case DW_OP_reg2:
2976 case DW_OP_reg3:
2977 case DW_OP_reg4:
2978 case DW_OP_reg5:
2979 case DW_OP_reg6:
2980 case DW_OP_reg7:
2981 case DW_OP_reg8:
2982 case DW_OP_reg9:
2983 case DW_OP_reg10:
2984 case DW_OP_reg11:
2985 case DW_OP_reg12:
2986 case DW_OP_reg13:
2987 case DW_OP_reg14:
2988 case DW_OP_reg15:
2989 case DW_OP_reg16:
2990 case DW_OP_reg17:
2991 case DW_OP_reg18:
2992 case DW_OP_reg19:
2993 case DW_OP_reg20:
2994 case DW_OP_reg21:
2995 case DW_OP_reg22:
2996 case DW_OP_reg23:
2997 case DW_OP_reg24:
2998 case DW_OP_reg25:
2999 case DW_OP_reg26:
3000 case DW_OP_reg27:
3001 case DW_OP_reg28:
3002 case DW_OP_reg29:
3003 case DW_OP_reg30:
3004 case DW_OP_reg31:
3005 cfa->reg = op - DW_OP_reg0;
3006 break;
3007 case DW_OP_regx:
3008 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3009 break;
3010 case DW_OP_breg0:
3011 case DW_OP_breg1:
3012 case DW_OP_breg2:
3013 case DW_OP_breg3:
3014 case DW_OP_breg4:
3015 case DW_OP_breg5:
3016 case DW_OP_breg6:
3017 case DW_OP_breg7:
3018 case DW_OP_breg8:
3019 case DW_OP_breg9:
3020 case DW_OP_breg10:
3021 case DW_OP_breg11:
3022 case DW_OP_breg12:
3023 case DW_OP_breg13:
3024 case DW_OP_breg14:
3025 case DW_OP_breg15:
3026 case DW_OP_breg16:
3027 case DW_OP_breg17:
3028 case DW_OP_breg18:
3029 case DW_OP_breg19:
3030 case DW_OP_breg20:
3031 case DW_OP_breg21:
3032 case DW_OP_breg22:
3033 case DW_OP_breg23:
3034 case DW_OP_breg24:
3035 case DW_OP_breg25:
3036 case DW_OP_breg26:
3037 case DW_OP_breg27:
3038 case DW_OP_breg28:
3039 case DW_OP_breg29:
3040 case DW_OP_breg30:
3041 case DW_OP_breg31:
3042 cfa->reg = op - DW_OP_breg0;
3043 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3044 break;
3045 case DW_OP_bregx:
3046 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3047 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3048 break;
3049 case DW_OP_deref:
3050 cfa->indirect = 1;
3051 break;
3052 case DW_OP_plus_uconst:
3053 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3054 break;
3055 default:
3056 internal_error ("DW_LOC_OP %s not implemented\n",
3057 dwarf_stack_op_name (ptr->dw_loc_opc));
3061 #endif /* .debug_frame support */
3063 /* And now, the support for symbolic debugging information. */
3064 #ifdef DWARF2_DEBUGGING_INFO
3066 /* .debug_str support. */
3067 static hashnode indirect_string_alloc PARAMS ((hash_table *));
3068 static int output_indirect_string PARAMS ((struct cpp_reader *,
3069 hashnode, const PTR));
3072 static void dwarf2out_init PARAMS ((const char *));
3073 static void dwarf2out_finish PARAMS ((const char *));
3074 static void dwarf2out_define PARAMS ((unsigned int, const char *));
3075 static void dwarf2out_undef PARAMS ((unsigned int, const char *));
3076 static void dwarf2out_start_source_file PARAMS ((unsigned, const char *));
3077 static void dwarf2out_end_source_file PARAMS ((unsigned));
3078 static void dwarf2out_begin_block PARAMS ((unsigned, unsigned));
3079 static void dwarf2out_end_block PARAMS ((unsigned, unsigned));
3080 static bool dwarf2out_ignore_block PARAMS ((tree));
3081 static void dwarf2out_global_decl PARAMS ((tree));
3082 static void dwarf2out_abstract_function PARAMS ((tree));
3084 /* The debug hooks structure. */
3086 struct gcc_debug_hooks dwarf2_debug_hooks =
3088 dwarf2out_init,
3089 dwarf2out_finish,
3090 dwarf2out_define,
3091 dwarf2out_undef,
3092 dwarf2out_start_source_file,
3093 dwarf2out_end_source_file,
3094 dwarf2out_begin_block,
3095 dwarf2out_end_block,
3096 dwarf2out_ignore_block,
3097 dwarf2out_source_line,
3098 dwarf2out_begin_prologue,
3099 debug_nothing_int, /* end_prologue */
3100 dwarf2out_end_epilogue,
3101 debug_nothing_tree, /* begin_function */
3102 debug_nothing_int, /* end_function */
3103 dwarf2out_decl, /* function_decl */
3104 dwarf2out_global_decl,
3105 debug_nothing_tree, /* deferred_inline_function */
3106 /* The DWARF 2 backend tries to reduce debugging bloat by not
3107 emitting the abstract description of inline functions until
3108 something tries to reference them. */
3109 dwarf2out_abstract_function, /* outlining_inline_function */
3110 debug_nothing_rtx /* label */
3113 /* NOTE: In the comments in this file, many references are made to
3114 "Debugging Information Entries". This term is abbreviated as `DIE'
3115 throughout the remainder of this file. */
3117 /* An internal representation of the DWARF output is built, and then
3118 walked to generate the DWARF debugging info. The walk of the internal
3119 representation is done after the entire program has been compiled.
3120 The types below are used to describe the internal representation. */
3122 /* Various DIE's use offsets relative to the beginning of the
3123 .debug_info section to refer to each other. */
3125 typedef long int dw_offset;
3127 /* Define typedefs here to avoid circular dependencies. */
3129 typedef struct dw_attr_struct *dw_attr_ref;
3130 typedef struct dw_line_info_struct *dw_line_info_ref;
3131 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3132 typedef struct pubname_struct *pubname_ref;
3133 typedef struct dw_ranges_struct *dw_ranges_ref;
3135 /* Each entry in the line_info_table maintains the file and
3136 line number associated with the label generated for that
3137 entry. The label gives the PC value associated with
3138 the line number entry. */
3140 typedef struct dw_line_info_struct
3142 unsigned long dw_file_num;
3143 unsigned long dw_line_num;
3145 dw_line_info_entry;
3147 /* Line information for functions in separate sections; each one gets its
3148 own sequence. */
3149 typedef struct dw_separate_line_info_struct
3151 unsigned long dw_file_num;
3152 unsigned long dw_line_num;
3153 unsigned long function;
3155 dw_separate_line_info_entry;
3157 /* Each DIE attribute has a field specifying the attribute kind,
3158 a link to the next attribute in the chain, and an attribute value.
3159 Attributes are typically linked below the DIE they modify. */
3161 typedef struct dw_attr_struct
3163 enum dwarf_attribute dw_attr;
3164 dw_attr_ref dw_attr_next;
3165 dw_val_node dw_attr_val;
3167 dw_attr_node;
3169 /* The Debugging Information Entry (DIE) structure */
3171 typedef struct die_struct
3173 enum dwarf_tag die_tag;
3174 char *die_symbol;
3175 dw_attr_ref die_attr;
3176 dw_die_ref die_parent;
3177 dw_die_ref die_child;
3178 dw_die_ref die_sib;
3179 dw_offset die_offset;
3180 unsigned long die_abbrev;
3181 int die_mark;
3183 die_node;
3185 /* The pubname structure */
3187 typedef struct pubname_struct
3189 dw_die_ref die;
3190 char *name;
3192 pubname_entry;
3194 struct dw_ranges_struct
3196 int block_num;
3199 /* The limbo die list structure. */
3200 typedef struct limbo_die_struct
3202 dw_die_ref die;
3203 tree created_for;
3204 struct limbo_die_struct *next;
3206 limbo_die_node;
3208 /* How to start an assembler comment. */
3209 #ifndef ASM_COMMENT_START
3210 #define ASM_COMMENT_START ";#"
3211 #endif
3213 /* Define a macro which returns non-zero for a TYPE_DECL which was
3214 implicitly generated for a tagged type.
3216 Note that unlike the gcc front end (which generates a NULL named
3217 TYPE_DECL node for each complete tagged type, each array type, and
3218 each function type node created) the g++ front end generates a
3219 _named_ TYPE_DECL node for each tagged type node created.
3220 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3221 generate a DW_TAG_typedef DIE for them. */
3223 #define TYPE_DECL_IS_STUB(decl) \
3224 (DECL_NAME (decl) == NULL_TREE \
3225 || (DECL_ARTIFICIAL (decl) \
3226 && is_tagged_type (TREE_TYPE (decl)) \
3227 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3228 /* This is necessary for stub decls that \
3229 appear in nested inline functions. */ \
3230 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3231 && (decl_ultimate_origin (decl) \
3232 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3234 /* Information concerning the compilation unit's programming
3235 language, and compiler version. */
3237 extern int flag_traditional;
3239 /* Fixed size portion of the DWARF compilation unit header. */
3240 #define DWARF_COMPILE_UNIT_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 3)
3242 /* Fixed size portion of debugging line information prolog. */
3243 #define DWARF_LINE_PROLOG_HEADER_SIZE 5
3245 /* Fixed size portion of public names info. */
3246 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3248 /* Fixed size portion of the address range info. */
3249 #define DWARF_ARANGES_HEADER_SIZE \
3250 (DWARF_ROUND (2 * DWARF_OFFSET_SIZE + 4, DWARF2_ADDR_SIZE * 2) \
3251 - DWARF_OFFSET_SIZE)
3253 /* Size of padding portion in the address range info. It must be
3254 aligned to twice the pointer size. */
3255 #define DWARF_ARANGES_PAD_SIZE \
3256 (DWARF_ROUND (2 * DWARF_OFFSET_SIZE + 4, DWARF2_ADDR_SIZE * 2) \
3257 - (2 * DWARF_OFFSET_SIZE + 4))
3259 /* Use assembler line directives if available. */
3260 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3261 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3262 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3263 #else
3264 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3265 #endif
3266 #endif
3268 /* Define the architecture-dependent minimum instruction length (in bytes).
3269 In this implementation of DWARF, this field is used for information
3270 purposes only. Since GCC generates assembly language, we have
3271 no a priori knowledge of how many instruction bytes are generated
3272 for each source line, and therefore can use only the DW_LNE_set_address
3273 and DW_LNS_fixed_advance_pc line information commands. */
3274 #ifndef DWARF_LINE_MIN_INSTR_LENGTH
3275 #define DWARF_LINE_MIN_INSTR_LENGTH 4
3276 #endif
3278 /* Minimum line offset in a special line info. opcode.
3279 This value was chosen to give a reasonable range of values. */
3280 #define DWARF_LINE_BASE -10
3282 /* First special line opcode - leave room for the standard opcodes. */
3283 #define DWARF_LINE_OPCODE_BASE 10
3285 /* Range of line offsets in a special line info. opcode. */
3286 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3288 /* Flag that indicates the initial value of the is_stmt_start flag.
3289 In the present implementation, we do not mark any lines as
3290 the beginning of a source statement, because that information
3291 is not made available by the GCC front-end. */
3292 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3294 /* This location is used by calc_die_sizes() to keep track
3295 the offset of each DIE within the .debug_info section. */
3296 static unsigned long next_die_offset;
3298 /* Record the root of the DIE's built for the current compilation unit. */
3299 static dw_die_ref comp_unit_die;
3301 /* A list of DIEs with a NULL parent waiting to be relocated. */
3302 static limbo_die_node *limbo_die_list = 0;
3304 /* Structure used by lookup_filename to manage sets of filenames. */
3305 struct file_table
3307 char **table;
3308 unsigned allocated;
3309 unsigned in_use;
3310 unsigned last_lookup_index;
3313 /* Size (in elements) of increments by which we may expand the filename
3314 table. */
3315 #define FILE_TABLE_INCREMENT 64
3317 /* Filenames referenced by this compilation unit. */
3318 static struct file_table file_table;
3320 /* Local pointer to the name of the main input file. Initialized in
3321 dwarf2out_init. */
3322 static const char *primary_filename;
3324 /* A pointer to the base of a table of references to DIE's that describe
3325 declarations. The table is indexed by DECL_UID() which is a unique
3326 number identifying each decl. */
3327 static dw_die_ref *decl_die_table;
3329 /* Number of elements currently allocated for the decl_die_table. */
3330 static unsigned decl_die_table_allocated;
3332 /* Number of elements in decl_die_table currently in use. */
3333 static unsigned decl_die_table_in_use;
3335 /* Size (in elements) of increments by which we may expand the
3336 decl_die_table. */
3337 #define DECL_DIE_TABLE_INCREMENT 256
3339 /* A pointer to the base of a table of references to declaration
3340 scopes. This table is a display which tracks the nesting
3341 of declaration scopes at the current scope and containing
3342 scopes. This table is used to find the proper place to
3343 define type declaration DIE's. */
3344 varray_type decl_scope_table;
3346 /* A pointer to the base of a list of references to DIE's that
3347 are uniquely identified by their tag, presence/absence of
3348 children DIE's, and list of attribute/value pairs. */
3349 static dw_die_ref *abbrev_die_table;
3351 /* Number of elements currently allocated for abbrev_die_table. */
3352 static unsigned abbrev_die_table_allocated;
3354 /* Number of elements in type_die_table currently in use. */
3355 static unsigned abbrev_die_table_in_use;
3357 /* Size (in elements) of increments by which we may expand the
3358 abbrev_die_table. */
3359 #define ABBREV_DIE_TABLE_INCREMENT 256
3361 /* A pointer to the base of a table that contains line information
3362 for each source code line in .text in the compilation unit. */
3363 static dw_line_info_ref line_info_table;
3365 /* Number of elements currently allocated for line_info_table. */
3366 static unsigned line_info_table_allocated;
3368 /* Number of elements in separate_line_info_table currently in use. */
3369 static unsigned separate_line_info_table_in_use;
3371 /* A pointer to the base of a table that contains line information
3372 for each source code line outside of .text in the compilation unit. */
3373 static dw_separate_line_info_ref separate_line_info_table;
3375 /* Number of elements currently allocated for separate_line_info_table. */
3376 static unsigned separate_line_info_table_allocated;
3378 /* Number of elements in line_info_table currently in use. */
3379 static unsigned line_info_table_in_use;
3381 /* Size (in elements) of increments by which we may expand the
3382 line_info_table. */
3383 #define LINE_INFO_TABLE_INCREMENT 1024
3385 /* A pointer to the base of a table that contains a list of publicly
3386 accessible names. */
3387 static pubname_ref pubname_table;
3389 /* Number of elements currently allocated for pubname_table. */
3390 static unsigned pubname_table_allocated;
3392 /* Number of elements in pubname_table currently in use. */
3393 static unsigned pubname_table_in_use;
3395 /* Size (in elements) of increments by which we may expand the
3396 pubname_table. */
3397 #define PUBNAME_TABLE_INCREMENT 64
3399 /* Array of dies for which we should generate .debug_arange info. */
3400 static dw_die_ref *arange_table;
3402 /* Number of elements currently allocated for arange_table. */
3403 static unsigned arange_table_allocated;
3405 /* Number of elements in arange_table currently in use. */
3406 static unsigned arange_table_in_use;
3408 /* Size (in elements) of increments by which we may expand the
3409 arange_table. */
3410 #define ARANGE_TABLE_INCREMENT 64
3412 /* Array of dies for which we should generate .debug_ranges info. */
3413 static dw_ranges_ref ranges_table;
3415 /* Number of elements currently allocated for ranges_table. */
3416 static unsigned ranges_table_allocated;
3418 /* Number of elements in ranges_table currently in use. */
3419 static unsigned ranges_table_in_use;
3421 /* Size (in elements) of increments by which we may expand the
3422 ranges_table. */
3423 #define RANGES_TABLE_INCREMENT 64
3425 /* Whether we have location lists that need outputting */
3426 static unsigned have_location_lists;
3428 /* A pointer to the base of a list of incomplete types which might be
3429 completed at some later time. incomplete_types_list needs to be a VARRAY
3430 because we want to tell the garbage collector about it. */
3431 varray_type incomplete_types;
3433 /* Record whether the function being analyzed contains inlined functions. */
3434 static int current_function_has_inlines;
3435 #if 0 && defined (MIPS_DEBUGGING_INFO)
3436 static int comp_unit_has_inlines;
3437 #endif
3439 /* Array of RTXes referenced by the debugging information, which therefore
3440 must be kept around forever. This is a GC root. */
3441 static varray_type used_rtx_varray;
3443 /* Forward declarations for functions defined in this file. */
3445 static int is_pseudo_reg PARAMS ((rtx));
3446 static tree type_main_variant PARAMS ((tree));
3447 static int is_tagged_type PARAMS ((tree));
3448 static const char *dwarf_tag_name PARAMS ((unsigned));
3449 static const char *dwarf_attr_name PARAMS ((unsigned));
3450 static const char *dwarf_form_name PARAMS ((unsigned));
3451 #if 0
3452 static const char *dwarf_type_encoding_name PARAMS ((unsigned));
3453 #endif
3454 static tree decl_ultimate_origin PARAMS ((tree));
3455 static tree block_ultimate_origin PARAMS ((tree));
3456 static tree decl_class_context PARAMS ((tree));
3457 static void add_dwarf_attr PARAMS ((dw_die_ref, dw_attr_ref));
3458 static inline dw_val_class AT_class PARAMS ((dw_attr_ref));
3459 static void add_AT_flag PARAMS ((dw_die_ref,
3460 enum dwarf_attribute,
3461 unsigned));
3462 static inline unsigned AT_flag PARAMS ((dw_attr_ref));
3463 static void add_AT_int PARAMS ((dw_die_ref,
3464 enum dwarf_attribute, long));
3465 static inline long int AT_int PARAMS ((dw_attr_ref));
3466 static void add_AT_unsigned PARAMS ((dw_die_ref,
3467 enum dwarf_attribute,
3468 unsigned long));
3469 static inline unsigned long AT_unsigned PARAMS ((dw_attr_ref));
3470 static void add_AT_long_long PARAMS ((dw_die_ref,
3471 enum dwarf_attribute,
3472 unsigned long,
3473 unsigned long));
3474 static void add_AT_float PARAMS ((dw_die_ref,
3475 enum dwarf_attribute,
3476 unsigned, long *));
3477 static void add_AT_string PARAMS ((dw_die_ref,
3478 enum dwarf_attribute,
3479 const char *));
3480 static inline const char *AT_string PARAMS ((dw_attr_ref));
3481 static int AT_string_form PARAMS ((dw_attr_ref));
3482 static void add_AT_die_ref PARAMS ((dw_die_ref,
3483 enum dwarf_attribute,
3484 dw_die_ref));
3485 static inline dw_die_ref AT_ref PARAMS ((dw_attr_ref));
3486 static inline int AT_ref_external PARAMS ((dw_attr_ref));
3487 static inline void set_AT_ref_external PARAMS ((dw_attr_ref, int));
3488 static void add_AT_fde_ref PARAMS ((dw_die_ref,
3489 enum dwarf_attribute,
3490 unsigned));
3491 static void add_AT_loc PARAMS ((dw_die_ref,
3492 enum dwarf_attribute,
3493 dw_loc_descr_ref));
3494 static inline dw_loc_descr_ref AT_loc PARAMS ((dw_attr_ref));
3495 static void add_AT_loc_list PARAMS ((dw_die_ref,
3496 enum dwarf_attribute,
3497 dw_loc_list_ref));
3498 static inline dw_loc_list_ref AT_loc_list PARAMS ((dw_attr_ref));
3499 static void add_AT_addr PARAMS ((dw_die_ref,
3500 enum dwarf_attribute,
3501 rtx));
3502 static inline rtx AT_addr PARAMS ((dw_attr_ref));
3503 static void add_AT_lbl_id PARAMS ((dw_die_ref,
3504 enum dwarf_attribute,
3505 const char *));
3506 static void add_AT_lbl_offset PARAMS ((dw_die_ref,
3507 enum dwarf_attribute,
3508 const char *));
3509 static void add_AT_offset PARAMS ((dw_die_ref,
3510 enum dwarf_attribute,
3511 unsigned long));
3512 static void add_AT_range_list PARAMS ((dw_die_ref,
3513 enum dwarf_attribute,
3514 unsigned long));
3515 static inline const char *AT_lbl PARAMS ((dw_attr_ref));
3516 static dw_attr_ref get_AT PARAMS ((dw_die_ref,
3517 enum dwarf_attribute));
3518 static const char *get_AT_low_pc PARAMS ((dw_die_ref));
3519 static const char *get_AT_hi_pc PARAMS ((dw_die_ref));
3520 static const char *get_AT_string PARAMS ((dw_die_ref,
3521 enum dwarf_attribute));
3522 static int get_AT_flag PARAMS ((dw_die_ref,
3523 enum dwarf_attribute));
3524 static unsigned get_AT_unsigned PARAMS ((dw_die_ref,
3525 enum dwarf_attribute));
3526 static inline dw_die_ref get_AT_ref PARAMS ((dw_die_ref,
3527 enum dwarf_attribute));
3528 static int is_c_family PARAMS ((void));
3529 static int is_cxx PARAMS ((void));
3530 static int is_java PARAMS ((void));
3531 static int is_fortran PARAMS ((void));
3532 static void remove_AT PARAMS ((dw_die_ref,
3533 enum dwarf_attribute));
3534 static inline void free_die PARAMS ((dw_die_ref));
3535 static void remove_children PARAMS ((dw_die_ref));
3536 static void add_child_die PARAMS ((dw_die_ref, dw_die_ref));
3537 static dw_die_ref new_die PARAMS ((enum dwarf_tag, dw_die_ref,
3538 tree));
3539 static dw_die_ref lookup_type_die PARAMS ((tree));
3540 static void equate_type_number_to_die PARAMS ((tree, dw_die_ref));
3541 static dw_die_ref lookup_decl_die PARAMS ((tree));
3542 static void equate_decl_number_to_die PARAMS ((tree, dw_die_ref));
3543 static void print_spaces PARAMS ((FILE *));
3544 static void print_die PARAMS ((dw_die_ref, FILE *));
3545 static void print_dwarf_line_table PARAMS ((FILE *));
3546 static void reverse_die_lists PARAMS ((dw_die_ref));
3547 static void reverse_all_dies PARAMS ((dw_die_ref));
3548 static dw_die_ref push_new_compile_unit PARAMS ((dw_die_ref, dw_die_ref));
3549 static dw_die_ref pop_compile_unit PARAMS ((dw_die_ref));
3550 static void loc_checksum PARAMS ((dw_loc_descr_ref,
3551 struct md5_ctx *));
3552 static void attr_checksum PARAMS ((dw_attr_ref,
3553 struct md5_ctx *));
3554 static void die_checksum PARAMS ((dw_die_ref,
3555 struct md5_ctx *));
3556 static void compute_section_prefix PARAMS ((dw_die_ref));
3557 static int is_type_die PARAMS ((dw_die_ref));
3558 static int is_comdat_die PARAMS ((dw_die_ref));
3559 static int is_symbol_die PARAMS ((dw_die_ref));
3560 static void assign_symbol_names PARAMS ((dw_die_ref));
3561 static void break_out_includes PARAMS ((dw_die_ref));
3562 static void add_sibling_attributes PARAMS ((dw_die_ref));
3563 static void build_abbrev_table PARAMS ((dw_die_ref));
3564 static void output_location_lists PARAMS ((dw_die_ref));
3565 static int constant_size PARAMS ((long unsigned));
3566 static unsigned long size_of_die PARAMS ((dw_die_ref));
3567 static void calc_die_sizes PARAMS ((dw_die_ref));
3568 static void mark_dies PARAMS ((dw_die_ref));
3569 static void unmark_dies PARAMS ((dw_die_ref));
3570 static unsigned long size_of_pubnames PARAMS ((void));
3571 static unsigned long size_of_aranges PARAMS ((void));
3572 static enum dwarf_form value_format PARAMS ((dw_attr_ref));
3573 static void output_value_format PARAMS ((dw_attr_ref));
3574 static void output_abbrev_section PARAMS ((void));
3575 static void output_die_symbol PARAMS ((dw_die_ref));
3576 static void output_die PARAMS ((dw_die_ref));
3577 static void output_compilation_unit_header PARAMS ((void));
3578 static void output_comp_unit PARAMS ((dw_die_ref));
3579 static const char *dwarf2_name PARAMS ((tree, int));
3580 static void add_pubname PARAMS ((tree, dw_die_ref));
3581 static void output_pubnames PARAMS ((void));
3582 static void add_arange PARAMS ((tree, dw_die_ref));
3583 static void output_aranges PARAMS ((void));
3584 static unsigned int add_ranges PARAMS ((tree));
3585 static void output_ranges PARAMS ((void));
3586 static void output_line_info PARAMS ((void));
3587 static void output_file_names PARAMS ((void));
3588 static dw_die_ref base_type_die PARAMS ((tree));
3589 static tree root_type PARAMS ((tree));
3590 static int is_base_type PARAMS ((tree));
3591 static dw_die_ref modified_type_die PARAMS ((tree, int, int, dw_die_ref));
3592 static int type_is_enum PARAMS ((tree));
3593 static unsigned int reg_number PARAMS ((rtx));
3594 static dw_loc_descr_ref reg_loc_descriptor PARAMS ((rtx));
3595 static dw_loc_descr_ref int_loc_descriptor PARAMS ((HOST_WIDE_INT));
3596 static dw_loc_descr_ref based_loc_descr PARAMS ((unsigned, long));
3597 static int is_based_loc PARAMS ((rtx));
3598 static dw_loc_descr_ref mem_loc_descriptor PARAMS ((rtx, enum machine_mode mode));
3599 static dw_loc_descr_ref concat_loc_descriptor PARAMS ((rtx, rtx));
3600 static dw_loc_descr_ref loc_descriptor PARAMS ((rtx));
3601 static dw_loc_descr_ref loc_descriptor_from_tree PARAMS ((tree, int));
3602 static HOST_WIDE_INT ceiling PARAMS ((HOST_WIDE_INT, unsigned int));
3603 static tree field_type PARAMS ((tree));
3604 static unsigned int simple_type_align_in_bits PARAMS ((tree));
3605 static unsigned int simple_decl_align_in_bits PARAMS ((tree));
3606 static unsigned HOST_WIDE_INT simple_type_size_in_bits PARAMS ((tree));
3607 static HOST_WIDE_INT field_byte_offset PARAMS ((tree));
3608 static void add_AT_location_description PARAMS ((dw_die_ref,
3609 enum dwarf_attribute, rtx));
3610 static void add_data_member_location_attribute PARAMS ((dw_die_ref, tree));
3611 static void add_const_value_attribute PARAMS ((dw_die_ref, rtx));
3612 static rtx rtl_for_decl_location PARAMS ((tree));
3613 static void add_location_or_const_value_attribute PARAMS ((dw_die_ref, tree));
3614 static void tree_add_const_value_attribute PARAMS ((dw_die_ref, tree));
3615 static void add_name_attribute PARAMS ((dw_die_ref, const char *));
3616 static void add_bound_info PARAMS ((dw_die_ref,
3617 enum dwarf_attribute, tree));
3618 static void add_subscript_info PARAMS ((dw_die_ref, tree));
3619 static void add_byte_size_attribute PARAMS ((dw_die_ref, tree));
3620 static void add_bit_offset_attribute PARAMS ((dw_die_ref, tree));
3621 static void add_bit_size_attribute PARAMS ((dw_die_ref, tree));
3622 static void add_prototyped_attribute PARAMS ((dw_die_ref, tree));
3623 static void add_abstract_origin_attribute PARAMS ((dw_die_ref, tree));
3624 static void add_pure_or_virtual_attribute PARAMS ((dw_die_ref, tree));
3625 static void add_src_coords_attributes PARAMS ((dw_die_ref, tree));
3626 static void add_name_and_src_coords_attributes PARAMS ((dw_die_ref, tree));
3627 static void push_decl_scope PARAMS ((tree));
3628 static void pop_decl_scope PARAMS ((void));
3629 static dw_die_ref scope_die_for PARAMS ((tree, dw_die_ref));
3630 static inline int local_scope_p PARAMS ((dw_die_ref));
3631 static inline int class_scope_p PARAMS ((dw_die_ref));
3632 static void add_type_attribute PARAMS ((dw_die_ref, tree, int, int,
3633 dw_die_ref));
3634 static const char *type_tag PARAMS ((tree));
3635 static tree member_declared_type PARAMS ((tree));
3636 #if 0
3637 static const char *decl_start_label PARAMS ((tree));
3638 #endif
3639 static void gen_array_type_die PARAMS ((tree, dw_die_ref));
3640 static void gen_set_type_die PARAMS ((tree, dw_die_ref));
3641 #if 0
3642 static void gen_entry_point_die PARAMS ((tree, dw_die_ref));
3643 #endif
3644 static void gen_inlined_enumeration_type_die PARAMS ((tree, dw_die_ref));
3645 static void gen_inlined_structure_type_die PARAMS ((tree, dw_die_ref));
3646 static void gen_inlined_union_type_die PARAMS ((tree, dw_die_ref));
3647 static void gen_enumeration_type_die PARAMS ((tree, dw_die_ref));
3648 static dw_die_ref gen_formal_parameter_die PARAMS ((tree, dw_die_ref));
3649 static void gen_unspecified_parameters_die PARAMS ((tree, dw_die_ref));
3650 static void gen_formal_types_die PARAMS ((tree, dw_die_ref));
3651 static void gen_subprogram_die PARAMS ((tree, dw_die_ref));
3652 static void gen_variable_die PARAMS ((tree, dw_die_ref));
3653 static void gen_label_die PARAMS ((tree, dw_die_ref));
3654 static void gen_lexical_block_die PARAMS ((tree, dw_die_ref, int));
3655 static void gen_inlined_subroutine_die PARAMS ((tree, dw_die_ref, int));
3656 static void gen_field_die PARAMS ((tree, dw_die_ref));
3657 static void gen_ptr_to_mbr_type_die PARAMS ((tree, dw_die_ref));
3658 static dw_die_ref gen_compile_unit_die PARAMS ((const char *));
3659 static void gen_string_type_die PARAMS ((tree, dw_die_ref));
3660 static void gen_inheritance_die PARAMS ((tree, dw_die_ref));
3661 static void gen_member_die PARAMS ((tree, dw_die_ref));
3662 static void gen_struct_or_union_type_die PARAMS ((tree, dw_die_ref));
3663 static void gen_subroutine_type_die PARAMS ((tree, dw_die_ref));
3664 static void gen_typedef_die PARAMS ((tree, dw_die_ref));
3665 static void gen_type_die PARAMS ((tree, dw_die_ref));
3666 static void gen_tagged_type_instantiation_die PARAMS ((tree, dw_die_ref));
3667 static void gen_block_die PARAMS ((tree, dw_die_ref, int));
3668 static void decls_for_scope PARAMS ((tree, dw_die_ref, int));
3669 static int is_redundant_typedef PARAMS ((tree));
3670 static void gen_decl_die PARAMS ((tree, dw_die_ref));
3671 static unsigned lookup_filename PARAMS ((const char *));
3672 static void init_file_table PARAMS ((void));
3673 static void retry_incomplete_types PARAMS ((void));
3674 static void gen_type_die_for_member PARAMS ((tree, tree, dw_die_ref));
3675 static void splice_child_die PARAMS ((dw_die_ref, dw_die_ref));
3676 static int file_info_cmp PARAMS ((const void *, const void *));
3677 static dw_loc_list_ref new_loc_list PARAMS ((dw_loc_descr_ref,
3678 const char *, const char *,
3679 const char *, unsigned));
3680 static void add_loc_descr_to_loc_list PARAMS ((dw_loc_list_ref *,
3681 dw_loc_descr_ref,
3682 const char *, const char *, const char *));
3683 static void output_loc_list PARAMS ((dw_loc_list_ref));
3684 static char *gen_internal_sym PARAMS ((const char *));
3685 static void mark_limbo_die_list PARAMS ((void *));
3687 /* Section names used to hold DWARF debugging information. */
3688 #ifndef DEBUG_INFO_SECTION
3689 #define DEBUG_INFO_SECTION ".debug_info"
3690 #endif
3691 #ifndef DEBUG_ABBREV_SECTION
3692 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3693 #endif
3694 #ifndef DEBUG_ARANGES_SECTION
3695 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3696 #endif
3697 #ifndef DEBUG_MACINFO_SECTION
3698 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3699 #endif
3700 #ifndef DEBUG_LINE_SECTION
3701 #define DEBUG_LINE_SECTION ".debug_line"
3702 #endif
3703 #ifndef DEBUG_LOC_SECTION
3704 #define DEBUG_LOC_SECTION ".debug_loc"
3705 #endif
3706 #ifndef DEBUG_PUBNAMES_SECTION
3707 #define DEBUG_PUBNAMES_SECTION ".debug_pubnames"
3708 #endif
3709 #ifndef DEBUG_STR_SECTION
3710 #define DEBUG_STR_SECTION ".debug_str"
3711 #endif
3712 #ifndef DEBUG_RANGES_SECTION
3713 #define DEBUG_RANGES_SECTION ".debug_ranges"
3714 #endif
3716 /* Standard ELF section names for compiled code and data. */
3717 #ifndef TEXT_SECTION_NAME
3718 #define TEXT_SECTION_NAME ".text"
3719 #endif
3721 /* Section flags for .debug_str section. */
3722 #ifdef HAVE_GAS_SHF_MERGE
3723 #define DEBUG_STR_SECTION_FLAGS \
3724 (SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1)
3725 #else
3726 #define DEBUG_STR_SECTION_FLAGS SECTION_DEBUG
3727 #endif
3729 /* Labels we insert at beginning sections we can reference instead of
3730 the section names themselves. */
3732 #ifndef TEXT_SECTION_LABEL
3733 #define TEXT_SECTION_LABEL "Ltext"
3734 #endif
3735 #ifndef DEBUG_LINE_SECTION_LABEL
3736 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3737 #endif
3738 #ifndef DEBUG_INFO_SECTION_LABEL
3739 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3740 #endif
3741 #ifndef DEBUG_ABBREV_SECTION_LABEL
3742 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3743 #endif
3744 #ifndef DEBUG_LOC_SECTION_LABEL
3745 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3746 #endif
3747 #ifndef DEBUG_RANGES_SECTION_LABEL
3748 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3749 #endif
3750 #ifndef DEBUG_MACINFO_SECTION_LABEL
3751 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3752 #endif
3754 /* Definitions of defaults for formats and names of various special
3755 (artificial) labels which may be generated within this file (when the -g
3756 options is used and DWARF_DEBUGGING_INFO is in effect.
3757 If necessary, these may be overridden from within the tm.h file, but
3758 typically, overriding these defaults is unnecessary. */
3760 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3761 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3762 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3763 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3764 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3765 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3766 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3767 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3769 #ifndef TEXT_END_LABEL
3770 #define TEXT_END_LABEL "Letext"
3771 #endif
3772 #ifndef DATA_END_LABEL
3773 #define DATA_END_LABEL "Ledata"
3774 #endif
3775 #ifndef BSS_END_LABEL
3776 #define BSS_END_LABEL "Lebss"
3777 #endif
3778 #ifndef BLOCK_BEGIN_LABEL
3779 #define BLOCK_BEGIN_LABEL "LBB"
3780 #endif
3781 #ifndef BLOCK_END_LABEL
3782 #define BLOCK_END_LABEL "LBE"
3783 #endif
3784 #ifndef BODY_BEGIN_LABEL
3785 #define BODY_BEGIN_LABEL "Lbb"
3786 #endif
3787 #ifndef BODY_END_LABEL
3788 #define BODY_END_LABEL "Lbe"
3789 #endif
3790 #ifndef LINE_CODE_LABEL
3791 #define LINE_CODE_LABEL "LM"
3792 #endif
3793 #ifndef SEPARATE_LINE_CODE_LABEL
3794 #define SEPARATE_LINE_CODE_LABEL "LSM"
3795 #endif
3797 /* We allow a language front-end to designate a function that is to be
3798 called to "demangle" any name before it it put into a DIE. */
3800 static const char *(*demangle_name_func) PARAMS ((const char *));
3802 void
3803 dwarf2out_set_demangle_name_func (func)
3804 const char *(*func) PARAMS ((const char *));
3806 demangle_name_func = func;
3809 /* Test if rtl node points to a pseudo register. */
3811 static inline int
3812 is_pseudo_reg (rtl)
3813 rtx rtl;
3815 return ((GET_CODE (rtl) == REG && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3816 || (GET_CODE (rtl) == SUBREG
3817 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3820 /* Return a reference to a type, with its const and volatile qualifiers
3821 removed. */
3823 static inline tree
3824 type_main_variant (type)
3825 tree type;
3827 type = TYPE_MAIN_VARIANT (type);
3829 /* ??? There really should be only one main variant among any group of
3830 variants of a given type (and all of the MAIN_VARIANT values for all
3831 members of the group should point to that one type) but sometimes the C
3832 front-end messes this up for array types, so we work around that bug
3833 here. */
3834 if (TREE_CODE (type) == ARRAY_TYPE)
3835 while (type != TYPE_MAIN_VARIANT (type))
3836 type = TYPE_MAIN_VARIANT (type);
3838 return type;
3841 /* Return non-zero if the given type node represents a tagged type. */
3843 static inline int
3844 is_tagged_type (type)
3845 tree type;
3847 enum tree_code code = TREE_CODE (type);
3849 return (code == RECORD_TYPE || code == UNION_TYPE
3850 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3853 /* Convert a DIE tag into its string name. */
3855 static const char *
3856 dwarf_tag_name (tag)
3857 unsigned tag;
3859 switch (tag)
3861 case DW_TAG_padding:
3862 return "DW_TAG_padding";
3863 case DW_TAG_array_type:
3864 return "DW_TAG_array_type";
3865 case DW_TAG_class_type:
3866 return "DW_TAG_class_type";
3867 case DW_TAG_entry_point:
3868 return "DW_TAG_entry_point";
3869 case DW_TAG_enumeration_type:
3870 return "DW_TAG_enumeration_type";
3871 case DW_TAG_formal_parameter:
3872 return "DW_TAG_formal_parameter";
3873 case DW_TAG_imported_declaration:
3874 return "DW_TAG_imported_declaration";
3875 case DW_TAG_label:
3876 return "DW_TAG_label";
3877 case DW_TAG_lexical_block:
3878 return "DW_TAG_lexical_block";
3879 case DW_TAG_member:
3880 return "DW_TAG_member";
3881 case DW_TAG_pointer_type:
3882 return "DW_TAG_pointer_type";
3883 case DW_TAG_reference_type:
3884 return "DW_TAG_reference_type";
3885 case DW_TAG_compile_unit:
3886 return "DW_TAG_compile_unit";
3887 case DW_TAG_string_type:
3888 return "DW_TAG_string_type";
3889 case DW_TAG_structure_type:
3890 return "DW_TAG_structure_type";
3891 case DW_TAG_subroutine_type:
3892 return "DW_TAG_subroutine_type";
3893 case DW_TAG_typedef:
3894 return "DW_TAG_typedef";
3895 case DW_TAG_union_type:
3896 return "DW_TAG_union_type";
3897 case DW_TAG_unspecified_parameters:
3898 return "DW_TAG_unspecified_parameters";
3899 case DW_TAG_variant:
3900 return "DW_TAG_variant";
3901 case DW_TAG_common_block:
3902 return "DW_TAG_common_block";
3903 case DW_TAG_common_inclusion:
3904 return "DW_TAG_common_inclusion";
3905 case DW_TAG_inheritance:
3906 return "DW_TAG_inheritance";
3907 case DW_TAG_inlined_subroutine:
3908 return "DW_TAG_inlined_subroutine";
3909 case DW_TAG_module:
3910 return "DW_TAG_module";
3911 case DW_TAG_ptr_to_member_type:
3912 return "DW_TAG_ptr_to_member_type";
3913 case DW_TAG_set_type:
3914 return "DW_TAG_set_type";
3915 case DW_TAG_subrange_type:
3916 return "DW_TAG_subrange_type";
3917 case DW_TAG_with_stmt:
3918 return "DW_TAG_with_stmt";
3919 case DW_TAG_access_declaration:
3920 return "DW_TAG_access_declaration";
3921 case DW_TAG_base_type:
3922 return "DW_TAG_base_type";
3923 case DW_TAG_catch_block:
3924 return "DW_TAG_catch_block";
3925 case DW_TAG_const_type:
3926 return "DW_TAG_const_type";
3927 case DW_TAG_constant:
3928 return "DW_TAG_constant";
3929 case DW_TAG_enumerator:
3930 return "DW_TAG_enumerator";
3931 case DW_TAG_file_type:
3932 return "DW_TAG_file_type";
3933 case DW_TAG_friend:
3934 return "DW_TAG_friend";
3935 case DW_TAG_namelist:
3936 return "DW_TAG_namelist";
3937 case DW_TAG_namelist_item:
3938 return "DW_TAG_namelist_item";
3939 case DW_TAG_packed_type:
3940 return "DW_TAG_packed_type";
3941 case DW_TAG_subprogram:
3942 return "DW_TAG_subprogram";
3943 case DW_TAG_template_type_param:
3944 return "DW_TAG_template_type_param";
3945 case DW_TAG_template_value_param:
3946 return "DW_TAG_template_value_param";
3947 case DW_TAG_thrown_type:
3948 return "DW_TAG_thrown_type";
3949 case DW_TAG_try_block:
3950 return "DW_TAG_try_block";
3951 case DW_TAG_variant_part:
3952 return "DW_TAG_variant_part";
3953 case DW_TAG_variable:
3954 return "DW_TAG_variable";
3955 case DW_TAG_volatile_type:
3956 return "DW_TAG_volatile_type";
3957 case DW_TAG_MIPS_loop:
3958 return "DW_TAG_MIPS_loop";
3959 case DW_TAG_format_label:
3960 return "DW_TAG_format_label";
3961 case DW_TAG_function_template:
3962 return "DW_TAG_function_template";
3963 case DW_TAG_class_template:
3964 return "DW_TAG_class_template";
3965 case DW_TAG_GNU_BINCL:
3966 return "DW_TAG_GNU_BINCL";
3967 case DW_TAG_GNU_EINCL:
3968 return "DW_TAG_GNU_EINCL";
3969 default:
3970 return "DW_TAG_<unknown>";
3974 /* Convert a DWARF attribute code into its string name. */
3976 static const char *
3977 dwarf_attr_name (attr)
3978 unsigned attr;
3980 switch (attr)
3982 case DW_AT_sibling:
3983 return "DW_AT_sibling";
3984 case DW_AT_location:
3985 return "DW_AT_location";
3986 case DW_AT_name:
3987 return "DW_AT_name";
3988 case DW_AT_ordering:
3989 return "DW_AT_ordering";
3990 case DW_AT_subscr_data:
3991 return "DW_AT_subscr_data";
3992 case DW_AT_byte_size:
3993 return "DW_AT_byte_size";
3994 case DW_AT_bit_offset:
3995 return "DW_AT_bit_offset";
3996 case DW_AT_bit_size:
3997 return "DW_AT_bit_size";
3998 case DW_AT_element_list:
3999 return "DW_AT_element_list";
4000 case DW_AT_stmt_list:
4001 return "DW_AT_stmt_list";
4002 case DW_AT_low_pc:
4003 return "DW_AT_low_pc";
4004 case DW_AT_high_pc:
4005 return "DW_AT_high_pc";
4006 case DW_AT_language:
4007 return "DW_AT_language";
4008 case DW_AT_member:
4009 return "DW_AT_member";
4010 case DW_AT_discr:
4011 return "DW_AT_discr";
4012 case DW_AT_discr_value:
4013 return "DW_AT_discr_value";
4014 case DW_AT_visibility:
4015 return "DW_AT_visibility";
4016 case DW_AT_import:
4017 return "DW_AT_import";
4018 case DW_AT_string_length:
4019 return "DW_AT_string_length";
4020 case DW_AT_common_reference:
4021 return "DW_AT_common_reference";
4022 case DW_AT_comp_dir:
4023 return "DW_AT_comp_dir";
4024 case DW_AT_const_value:
4025 return "DW_AT_const_value";
4026 case DW_AT_containing_type:
4027 return "DW_AT_containing_type";
4028 case DW_AT_default_value:
4029 return "DW_AT_default_value";
4030 case DW_AT_inline:
4031 return "DW_AT_inline";
4032 case DW_AT_is_optional:
4033 return "DW_AT_is_optional";
4034 case DW_AT_lower_bound:
4035 return "DW_AT_lower_bound";
4036 case DW_AT_producer:
4037 return "DW_AT_producer";
4038 case DW_AT_prototyped:
4039 return "DW_AT_prototyped";
4040 case DW_AT_return_addr:
4041 return "DW_AT_return_addr";
4042 case DW_AT_start_scope:
4043 return "DW_AT_start_scope";
4044 case DW_AT_stride_size:
4045 return "DW_AT_stride_size";
4046 case DW_AT_upper_bound:
4047 return "DW_AT_upper_bound";
4048 case DW_AT_abstract_origin:
4049 return "DW_AT_abstract_origin";
4050 case DW_AT_accessibility:
4051 return "DW_AT_accessibility";
4052 case DW_AT_address_class:
4053 return "DW_AT_address_class";
4054 case DW_AT_artificial:
4055 return "DW_AT_artificial";
4056 case DW_AT_base_types:
4057 return "DW_AT_base_types";
4058 case DW_AT_calling_convention:
4059 return "DW_AT_calling_convention";
4060 case DW_AT_count:
4061 return "DW_AT_count";
4062 case DW_AT_data_member_location:
4063 return "DW_AT_data_member_location";
4064 case DW_AT_decl_column:
4065 return "DW_AT_decl_column";
4066 case DW_AT_decl_file:
4067 return "DW_AT_decl_file";
4068 case DW_AT_decl_line:
4069 return "DW_AT_decl_line";
4070 case DW_AT_declaration:
4071 return "DW_AT_declaration";
4072 case DW_AT_discr_list:
4073 return "DW_AT_discr_list";
4074 case DW_AT_encoding:
4075 return "DW_AT_encoding";
4076 case DW_AT_external:
4077 return "DW_AT_external";
4078 case DW_AT_frame_base:
4079 return "DW_AT_frame_base";
4080 case DW_AT_friend:
4081 return "DW_AT_friend";
4082 case DW_AT_identifier_case:
4083 return "DW_AT_identifier_case";
4084 case DW_AT_macro_info:
4085 return "DW_AT_macro_info";
4086 case DW_AT_namelist_items:
4087 return "DW_AT_namelist_items";
4088 case DW_AT_priority:
4089 return "DW_AT_priority";
4090 case DW_AT_segment:
4091 return "DW_AT_segment";
4092 case DW_AT_specification:
4093 return "DW_AT_specification";
4094 case DW_AT_static_link:
4095 return "DW_AT_static_link";
4096 case DW_AT_type:
4097 return "DW_AT_type";
4098 case DW_AT_use_location:
4099 return "DW_AT_use_location";
4100 case DW_AT_variable_parameter:
4101 return "DW_AT_variable_parameter";
4102 case DW_AT_virtuality:
4103 return "DW_AT_virtuality";
4104 case DW_AT_vtable_elem_location:
4105 return "DW_AT_vtable_elem_location";
4107 case DW_AT_allocated:
4108 return "DW_AT_allocated";
4109 case DW_AT_associated:
4110 return "DW_AT_associated";
4111 case DW_AT_data_location:
4112 return "DW_AT_data_location";
4113 case DW_AT_stride:
4114 return "DW_AT_stride";
4115 case DW_AT_entry_pc:
4116 return "DW_AT_entry_pc";
4117 case DW_AT_use_UTF8:
4118 return "DW_AT_use_UTF8";
4119 case DW_AT_extension:
4120 return "DW_AT_extension";
4121 case DW_AT_ranges:
4122 return "DW_AT_ranges";
4123 case DW_AT_trampoline:
4124 return "DW_AT_trampoline";
4125 case DW_AT_call_column:
4126 return "DW_AT_call_column";
4127 case DW_AT_call_file:
4128 return "DW_AT_call_file";
4129 case DW_AT_call_line:
4130 return "DW_AT_call_line";
4132 case DW_AT_MIPS_fde:
4133 return "DW_AT_MIPS_fde";
4134 case DW_AT_MIPS_loop_begin:
4135 return "DW_AT_MIPS_loop_begin";
4136 case DW_AT_MIPS_tail_loop_begin:
4137 return "DW_AT_MIPS_tail_loop_begin";
4138 case DW_AT_MIPS_epilog_begin:
4139 return "DW_AT_MIPS_epilog_begin";
4140 case DW_AT_MIPS_loop_unroll_factor:
4141 return "DW_AT_MIPS_loop_unroll_factor";
4142 case DW_AT_MIPS_software_pipeline_depth:
4143 return "DW_AT_MIPS_software_pipeline_depth";
4144 case DW_AT_MIPS_linkage_name:
4145 return "DW_AT_MIPS_linkage_name";
4146 case DW_AT_MIPS_stride:
4147 return "DW_AT_MIPS_stride";
4148 case DW_AT_MIPS_abstract_name:
4149 return "DW_AT_MIPS_abstract_name";
4150 case DW_AT_MIPS_clone_origin:
4151 return "DW_AT_MIPS_clone_origin";
4152 case DW_AT_MIPS_has_inlines:
4153 return "DW_AT_MIPS_has_inlines";
4155 case DW_AT_sf_names:
4156 return "DW_AT_sf_names";
4157 case DW_AT_src_info:
4158 return "DW_AT_src_info";
4159 case DW_AT_mac_info:
4160 return "DW_AT_mac_info";
4161 case DW_AT_src_coords:
4162 return "DW_AT_src_coords";
4163 case DW_AT_body_begin:
4164 return "DW_AT_body_begin";
4165 case DW_AT_body_end:
4166 return "DW_AT_body_end";
4167 case DW_AT_VMS_rtnbeg_pd_address:
4168 return "DW_AT_VMS_rtnbeg_pd_address";
4170 default:
4171 return "DW_AT_<unknown>";
4175 /* Convert a DWARF value form code into its string name. */
4177 static const char *
4178 dwarf_form_name (form)
4179 unsigned form;
4181 switch (form)
4183 case DW_FORM_addr:
4184 return "DW_FORM_addr";
4185 case DW_FORM_block2:
4186 return "DW_FORM_block2";
4187 case DW_FORM_block4:
4188 return "DW_FORM_block4";
4189 case DW_FORM_data2:
4190 return "DW_FORM_data2";
4191 case DW_FORM_data4:
4192 return "DW_FORM_data4";
4193 case DW_FORM_data8:
4194 return "DW_FORM_data8";
4195 case DW_FORM_string:
4196 return "DW_FORM_string";
4197 case DW_FORM_block:
4198 return "DW_FORM_block";
4199 case DW_FORM_block1:
4200 return "DW_FORM_block1";
4201 case DW_FORM_data1:
4202 return "DW_FORM_data1";
4203 case DW_FORM_flag:
4204 return "DW_FORM_flag";
4205 case DW_FORM_sdata:
4206 return "DW_FORM_sdata";
4207 case DW_FORM_strp:
4208 return "DW_FORM_strp";
4209 case DW_FORM_udata:
4210 return "DW_FORM_udata";
4211 case DW_FORM_ref_addr:
4212 return "DW_FORM_ref_addr";
4213 case DW_FORM_ref1:
4214 return "DW_FORM_ref1";
4215 case DW_FORM_ref2:
4216 return "DW_FORM_ref2";
4217 case DW_FORM_ref4:
4218 return "DW_FORM_ref4";
4219 case DW_FORM_ref8:
4220 return "DW_FORM_ref8";
4221 case DW_FORM_ref_udata:
4222 return "DW_FORM_ref_udata";
4223 case DW_FORM_indirect:
4224 return "DW_FORM_indirect";
4225 default:
4226 return "DW_FORM_<unknown>";
4230 /* Convert a DWARF type code into its string name. */
4232 #if 0
4233 static const char *
4234 dwarf_type_encoding_name (enc)
4235 unsigned enc;
4237 switch (enc)
4239 case DW_ATE_address:
4240 return "DW_ATE_address";
4241 case DW_ATE_boolean:
4242 return "DW_ATE_boolean";
4243 case DW_ATE_complex_float:
4244 return "DW_ATE_complex_float";
4245 case DW_ATE_float:
4246 return "DW_ATE_float";
4247 case DW_ATE_signed:
4248 return "DW_ATE_signed";
4249 case DW_ATE_signed_char:
4250 return "DW_ATE_signed_char";
4251 case DW_ATE_unsigned:
4252 return "DW_ATE_unsigned";
4253 case DW_ATE_unsigned_char:
4254 return "DW_ATE_unsigned_char";
4255 default:
4256 return "DW_ATE_<unknown>";
4259 #endif
4261 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4262 instance of an inlined instance of a decl which is local to an inline
4263 function, so we have to trace all of the way back through the origin chain
4264 to find out what sort of node actually served as the original seed for the
4265 given block. */
4267 static tree
4268 decl_ultimate_origin (decl)
4269 tree decl;
4271 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4272 nodes in the function to point to themselves; ignore that if
4273 we're trying to output the abstract instance of this function. */
4274 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4275 return NULL_TREE;
4277 #ifdef ENABLE_CHECKING
4278 if (DECL_FROM_INLINE (DECL_ORIGIN (decl)))
4279 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4280 most distant ancestor, this should never happen. */
4281 abort ();
4282 #endif
4284 return DECL_ABSTRACT_ORIGIN (decl);
4287 /* Determine the "ultimate origin" of a block. The block may be an inlined
4288 instance of an inlined instance of a block which is local to an inline
4289 function, so we have to trace all of the way back through the origin chain
4290 to find out what sort of node actually served as the original seed for the
4291 given block. */
4293 static tree
4294 block_ultimate_origin (block)
4295 tree block;
4297 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4299 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4300 nodes in the function to point to themselves; ignore that if
4301 we're trying to output the abstract instance of this function. */
4302 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4303 return NULL_TREE;
4305 if (immediate_origin == NULL_TREE)
4306 return NULL_TREE;
4307 else
4309 tree ret_val;
4310 tree lookahead = immediate_origin;
4314 ret_val = lookahead;
4315 lookahead = (TREE_CODE (ret_val) == BLOCK
4316 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4318 while (lookahead != NULL && lookahead != ret_val);
4320 return ret_val;
4324 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4325 of a virtual function may refer to a base class, so we check the 'this'
4326 parameter. */
4328 static tree
4329 decl_class_context (decl)
4330 tree decl;
4332 tree context = NULL_TREE;
4334 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4335 context = DECL_CONTEXT (decl);
4336 else
4337 context = TYPE_MAIN_VARIANT
4338 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4340 if (context && !TYPE_P (context))
4341 context = NULL_TREE;
4343 return context;
4346 /* Add an attribute/value pair to a DIE. We build the lists up in reverse
4347 addition order, and correct that in reverse_all_dies. */
4349 static inline void
4350 add_dwarf_attr (die, attr)
4351 dw_die_ref die;
4352 dw_attr_ref attr;
4354 if (die != NULL && attr != NULL)
4356 attr->dw_attr_next = die->die_attr;
4357 die->die_attr = attr;
4361 static inline dw_val_class
4362 AT_class (a)
4363 dw_attr_ref a;
4365 return a->dw_attr_val.val_class;
4368 /* Add a flag value attribute to a DIE. */
4370 static inline void
4371 add_AT_flag (die, attr_kind, flag)
4372 dw_die_ref die;
4373 enum dwarf_attribute attr_kind;
4374 unsigned flag;
4376 dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4378 attr->dw_attr_next = NULL;
4379 attr->dw_attr = attr_kind;
4380 attr->dw_attr_val.val_class = dw_val_class_flag;
4381 attr->dw_attr_val.v.val_flag = flag;
4382 add_dwarf_attr (die, attr);
4385 static inline unsigned
4386 AT_flag (a)
4387 dw_attr_ref a;
4389 if (a && AT_class (a) == dw_val_class_flag)
4390 return a->dw_attr_val.v.val_flag;
4392 abort ();
4395 /* Add a signed integer attribute value to a DIE. */
4397 static inline void
4398 add_AT_int (die, attr_kind, int_val)
4399 dw_die_ref die;
4400 enum dwarf_attribute attr_kind;
4401 long int int_val;
4403 dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4405 attr->dw_attr_next = NULL;
4406 attr->dw_attr = attr_kind;
4407 attr->dw_attr_val.val_class = dw_val_class_const;
4408 attr->dw_attr_val.v.val_int = int_val;
4409 add_dwarf_attr (die, attr);
4412 static inline long int
4413 AT_int (a)
4414 dw_attr_ref a;
4416 if (a && AT_class (a) == dw_val_class_const)
4417 return a->dw_attr_val.v.val_int;
4419 abort ();
4422 /* Add an unsigned integer attribute value to a DIE. */
4424 static inline void
4425 add_AT_unsigned (die, attr_kind, unsigned_val)
4426 dw_die_ref die;
4427 enum dwarf_attribute attr_kind;
4428 unsigned long unsigned_val;
4430 dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4432 attr->dw_attr_next = NULL;
4433 attr->dw_attr = attr_kind;
4434 attr->dw_attr_val.val_class = dw_val_class_unsigned_const;
4435 attr->dw_attr_val.v.val_unsigned = unsigned_val;
4436 add_dwarf_attr (die, attr);
4439 static inline unsigned long
4440 AT_unsigned (a)
4441 dw_attr_ref a;
4443 if (a && AT_class (a) == dw_val_class_unsigned_const)
4444 return a->dw_attr_val.v.val_unsigned;
4446 abort ();
4449 /* Add an unsigned double integer attribute value to a DIE. */
4451 static inline void
4452 add_AT_long_long (die, attr_kind, val_hi, val_low)
4453 dw_die_ref die;
4454 enum dwarf_attribute attr_kind;
4455 unsigned long val_hi;
4456 unsigned long val_low;
4458 dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4460 attr->dw_attr_next = NULL;
4461 attr->dw_attr = attr_kind;
4462 attr->dw_attr_val.val_class = dw_val_class_long_long;
4463 attr->dw_attr_val.v.val_long_long.hi = val_hi;
4464 attr->dw_attr_val.v.val_long_long.low = val_low;
4465 add_dwarf_attr (die, attr);
4468 /* Add a floating point attribute value to a DIE and return it. */
4470 static inline void
4471 add_AT_float (die, attr_kind, length, array)
4472 dw_die_ref die;
4473 enum dwarf_attribute attr_kind;
4474 unsigned length;
4475 long *array;
4477 dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4479 attr->dw_attr_next = NULL;
4480 attr->dw_attr = attr_kind;
4481 attr->dw_attr_val.val_class = dw_val_class_float;
4482 attr->dw_attr_val.v.val_float.length = length;
4483 attr->dw_attr_val.v.val_float.array = array;
4484 add_dwarf_attr (die, attr);
4487 /* Add a string attribute value to a DIE. */
4489 static inline void
4490 add_AT_string (die, attr_kind, str)
4491 dw_die_ref die;
4492 enum dwarf_attribute attr_kind;
4493 const char *str;
4495 dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4496 struct indirect_string_node *node;
4498 if (! debug_str_hash)
4500 debug_str_hash = ht_create (10);
4501 debug_str_hash->alloc_node = indirect_string_alloc;
4504 node = (struct indirect_string_node *)
4505 ht_lookup (debug_str_hash, (const unsigned char *) str,
4506 strlen (str), HT_ALLOC);
4507 node->refcount++;
4509 attr->dw_attr_next = NULL;
4510 attr->dw_attr = attr_kind;
4511 attr->dw_attr_val.val_class = dw_val_class_str;
4512 attr->dw_attr_val.v.val_str = node;
4513 add_dwarf_attr (die, attr);
4516 static inline const char *
4517 AT_string (a)
4518 dw_attr_ref a;
4520 if (a && AT_class (a) == dw_val_class_str)
4521 return (const char *) HT_STR (&a->dw_attr_val.v.val_str->id);
4523 abort ();
4526 /* Find out whether a string should be output inline in DIE
4527 or out-of-line in .debug_str section. */
4529 static int
4530 AT_string_form (a)
4531 dw_attr_ref a;
4533 if (a && AT_class (a) == dw_val_class_str)
4535 struct indirect_string_node *node;
4536 unsigned int len;
4537 extern int const_labelno;
4538 char label[32];
4540 node = a->dw_attr_val.v.val_str;
4541 if (node->form)
4542 return node->form;
4544 len = HT_LEN (&node->id) + 1;
4546 /* If the string is shorter or equal to the size of the reference, it is
4547 always better to put it inline. */
4548 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4549 return node->form = DW_FORM_string;
4551 /* If we cannot expect the linker to merge strings in .debug_str
4552 section, only put it into .debug_str if it is worth even in this
4553 single module. */
4554 if ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) == 0
4555 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
4556 return node->form = DW_FORM_string;
4558 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
4559 ++const_labelno;
4560 node->label = xstrdup (label);
4562 return node->form = DW_FORM_strp;
4565 abort ();
4568 /* Add a DIE reference attribute value to a DIE. */
4570 static inline void
4571 add_AT_die_ref (die, attr_kind, targ_die)
4572 dw_die_ref die;
4573 enum dwarf_attribute attr_kind;
4574 dw_die_ref targ_die;
4576 dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4578 attr->dw_attr_next = NULL;
4579 attr->dw_attr = attr_kind;
4580 attr->dw_attr_val.val_class = dw_val_class_die_ref;
4581 attr->dw_attr_val.v.val_die_ref.die = targ_die;
4582 attr->dw_attr_val.v.val_die_ref.external = 0;
4583 add_dwarf_attr (die, attr);
4586 static inline dw_die_ref
4587 AT_ref (a)
4588 dw_attr_ref a;
4590 if (a && AT_class (a) == dw_val_class_die_ref)
4591 return a->dw_attr_val.v.val_die_ref.die;
4593 abort ();
4596 static inline int
4597 AT_ref_external (a)
4598 dw_attr_ref a;
4600 if (a && AT_class (a) == dw_val_class_die_ref)
4601 return a->dw_attr_val.v.val_die_ref.external;
4603 return 0;
4606 static inline void
4607 set_AT_ref_external (a, i)
4608 dw_attr_ref a;
4609 int i;
4611 if (a && AT_class (a) == dw_val_class_die_ref)
4612 a->dw_attr_val.v.val_die_ref.external = i;
4613 else
4614 abort ();
4617 /* Add an FDE reference attribute value to a DIE. */
4619 static inline void
4620 add_AT_fde_ref (die, attr_kind, targ_fde)
4621 dw_die_ref die;
4622 enum dwarf_attribute attr_kind;
4623 unsigned targ_fde;
4625 dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4627 attr->dw_attr_next = NULL;
4628 attr->dw_attr = attr_kind;
4629 attr->dw_attr_val.val_class = dw_val_class_fde_ref;
4630 attr->dw_attr_val.v.val_fde_index = targ_fde;
4631 add_dwarf_attr (die, attr);
4634 /* Add a location description attribute value to a DIE. */
4636 static inline void
4637 add_AT_loc (die, attr_kind, loc)
4638 dw_die_ref die;
4639 enum dwarf_attribute attr_kind;
4640 dw_loc_descr_ref loc;
4642 dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4644 attr->dw_attr_next = NULL;
4645 attr->dw_attr = attr_kind;
4646 attr->dw_attr_val.val_class = dw_val_class_loc;
4647 attr->dw_attr_val.v.val_loc = loc;
4648 add_dwarf_attr (die, attr);
4651 static inline dw_loc_descr_ref
4652 AT_loc (a)
4653 dw_attr_ref a;
4655 if (a && AT_class (a) == dw_val_class_loc)
4656 return a->dw_attr_val.v.val_loc;
4658 abort ();
4661 static inline void
4662 add_AT_loc_list (die, attr_kind, loc_list)
4663 dw_die_ref die;
4664 enum dwarf_attribute attr_kind;
4665 dw_loc_list_ref loc_list;
4667 dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4669 attr->dw_attr_next = NULL;
4670 attr->dw_attr = attr_kind;
4671 attr->dw_attr_val.val_class = dw_val_class_loc_list;
4672 attr->dw_attr_val.v.val_loc_list = loc_list;
4673 add_dwarf_attr (die, attr);
4674 have_location_lists = 1;
4677 static inline dw_loc_list_ref
4678 AT_loc_list (a)
4679 dw_attr_ref a;
4681 if (a && AT_class (a) == dw_val_class_loc_list)
4682 return a->dw_attr_val.v.val_loc_list;
4684 abort ();
4687 /* Add an address constant attribute value to a DIE. */
4689 static inline void
4690 add_AT_addr (die, attr_kind, addr)
4691 dw_die_ref die;
4692 enum dwarf_attribute attr_kind;
4693 rtx addr;
4695 dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4697 attr->dw_attr_next = NULL;
4698 attr->dw_attr = attr_kind;
4699 attr->dw_attr_val.val_class = dw_val_class_addr;
4700 attr->dw_attr_val.v.val_addr = addr;
4701 add_dwarf_attr (die, attr);
4704 static inline rtx
4705 AT_addr (a)
4706 dw_attr_ref a;
4708 if (a && AT_class (a) == dw_val_class_addr)
4709 return a->dw_attr_val.v.val_addr;
4711 abort ();
4714 /* Add a label identifier attribute value to a DIE. */
4716 static inline void
4717 add_AT_lbl_id (die, attr_kind, lbl_id)
4718 dw_die_ref die;
4719 enum dwarf_attribute attr_kind;
4720 const char *lbl_id;
4722 dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4724 attr->dw_attr_next = NULL;
4725 attr->dw_attr = attr_kind;
4726 attr->dw_attr_val.val_class = dw_val_class_lbl_id;
4727 attr->dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4728 add_dwarf_attr (die, attr);
4731 /* Add a section offset attribute value to a DIE. */
4733 static inline void
4734 add_AT_lbl_offset (die, attr_kind, label)
4735 dw_die_ref die;
4736 enum dwarf_attribute attr_kind;
4737 const char *label;
4739 dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4741 attr->dw_attr_next = NULL;
4742 attr->dw_attr = attr_kind;
4743 attr->dw_attr_val.val_class = dw_val_class_lbl_offset;
4744 attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
4745 add_dwarf_attr (die, attr);
4748 /* Add an offset attribute value to a DIE. */
4750 static inline void
4751 add_AT_offset (die, attr_kind, offset)
4752 dw_die_ref die;
4753 enum dwarf_attribute attr_kind;
4754 unsigned long offset;
4756 dw_attr_ref attr = (dw_attr_ref) xmalloc (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_offset;
4761 attr->dw_attr_val.v.val_offset = offset;
4762 add_dwarf_attr (die, attr);
4765 /* Add an range_list attribute value to a DIE. */
4767 static void
4768 add_AT_range_list (die, attr_kind, offset)
4769 dw_die_ref die;
4770 enum dwarf_attribute attr_kind;
4771 unsigned long offset;
4773 dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4775 attr->dw_attr_next = NULL;
4776 attr->dw_attr = attr_kind;
4777 attr->dw_attr_val.val_class = dw_val_class_range_list;
4778 attr->dw_attr_val.v.val_offset = offset;
4779 add_dwarf_attr (die, attr);
4782 static inline const char *
4783 AT_lbl (a)
4784 dw_attr_ref a;
4786 if (a && (AT_class (a) == dw_val_class_lbl_id
4787 || AT_class (a) == dw_val_class_lbl_offset))
4788 return a->dw_attr_val.v.val_lbl_id;
4790 abort ();
4793 /* Get the attribute of type attr_kind. */
4795 static inline dw_attr_ref
4796 get_AT (die, attr_kind)
4797 dw_die_ref die;
4798 enum dwarf_attribute attr_kind;
4800 dw_attr_ref a;
4801 dw_die_ref spec = NULL;
4803 if (die != NULL)
4805 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
4806 if (a->dw_attr == attr_kind)
4807 return a;
4808 else if (a->dw_attr == DW_AT_specification
4809 || a->dw_attr == DW_AT_abstract_origin)
4810 spec = AT_ref (a);
4812 if (spec)
4813 return get_AT (spec, attr_kind);
4816 return NULL;
4819 /* Return the "low pc" attribute value, typically associated with a subprogram
4820 DIE. Return null if the "low pc" attribute is either not present, or if it
4821 cannot be represented as an assembler label identifier. */
4823 static inline const char *
4824 get_AT_low_pc (die)
4825 dw_die_ref die;
4827 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4829 return a ? AT_lbl (a) : NULL;
4832 /* Return the "high pc" attribute value, typically associated with a subprogram
4833 DIE. Return null if the "high pc" attribute is either not present, or if it
4834 cannot be represented as an assembler label identifier. */
4836 static inline const char *
4837 get_AT_hi_pc (die)
4838 dw_die_ref die;
4840 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4842 return a ? AT_lbl (a) : NULL;
4845 /* Return the value of the string attribute designated by ATTR_KIND, or
4846 NULL if it is not present. */
4848 static inline const char *
4849 get_AT_string (die, attr_kind)
4850 dw_die_ref die;
4851 enum dwarf_attribute attr_kind;
4853 dw_attr_ref a = get_AT (die, attr_kind);
4855 return a ? AT_string (a) : NULL;
4858 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4859 if it is not present. */
4861 static inline int
4862 get_AT_flag (die, attr_kind)
4863 dw_die_ref die;
4864 enum dwarf_attribute attr_kind;
4866 dw_attr_ref a = get_AT (die, attr_kind);
4868 return a ? AT_flag (a) : 0;
4871 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4872 if it is not present. */
4874 static inline unsigned
4875 get_AT_unsigned (die, attr_kind)
4876 dw_die_ref die;
4877 enum dwarf_attribute attr_kind;
4879 dw_attr_ref a = get_AT (die, attr_kind);
4881 return a ? AT_unsigned (a) : 0;
4884 static inline dw_die_ref
4885 get_AT_ref (die, attr_kind)
4886 dw_die_ref die;
4887 enum dwarf_attribute attr_kind;
4889 dw_attr_ref a = get_AT (die, attr_kind);
4891 return a ? AT_ref (a) : NULL;
4894 static inline int
4895 is_c_family ()
4897 unsigned lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
4899 return (lang == DW_LANG_C || lang == DW_LANG_C89
4900 || lang == DW_LANG_C_plus_plus);
4903 static inline int
4904 is_cxx ()
4906 return (get_AT_unsigned (comp_unit_die, DW_AT_language)
4907 == DW_LANG_C_plus_plus);
4910 static inline int
4911 is_fortran ()
4913 unsigned lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
4915 return (lang == DW_LANG_Fortran77 || lang == DW_LANG_Fortran90);
4918 static inline int
4919 is_java ()
4921 unsigned lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
4923 return (lang == DW_LANG_Java);
4926 /* Free up the memory used by A. */
4928 static inline void free_AT PARAMS ((dw_attr_ref));
4929 static inline void
4930 free_AT (a)
4931 dw_attr_ref a;
4933 switch (AT_class (a))
4935 case dw_val_class_str:
4936 if (a->dw_attr_val.v.val_str->refcount)
4937 a->dw_attr_val.v.val_str->refcount--;
4938 break;
4940 case dw_val_class_lbl_id:
4941 case dw_val_class_lbl_offset:
4942 free (a->dw_attr_val.v.val_lbl_id);
4943 break;
4945 case dw_val_class_float:
4946 free (a->dw_attr_val.v.val_float.array);
4947 break;
4949 default:
4950 break;
4953 free (a);
4956 /* Remove the specified attribute if present. */
4958 static void
4959 remove_AT (die, attr_kind)
4960 dw_die_ref die;
4961 enum dwarf_attribute attr_kind;
4963 dw_attr_ref *p;
4964 dw_attr_ref removed = NULL;
4966 if (die != NULL)
4968 for (p = &(die->die_attr); *p; p = &((*p)->dw_attr_next))
4969 if ((*p)->dw_attr == attr_kind)
4971 removed = *p;
4972 *p = (*p)->dw_attr_next;
4973 break;
4976 if (removed != 0)
4977 free_AT (removed);
4981 /* Free up the memory used by DIE. */
4983 static inline void
4984 free_die (die)
4985 dw_die_ref die;
4987 remove_children (die);
4988 free (die);
4991 /* Discard the children of this DIE. */
4993 static void
4994 remove_children (die)
4995 dw_die_ref die;
4997 dw_die_ref child_die = die->die_child;
4999 die->die_child = NULL;
5001 while (child_die != NULL)
5003 dw_die_ref tmp_die = child_die;
5004 dw_attr_ref a;
5006 child_die = child_die->die_sib;
5008 for (a = tmp_die->die_attr; a != NULL;)
5010 dw_attr_ref tmp_a = a;
5012 a = a->dw_attr_next;
5013 free_AT (tmp_a);
5016 free_die (tmp_die);
5020 /* Add a child DIE below its parent. We build the lists up in reverse
5021 addition order, and correct that in reverse_all_dies. */
5023 static inline void
5024 add_child_die (die, child_die)
5025 dw_die_ref die;
5026 dw_die_ref child_die;
5028 if (die != NULL && child_die != NULL)
5030 if (die == child_die)
5031 abort ();
5033 child_die->die_parent = die;
5034 child_die->die_sib = die->die_child;
5035 die->die_child = child_die;
5039 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5040 is the specification, to the front of PARENT's list of children. */
5042 static void
5043 splice_child_die (parent, child)
5044 dw_die_ref parent, child;
5046 dw_die_ref *p;
5048 /* We want the declaration DIE from inside the class, not the
5049 specification DIE at toplevel. */
5050 if (child->die_parent != parent)
5052 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5054 if (tmp)
5055 child = tmp;
5058 if (child->die_parent != parent
5059 && child->die_parent != get_AT_ref (parent, DW_AT_specification))
5060 abort ();
5062 for (p = &(child->die_parent->die_child); *p; p = &((*p)->die_sib))
5063 if (*p == child)
5065 *p = child->die_sib;
5066 break;
5069 child->die_sib = parent->die_child;
5070 parent->die_child = child;
5073 /* Return a pointer to a newly created DIE node. */
5075 static inline dw_die_ref
5076 new_die (tag_value, parent_die, t)
5077 enum dwarf_tag tag_value;
5078 dw_die_ref parent_die;
5079 tree t;
5081 dw_die_ref die = (dw_die_ref) xcalloc (1, sizeof (die_node));
5083 die->die_tag = tag_value;
5085 if (parent_die != NULL)
5086 add_child_die (parent_die, die);
5087 else
5089 limbo_die_node *limbo_node;
5091 limbo_node = (limbo_die_node *) xmalloc (sizeof (limbo_die_node));
5092 limbo_node->die = die;
5093 limbo_node->created_for = t;
5094 limbo_node->next = limbo_die_list;
5095 limbo_die_list = limbo_node;
5098 return die;
5101 /* Return the DIE associated with the given type specifier. */
5103 static inline dw_die_ref
5104 lookup_type_die (type)
5105 tree type;
5107 if (TREE_CODE (type) == VECTOR_TYPE)
5108 type = TYPE_DEBUG_REPRESENTATION_TYPE (type);
5110 return (dw_die_ref) TYPE_SYMTAB_POINTER (type);
5113 /* Equate a DIE to a given type specifier. */
5115 static inline void
5116 equate_type_number_to_die (type, type_die)
5117 tree type;
5118 dw_die_ref type_die;
5120 TYPE_SYMTAB_POINTER (type) = (char *) type_die;
5123 /* Return the DIE associated with a given declaration. */
5125 static inline dw_die_ref
5126 lookup_decl_die (decl)
5127 tree decl;
5129 unsigned decl_id = DECL_UID (decl);
5131 return (decl_id < decl_die_table_in_use ? decl_die_table[decl_id] : NULL);
5134 /* Equate a DIE to a particular declaration. */
5136 static void
5137 equate_decl_number_to_die (decl, decl_die)
5138 tree decl;
5139 dw_die_ref decl_die;
5141 unsigned int decl_id = DECL_UID (decl);
5142 unsigned int num_allocated;
5144 if (decl_id >= decl_die_table_allocated)
5146 num_allocated
5147 = ((decl_id + 1 + DECL_DIE_TABLE_INCREMENT - 1)
5148 / DECL_DIE_TABLE_INCREMENT)
5149 * DECL_DIE_TABLE_INCREMENT;
5151 decl_die_table
5152 = (dw_die_ref *) xrealloc (decl_die_table,
5153 sizeof (dw_die_ref) * num_allocated);
5155 memset ((char *) &decl_die_table[decl_die_table_allocated], 0,
5156 (num_allocated - decl_die_table_allocated) * sizeof (dw_die_ref));
5157 decl_die_table_allocated = num_allocated;
5160 if (decl_id >= decl_die_table_in_use)
5161 decl_die_table_in_use = (decl_id + 1);
5163 decl_die_table[decl_id] = decl_die;
5166 /* Keep track of the number of spaces used to indent the
5167 output of the debugging routines that print the structure of
5168 the DIE internal representation. */
5169 static int print_indent;
5171 /* Indent the line the number of spaces given by print_indent. */
5173 static inline void
5174 print_spaces (outfile)
5175 FILE *outfile;
5177 fprintf (outfile, "%*s", print_indent, "");
5180 /* Print the information associated with a given DIE, and its children.
5181 This routine is a debugging aid only. */
5183 static void
5184 print_die (die, outfile)
5185 dw_die_ref die;
5186 FILE *outfile;
5188 dw_attr_ref a;
5189 dw_die_ref c;
5191 print_spaces (outfile);
5192 fprintf (outfile, "DIE %4lu: %s\n",
5193 die->die_offset, dwarf_tag_name (die->die_tag));
5194 print_spaces (outfile);
5195 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5196 fprintf (outfile, " offset: %lu\n", die->die_offset);
5198 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5200 print_spaces (outfile);
5201 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5203 switch (AT_class (a))
5205 case dw_val_class_addr:
5206 fprintf (outfile, "address");
5207 break;
5208 case dw_val_class_offset:
5209 fprintf (outfile, "offset");
5210 break;
5211 case dw_val_class_loc:
5212 fprintf (outfile, "location descriptor");
5213 break;
5214 case dw_val_class_loc_list:
5215 fprintf (outfile, "location list -> label:%s",
5216 AT_loc_list (a)->ll_symbol);
5217 break;
5218 case dw_val_class_range_list:
5219 fprintf (outfile, "range list");
5220 break;
5221 case dw_val_class_const:
5222 fprintf (outfile, "%ld", AT_int (a));
5223 break;
5224 case dw_val_class_unsigned_const:
5225 fprintf (outfile, "%lu", AT_unsigned (a));
5226 break;
5227 case dw_val_class_long_long:
5228 fprintf (outfile, "constant (%lu,%lu)",
5229 a->dw_attr_val.v.val_long_long.hi,
5230 a->dw_attr_val.v.val_long_long.low);
5231 break;
5232 case dw_val_class_float:
5233 fprintf (outfile, "floating-point constant");
5234 break;
5235 case dw_val_class_flag:
5236 fprintf (outfile, "%u", AT_flag (a));
5237 break;
5238 case dw_val_class_die_ref:
5239 if (AT_ref (a) != NULL)
5241 if (AT_ref (a)->die_symbol)
5242 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5243 else
5244 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5246 else
5247 fprintf (outfile, "die -> <null>");
5248 break;
5249 case dw_val_class_lbl_id:
5250 case dw_val_class_lbl_offset:
5251 fprintf (outfile, "label: %s", AT_lbl (a));
5252 break;
5253 case dw_val_class_str:
5254 if (AT_string (a) != NULL)
5255 fprintf (outfile, "\"%s\"", AT_string (a));
5256 else
5257 fprintf (outfile, "<null>");
5258 break;
5259 default:
5260 break;
5263 fprintf (outfile, "\n");
5266 if (die->die_child != NULL)
5268 print_indent += 4;
5269 for (c = die->die_child; c != NULL; c = c->die_sib)
5270 print_die (c, outfile);
5272 print_indent -= 4;
5274 if (print_indent == 0)
5275 fprintf (outfile, "\n");
5278 /* Print the contents of the source code line number correspondence table.
5279 This routine is a debugging aid only. */
5281 static void
5282 print_dwarf_line_table (outfile)
5283 FILE *outfile;
5285 unsigned i;
5286 dw_line_info_ref line_info;
5288 fprintf (outfile, "\n\nDWARF source line information\n");
5289 for (i = 1; i < line_info_table_in_use; i++)
5291 line_info = &line_info_table[i];
5292 fprintf (outfile, "%5d: ", i);
5293 fprintf (outfile, "%-20s", file_table.table[line_info->dw_file_num]);
5294 fprintf (outfile, "%6ld", line_info->dw_line_num);
5295 fprintf (outfile, "\n");
5298 fprintf (outfile, "\n\n");
5301 /* Print the information collected for a given DIE. */
5303 void
5304 debug_dwarf_die (die)
5305 dw_die_ref die;
5307 print_die (die, stderr);
5310 /* Print all DWARF information collected for the compilation unit.
5311 This routine is a debugging aid only. */
5313 void
5314 debug_dwarf ()
5316 print_indent = 0;
5317 print_die (comp_unit_die, stderr);
5318 if (! DWARF2_ASM_LINE_DEBUG_INFO)
5319 print_dwarf_line_table (stderr);
5322 /* We build up the lists of children and attributes by pushing new ones
5323 onto the beginning of the list. Reverse the lists for DIE so that
5324 they are in order of addition. */
5326 static void
5327 reverse_die_lists (die)
5328 dw_die_ref die;
5330 dw_die_ref c, cp, cn;
5331 dw_attr_ref a, ap, an;
5333 for (a = die->die_attr, ap = 0; a; a = an)
5335 an = a->dw_attr_next;
5336 a->dw_attr_next = ap;
5337 ap = a;
5340 die->die_attr = ap;
5342 for (c = die->die_child, cp = 0; c; c = cn)
5344 cn = c->die_sib;
5345 c->die_sib = cp;
5346 cp = c;
5349 die->die_child = cp;
5352 /* reverse_die_lists only reverses the single die you pass it. Since we used to
5353 reverse all dies in add_sibling_attributes, which runs through all the dies,
5354 it would reverse all the dies. Now, however, since we don't call
5355 reverse_die_lists in add_sibling_attributes, we need a routine to
5356 recursively reverse all the dies. This is that routine. */
5358 static void
5359 reverse_all_dies (die)
5360 dw_die_ref die;
5362 dw_die_ref c;
5364 reverse_die_lists (die);
5366 for (c = die->die_child; c; c = c->die_sib)
5367 reverse_all_dies (c);
5370 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5371 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5372 DIE that marks the start of the DIEs for this include file. */
5374 static dw_die_ref
5375 push_new_compile_unit (old_unit, bincl_die)
5376 dw_die_ref old_unit, bincl_die;
5378 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5379 dw_die_ref new_unit = gen_compile_unit_die (filename);
5381 new_unit->die_sib = old_unit;
5382 return new_unit;
5385 /* Close an include-file CU and reopen the enclosing one. */
5387 static dw_die_ref
5388 pop_compile_unit (old_unit)
5389 dw_die_ref old_unit;
5391 dw_die_ref new_unit = old_unit->die_sib;
5393 old_unit->die_sib = NULL;
5394 return new_unit;
5397 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5398 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5400 /* Calculate the checksum of a location expression. */
5402 static inline void
5403 loc_checksum (loc, ctx)
5404 dw_loc_descr_ref loc;
5405 struct md5_ctx *ctx;
5407 CHECKSUM (loc->dw_loc_opc);
5408 CHECKSUM (loc->dw_loc_oprnd1);
5409 CHECKSUM (loc->dw_loc_oprnd2);
5412 /* Calculate the checksum of an attribute. */
5414 static void
5415 attr_checksum (at, ctx)
5416 dw_attr_ref at;
5417 struct md5_ctx *ctx;
5419 dw_loc_descr_ref loc;
5420 rtx r;
5422 CHECKSUM (at->dw_attr);
5424 /* We don't care about differences in file numbering. */
5425 if (at->dw_attr == DW_AT_decl_file
5426 /* Or that this was compiled with a different compiler snapshot; if
5427 the output is the same, that's what matters. */
5428 || at->dw_attr == DW_AT_producer)
5429 return;
5431 switch (AT_class (at))
5433 case dw_val_class_const:
5434 CHECKSUM (at->dw_attr_val.v.val_int);
5435 break;
5436 case dw_val_class_unsigned_const:
5437 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5438 break;
5439 case dw_val_class_long_long:
5440 CHECKSUM (at->dw_attr_val.v.val_long_long);
5441 break;
5442 case dw_val_class_float:
5443 CHECKSUM (at->dw_attr_val.v.val_float);
5444 break;
5445 case dw_val_class_flag:
5446 CHECKSUM (at->dw_attr_val.v.val_flag);
5447 break;
5448 case dw_val_class_str:
5449 CHECKSUM_STRING (AT_string (at));
5450 break;
5452 case dw_val_class_addr:
5453 r = AT_addr (at);
5454 switch (GET_CODE (r))
5456 case SYMBOL_REF:
5457 CHECKSUM_STRING (XSTR (r, 0));
5458 break;
5460 default:
5461 abort ();
5463 break;
5465 case dw_val_class_offset:
5466 CHECKSUM (at->dw_attr_val.v.val_offset);
5467 break;
5469 case dw_val_class_loc:
5470 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5471 loc_checksum (loc, ctx);
5472 break;
5474 case dw_val_class_die_ref:
5475 if (AT_ref (at)->die_offset)
5476 CHECKSUM (AT_ref (at)->die_offset);
5477 /* FIXME else use target die name or something. */
5479 case dw_val_class_fde_ref:
5480 case dw_val_class_lbl_id:
5481 case dw_val_class_lbl_offset:
5482 break;
5484 default:
5485 break;
5489 /* Calculate the checksum of a DIE. */
5491 static void
5492 die_checksum (die, ctx)
5493 dw_die_ref die;
5494 struct md5_ctx *ctx;
5496 dw_die_ref c;
5497 dw_attr_ref a;
5499 CHECKSUM (die->die_tag);
5501 for (a = die->die_attr; a; a = a->dw_attr_next)
5502 attr_checksum (a, ctx);
5504 for (c = die->die_child; c; c = c->die_sib)
5505 die_checksum (c, ctx);
5508 #undef CHECKSUM
5509 #undef CHECKSUM_STRING
5511 /* The prefix to attach to symbols on DIEs in the current comdat debug
5512 info section. */
5513 static char *comdat_symbol_id;
5515 /* The index of the current symbol within the current comdat CU. */
5516 static unsigned int comdat_symbol_number;
5518 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
5519 children, and set comdat_symbol_id accordingly. */
5521 static void
5522 compute_section_prefix (unit_die)
5523 dw_die_ref unit_die;
5525 const char *base = lbasename (get_AT_string (unit_die, DW_AT_name));
5526 char *name = (char *) alloca (strlen (base) + 64);
5527 char *p;
5528 int i;
5529 unsigned char checksum[16];
5530 struct md5_ctx ctx;
5532 /* Compute the checksum of the DIE, then append part of it as hex digits to
5533 the name filename of the unit. */
5535 md5_init_ctx (&ctx);
5536 die_checksum (unit_die, &ctx);
5537 md5_finish_ctx (&ctx, checksum);
5539 sprintf (name, "%s.", base);
5540 clean_symbol_name (name);
5542 p = name + strlen (name);
5543 for (i = 0; i < 4; i++)
5545 sprintf (p, "%.2x", checksum[i]);
5546 p += 2;
5549 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
5550 comdat_symbol_number = 0;
5553 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
5555 static int
5556 is_type_die (die)
5557 dw_die_ref die;
5559 switch (die->die_tag)
5561 case DW_TAG_array_type:
5562 case DW_TAG_class_type:
5563 case DW_TAG_enumeration_type:
5564 case DW_TAG_pointer_type:
5565 case DW_TAG_reference_type:
5566 case DW_TAG_string_type:
5567 case DW_TAG_structure_type:
5568 case DW_TAG_subroutine_type:
5569 case DW_TAG_union_type:
5570 case DW_TAG_ptr_to_member_type:
5571 case DW_TAG_set_type:
5572 case DW_TAG_subrange_type:
5573 case DW_TAG_base_type:
5574 case DW_TAG_const_type:
5575 case DW_TAG_file_type:
5576 case DW_TAG_packed_type:
5577 case DW_TAG_volatile_type:
5578 return 1;
5579 default:
5580 return 0;
5584 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
5585 Basically, we want to choose the bits that are likely to be shared between
5586 compilations (types) and leave out the bits that are specific to individual
5587 compilations (functions). */
5589 static int
5590 is_comdat_die (c)
5591 dw_die_ref c;
5593 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
5594 we do for stabs. The advantage is a greater likelihood of sharing between
5595 objects that don't include headers in the same order (and therefore would
5596 put the base types in a different comdat). jason 8/28/00 */
5598 if (c->die_tag == DW_TAG_base_type)
5599 return 0;
5601 if (c->die_tag == DW_TAG_pointer_type
5602 || c->die_tag == DW_TAG_reference_type
5603 || c->die_tag == DW_TAG_const_type
5604 || c->die_tag == DW_TAG_volatile_type)
5606 dw_die_ref t = get_AT_ref (c, DW_AT_type);
5608 return t ? is_comdat_die (t) : 0;
5611 return is_type_die (c);
5614 /* Returns 1 iff C is the sort of DIE that might be referred to from another
5615 compilation unit. */
5617 static int
5618 is_symbol_die (c)
5619 dw_die_ref c;
5621 return (is_type_die (c)
5622 || (get_AT (c, DW_AT_declaration)
5623 && !get_AT (c, DW_AT_specification)));
5626 static char *
5627 gen_internal_sym (prefix)
5628 const char *prefix;
5630 char buf[256];
5631 static int label_num;
5633 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
5634 return xstrdup (buf);
5637 /* Assign symbols to all worthy DIEs under DIE. */
5639 static void
5640 assign_symbol_names (die)
5641 dw_die_ref die;
5643 dw_die_ref c;
5645 if (is_symbol_die (die))
5647 if (comdat_symbol_id)
5649 char *p = alloca (strlen (comdat_symbol_id) + 64);
5651 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
5652 comdat_symbol_id, comdat_symbol_number++);
5653 die->die_symbol = xstrdup (p);
5655 else
5656 die->die_symbol = gen_internal_sym ("LDIE");
5659 for (c = die->die_child; c != NULL; c = c->die_sib)
5660 assign_symbol_names (c);
5663 /* Traverse the DIE (which is always comp_unit_die), and set up
5664 additional compilation units for each of the include files we see
5665 bracketed by BINCL/EINCL. */
5667 static void
5668 break_out_includes (die)
5669 dw_die_ref die;
5671 dw_die_ref *ptr;
5672 dw_die_ref unit = NULL;
5673 limbo_die_node *node;
5675 for (ptr = &(die->die_child); *ptr; )
5677 dw_die_ref c = *ptr;
5679 if (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
5680 || (unit && is_comdat_die (c)))
5682 /* This DIE is for a secondary CU; remove it from the main one. */
5683 *ptr = c->die_sib;
5685 if (c->die_tag == DW_TAG_GNU_BINCL)
5687 unit = push_new_compile_unit (unit, c);
5688 free_die (c);
5690 else if (c->die_tag == DW_TAG_GNU_EINCL)
5692 unit = pop_compile_unit (unit);
5693 free_die (c);
5695 else
5696 add_child_die (unit, c);
5698 else
5700 /* Leave this DIE in the main CU. */
5701 ptr = &(c->die_sib);
5702 continue;
5706 #if 0
5707 /* We can only use this in debugging, since the frontend doesn't check
5708 to make sure that we leave every include file we enter. */
5709 if (unit != NULL)
5710 abort ();
5711 #endif
5713 assign_symbol_names (die);
5714 for (node = limbo_die_list; node; node = node->next)
5716 compute_section_prefix (node->die);
5717 assign_symbol_names (node->die);
5721 /* Traverse the DIE and add a sibling attribute if it may have the
5722 effect of speeding up access to siblings. To save some space,
5723 avoid generating sibling attributes for DIE's without children. */
5725 static void
5726 add_sibling_attributes (die)
5727 dw_die_ref die;
5729 dw_die_ref c;
5731 if (die->die_tag != DW_TAG_compile_unit
5732 && die->die_sib && die->die_child != NULL)
5733 /* Add the sibling link to the front of the attribute list. */
5734 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
5736 for (c = die->die_child; c != NULL; c = c->die_sib)
5737 add_sibling_attributes (c);
5740 /* Output all location lists for the DIE and its children. */
5742 static void
5743 output_location_lists (die)
5744 dw_die_ref die;
5746 dw_die_ref c;
5747 dw_attr_ref d_attr;
5749 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
5750 if (AT_class (d_attr) == dw_val_class_loc_list)
5751 output_loc_list (AT_loc_list (d_attr));
5753 for (c = die->die_child; c != NULL; c = c->die_sib)
5754 output_location_lists (c);
5757 /* The format of each DIE (and its attribute value pairs) is encoded in an
5758 abbreviation table. This routine builds the abbreviation table and assigns
5759 a unique abbreviation id for each abbreviation entry. The children of each
5760 die are visited recursively. */
5762 static void
5763 build_abbrev_table (die)
5764 dw_die_ref die;
5766 unsigned long abbrev_id;
5767 unsigned int n_alloc;
5768 dw_die_ref c;
5769 dw_attr_ref d_attr, a_attr;
5771 /* Scan the DIE references, and mark as external any that refer to
5772 DIEs from other CUs (i.e. those which are not marked). */
5773 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
5774 if (AT_class (d_attr) == dw_val_class_die_ref
5775 && AT_ref (d_attr)->die_mark == 0)
5777 if (AT_ref (d_attr)->die_symbol == 0)
5778 abort ();
5780 set_AT_ref_external (d_attr, 1);
5783 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
5785 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
5787 if (abbrev->die_tag == die->die_tag)
5789 if ((abbrev->die_child != NULL) == (die->die_child != NULL))
5791 a_attr = abbrev->die_attr;
5792 d_attr = die->die_attr;
5794 while (a_attr != NULL && d_attr != NULL)
5796 if ((a_attr->dw_attr != d_attr->dw_attr)
5797 || (value_format (a_attr) != value_format (d_attr)))
5798 break;
5800 a_attr = a_attr->dw_attr_next;
5801 d_attr = d_attr->dw_attr_next;
5804 if (a_attr == NULL && d_attr == NULL)
5805 break;
5810 if (abbrev_id >= abbrev_die_table_in_use)
5812 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
5814 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
5815 abbrev_die_table
5816 = (dw_die_ref *) xrealloc (abbrev_die_table,
5817 sizeof (dw_die_ref) * n_alloc);
5819 memset ((char *) &abbrev_die_table[abbrev_die_table_allocated], 0,
5820 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
5821 abbrev_die_table_allocated = n_alloc;
5824 ++abbrev_die_table_in_use;
5825 abbrev_die_table[abbrev_id] = die;
5828 die->die_abbrev = abbrev_id;
5829 for (c = die->die_child; c != NULL; c = c->die_sib)
5830 build_abbrev_table (c);
5833 /* Return the power-of-two number of bytes necessary to represent VALUE. */
5835 static int
5836 constant_size (value)
5837 long unsigned value;
5839 int log;
5841 if (value == 0)
5842 log = 0;
5843 else
5844 log = floor_log2 (value);
5846 log = log / 8;
5847 log = 1 << (floor_log2 (log) + 1);
5849 return log;
5852 /* Return the size of a DIE as it is represented in the
5853 .debug_info section. */
5855 static unsigned long
5856 size_of_die (die)
5857 dw_die_ref die;
5859 unsigned long size = 0;
5860 dw_attr_ref a;
5862 size += size_of_uleb128 (die->die_abbrev);
5863 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5865 switch (AT_class (a))
5867 case dw_val_class_addr:
5868 size += DWARF2_ADDR_SIZE;
5869 break;
5870 case dw_val_class_offset:
5871 size += DWARF_OFFSET_SIZE;
5872 break;
5873 case dw_val_class_loc:
5875 unsigned long lsize = size_of_locs (AT_loc (a));
5877 /* Block length. */
5878 size += constant_size (lsize);
5879 size += lsize;
5881 break;
5882 case dw_val_class_loc_list:
5883 size += DWARF_OFFSET_SIZE;
5884 break;
5885 case dw_val_class_range_list:
5886 size += DWARF_OFFSET_SIZE;
5887 break;
5888 case dw_val_class_const:
5889 size += size_of_sleb128 (AT_int (a));
5890 break;
5891 case dw_val_class_unsigned_const:
5892 size += constant_size (AT_unsigned (a));
5893 break;
5894 case dw_val_class_long_long:
5895 size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
5896 break;
5897 case dw_val_class_float:
5898 size += 1 + a->dw_attr_val.v.val_float.length * 4; /* block */
5899 break;
5900 case dw_val_class_flag:
5901 size += 1;
5902 break;
5903 case dw_val_class_die_ref:
5904 size += DWARF_OFFSET_SIZE;
5905 break;
5906 case dw_val_class_fde_ref:
5907 size += DWARF_OFFSET_SIZE;
5908 break;
5909 case dw_val_class_lbl_id:
5910 size += DWARF2_ADDR_SIZE;
5911 break;
5912 case dw_val_class_lbl_offset:
5913 size += DWARF_OFFSET_SIZE;
5914 break;
5915 case dw_val_class_str:
5916 if (AT_string_form (a) == DW_FORM_strp)
5917 size += DWARF_OFFSET_SIZE;
5918 else
5919 size += HT_LEN (&a->dw_attr_val.v.val_str->id) + 1;
5920 break;
5921 default:
5922 abort ();
5926 return size;
5929 /* Size the debugging information associated with a given DIE. Visits the
5930 DIE's children recursively. Updates the global variable next_die_offset, on
5931 each time through. Uses the current value of next_die_offset to update the
5932 die_offset field in each DIE. */
5934 static void
5935 calc_die_sizes (die)
5936 dw_die_ref die;
5938 dw_die_ref c;
5940 die->die_offset = next_die_offset;
5941 next_die_offset += size_of_die (die);
5943 for (c = die->die_child; c != NULL; c = c->die_sib)
5944 calc_die_sizes (c);
5946 if (die->die_child != NULL)
5947 /* Count the null byte used to terminate sibling lists. */
5948 next_die_offset += 1;
5951 /* Set the marks for a die and its children. We do this so
5952 that we know whether or not a reference needs to use FORM_ref_addr; only
5953 DIEs in the same CU will be marked. We used to clear out the offset
5954 and use that as the flag, but ran into ordering problems. */
5956 static void
5957 mark_dies (die)
5958 dw_die_ref die;
5960 dw_die_ref c;
5962 die->die_mark = 1;
5963 for (c = die->die_child; c; c = c->die_sib)
5964 mark_dies (c);
5967 /* Clear the marks for a die and its children. */
5969 static void
5970 unmark_dies (die)
5971 dw_die_ref die;
5973 dw_die_ref c;
5975 die->die_mark = 0;
5976 for (c = die->die_child; c; c = c->die_sib)
5977 unmark_dies (c);
5980 /* Return the size of the .debug_pubnames table generated for the
5981 compilation unit. */
5983 static unsigned long
5984 size_of_pubnames ()
5986 unsigned long size;
5987 unsigned i;
5989 size = DWARF_PUBNAMES_HEADER_SIZE;
5990 for (i = 0; i < pubname_table_in_use; i++)
5992 pubname_ref p = &pubname_table[i];
5993 size += DWARF_OFFSET_SIZE + strlen (p->name) + 1;
5996 size += DWARF_OFFSET_SIZE;
5997 return size;
6000 /* Return the size of the information in the .debug_aranges section. */
6002 static unsigned long
6003 size_of_aranges ()
6005 unsigned long size;
6007 size = DWARF_ARANGES_HEADER_SIZE;
6009 /* Count the address/length pair for this compilation unit. */
6010 size += 2 * DWARF2_ADDR_SIZE;
6011 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6013 /* Count the two zero words used to terminated the address range table. */
6014 size += 2 * DWARF2_ADDR_SIZE;
6015 return size;
6018 /* Select the encoding of an attribute value. */
6020 static enum dwarf_form
6021 value_format (a)
6022 dw_attr_ref a;
6024 switch (a->dw_attr_val.val_class)
6026 case dw_val_class_addr:
6027 return DW_FORM_addr;
6028 case dw_val_class_range_list:
6029 case dw_val_class_offset:
6030 if (DWARF_OFFSET_SIZE == 4)
6031 return DW_FORM_data4;
6032 if (DWARF_OFFSET_SIZE == 8)
6033 return DW_FORM_data8;
6034 abort ();
6035 case dw_val_class_loc_list:
6036 /* FIXME: Could be DW_FORM_data8, with a > 32 bit size
6037 .debug_loc section */
6038 return DW_FORM_data4;
6039 case dw_val_class_loc:
6040 switch (constant_size (size_of_locs (AT_loc (a))))
6042 case 1:
6043 return DW_FORM_block1;
6044 case 2:
6045 return DW_FORM_block2;
6046 default:
6047 abort ();
6049 case dw_val_class_const:
6050 return DW_FORM_sdata;
6051 case dw_val_class_unsigned_const:
6052 switch (constant_size (AT_unsigned (a)))
6054 case 1:
6055 return DW_FORM_data1;
6056 case 2:
6057 return DW_FORM_data2;
6058 case 4:
6059 return DW_FORM_data4;
6060 case 8:
6061 return DW_FORM_data8;
6062 default:
6063 abort ();
6065 case dw_val_class_long_long:
6066 return DW_FORM_block1;
6067 case dw_val_class_float:
6068 return DW_FORM_block1;
6069 case dw_val_class_flag:
6070 return DW_FORM_flag;
6071 case dw_val_class_die_ref:
6072 if (AT_ref_external (a))
6073 return DW_FORM_ref_addr;
6074 else
6075 return DW_FORM_ref;
6076 case dw_val_class_fde_ref:
6077 return DW_FORM_data;
6078 case dw_val_class_lbl_id:
6079 return DW_FORM_addr;
6080 case dw_val_class_lbl_offset:
6081 return DW_FORM_data;
6082 case dw_val_class_str:
6083 return AT_string_form (a);
6085 default:
6086 abort ();
6090 /* Output the encoding of an attribute value. */
6092 static void
6093 output_value_format (a)
6094 dw_attr_ref a;
6096 enum dwarf_form form = value_format (a);
6098 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6101 /* Output the .debug_abbrev section which defines the DIE abbreviation
6102 table. */
6104 static void
6105 output_abbrev_section ()
6107 unsigned long abbrev_id;
6109 dw_attr_ref a_attr;
6111 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6113 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6115 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6116 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6117 dwarf_tag_name (abbrev->die_tag));
6119 if (abbrev->die_child != NULL)
6120 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6121 else
6122 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6124 for (a_attr = abbrev->die_attr; a_attr != NULL;
6125 a_attr = a_attr->dw_attr_next)
6127 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6128 dwarf_attr_name (a_attr->dw_attr));
6129 output_value_format (a_attr);
6132 dw2_asm_output_data (1, 0, NULL);
6133 dw2_asm_output_data (1, 0, NULL);
6136 /* Terminate the table. */
6137 dw2_asm_output_data (1, 0, NULL);
6140 /* Output a symbol we can use to refer to this DIE from another CU. */
6142 static inline void
6143 output_die_symbol (die)
6144 dw_die_ref die;
6146 char *sym = die->die_symbol;
6148 if (sym == 0)
6149 return;
6151 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6152 /* We make these global, not weak; if the target doesn't support
6153 .linkonce, it doesn't support combining the sections, so debugging
6154 will break. */
6155 ASM_GLOBALIZE_LABEL (asm_out_file, sym);
6157 ASM_OUTPUT_LABEL (asm_out_file, sym);
6160 /* Return a new location list, given the begin and end range, and the
6161 expression. gensym tells us whether to generate a new internal symbol for
6162 this location list node, which is done for the head of the list only. */
6164 static inline dw_loc_list_ref
6165 new_loc_list (expr, begin, end, section, gensym)
6166 dw_loc_descr_ref expr;
6167 const char *begin;
6168 const char *end;
6169 const char *section;
6170 unsigned gensym;
6172 dw_loc_list_ref retlist
6173 = (dw_loc_list_ref) xcalloc (1, sizeof (dw_loc_list_node));
6175 retlist->begin = begin;
6176 retlist->end = end;
6177 retlist->expr = expr;
6178 retlist->section = section;
6179 if (gensym)
6180 retlist->ll_symbol = gen_internal_sym ("LLST");
6182 return retlist;
6185 /* Add a location description expression to a location list */
6187 static inline void
6188 add_loc_descr_to_loc_list (list_head, descr, begin, end, section)
6189 dw_loc_list_ref *list_head;
6190 dw_loc_descr_ref descr;
6191 const char *begin;
6192 const char *end;
6193 const char *section;
6195 dw_loc_list_ref *d;
6197 /* Find the end of the chain. */
6198 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
6201 /* Add a new location list node to the list */
6202 *d = new_loc_list (descr, begin, end, section, 0);
6205 /* Output the location list given to us */
6207 static void
6208 output_loc_list (list_head)
6209 dw_loc_list_ref list_head;
6211 dw_loc_list_ref curr = list_head;
6213 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
6215 /* ??? This shouldn't be needed now that we've forced the
6216 compilation unit base address to zero when there is code
6217 in more than one section. */
6218 if (strcmp (curr->section, ".text") == 0)
6220 /* dw2_asm_output_data will mask off any extra bits in the ~0. */
6221 dw2_asm_output_data (DWARF2_ADDR_SIZE, ~(unsigned HOST_WIDE_INT) 0,
6222 "Location list base address specifier fake entry");
6223 dw2_asm_output_offset (DWARF2_ADDR_SIZE, curr->section,
6224 "Location list base address specifier base");
6227 for (curr = list_head; curr != NULL; curr=curr->dw_loc_next)
6229 unsigned long size;
6231 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
6232 "Location list begin address (%s)",
6233 list_head->ll_symbol);
6234 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
6235 "Location list end address (%s)",
6236 list_head->ll_symbol);
6237 size = size_of_locs (curr->expr);
6239 /* Output the block length for this list of location operations. */
6240 if (size > 0xffff)
6241 abort ();
6242 dw2_asm_output_data (2, size, "%s", "Location expression size");
6244 output_loc_sequence (curr->expr);
6247 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0,
6248 "Location list terminator begin (%s)",
6249 list_head->ll_symbol);
6250 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0,
6251 "Location list terminator end (%s)",
6252 list_head->ll_symbol);
6255 /* Output the DIE and its attributes. Called recursively to generate
6256 the definitions of each child DIE. */
6258 static void
6259 output_die (die)
6260 dw_die_ref die;
6262 dw_attr_ref a;
6263 dw_die_ref c;
6264 unsigned long size;
6266 /* If someone in another CU might refer to us, set up a symbol for
6267 them to point to. */
6268 if (die->die_symbol)
6269 output_die_symbol (die);
6271 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
6272 die->die_offset, dwarf_tag_name (die->die_tag));
6274 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6276 const char *name = dwarf_attr_name (a->dw_attr);
6278 switch (AT_class (a))
6280 case dw_val_class_addr:
6281 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
6282 break;
6284 case dw_val_class_offset:
6285 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
6286 "%s", name);
6287 break;
6289 case dw_val_class_range_list:
6291 char *p = strchr (ranges_section_label, '\0');
6293 sprintf (p, "+0x%lx", a->dw_attr_val.v.val_offset);
6294 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
6295 "%s", name);
6296 *p = '\0';
6298 break;
6300 case dw_val_class_loc:
6301 size = size_of_locs (AT_loc (a));
6303 /* Output the block length for this list of location operations. */
6304 dw2_asm_output_data (constant_size (size), size, "%s", name);
6306 output_loc_sequence (AT_loc (a));
6307 break;
6309 case dw_val_class_const:
6310 /* ??? It would be slightly more efficient to use a scheme like is
6311 used for unsigned constants below, but gdb 4.x does not sign
6312 extend. Gdb 5.x does sign extend. */
6313 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
6314 break;
6316 case dw_val_class_unsigned_const:
6317 dw2_asm_output_data (constant_size (AT_unsigned (a)),
6318 AT_unsigned (a), "%s", name);
6319 break;
6321 case dw_val_class_long_long:
6323 unsigned HOST_WIDE_INT first, second;
6325 dw2_asm_output_data (1,
6326 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6327 "%s", name);
6329 if (WORDS_BIG_ENDIAN)
6331 first = a->dw_attr_val.v.val_long_long.hi;
6332 second = a->dw_attr_val.v.val_long_long.low;
6334 else
6336 first = a->dw_attr_val.v.val_long_long.low;
6337 second = a->dw_attr_val.v.val_long_long.hi;
6340 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6341 first, "long long constant");
6342 dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6343 second, NULL);
6345 break;
6347 case dw_val_class_float:
6349 unsigned int i;
6351 dw2_asm_output_data (1, a->dw_attr_val.v.val_float.length * 4,
6352 "%s", name);
6354 for (i = 0; i < a->dw_attr_val.v.val_float.length; i++)
6355 dw2_asm_output_data (4, a->dw_attr_val.v.val_float.array[i],
6356 "fp constant word %u", i);
6357 break;
6360 case dw_val_class_flag:
6361 dw2_asm_output_data (1, AT_flag (a), "%s", name);
6362 break;
6364 case dw_val_class_loc_list:
6366 char *sym = AT_loc_list (a)->ll_symbol;
6368 if (sym == 0)
6369 abort ();
6370 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym,
6371 loc_section_label, "%s", name);
6373 break;
6375 case dw_val_class_die_ref:
6376 if (AT_ref_external (a))
6378 char *sym = AT_ref (a)->die_symbol;
6380 if (sym == 0)
6381 abort ();
6382 dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, "%s", name);
6384 else if (AT_ref (a)->die_offset == 0)
6385 abort ();
6386 else
6387 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
6388 "%s", name);
6389 break;
6391 case dw_val_class_fde_ref:
6393 char l1[20];
6395 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
6396 a->dw_attr_val.v.val_fde_index * 2);
6397 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, "%s", name);
6399 break;
6401 case dw_val_class_lbl_id:
6402 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
6403 break;
6405 case dw_val_class_lbl_offset:
6406 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a), "%s", name);
6407 break;
6409 case dw_val_class_str:
6410 if (AT_string_form (a) == DW_FORM_strp)
6411 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
6412 a->dw_attr_val.v.val_str->label,
6413 "%s: \"%s\"", name, AT_string (a));
6414 else
6415 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
6416 break;
6418 default:
6419 abort ();
6423 for (c = die->die_child; c != NULL; c = c->die_sib)
6424 output_die (c);
6426 /* Add null byte to terminate sibling list. */
6427 if (die->die_child != NULL)
6428 dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
6429 die->die_offset);
6432 /* Output the compilation unit that appears at the beginning of the
6433 .debug_info section, and precedes the DIE descriptions. */
6435 static void
6436 output_compilation_unit_header ()
6438 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset - DWARF_OFFSET_SIZE,
6439 "Length of Compilation Unit Info");
6440 dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
6441 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
6442 "Offset Into Abbrev. Section");
6443 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
6446 /* Output the compilation unit DIE and its children. */
6448 static void
6449 output_comp_unit (die)
6450 dw_die_ref die;
6452 const char *secname;
6454 /* Even if there are no children of this DIE, we must output the information
6455 about the compilation unit. Otherwise, on an empty translation unit, we
6456 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
6457 will then complain when examining the file. First mark all the DIEs in
6458 this CU so we know which get local refs. */
6459 mark_dies (die);
6461 build_abbrev_table (die);
6463 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
6464 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
6465 calc_die_sizes (die);
6467 if (die->die_symbol)
6469 char *tmp = (char *) alloca (strlen (die->die_symbol) + 24);
6471 sprintf (tmp, ".gnu.linkonce.wi.%s", die->die_symbol);
6472 secname = tmp;
6473 die->die_symbol = NULL;
6475 else
6476 secname = (const char *) DEBUG_INFO_SECTION;
6478 /* Output debugging information. */
6479 named_section_flags (secname, SECTION_DEBUG);
6480 output_compilation_unit_header ();
6481 output_die (die);
6483 /* Leave the marks on the main CU, so we can check them in
6484 output_pubnames. */
6485 if (die->die_symbol)
6486 unmark_dies (die);
6489 /* The DWARF2 pubname for a nested thingy looks like "A::f". The output
6490 of decl_printable_name for C++ looks like "A::f(int)". Let's drop the
6491 argument list, and maybe the scope. */
6493 static const char *
6494 dwarf2_name (decl, scope)
6495 tree decl;
6496 int scope;
6498 return (*decl_printable_name) (decl, scope ? 1 : 0);
6501 /* Add a new entry to .debug_pubnames if appropriate. */
6503 static void
6504 add_pubname (decl, die)
6505 tree decl;
6506 dw_die_ref die;
6508 pubname_ref p;
6510 if (! TREE_PUBLIC (decl))
6511 return;
6513 if (pubname_table_in_use == pubname_table_allocated)
6515 pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
6516 pubname_table
6517 = (pubname_ref) xrealloc (pubname_table,
6518 (pubname_table_allocated
6519 * sizeof (pubname_entry)));
6522 p = &pubname_table[pubname_table_in_use++];
6523 p->die = die;
6524 p->name = xstrdup (dwarf2_name (decl, 1));
6527 /* Output the public names table used to speed up access to externally
6528 visible names. For now, only generate entries for externally
6529 visible procedures. */
6531 static void
6532 output_pubnames ()
6534 unsigned i;
6535 unsigned long pubnames_length = size_of_pubnames ();
6537 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
6538 "Length of Public Names Info");
6539 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
6540 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
6541 "Offset of Compilation Unit Info");
6542 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
6543 "Compilation Unit Length");
6545 for (i = 0; i < pubname_table_in_use; i++)
6547 pubname_ref pub = &pubname_table[i];
6549 /* We shouldn't see pubnames for DIEs outside of the main CU. */
6550 if (pub->die->die_mark == 0)
6551 abort ();
6553 dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
6554 "DIE offset");
6556 dw2_asm_output_nstring (pub->name, -1, "external name");
6559 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
6562 /* Add a new entry to .debug_aranges if appropriate. */
6564 static void
6565 add_arange (decl, die)
6566 tree decl;
6567 dw_die_ref die;
6569 if (! DECL_SECTION_NAME (decl))
6570 return;
6572 if (arange_table_in_use == arange_table_allocated)
6574 arange_table_allocated += ARANGE_TABLE_INCREMENT;
6575 arange_table = (dw_die_ref *)
6576 xrealloc (arange_table, arange_table_allocated * sizeof (dw_die_ref));
6579 arange_table[arange_table_in_use++] = die;
6582 /* Output the information that goes into the .debug_aranges table.
6583 Namely, define the beginning and ending address range of the
6584 text section generated for this compilation unit. */
6586 static void
6587 output_aranges ()
6589 unsigned i;
6590 unsigned long aranges_length = size_of_aranges ();
6592 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
6593 "Length of Address Ranges Info");
6594 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
6595 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
6596 "Offset of Compilation Unit Info");
6597 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
6598 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
6600 /* We need to align to twice the pointer size here. */
6601 if (DWARF_ARANGES_PAD_SIZE)
6603 /* Pad using a 2 byte words so that padding is correct for any
6604 pointer size. */
6605 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
6606 2 * DWARF2_ADDR_SIZE);
6607 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
6608 dw2_asm_output_data (2, 0, NULL);
6611 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
6612 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
6613 text_section_label, "Length");
6615 for (i = 0; i < arange_table_in_use; i++)
6617 dw_die_ref die = arange_table[i];
6619 /* We shouldn't see aranges for DIEs outside of the main CU. */
6620 if (die->die_mark == 0)
6621 abort ();
6623 if (die->die_tag == DW_TAG_subprogram)
6625 dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
6626 "Address");
6627 dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
6628 get_AT_low_pc (die), "Length");
6630 else
6632 /* A static variable; extract the symbol from DW_AT_location.
6633 Note that this code isn't currently hit, as we only emit
6634 aranges for functions (jason 9/23/99). */
6635 dw_attr_ref a = get_AT (die, DW_AT_location);
6636 dw_loc_descr_ref loc;
6638 if (! a || AT_class (a) != dw_val_class_loc)
6639 abort ();
6641 loc = AT_loc (a);
6642 if (loc->dw_loc_opc != DW_OP_addr)
6643 abort ();
6645 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
6646 loc->dw_loc_oprnd1.v.val_addr, "Address");
6647 dw2_asm_output_data (DWARF2_ADDR_SIZE,
6648 get_AT_unsigned (die, DW_AT_byte_size),
6649 "Length");
6653 /* Output the terminator words. */
6654 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
6655 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
6658 /* Add a new entry to .debug_ranges. Return the offset at which it
6659 was placed. */
6661 static unsigned int
6662 add_ranges (block)
6663 tree block;
6665 unsigned int in_use = ranges_table_in_use;
6667 if (in_use == ranges_table_allocated)
6669 ranges_table_allocated += RANGES_TABLE_INCREMENT;
6670 ranges_table = (dw_ranges_ref)
6671 xrealloc (ranges_table, (ranges_table_allocated
6672 * sizeof (struct dw_ranges_struct)));
6675 ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
6676 ranges_table_in_use = in_use + 1;
6678 return in_use * 2 * DWARF2_ADDR_SIZE;
6681 static void
6682 output_ranges ()
6684 unsigned i;
6685 static const char *const start_fmt = "Offset 0x%x";
6686 const char *fmt = start_fmt;
6688 for (i = 0; i < ranges_table_in_use; i++)
6690 int block_num = ranges_table[i].block_num;
6692 if (block_num)
6694 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
6695 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
6697 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
6698 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
6700 /* If all code is in the text section, then the compilation
6701 unit base address defaults to DW_AT_low_pc, which is the
6702 base of the text section. */
6703 if (separate_line_info_table_in_use == 0)
6705 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
6706 text_section_label,
6707 fmt, i * 2 * DWARF2_ADDR_SIZE);
6708 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
6709 text_section_label, NULL);
6712 /* Otherwise, we add a DW_AT_entry_pc attribute to force the
6713 compilation unit base address to zero, which allows us to
6714 use absolute addresses, and not worry about whether the
6715 target supports cross-section arithmetic. */
6716 else
6718 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
6719 fmt, i * 2 * DWARF2_ADDR_SIZE);
6720 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
6723 fmt = NULL;
6725 else
6727 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
6728 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
6729 fmt = start_fmt;
6734 /* Data structure containing information about input files. */
6735 struct file_info
6737 char *path; /* Complete file name. */
6738 char *fname; /* File name part. */
6739 int length; /* Length of entire string. */
6740 int file_idx; /* Index in input file table. */
6741 int dir_idx; /* Index in directory table. */
6744 /* Data structure containing information about directories with source
6745 files. */
6746 struct dir_info
6748 char *path; /* Path including directory name. */
6749 int length; /* Path length. */
6750 int prefix; /* Index of directory entry which is a prefix. */
6751 int count; /* Number of files in this directory. */
6752 int dir_idx; /* Index of directory used as base. */
6753 int used; /* Used in the end? */
6756 /* Callback function for file_info comparison. We sort by looking at
6757 the directories in the path. */
6759 static int
6760 file_info_cmp (p1, p2)
6761 const void *p1;
6762 const void *p2;
6764 const struct file_info *s1 = p1;
6765 const struct file_info *s2 = p2;
6766 unsigned char *cp1;
6767 unsigned char *cp2;
6769 /* Take care of file names without directories. We need to make sure that
6770 we return consistent values to qsort since some will get confused if
6771 we return the same value when identical operands are passed in opposite
6772 orders. So if neither has a directory, return 0 and otherwise return
6773 1 or -1 depending on which one has the directory. */
6774 if ((s1->path == s1->fname || s2->path == s2->fname))
6775 return (s2->path == s2->fname) - (s1->path == s1->fname);
6777 cp1 = (unsigned char *) s1->path;
6778 cp2 = (unsigned char *) s2->path;
6780 while (1)
6782 ++cp1;
6783 ++cp2;
6784 /* Reached the end of the first path? If so, handle like above. */
6785 if ((cp1 == (unsigned char *) s1->fname)
6786 || (cp2 == (unsigned char *) s2->fname))
6787 return ((cp2 == (unsigned char *) s2->fname)
6788 - (cp1 == (unsigned char *) s1->fname));
6790 /* Character of current path component the same? */
6791 else if (*cp1 != *cp2)
6792 return *cp1 - *cp2;
6796 /* Output the directory table and the file name table. We try to minimize
6797 the total amount of memory needed. A heuristic is used to avoid large
6798 slowdowns with many input files. */
6800 static void
6801 output_file_names ()
6803 struct file_info *files;
6804 struct dir_info *dirs;
6805 int *saved;
6806 int *savehere;
6807 int *backmap;
6808 int ndirs;
6809 int idx_offset;
6810 int i;
6811 int idx;
6813 /* Allocate the various arrays we need. */
6814 files = (struct file_info *) alloca (file_table.in_use
6815 * sizeof (struct file_info));
6816 dirs = (struct dir_info *) alloca (file_table.in_use
6817 * sizeof (struct dir_info));
6819 /* Sort the file names. */
6820 for (i = 1; i < (int) file_table.in_use; i++)
6822 char *f;
6824 /* Skip all leading "./". */
6825 f = file_table.table[i];
6826 while (f[0] == '.' && f[1] == '/')
6827 f += 2;
6829 /* Create a new array entry. */
6830 files[i].path = f;
6831 files[i].length = strlen (f);
6832 files[i].file_idx = i;
6834 /* Search for the file name part. */
6835 f = strrchr (f, '/');
6836 files[i].fname = f == NULL ? files[i].path : f + 1;
6839 qsort (files + 1, file_table.in_use - 1, sizeof (files[0]), file_info_cmp);
6841 /* Find all the different directories used. */
6842 dirs[0].path = files[1].path;
6843 dirs[0].length = files[1].fname - files[1].path;
6844 dirs[0].prefix = -1;
6845 dirs[0].count = 1;
6846 dirs[0].dir_idx = 0;
6847 dirs[0].used = 0;
6848 files[1].dir_idx = 0;
6849 ndirs = 1;
6851 for (i = 2; i < (int) file_table.in_use; i++)
6852 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
6853 && memcmp (dirs[ndirs - 1].path, files[i].path,
6854 dirs[ndirs - 1].length) == 0)
6856 /* Same directory as last entry. */
6857 files[i].dir_idx = ndirs - 1;
6858 ++dirs[ndirs - 1].count;
6860 else
6862 int j;
6864 /* This is a new directory. */
6865 dirs[ndirs].path = files[i].path;
6866 dirs[ndirs].length = files[i].fname - files[i].path;
6867 dirs[ndirs].count = 1;
6868 dirs[ndirs].dir_idx = ndirs;
6869 dirs[ndirs].used = 0;
6870 files[i].dir_idx = ndirs;
6872 /* Search for a prefix. */
6873 dirs[ndirs].prefix = -1;
6874 for (j = 0; j < ndirs; j++)
6875 if (dirs[j].length < dirs[ndirs].length
6876 && dirs[j].length > 1
6877 && (dirs[ndirs].prefix == -1
6878 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
6879 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
6880 dirs[ndirs].prefix = j;
6882 ++ndirs;
6885 /* Now to the actual work. We have to find a subset of the directories which
6886 allow expressing the file name using references to the directory table
6887 with the least amount of characters. We do not do an exhaustive search
6888 where we would have to check out every combination of every single
6889 possible prefix. Instead we use a heuristic which provides nearly optimal
6890 results in most cases and never is much off. */
6891 saved = (int *) alloca (ndirs * sizeof (int));
6892 savehere = (int *) alloca (ndirs * sizeof (int));
6894 memset (saved, '\0', ndirs * sizeof (saved[0]));
6895 for (i = 0; i < ndirs; i++)
6897 int j;
6898 int total;
6900 /* We can always save some space for the current directory. But this
6901 does not mean it will be enough to justify adding the directory. */
6902 savehere[i] = dirs[i].length;
6903 total = (savehere[i] - saved[i]) * dirs[i].count;
6905 for (j = i + 1; j < ndirs; j++)
6907 savehere[j] = 0;
6908 if (saved[j] < dirs[i].length)
6910 /* Determine whether the dirs[i] path is a prefix of the
6911 dirs[j] path. */
6912 int k;
6914 k = dirs[j].prefix;
6915 while (k != -1 && k != i)
6916 k = dirs[k].prefix;
6918 if (k == i)
6920 /* Yes it is. We can possibly safe some memory but
6921 writing the filenames in dirs[j] relative to
6922 dirs[i]. */
6923 savehere[j] = dirs[i].length;
6924 total += (savehere[j] - saved[j]) * dirs[j].count;
6929 /* Check whether we can safe enough to justify adding the dirs[i]
6930 directory. */
6931 if (total > dirs[i].length + 1)
6933 /* It's worthwhile adding. */
6934 for (j = i; j < ndirs; j++)
6935 if (savehere[j] > 0)
6937 /* Remember how much we saved for this directory so far. */
6938 saved[j] = savehere[j];
6940 /* Remember the prefix directory. */
6941 dirs[j].dir_idx = i;
6946 /* We have to emit them in the order they appear in the file_table array
6947 since the index is used in the debug info generation. To do this
6948 efficiently we generate a back-mapping of the indices first. */
6949 backmap = (int *) alloca (file_table.in_use * sizeof (int));
6950 for (i = 1; i < (int) file_table.in_use; i++)
6952 backmap[files[i].file_idx] = i;
6954 /* Mark this directory as used. */
6955 dirs[dirs[files[i].dir_idx].dir_idx].used = 1;
6958 /* That was it. We are ready to emit the information. First emit the
6959 directory name table. We have to make sure the first actually emitted
6960 directory name has index one; zero is reserved for the current working
6961 directory. Make sure we do not confuse these indices with the one for the
6962 constructed table (even though most of the time they are identical). */
6963 idx = 1;
6964 idx_offset = dirs[0].length > 0 ? 1 : 0;
6965 for (i = 1 - idx_offset; i < ndirs; i++)
6966 if (dirs[i].used != 0)
6968 dirs[i].used = idx++;
6969 dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
6970 "Directory Entry: 0x%x", dirs[i].used);
6973 dw2_asm_output_data (1, 0, "End directory table");
6975 /* Correct the index for the current working directory entry if it
6976 exists. */
6977 if (idx_offset == 0)
6978 dirs[0].used = 0;
6980 /* Now write all the file names. */
6981 for (i = 1; i < (int) file_table.in_use; i++)
6983 int file_idx = backmap[i];
6984 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
6986 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
6987 "File Entry: 0x%x", i);
6989 /* Include directory index. */
6990 dw2_asm_output_data_uleb128 (dirs[dir_idx].used, NULL);
6992 /* Modification time. */
6993 dw2_asm_output_data_uleb128 (0, NULL);
6995 /* File length in bytes. */
6996 dw2_asm_output_data_uleb128 (0, NULL);
6999 dw2_asm_output_data (1, 0, "End file name table");
7003 /* Output the source line number correspondence information. This
7004 information goes into the .debug_line section. */
7006 static void
7007 output_line_info ()
7009 char l1[20], l2[20], p1[20], p2[20];
7010 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7011 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7012 unsigned opc;
7013 unsigned n_op_args;
7014 unsigned long lt_index;
7015 unsigned long current_line;
7016 long line_offset;
7017 long line_delta;
7018 unsigned long current_file;
7019 unsigned long function;
7021 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7022 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7023 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7024 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7026 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7027 "Length of Source Line Info");
7028 ASM_OUTPUT_LABEL (asm_out_file, l1);
7030 dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7031 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7032 ASM_OUTPUT_LABEL (asm_out_file, p1);
7034 dw2_asm_output_data (1, DWARF_LINE_MIN_INSTR_LENGTH,
7035 "Minimum Instruction Length");
7036 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7037 "Default is_stmt_start flag");
7038 dw2_asm_output_data (1, DWARF_LINE_BASE,
7039 "Line Base Value (Special Opcodes)");
7040 dw2_asm_output_data (1, DWARF_LINE_RANGE,
7041 "Line Range Value (Special Opcodes)");
7042 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7043 "Special Opcode Base");
7045 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7047 switch (opc)
7049 case DW_LNS_advance_pc:
7050 case DW_LNS_advance_line:
7051 case DW_LNS_set_file:
7052 case DW_LNS_set_column:
7053 case DW_LNS_fixed_advance_pc:
7054 n_op_args = 1;
7055 break;
7056 default:
7057 n_op_args = 0;
7058 break;
7061 dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
7062 opc, n_op_args);
7065 /* Write out the information about the files we use. */
7066 output_file_names ();
7067 ASM_OUTPUT_LABEL (asm_out_file, p2);
7069 /* We used to set the address register to the first location in the text
7070 section here, but that didn't accomplish anything since we already
7071 have a line note for the opening brace of the first function. */
7073 /* Generate the line number to PC correspondence table, encoded as
7074 a series of state machine operations. */
7075 current_file = 1;
7076 current_line = 1;
7077 strcpy (prev_line_label, text_section_label);
7078 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
7080 dw_line_info_ref line_info = &line_info_table[lt_index];
7082 #if 0
7083 /* Disable this optimization for now; GDB wants to see two line notes
7084 at the beginning of a function so it can find the end of the
7085 prologue. */
7087 /* Don't emit anything for redundant notes. Just updating the
7088 address doesn't accomplish anything, because we already assume
7089 that anything after the last address is this line. */
7090 if (line_info->dw_line_num == current_line
7091 && line_info->dw_file_num == current_file)
7092 continue;
7093 #endif
7095 /* Emit debug info for the address of the current line.
7097 Unfortunately, we have little choice here currently, and must always
7098 use the most general form. GCC does not know the address delta
7099 itself, so we can't use DW_LNS_advance_pc. Many ports do have length
7100 attributes which will give an upper bound on the address range. We
7101 could perhaps use length attributes to determine when it is safe to
7102 use DW_LNS_fixed_advance_pc. */
7104 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
7105 if (0)
7107 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
7108 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7109 "DW_LNS_fixed_advance_pc");
7110 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7112 else
7114 /* This can handle any delta. This takes
7115 4+DWARF2_ADDR_SIZE bytes. */
7116 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7117 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7118 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7119 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7122 strcpy (prev_line_label, line_label);
7124 /* Emit debug info for the source file of the current line, if
7125 different from the previous line. */
7126 if (line_info->dw_file_num != current_file)
7128 current_file = line_info->dw_file_num;
7129 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7130 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7131 file_table.table[current_file]);
7134 /* Emit debug info for the current line number, choosing the encoding
7135 that uses the least amount of space. */
7136 if (line_info->dw_line_num != current_line)
7138 line_offset = line_info->dw_line_num - current_line;
7139 line_delta = line_offset - DWARF_LINE_BASE;
7140 current_line = line_info->dw_line_num;
7141 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7142 /* This can handle deltas from -10 to 234, using the current
7143 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
7144 takes 1 byte. */
7145 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7146 "line %lu", current_line);
7147 else
7149 /* This can handle any delta. This takes at least 4 bytes,
7150 depending on the value being encoded. */
7151 dw2_asm_output_data (1, DW_LNS_advance_line,
7152 "advance to line %lu", current_line);
7153 dw2_asm_output_data_sleb128 (line_offset, NULL);
7154 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7157 else
7158 /* We still need to start a new row, so output a copy insn. */
7159 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7162 /* Emit debug info for the address of the end of the function. */
7163 if (0)
7165 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7166 "DW_LNS_fixed_advance_pc");
7167 dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
7169 else
7171 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7172 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7173 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7174 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
7177 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7178 dw2_asm_output_data_uleb128 (1, NULL);
7179 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7181 function = 0;
7182 current_file = 1;
7183 current_line = 1;
7184 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
7186 dw_separate_line_info_ref line_info
7187 = &separate_line_info_table[lt_index];
7189 #if 0
7190 /* Don't emit anything for redundant notes. */
7191 if (line_info->dw_line_num == current_line
7192 && line_info->dw_file_num == current_file
7193 && line_info->function == function)
7194 goto cont;
7195 #endif
7197 /* Emit debug info for the address of the current line. If this is
7198 a new function, or the first line of a function, then we need
7199 to handle it differently. */
7200 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
7201 lt_index);
7202 if (function != line_info->function)
7204 function = line_info->function;
7206 /* Set the address register to the first line in the function */
7207 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7208 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7209 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7210 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7212 else
7214 /* ??? See the DW_LNS_advance_pc comment above. */
7215 if (0)
7217 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7218 "DW_LNS_fixed_advance_pc");
7219 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7221 else
7223 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7224 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7225 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7226 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7230 strcpy (prev_line_label, line_label);
7232 /* Emit debug info for the source file of the current line, if
7233 different from the previous line. */
7234 if (line_info->dw_file_num != current_file)
7236 current_file = line_info->dw_file_num;
7237 dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7238 dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7239 file_table.table[current_file]);
7242 /* Emit debug info for the current line number, choosing the encoding
7243 that uses the least amount of space. */
7244 if (line_info->dw_line_num != current_line)
7246 line_offset = line_info->dw_line_num - current_line;
7247 line_delta = line_offset - DWARF_LINE_BASE;
7248 current_line = line_info->dw_line_num;
7249 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7250 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7251 "line %lu", current_line);
7252 else
7254 dw2_asm_output_data (1, DW_LNS_advance_line,
7255 "advance to line %lu", current_line);
7256 dw2_asm_output_data_sleb128 (line_offset, NULL);
7257 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7260 else
7261 dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7263 #if 0
7264 cont:
7265 #endif
7267 lt_index++;
7269 /* If we're done with a function, end its sequence. */
7270 if (lt_index == separate_line_info_table_in_use
7271 || separate_line_info_table[lt_index].function != function)
7273 current_file = 1;
7274 current_line = 1;
7276 /* Emit debug info for the address of the end of the function. */
7277 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
7278 if (0)
7280 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7281 "DW_LNS_fixed_advance_pc");
7282 dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7284 else
7286 dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7287 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7288 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7289 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7292 /* Output the marker for the end of this sequence. */
7293 dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7294 dw2_asm_output_data_uleb128 (1, NULL);
7295 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7299 /* Output the marker for the end of the line number info. */
7300 ASM_OUTPUT_LABEL (asm_out_file, l2);
7303 /* Given a pointer to a tree node for some base type, return a pointer to
7304 a DIE that describes the given type.
7306 This routine must only be called for GCC type nodes that correspond to
7307 Dwarf base (fundamental) types. */
7309 static dw_die_ref
7310 base_type_die (type)
7311 tree type;
7313 dw_die_ref base_type_result;
7314 const char *type_name;
7315 enum dwarf_type encoding;
7316 tree name = TYPE_NAME (type);
7318 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
7319 return 0;
7321 if (name)
7323 if (TREE_CODE (name) == TYPE_DECL)
7324 name = DECL_NAME (name);
7326 type_name = IDENTIFIER_POINTER (name);
7328 else
7329 type_name = "__unknown__";
7331 switch (TREE_CODE (type))
7333 case INTEGER_TYPE:
7334 /* Carefully distinguish the C character types, without messing
7335 up if the language is not C. Note that we check only for the names
7336 that contain spaces; other names might occur by coincidence in other
7337 languages. */
7338 if (! (TYPE_PRECISION (type) == CHAR_TYPE_SIZE
7339 && (type == char_type_node
7340 || ! strcmp (type_name, "signed char")
7341 || ! strcmp (type_name, "unsigned char"))))
7343 if (TREE_UNSIGNED (type))
7344 encoding = DW_ATE_unsigned;
7345 else
7346 encoding = DW_ATE_signed;
7347 break;
7349 /* else fall through. */
7351 case CHAR_TYPE:
7352 /* GNU Pascal/Ada CHAR type. Not used in C. */
7353 if (TREE_UNSIGNED (type))
7354 encoding = DW_ATE_unsigned_char;
7355 else
7356 encoding = DW_ATE_signed_char;
7357 break;
7359 case REAL_TYPE:
7360 encoding = DW_ATE_float;
7361 break;
7363 /* Dwarf2 doesn't know anything about complex ints, so use
7364 a user defined type for it. */
7365 case COMPLEX_TYPE:
7366 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
7367 encoding = DW_ATE_complex_float;
7368 else
7369 encoding = DW_ATE_lo_user;
7370 break;
7372 case BOOLEAN_TYPE:
7373 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
7374 encoding = DW_ATE_boolean;
7375 break;
7377 default:
7378 /* No other TREE_CODEs are Dwarf fundamental types. */
7379 abort ();
7382 base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
7383 if (demangle_name_func)
7384 type_name = (*demangle_name_func) (type_name);
7386 add_AT_string (base_type_result, DW_AT_name, type_name);
7387 add_AT_unsigned (base_type_result, DW_AT_byte_size,
7388 int_size_in_bytes (type));
7389 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
7391 return base_type_result;
7394 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
7395 the Dwarf "root" type for the given input type. The Dwarf "root" type of
7396 a given type is generally the same as the given type, except that if the
7397 given type is a pointer or reference type, then the root type of the given
7398 type is the root type of the "basis" type for the pointer or reference
7399 type. (This definition of the "root" type is recursive.) Also, the root
7400 type of a `const' qualified type or a `volatile' qualified type is the
7401 root type of the given type without the qualifiers. */
7403 static tree
7404 root_type (type)
7405 tree type;
7407 if (TREE_CODE (type) == ERROR_MARK)
7408 return error_mark_node;
7410 switch (TREE_CODE (type))
7412 case ERROR_MARK:
7413 return error_mark_node;
7415 case POINTER_TYPE:
7416 case REFERENCE_TYPE:
7417 return type_main_variant (root_type (TREE_TYPE (type)));
7419 default:
7420 return type_main_variant (type);
7424 /* Given a pointer to an arbitrary ..._TYPE tree node, return non-zero if the
7425 given input type is a Dwarf "fundamental" type. Otherwise return null. */
7427 static inline int
7428 is_base_type (type)
7429 tree type;
7431 switch (TREE_CODE (type))
7433 case ERROR_MARK:
7434 case VOID_TYPE:
7435 case INTEGER_TYPE:
7436 case REAL_TYPE:
7437 case COMPLEX_TYPE:
7438 case BOOLEAN_TYPE:
7439 case CHAR_TYPE:
7440 return 1;
7442 case SET_TYPE:
7443 case ARRAY_TYPE:
7444 case RECORD_TYPE:
7445 case UNION_TYPE:
7446 case QUAL_UNION_TYPE:
7447 case ENUMERAL_TYPE:
7448 case FUNCTION_TYPE:
7449 case METHOD_TYPE:
7450 case POINTER_TYPE:
7451 case REFERENCE_TYPE:
7452 case FILE_TYPE:
7453 case OFFSET_TYPE:
7454 case LANG_TYPE:
7455 case VECTOR_TYPE:
7456 return 0;
7458 default:
7459 abort ();
7462 return 0;
7465 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
7466 entry that chains various modifiers in front of the given type. */
7468 static dw_die_ref
7469 modified_type_die (type, is_const_type, is_volatile_type, context_die)
7470 tree type;
7471 int is_const_type;
7472 int is_volatile_type;
7473 dw_die_ref context_die;
7475 enum tree_code code = TREE_CODE (type);
7476 dw_die_ref mod_type_die = NULL;
7477 dw_die_ref sub_die = NULL;
7478 tree item_type = NULL;
7480 if (code != ERROR_MARK)
7482 tree qualified_type;
7484 /* See if we already have the appropriately qualified variant of
7485 this type. */
7486 qualified_type
7487 = get_qualified_type (type,
7488 ((is_const_type ? TYPE_QUAL_CONST : 0)
7489 | (is_volatile_type
7490 ? TYPE_QUAL_VOLATILE : 0)));
7492 /* If we do, then we can just use its DIE, if it exists. */
7493 if (qualified_type)
7495 mod_type_die = lookup_type_die (qualified_type);
7496 if (mod_type_die)
7497 return mod_type_die;
7500 /* Handle C typedef types. */
7501 if (qualified_type && TYPE_NAME (qualified_type)
7502 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL
7503 && DECL_ORIGINAL_TYPE (TYPE_NAME (qualified_type)))
7505 tree type_name = TYPE_NAME (qualified_type);
7506 tree dtype = TREE_TYPE (type_name);
7508 if (qualified_type == dtype)
7510 /* For a named type, use the typedef. */
7511 gen_type_die (qualified_type, context_die);
7512 mod_type_die = lookup_type_die (qualified_type);
7514 else if (is_const_type < TYPE_READONLY (dtype)
7515 || is_volatile_type < TYPE_VOLATILE (dtype))
7516 /* cv-unqualified version of named type. Just use the unnamed
7517 type to which it refers. */
7518 mod_type_die
7519 = modified_type_die (DECL_ORIGINAL_TYPE (type_name),
7520 is_const_type, is_volatile_type,
7521 context_die);
7523 /* Else cv-qualified version of named type; fall through. */
7526 if (mod_type_die)
7527 /* OK. */
7529 else if (is_const_type)
7531 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
7532 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
7534 else if (is_volatile_type)
7536 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
7537 sub_die = modified_type_die (type, 0, 0, context_die);
7539 else if (code == POINTER_TYPE)
7541 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
7542 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
7543 #if 0
7544 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
7545 #endif
7546 item_type = TREE_TYPE (type);
7548 else if (code == REFERENCE_TYPE)
7550 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
7551 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
7552 #if 0
7553 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
7554 #endif
7555 item_type = TREE_TYPE (type);
7557 else if (is_base_type (type))
7558 mod_type_die = base_type_die (type);
7559 else
7561 gen_type_die (type, context_die);
7563 /* We have to get the type_main_variant here (and pass that to the
7564 `lookup_type_die' routine) because the ..._TYPE node we have
7565 might simply be a *copy* of some original type node (where the
7566 copy was created to help us keep track of typedef names) and
7567 that copy might have a different TYPE_UID from the original
7568 ..._TYPE node. */
7569 mod_type_die = lookup_type_die (type_main_variant (type));
7570 if (mod_type_die == NULL)
7571 abort ();
7574 /* We want to equate the qualified type to the die below. */
7575 if (qualified_type)
7576 type = qualified_type;
7579 equate_type_number_to_die (type, mod_type_die);
7580 if (item_type)
7581 /* We must do this after the equate_type_number_to_die call, in case
7582 this is a recursive type. This ensures that the modified_type_die
7583 recursion will terminate even if the type is recursive. Recursive
7584 types are possible in Ada. */
7585 sub_die = modified_type_die (item_type,
7586 TYPE_READONLY (item_type),
7587 TYPE_VOLATILE (item_type),
7588 context_die);
7590 if (sub_die != NULL)
7591 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
7593 return mod_type_die;
7596 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
7597 an enumerated type. */
7599 static inline int
7600 type_is_enum (type)
7601 tree type;
7603 return TREE_CODE (type) == ENUMERAL_TYPE;
7606 /* Return the register number described by a given RTL node. */
7608 static unsigned int
7609 reg_number (rtl)
7610 rtx rtl;
7612 unsigned regno = REGNO (rtl);
7614 if (regno >= FIRST_PSEUDO_REGISTER)
7615 abort ();
7617 return DBX_REGISTER_NUMBER (regno);
7620 /* Return a location descriptor that designates a machine register or
7621 zero if there is no such. */
7623 static dw_loc_descr_ref
7624 reg_loc_descriptor (rtl)
7625 rtx rtl;
7627 dw_loc_descr_ref loc_result = NULL;
7628 unsigned reg;
7630 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
7631 return 0;
7633 reg = reg_number (rtl);
7634 if (reg <= 31)
7635 loc_result = new_loc_descr (DW_OP_reg0 + reg, 0, 0);
7636 else
7637 loc_result = new_loc_descr (DW_OP_regx, reg, 0);
7639 return loc_result;
7642 /* Return a location descriptor that designates a constant. */
7644 static dw_loc_descr_ref
7645 int_loc_descriptor (i)
7646 HOST_WIDE_INT i;
7648 enum dwarf_location_atom op;
7650 /* Pick the smallest representation of a constant, rather than just
7651 defaulting to the LEB encoding. */
7652 if (i >= 0)
7654 if (i <= 31)
7655 op = DW_OP_lit0 + i;
7656 else if (i <= 0xff)
7657 op = DW_OP_const1u;
7658 else if (i <= 0xffff)
7659 op = DW_OP_const2u;
7660 else if (HOST_BITS_PER_WIDE_INT == 32
7661 || i <= 0xffffffff)
7662 op = DW_OP_const4u;
7663 else
7664 op = DW_OP_constu;
7666 else
7668 if (i >= -0x80)
7669 op = DW_OP_const1s;
7670 else if (i >= -0x8000)
7671 op = DW_OP_const2s;
7672 else if (HOST_BITS_PER_WIDE_INT == 32
7673 || i >= -0x80000000)
7674 op = DW_OP_const4s;
7675 else
7676 op = DW_OP_consts;
7679 return new_loc_descr (op, i, 0);
7682 /* Return a location descriptor that designates a base+offset location. */
7684 static dw_loc_descr_ref
7685 based_loc_descr (reg, offset)
7686 unsigned reg;
7687 long int offset;
7689 dw_loc_descr_ref loc_result;
7690 /* For the "frame base", we use the frame pointer or stack pointer
7691 registers, since the RTL for local variables is relative to one of
7692 them. */
7693 unsigned fp_reg = DBX_REGISTER_NUMBER (frame_pointer_needed
7694 ? HARD_FRAME_POINTER_REGNUM
7695 : STACK_POINTER_REGNUM);
7697 if (reg == fp_reg)
7698 loc_result = new_loc_descr (DW_OP_fbreg, offset, 0);
7699 else if (reg <= 31)
7700 loc_result = new_loc_descr (DW_OP_breg0 + reg, offset, 0);
7701 else
7702 loc_result = new_loc_descr (DW_OP_bregx, reg, offset);
7704 return loc_result;
7707 /* Return true if this RTL expression describes a base+offset calculation. */
7709 static inline int
7710 is_based_loc (rtl)
7711 rtx rtl;
7713 return (GET_CODE (rtl) == PLUS
7714 && ((GET_CODE (XEXP (rtl, 0)) == REG
7715 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
7716 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
7719 /* The following routine converts the RTL for a variable or parameter
7720 (resident in memory) into an equivalent Dwarf representation of a
7721 mechanism for getting the address of that same variable onto the top of a
7722 hypothetical "address evaluation" stack.
7724 When creating memory location descriptors, we are effectively transforming
7725 the RTL for a memory-resident object into its Dwarf postfix expression
7726 equivalent. This routine recursively descends an RTL tree, turning
7727 it into Dwarf postfix code as it goes.
7729 MODE is the mode of the memory reference, needed to handle some
7730 autoincrement addressing modes.
7732 Return 0 if we can't represent the location. */
7734 static dw_loc_descr_ref
7735 mem_loc_descriptor (rtl, mode)
7736 rtx rtl;
7737 enum machine_mode mode;
7739 dw_loc_descr_ref mem_loc_result = NULL;
7741 /* Note that for a dynamically sized array, the location we will generate a
7742 description of here will be the lowest numbered location which is
7743 actually within the array. That's *not* necessarily the same as the
7744 zeroth element of the array. */
7746 #ifdef ASM_SIMPLIFY_DWARF_ADDR
7747 rtl = ASM_SIMPLIFY_DWARF_ADDR (rtl);
7748 #endif
7750 switch (GET_CODE (rtl))
7752 case POST_INC:
7753 case POST_DEC:
7754 case POST_MODIFY:
7755 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
7756 just fall into the SUBREG code. */
7758 /* ... fall through ... */
7760 case SUBREG:
7761 /* The case of a subreg may arise when we have a local (register)
7762 variable or a formal (register) parameter which doesn't quite fill
7763 up an entire register. For now, just assume that it is
7764 legitimate to make the Dwarf info refer to the whole register which
7765 contains the given subreg. */
7766 rtl = SUBREG_REG (rtl);
7768 /* ... fall through ... */
7770 case REG:
7771 /* Whenever a register number forms a part of the description of the
7772 method for calculating the (dynamic) address of a memory resident
7773 object, DWARF rules require the register number be referred to as
7774 a "base register". This distinction is not based in any way upon
7775 what category of register the hardware believes the given register
7776 belongs to. This is strictly DWARF terminology we're dealing with
7777 here. Note that in cases where the location of a memory-resident
7778 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
7779 OP_CONST (0)) the actual DWARF location descriptor that we generate
7780 may just be OP_BASEREG (basereg). This may look deceptively like
7781 the object in question was allocated to a register (rather than in
7782 memory) so DWARF consumers need to be aware of the subtle
7783 distinction between OP_REG and OP_BASEREG. */
7784 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
7785 mem_loc_result = based_loc_descr (reg_number (rtl), 0);
7786 break;
7788 case MEM:
7789 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
7790 if (mem_loc_result != 0)
7791 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
7792 break;
7794 case LABEL_REF:
7795 /* Some ports can transform a symbol ref into a label ref, because
7796 the symbol ref is too far away and has to be dumped into a constant
7797 pool. */
7798 case CONST:
7799 case SYMBOL_REF:
7800 /* Alternatively, the symbol in the constant pool might be referenced
7801 by a different symbol. */
7802 if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
7804 rtx tmp = get_pool_constant (rtl);
7806 if (GET_CODE (tmp) == SYMBOL_REF)
7807 rtl = tmp;
7810 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
7811 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
7812 mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
7813 VARRAY_PUSH_RTX (used_rtx_varray, rtl);
7814 break;
7816 case PRE_MODIFY:
7817 /* Extract the PLUS expression nested inside and fall into
7818 PLUS code below. */
7819 rtl = XEXP (rtl, 1);
7820 goto plus;
7822 case PRE_INC:
7823 case PRE_DEC:
7824 /* Turn these into a PLUS expression and fall into the PLUS code
7825 below. */
7826 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
7827 GEN_INT (GET_CODE (rtl) == PRE_INC
7828 ? GET_MODE_UNIT_SIZE (mode)
7829 : -GET_MODE_UNIT_SIZE (mode)));
7831 /* ... fall through ... */
7833 case PLUS:
7834 plus:
7835 if (is_based_loc (rtl))
7836 mem_loc_result = based_loc_descr (reg_number (XEXP (rtl, 0)),
7837 INTVAL (XEXP (rtl, 1)));
7838 else
7840 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
7841 if (mem_loc_result == 0)
7842 break;
7844 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
7845 && INTVAL (XEXP (rtl, 1)) >= 0)
7846 add_loc_descr (&mem_loc_result,
7847 new_loc_descr (DW_OP_plus_uconst,
7848 INTVAL (XEXP (rtl, 1)), 0));
7849 else
7851 add_loc_descr (&mem_loc_result,
7852 mem_loc_descriptor (XEXP (rtl, 1), mode));
7853 add_loc_descr (&mem_loc_result,
7854 new_loc_descr (DW_OP_plus, 0, 0));
7857 break;
7859 case MULT:
7861 /* If a pseudo-reg is optimized away, it is possible for it to
7862 be replaced with a MEM containing a multiply. */
7863 dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode);
7864 dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode);
7866 if (op0 == 0 || op1 == 0)
7867 break;
7869 mem_loc_result = op0;
7870 add_loc_descr (&mem_loc_result, op1);
7871 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
7872 break;
7875 case CONST_INT:
7876 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
7877 break;
7879 case ADDRESSOF:
7880 /* If this is a MEM, return its address. Otherwise, we can't
7881 represent this. */
7882 if (GET_CODE (XEXP (rtl, 0)) == MEM)
7883 return mem_loc_descriptor (XEXP (XEXP (rtl, 0), 0), mode);
7884 else
7885 return 0;
7887 default:
7888 abort ();
7891 return mem_loc_result;
7894 /* Return a descriptor that describes the concatenation of two locations.
7895 This is typically a complex variable. */
7897 static dw_loc_descr_ref
7898 concat_loc_descriptor (x0, x1)
7899 rtx x0, x1;
7901 dw_loc_descr_ref cc_loc_result = NULL;
7902 dw_loc_descr_ref x0_ref = loc_descriptor (x0);
7903 dw_loc_descr_ref x1_ref = loc_descriptor (x1);
7905 if (x0_ref == 0 || x1_ref == 0)
7906 return 0;
7908 cc_loc_result = x0_ref;
7909 add_loc_descr (&cc_loc_result,
7910 new_loc_descr (DW_OP_piece,
7911 GET_MODE_SIZE (GET_MODE (x0)), 0));
7913 add_loc_descr (&cc_loc_result, x1_ref);
7914 add_loc_descr (&cc_loc_result,
7915 new_loc_descr (DW_OP_piece,
7916 GET_MODE_SIZE (GET_MODE (x1)), 0));
7918 return cc_loc_result;
7921 /* Output a proper Dwarf location descriptor for a variable or parameter
7922 which is either allocated in a register or in a memory location. For a
7923 register, we just generate an OP_REG and the register number. For a
7924 memory location we provide a Dwarf postfix expression describing how to
7925 generate the (dynamic) address of the object onto the address stack.
7927 If we don't know how to describe it, return 0. */
7929 static dw_loc_descr_ref
7930 loc_descriptor (rtl)
7931 rtx rtl;
7933 dw_loc_descr_ref loc_result = NULL;
7935 switch (GET_CODE (rtl))
7937 case SUBREG:
7938 /* The case of a subreg may arise when we have a local (register)
7939 variable or a formal (register) parameter which doesn't quite fill
7940 up an entire register. For now, just assume that it is
7941 legitimate to make the Dwarf info refer to the whole register which
7942 contains the given subreg. */
7943 rtl = SUBREG_REG (rtl);
7945 /* ... fall through ... */
7947 case REG:
7948 loc_result = reg_loc_descriptor (rtl);
7949 break;
7951 case MEM:
7952 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
7953 break;
7955 case CONCAT:
7956 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
7957 break;
7959 default:
7960 abort ();
7963 return loc_result;
7966 /* Similar, but generate the descriptor from trees instead of rtl. This comes
7967 up particularly with variable length arrays. If ADDRESSP is nonzero, we are
7968 looking for an address. Otherwise, we return a value. If we can't make a
7969 descriptor, return 0. */
7971 static dw_loc_descr_ref
7972 loc_descriptor_from_tree (loc, addressp)
7973 tree loc;
7974 int addressp;
7976 dw_loc_descr_ref ret, ret1;
7977 int indirect_p = 0;
7978 int unsignedp = TREE_UNSIGNED (TREE_TYPE (loc));
7979 enum dwarf_location_atom op;
7981 /* ??? Most of the time we do not take proper care for sign/zero
7982 extending the values properly. Hopefully this won't be a real
7983 problem... */
7985 switch (TREE_CODE (loc))
7987 case ERROR_MARK:
7988 return 0;
7990 case WITH_RECORD_EXPR:
7991 case PLACEHOLDER_EXPR:
7992 /* This case involves extracting fields from an object to determine the
7993 position of other fields. We don't try to encode this here. The
7994 only user of this is Ada, which encodes the needed information using
7995 the names of types. */
7996 return 0;
7998 case CALL_EXPR:
7999 return 0;
8001 case ADDR_EXPR:
8002 /* We can support this only if we can look through conversions and
8003 find an INDIRECT_EXPR. */
8004 for (loc = TREE_OPERAND (loc, 0);
8005 TREE_CODE (loc) == CONVERT_EXPR || TREE_CODE (loc) == NOP_EXPR
8006 || TREE_CODE (loc) == NON_LVALUE_EXPR
8007 || TREE_CODE (loc) == VIEW_CONVERT_EXPR
8008 || TREE_CODE (loc) == SAVE_EXPR;
8009 loc = TREE_OPERAND (loc, 0))
8012 return (TREE_CODE (loc) == INDIRECT_REF
8013 ? loc_descriptor_from_tree (TREE_OPERAND (loc, 0), addressp)
8014 : 0);
8016 case VAR_DECL:
8017 case PARM_DECL:
8019 rtx rtl = rtl_for_decl_location (loc);
8021 if (rtl == NULL_RTX)
8022 return 0;
8023 else if (CONSTANT_P (rtl))
8025 ret = new_loc_descr (DW_OP_addr, 0, 0);
8026 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8027 ret->dw_loc_oprnd1.v.val_addr = rtl;
8028 indirect_p = 1;
8030 else
8032 enum machine_mode mode = GET_MODE (rtl);
8034 if (GET_CODE (rtl) == MEM)
8036 indirect_p = 1;
8037 rtl = XEXP (rtl, 0);
8040 ret = mem_loc_descriptor (rtl, mode);
8043 break;
8045 case INDIRECT_REF:
8046 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8047 indirect_p = 1;
8048 break;
8050 case COMPOUND_EXPR:
8051 return loc_descriptor_from_tree (TREE_OPERAND (loc, 1), addressp);
8053 case NOP_EXPR:
8054 case CONVERT_EXPR:
8055 case NON_LVALUE_EXPR:
8056 case VIEW_CONVERT_EXPR:
8057 case SAVE_EXPR:
8058 return loc_descriptor_from_tree (TREE_OPERAND (loc, 0), addressp);
8060 case COMPONENT_REF:
8061 case BIT_FIELD_REF:
8062 case ARRAY_REF:
8063 case ARRAY_RANGE_REF:
8065 tree obj, offset;
8066 HOST_WIDE_INT bitsize, bitpos, bytepos;
8067 enum machine_mode mode;
8068 int volatilep;
8070 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
8071 &unsignedp, &volatilep);
8073 if (obj == loc)
8074 return 0;
8076 ret = loc_descriptor_from_tree (obj, 1);
8077 if (ret == 0
8078 || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
8079 return 0;
8081 if (offset != NULL_TREE)
8083 /* Variable offset. */
8084 add_loc_descr (&ret, loc_descriptor_from_tree (offset, 0));
8085 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
8088 if (!addressp)
8089 indirect_p = 1;
8091 bytepos = bitpos / BITS_PER_UNIT;
8092 if (bytepos > 0)
8093 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
8094 else if (bytepos < 0)
8096 add_loc_descr (&ret, int_loc_descriptor (bytepos));
8097 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
8099 break;
8102 case INTEGER_CST:
8103 if (host_integerp (loc, 0))
8104 ret = int_loc_descriptor (tree_low_cst (loc, 0));
8105 else
8106 return 0;
8107 break;
8109 case TRUTH_AND_EXPR:
8110 case TRUTH_ANDIF_EXPR:
8111 case BIT_AND_EXPR:
8112 op = DW_OP_and;
8113 goto do_binop;
8115 case TRUTH_XOR_EXPR:
8116 case BIT_XOR_EXPR:
8117 op = DW_OP_xor;
8118 goto do_binop;
8120 case TRUTH_OR_EXPR:
8121 case TRUTH_ORIF_EXPR:
8122 case BIT_IOR_EXPR:
8123 op = DW_OP_or;
8124 goto do_binop;
8126 case TRUNC_DIV_EXPR:
8127 op = DW_OP_div;
8128 goto do_binop;
8130 case MINUS_EXPR:
8131 op = DW_OP_minus;
8132 goto do_binop;
8134 case TRUNC_MOD_EXPR:
8135 op = DW_OP_mod;
8136 goto do_binop;
8138 case MULT_EXPR:
8139 op = DW_OP_mul;
8140 goto do_binop;
8142 case LSHIFT_EXPR:
8143 op = DW_OP_shl;
8144 goto do_binop;
8146 case RSHIFT_EXPR:
8147 op = (unsignedp ? DW_OP_shr : DW_OP_shra);
8148 goto do_binop;
8150 case PLUS_EXPR:
8151 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
8152 && host_integerp (TREE_OPERAND (loc, 1), 0))
8154 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8155 if (ret == 0)
8156 return 0;
8158 add_loc_descr (&ret,
8159 new_loc_descr (DW_OP_plus_uconst,
8160 tree_low_cst (TREE_OPERAND (loc, 1),
8162 0));
8163 break;
8166 op = DW_OP_plus;
8167 goto do_binop;
8169 case LE_EXPR:
8170 if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
8171 return 0;
8173 op = DW_OP_le;
8174 goto do_binop;
8176 case GE_EXPR:
8177 if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
8178 return 0;
8180 op = DW_OP_ge;
8181 goto do_binop;
8183 case LT_EXPR:
8184 if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
8185 return 0;
8187 op = DW_OP_lt;
8188 goto do_binop;
8190 case GT_EXPR:
8191 if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
8192 return 0;
8194 op = DW_OP_gt;
8195 goto do_binop;
8197 case EQ_EXPR:
8198 op = DW_OP_eq;
8199 goto do_binop;
8201 case NE_EXPR:
8202 op = DW_OP_ne;
8203 goto do_binop;
8205 do_binop:
8206 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8207 ret1 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
8208 if (ret == 0 || ret1 == 0)
8209 return 0;
8211 add_loc_descr (&ret, ret1);
8212 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
8213 break;
8215 case TRUTH_NOT_EXPR:
8216 case BIT_NOT_EXPR:
8217 op = DW_OP_not;
8218 goto do_unop;
8220 case ABS_EXPR:
8221 op = DW_OP_abs;
8222 goto do_unop;
8224 case NEGATE_EXPR:
8225 op = DW_OP_neg;
8226 goto do_unop;
8228 do_unop:
8229 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8230 if (ret == 0)
8231 return 0;
8233 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
8234 break;
8236 case MAX_EXPR:
8237 loc = build (COND_EXPR, TREE_TYPE (loc),
8238 build (LT_EXPR, integer_type_node,
8239 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
8240 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
8242 /* ... fall through ... */
8244 case COND_EXPR:
8246 dw_loc_descr_ref lhs
8247 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
8248 dw_loc_descr_ref rhs
8249 = loc_descriptor_from_tree (TREE_OPERAND (loc, 2), 0);
8250 dw_loc_descr_ref bra_node, jump_node, tmp;
8252 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8253 if (ret == 0 || lhs == 0 || rhs == 0)
8254 return 0;
8256 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
8257 add_loc_descr (&ret, bra_node);
8259 add_loc_descr (&ret, rhs);
8260 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
8261 add_loc_descr (&ret, jump_node);
8263 add_loc_descr (&ret, lhs);
8264 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
8265 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
8267 /* ??? Need a node to point the skip at. Use a nop. */
8268 tmp = new_loc_descr (DW_OP_nop, 0, 0);
8269 add_loc_descr (&ret, tmp);
8270 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
8271 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
8273 break;
8275 default:
8276 abort ();
8279 /* Show if we can't fill the request for an address. */
8280 if (addressp && indirect_p == 0)
8281 return 0;
8283 /* If we've got an address and don't want one, dereference. */
8284 if (!addressp && indirect_p > 0)
8286 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
8288 if (size > DWARF2_ADDR_SIZE || size == -1)
8289 return 0;
8290 else if (size == DWARF2_ADDR_SIZE)
8291 op = DW_OP_deref;
8292 else
8293 op = DW_OP_deref_size;
8295 add_loc_descr (&ret, new_loc_descr (op, size, 0));
8298 return ret;
8301 /* Given a value, round it up to the lowest multiple of `boundary'
8302 which is not less than the value itself. */
8304 static inline HOST_WIDE_INT
8305 ceiling (value, boundary)
8306 HOST_WIDE_INT value;
8307 unsigned int boundary;
8309 return (((value + boundary - 1) / boundary) * boundary);
8312 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
8313 pointer to the declared type for the relevant field variable, or return
8314 `integer_type_node' if the given node turns out to be an
8315 ERROR_MARK node. */
8317 static inline tree
8318 field_type (decl)
8319 tree decl;
8321 tree type;
8323 if (TREE_CODE (decl) == ERROR_MARK)
8324 return integer_type_node;
8326 type = DECL_BIT_FIELD_TYPE (decl);
8327 if (type == NULL_TREE)
8328 type = TREE_TYPE (decl);
8330 return type;
8333 /* Given a pointer to a tree node, return the alignment in bits for
8334 it, or else return BITS_PER_WORD if the node actually turns out to
8335 be an ERROR_MARK node. */
8337 static inline unsigned
8338 simple_type_align_in_bits (type)
8339 tree type;
8341 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
8344 static inline unsigned
8345 simple_decl_align_in_bits (decl)
8346 tree decl;
8348 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
8351 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8352 node, return the size in bits for the type if it is a constant, or else
8353 return the alignment for the type if the type's size is not constant, or
8354 else return BITS_PER_WORD if the type actually turns out to be an
8355 ERROR_MARK node. */
8357 static inline unsigned HOST_WIDE_INT
8358 simple_type_size_in_bits (type)
8359 tree type;
8362 if (TREE_CODE (type) == ERROR_MARK)
8363 return BITS_PER_WORD;
8364 else if (TYPE_SIZE (type) == NULL_TREE)
8365 return 0;
8366 else if (host_integerp (TYPE_SIZE (type), 1))
8367 return tree_low_cst (TYPE_SIZE (type), 1);
8368 else
8369 return TYPE_ALIGN (type);
8372 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
8373 lowest addressed byte of the "containing object" for the given FIELD_DECL,
8374 or return 0 if we are unable to determine what that offset is, either
8375 because the argument turns out to be a pointer to an ERROR_MARK node, or
8376 because the offset is actually variable. (We can't handle the latter case
8377 just yet). */
8379 static HOST_WIDE_INT
8380 field_byte_offset (decl)
8381 tree decl;
8383 unsigned int type_align_in_bits;
8384 unsigned int decl_align_in_bits;
8385 unsigned HOST_WIDE_INT type_size_in_bits;
8386 HOST_WIDE_INT object_offset_in_bits;
8387 tree type;
8388 tree field_size_tree;
8389 HOST_WIDE_INT bitpos_int;
8390 HOST_WIDE_INT deepest_bitpos;
8391 unsigned HOST_WIDE_INT field_size_in_bits;
8393 if (TREE_CODE (decl) == ERROR_MARK)
8394 return 0;
8395 else if (TREE_CODE (decl) != FIELD_DECL)
8396 abort ();
8398 type = field_type (decl);
8399 field_size_tree = DECL_SIZE (decl);
8401 /* The size could be unspecified if there was an error, or for
8402 a flexible array member. */
8403 if (! field_size_tree)
8404 field_size_tree = bitsize_zero_node;
8406 /* We cannot yet cope with fields whose positions are variable, so
8407 for now, when we see such things, we simply return 0. Someday, we may
8408 be able to handle such cases, but it will be damn difficult. */
8409 if (! host_integerp (bit_position (decl), 0))
8410 return 0;
8412 bitpos_int = int_bit_position (decl);
8414 /* If we don't know the size of the field, pretend it's a full word. */
8415 if (host_integerp (field_size_tree, 1))
8416 field_size_in_bits = tree_low_cst (field_size_tree, 1);
8417 else
8418 field_size_in_bits = BITS_PER_WORD;
8420 type_size_in_bits = simple_type_size_in_bits (type);
8421 type_align_in_bits = simple_type_align_in_bits (type);
8422 decl_align_in_bits = simple_decl_align_in_bits (decl);
8424 /* The GCC front-end doesn't make any attempt to keep track of the starting
8425 bit offset (relative to the start of the containing structure type) of the
8426 hypothetical "containing object" for a bit-field. Thus, when computing
8427 the byte offset value for the start of the "containing object" of a
8428 bit-field, we must deduce this information on our own. This can be rather
8429 tricky to do in some cases. For example, handling the following structure
8430 type definition when compiling for an i386/i486 target (which only aligns
8431 long long's to 32-bit boundaries) can be very tricky:
8433 struct S { int field1; long long field2:31; };
8435 Fortunately, there is a simple rule-of-thumb which can be used in such
8436 cases. When compiling for an i386/i486, GCC will allocate 8 bytes for the
8437 structure shown above. It decides to do this based upon one simple rule
8438 for bit-field allocation. GCC allocates each "containing object" for each
8439 bit-field at the first (i.e. lowest addressed) legitimate alignment
8440 boundary (based upon the required minimum alignment for the declared type
8441 of the field) which it can possibly use, subject to the condition that
8442 there is still enough available space remaining in the containing object
8443 (when allocated at the selected point) to fully accommodate all of the
8444 bits of the bit-field itself.
8446 This simple rule makes it obvious why GCC allocates 8 bytes for each
8447 object of the structure type shown above. When looking for a place to
8448 allocate the "containing object" for `field2', the compiler simply tries
8449 to allocate a 64-bit "containing object" at each successive 32-bit
8450 boundary (starting at zero) until it finds a place to allocate that 64-
8451 bit field such that at least 31 contiguous (and previously unallocated)
8452 bits remain within that selected 64 bit field. (As it turns out, for the
8453 example above, the compiler finds it is OK to allocate the "containing
8454 object" 64-bit field at bit-offset zero within the structure type.)
8456 Here we attempt to work backwards from the limited set of facts we're
8457 given, and we try to deduce from those facts, where GCC must have believed
8458 that the containing object started (within the structure type). The value
8459 we deduce is then used (by the callers of this routine) to generate
8460 DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
8461 and, in the case of DW_AT_location, regular fields as well). */
8463 /* Figure out the bit-distance from the start of the structure to the
8464 "deepest" bit of the bit-field. */
8465 deepest_bitpos = bitpos_int + field_size_in_bits;
8467 /* This is the tricky part. Use some fancy footwork to deduce where the
8468 lowest addressed bit of the containing object must be. */
8469 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
8471 /* Round up to type_align by default. This works best for bitfields. */
8472 object_offset_in_bits += type_align_in_bits - 1;
8473 object_offset_in_bits /= type_align_in_bits;
8474 object_offset_in_bits *= type_align_in_bits;
8476 if (object_offset_in_bits > bitpos_int)
8478 /* Sigh, the decl must be packed. */
8479 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
8481 /* Round up to decl_align instead. */
8482 object_offset_in_bits += decl_align_in_bits - 1;
8483 object_offset_in_bits /= decl_align_in_bits;
8484 object_offset_in_bits *= decl_align_in_bits;
8487 return object_offset_in_bits / BITS_PER_UNIT;
8490 /* The following routines define various Dwarf attributes and any data
8491 associated with them. */
8493 /* Add a location description attribute value to a DIE.
8495 This emits location attributes suitable for whole variables and
8496 whole parameters. Note that the location attributes for struct fields are
8497 generated by the routine `data_member_location_attribute' below. */
8499 static void
8500 add_AT_location_description (die, attr_kind, rtl)
8501 dw_die_ref die;
8502 enum dwarf_attribute attr_kind;
8503 rtx rtl;
8505 dw_loc_descr_ref descr = loc_descriptor (rtl);
8507 if (descr != 0)
8508 add_AT_loc (die, attr_kind, descr);
8511 /* Attach the specialized form of location attribute used for data members of
8512 struct and union types. In the special case of a FIELD_DECL node which
8513 represents a bit-field, the "offset" part of this special location
8514 descriptor must indicate the distance in bytes from the lowest-addressed
8515 byte of the containing struct or union type to the lowest-addressed byte of
8516 the "containing object" for the bit-field. (See the `field_byte_offset'
8517 function above).
8519 For any given bit-field, the "containing object" is a hypothetical object
8520 (of some integral or enum type) within which the given bit-field lives. The
8521 type of this hypothetical "containing object" is always the same as the
8522 declared type of the individual bit-field itself (for GCC anyway... the
8523 DWARF spec doesn't actually mandate this). Note that it is the size (in
8524 bytes) of the hypothetical "containing object" which will be given in the
8525 DW_AT_byte_size attribute for this bit-field. (See the
8526 `byte_size_attribute' function below.) It is also used when calculating the
8527 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
8528 function below.) */
8530 static void
8531 add_data_member_location_attribute (die, decl)
8532 dw_die_ref die;
8533 tree decl;
8535 long offset;
8536 dw_loc_descr_ref loc_descr = 0;
8538 if (TREE_CODE (decl) == TREE_VEC)
8540 /* We're working on the TAG_inheritance for a base class. */
8541 if (TREE_VIA_VIRTUAL (decl) && is_cxx ())
8543 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
8544 aren't at a fixed offset from all (sub)objects of the same
8545 type. We need to extract the appropriate offset from our
8546 vtable. The following dwarf expression means
8548 BaseAddr = ObAddr + *((*ObAddr) - Offset)
8550 This is specific to the V3 ABI, of course. */
8552 dw_loc_descr_ref tmp;
8554 /* Make a copy of the object address. */
8555 tmp = new_loc_descr (DW_OP_dup, 0, 0);
8556 add_loc_descr (&loc_descr, tmp);
8558 /* Extract the vtable address. */
8559 tmp = new_loc_descr (DW_OP_deref, 0, 0);
8560 add_loc_descr (&loc_descr, tmp);
8562 /* Calculate the address of the offset. */
8563 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
8564 if (offset >= 0)
8565 abort ();
8567 tmp = int_loc_descriptor (-offset);
8568 add_loc_descr (&loc_descr, tmp);
8569 tmp = new_loc_descr (DW_OP_minus, 0, 0);
8570 add_loc_descr (&loc_descr, tmp);
8572 /* Extract the offset. */
8573 tmp = new_loc_descr (DW_OP_deref, 0, 0);
8574 add_loc_descr (&loc_descr, tmp);
8576 /* Add it to the object address. */
8577 tmp = new_loc_descr (DW_OP_plus, 0, 0);
8578 add_loc_descr (&loc_descr, tmp);
8580 else
8581 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
8583 else
8584 offset = field_byte_offset (decl);
8586 if (! loc_descr)
8588 enum dwarf_location_atom op;
8590 /* The DWARF2 standard says that we should assume that the structure
8591 address is already on the stack, so we can specify a structure field
8592 address by using DW_OP_plus_uconst. */
8594 #ifdef MIPS_DEBUGGING_INFO
8595 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
8596 operator correctly. It works only if we leave the offset on the
8597 stack. */
8598 op = DW_OP_constu;
8599 #else
8600 op = DW_OP_plus_uconst;
8601 #endif
8603 loc_descr = new_loc_descr (op, offset, 0);
8606 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
8609 /* Attach an DW_AT_const_value attribute for a variable or a parameter which
8610 does not have a "location" either in memory or in a register. These
8611 things can arise in GNU C when a constant is passed as an actual parameter
8612 to an inlined function. They can also arise in C++ where declared
8613 constants do not necessarily get memory "homes". */
8615 static void
8616 add_const_value_attribute (die, rtl)
8617 dw_die_ref die;
8618 rtx rtl;
8620 switch (GET_CODE (rtl))
8622 case CONST_INT:
8623 /* Note that a CONST_INT rtx could represent either an integer
8624 or a floating-point constant. A CONST_INT is used whenever
8625 the constant will fit into a single word. In all such
8626 cases, the original mode of the constant value is wiped
8627 out, and the CONST_INT rtx is assigned VOIDmode. */
8629 HOST_WIDE_INT val = INTVAL (rtl);
8631 /* ??? We really should be using HOST_WIDE_INT throughout. */
8632 if (val < 0 && (long) val == val)
8633 add_AT_int (die, DW_AT_const_value, (long) val);
8634 else if ((unsigned long) val == (unsigned HOST_WIDE_INT) val)
8635 add_AT_unsigned (die, DW_AT_const_value, (unsigned long) val);
8636 else
8638 #if HOST_BITS_PER_LONG * 2 == HOST_BITS_PER_WIDE_INT
8639 add_AT_long_long (die, DW_AT_const_value,
8640 val >> HOST_BITS_PER_LONG, val);
8641 #else
8642 abort ();
8643 #endif
8646 break;
8648 case CONST_DOUBLE:
8649 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
8650 floating-point constant. A CONST_DOUBLE is used whenever the
8651 constant requires more than one word in order to be adequately
8652 represented. We output CONST_DOUBLEs as blocks. */
8654 enum machine_mode mode = GET_MODE (rtl);
8656 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
8658 unsigned length = GET_MODE_SIZE (mode) / 4;
8659 long *array = (long *) xmalloc (sizeof (long) * length);
8660 REAL_VALUE_TYPE rv;
8662 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
8663 switch (mode)
8665 case SFmode:
8666 REAL_VALUE_TO_TARGET_SINGLE (rv, array[0]);
8667 break;
8669 case DFmode:
8670 REAL_VALUE_TO_TARGET_DOUBLE (rv, array);
8671 break;
8673 case XFmode:
8674 case TFmode:
8675 REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, array);
8676 break;
8678 default:
8679 abort ();
8682 add_AT_float (die, DW_AT_const_value, length, array);
8684 else
8686 /* ??? We really should be using HOST_WIDE_INT throughout. */
8687 if (HOST_BITS_PER_LONG != HOST_BITS_PER_WIDE_INT)
8688 abort ();
8690 add_AT_long_long (die, DW_AT_const_value,
8691 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
8694 break;
8696 case CONST_STRING:
8697 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
8698 break;
8700 case SYMBOL_REF:
8701 case LABEL_REF:
8702 case CONST:
8703 add_AT_addr (die, DW_AT_const_value, rtl);
8704 VARRAY_PUSH_RTX (used_rtx_varray, rtl);
8705 break;
8707 case PLUS:
8708 /* In cases where an inlined instance of an inline function is passed
8709 the address of an `auto' variable (which is local to the caller) we
8710 can get a situation where the DECL_RTL of the artificial local
8711 variable (for the inlining) which acts as a stand-in for the
8712 corresponding formal parameter (of the inline function) will look
8713 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
8714 exactly a compile-time constant expression, but it isn't the address
8715 of the (artificial) local variable either. Rather, it represents the
8716 *value* which the artificial local variable always has during its
8717 lifetime. We currently have no way to represent such quasi-constant
8718 values in Dwarf, so for now we just punt and generate nothing. */
8719 break;
8721 default:
8722 /* No other kinds of rtx should be possible here. */
8723 abort ();
8728 static rtx
8729 rtl_for_decl_location (decl)
8730 tree decl;
8732 rtx rtl;
8734 /* Here we have to decide where we are going to say the parameter "lives"
8735 (as far as the debugger is concerned). We only have a couple of
8736 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
8738 DECL_RTL normally indicates where the parameter lives during most of the
8739 activation of the function. If optimization is enabled however, this
8740 could be either NULL or else a pseudo-reg. Both of those cases indicate
8741 that the parameter doesn't really live anywhere (as far as the code
8742 generation parts of GCC are concerned) during most of the function's
8743 activation. That will happen (for example) if the parameter is never
8744 referenced within the function.
8746 We could just generate a location descriptor here for all non-NULL
8747 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
8748 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
8749 where DECL_RTL is NULL or is a pseudo-reg.
8751 Note however that we can only get away with using DECL_INCOMING_RTL as
8752 a backup substitute for DECL_RTL in certain limited cases. In cases
8753 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
8754 we can be sure that the parameter was passed using the same type as it is
8755 declared to have within the function, and that its DECL_INCOMING_RTL
8756 points us to a place where a value of that type is passed.
8758 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
8759 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
8760 because in these cases DECL_INCOMING_RTL points us to a value of some
8761 type which is *different* from the type of the parameter itself. Thus,
8762 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
8763 such cases, the debugger would end up (for example) trying to fetch a
8764 `float' from a place which actually contains the first part of a
8765 `double'. That would lead to really incorrect and confusing
8766 output at debug-time.
8768 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
8769 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
8770 are a couple of exceptions however. On little-endian machines we can
8771 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
8772 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
8773 an integral type that is smaller than TREE_TYPE (decl). These cases arise
8774 when (on a little-endian machine) a non-prototyped function has a
8775 parameter declared to be of type `short' or `char'. In such cases,
8776 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
8777 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
8778 passed `int' value. If the debugger then uses that address to fetch
8779 a `short' or a `char' (on a little-endian machine) the result will be
8780 the correct data, so we allow for such exceptional cases below.
8782 Note that our goal here is to describe the place where the given formal
8783 parameter lives during most of the function's activation (i.e. between the
8784 end of the prologue and the start of the epilogue). We'll do that as best
8785 as we can. Note however that if the given formal parameter is modified
8786 sometime during the execution of the function, then a stack backtrace (at
8787 debug-time) will show the function as having been called with the *new*
8788 value rather than the value which was originally passed in. This happens
8789 rarely enough that it is not a major problem, but it *is* a problem, and
8790 I'd like to fix it.
8792 A future version of dwarf2out.c may generate two additional attributes for
8793 any given DW_TAG_formal_parameter DIE which will describe the "passed
8794 type" and the "passed location" for the given formal parameter in addition
8795 to the attributes we now generate to indicate the "declared type" and the
8796 "active location" for each parameter. This additional set of attributes
8797 could be used by debuggers for stack backtraces. Separately, note that
8798 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
8799 This happens (for example) for inlined-instances of inline function formal
8800 parameters which are never referenced. This really shouldn't be
8801 happening. All PARM_DECL nodes should get valid non-NULL
8802 DECL_INCOMING_RTL values, but integrate.c doesn't currently generate these
8803 values for inlined instances of inline function parameters, so when we see
8804 such cases, we are just out-of-luck for the time being (until integrate.c
8805 gets fixed). */
8807 /* Use DECL_RTL as the "location" unless we find something better. */
8808 rtl = DECL_RTL_IF_SET (decl);
8810 /* When generating abstract instances, ignore everything except
8811 constants and symbols living in memory. */
8812 if (! reload_completed)
8814 if (rtl
8815 && (CONSTANT_P (rtl)
8816 || (GET_CODE (rtl) == MEM
8817 && CONSTANT_P (XEXP (rtl, 0)))))
8818 return rtl;
8819 rtl = NULL_RTX;
8821 else if (TREE_CODE (decl) == PARM_DECL)
8823 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
8825 tree declared_type = type_main_variant (TREE_TYPE (decl));
8826 tree passed_type = type_main_variant (DECL_ARG_TYPE (decl));
8828 /* This decl represents a formal parameter which was optimized out.
8829 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
8830 all cases where (rtl == NULL_RTX) just below. */
8831 if (declared_type == passed_type)
8832 rtl = DECL_INCOMING_RTL (decl);
8833 else if (! BYTES_BIG_ENDIAN
8834 && TREE_CODE (declared_type) == INTEGER_TYPE
8835 && (GET_MODE_SIZE (TYPE_MODE (declared_type))
8836 <= GET_MODE_SIZE (TYPE_MODE (passed_type))))
8837 rtl = DECL_INCOMING_RTL (decl);
8840 /* If the parm was passed in registers, but lives on the stack, then
8841 make a big endian correction if the mode of the type of the
8842 parameter is not the same as the mode of the rtl. */
8843 /* ??? This is the same series of checks that are made in dbxout.c before
8844 we reach the big endian correction code there. It isn't clear if all
8845 of these checks are necessary here, but keeping them all is the safe
8846 thing to do. */
8847 else if (GET_CODE (rtl) == MEM
8848 && XEXP (rtl, 0) != const0_rtx
8849 && ! CONSTANT_P (XEXP (rtl, 0))
8850 /* Not passed in memory. */
8851 && GET_CODE (DECL_INCOMING_RTL (decl)) != MEM
8852 /* Not passed by invisible reference. */
8853 && (GET_CODE (XEXP (rtl, 0)) != REG
8854 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
8855 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
8856 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
8857 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
8858 #endif
8860 /* Big endian correction check. */
8861 && BYTES_BIG_ENDIAN
8862 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
8863 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
8864 < UNITS_PER_WORD))
8866 int offset = (UNITS_PER_WORD
8867 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
8869 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
8870 plus_constant (XEXP (rtl, 0), offset));
8874 if (rtl != NULL_RTX)
8876 rtl = eliminate_regs (rtl, 0, NULL_RTX);
8877 #ifdef LEAF_REG_REMAP
8878 if (current_function_uses_only_leaf_regs)
8879 leaf_renumber_regs_insn (rtl);
8880 #endif
8883 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
8884 and will have been substituted directly into all expressions that use it.
8885 C does not have such a concept, but C++ and other languages do. */
8886 else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
8887 rtl = expand_expr (DECL_INITIAL (decl), NULL_RTX, VOIDmode,
8888 EXPAND_INITIALIZER);
8890 return rtl;
8893 /* Generate *either* an DW_AT_location attribute or else an DW_AT_const_value
8894 data attribute for a variable or a parameter. We generate the
8895 DW_AT_const_value attribute only in those cases where the given variable
8896 or parameter does not have a true "location" either in memory or in a
8897 register. This can happen (for example) when a constant is passed as an
8898 actual argument in a call to an inline function. (It's possible that
8899 these things can crop up in other ways also.) Note that one type of
8900 constant value which can be passed into an inlined function is a constant
8901 pointer. This can happen for example if an actual argument in an inlined
8902 function call evaluates to a compile-time constant address. */
8904 static void
8905 add_location_or_const_value_attribute (die, decl)
8906 dw_die_ref die;
8907 tree decl;
8909 rtx rtl;
8911 if (TREE_CODE (decl) == ERROR_MARK)
8912 return;
8913 else if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != PARM_DECL)
8914 abort ();
8916 rtl = rtl_for_decl_location (decl);
8917 if (rtl == NULL_RTX)
8918 return;
8920 /* If we don't look past the constant pool, we risk emitting a
8921 reference to a constant pool entry that isn't referenced from
8922 code, and thus is not emitted. */
8923 rtl = avoid_constant_pool_reference (rtl);
8925 switch (GET_CODE (rtl))
8927 case ADDRESSOF:
8928 /* The address of a variable that was optimized away; don't emit
8929 anything. */
8930 break;
8932 case CONST_INT:
8933 case CONST_DOUBLE:
8934 case CONST_STRING:
8935 case SYMBOL_REF:
8936 case LABEL_REF:
8937 case CONST:
8938 case PLUS:
8939 /* DECL_RTL could be (plus (reg ...) (const_int ...)) */
8940 add_const_value_attribute (die, rtl);
8941 break;
8943 case MEM:
8944 case REG:
8945 case SUBREG:
8946 case CONCAT:
8947 add_AT_location_description (die, DW_AT_location, rtl);
8948 break;
8950 default:
8951 abort ();
8955 /* If we don't have a copy of this variable in memory for some reason (such
8956 as a C++ member constant that doesn't have an out-of-line definition),
8957 we should tell the debugger about the constant value. */
8959 static void
8960 tree_add_const_value_attribute (var_die, decl)
8961 dw_die_ref var_die;
8962 tree decl;
8964 tree init = DECL_INITIAL (decl);
8965 tree type = TREE_TYPE (decl);
8967 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init
8968 && initializer_constant_valid_p (init, type) == null_pointer_node)
8969 /* OK */;
8970 else
8971 return;
8973 switch (TREE_CODE (type))
8975 case INTEGER_TYPE:
8976 if (host_integerp (init, 0))
8977 add_AT_unsigned (var_die, DW_AT_const_value,
8978 tree_low_cst (init, 0));
8979 else
8980 add_AT_long_long (var_die, DW_AT_const_value,
8981 TREE_INT_CST_HIGH (init),
8982 TREE_INT_CST_LOW (init));
8983 break;
8985 default:;
8989 /* Generate an DW_AT_name attribute given some string value to be included as
8990 the value of the attribute. */
8992 static inline void
8993 add_name_attribute (die, name_string)
8994 dw_die_ref die;
8995 const char *name_string;
8997 if (name_string != NULL && *name_string != 0)
8999 if (demangle_name_func)
9000 name_string = (*demangle_name_func) (name_string);
9002 add_AT_string (die, DW_AT_name, name_string);
9006 /* Given a tree node describing an array bound (either lower or upper) output
9007 a representation for that bound. */
9009 static void
9010 add_bound_info (subrange_die, bound_attr, bound)
9011 dw_die_ref subrange_die;
9012 enum dwarf_attribute bound_attr;
9013 tree bound;
9015 switch (TREE_CODE (bound))
9017 case ERROR_MARK:
9018 return;
9020 /* All fixed-bounds are represented by INTEGER_CST nodes. */
9021 case INTEGER_CST:
9022 if (! host_integerp (bound, 0)
9023 || (bound_attr == DW_AT_lower_bound
9024 && (((is_c_family () || is_java ()) && integer_zerop (bound))
9025 || (is_fortran () && integer_onep (bound)))))
9026 /* use the default */
9028 else
9029 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
9030 break;
9032 case CONVERT_EXPR:
9033 case NOP_EXPR:
9034 case NON_LVALUE_EXPR:
9035 case VIEW_CONVERT_EXPR:
9036 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
9037 break;
9039 case SAVE_EXPR:
9040 /* If optimization is turned on, the SAVE_EXPRs that describe how to
9041 access the upper bound values may be bogus. If they refer to a
9042 register, they may only describe how to get at these values at the
9043 points in the generated code right after they have just been
9044 computed. Worse yet, in the typical case, the upper bound values
9045 will not even *be* computed in the optimized code (though the
9046 number of elements will), so these SAVE_EXPRs are entirely
9047 bogus. In order to compensate for this fact, we check here to see
9048 if optimization is enabled, and if so, we don't add an attribute
9049 for the (unknown and unknowable) upper bound. This should not
9050 cause too much trouble for existing (stupid?) debuggers because
9051 they have to deal with empty upper bounds location descriptions
9052 anyway in order to be able to deal with incomplete array types.
9053 Of course an intelligent debugger (GDB?) should be able to
9054 comprehend that a missing upper bound specification in an array
9055 type used for a storage class `auto' local array variable
9056 indicates that the upper bound is both unknown (at compile- time)
9057 and unknowable (at run-time) due to optimization.
9059 We assume that a MEM rtx is safe because gcc wouldn't put the
9060 value there unless it was going to be used repeatedly in the
9061 function, i.e. for cleanups. */
9062 if (SAVE_EXPR_RTL (bound)
9063 && (! optimize || GET_CODE (SAVE_EXPR_RTL (bound)) == MEM))
9065 dw_die_ref ctx = lookup_decl_die (current_function_decl);
9066 dw_die_ref decl_die = new_die (DW_TAG_variable, ctx, bound);
9067 rtx loc = SAVE_EXPR_RTL (bound);
9069 /* If the RTL for the SAVE_EXPR is memory, handle the case where
9070 it references an outer function's frame. */
9071 if (GET_CODE (loc) == MEM)
9073 rtx new_addr = fix_lexical_addr (XEXP (loc, 0), bound);
9075 if (XEXP (loc, 0) != new_addr)
9076 loc = gen_rtx_MEM (GET_MODE (loc), new_addr);
9079 add_AT_flag (decl_die, DW_AT_artificial, 1);
9080 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
9081 add_AT_location_description (decl_die, DW_AT_location, loc);
9082 add_AT_die_ref (subrange_die, bound_attr, decl_die);
9085 /* Else leave out the attribute. */
9086 break;
9088 case VAR_DECL:
9089 case PARM_DECL:
9091 dw_die_ref decl_die = lookup_decl_die (bound);
9093 /* ??? Can this happen, or should the variable have been bound
9094 first? Probably it can, since I imagine that we try to create
9095 the types of parameters in the order in which they exist in
9096 the list, and won't have created a forward reference to a
9097 later parameter. */
9098 if (decl_die != NULL)
9099 add_AT_die_ref (subrange_die, bound_attr, decl_die);
9100 break;
9103 default:
9105 /* Otherwise try to create a stack operation procedure to
9106 evaluate the value of the array bound. */
9108 dw_die_ref ctx, decl_die;
9109 dw_loc_descr_ref loc;
9111 loc = loc_descriptor_from_tree (bound, 0);
9112 if (loc == NULL)
9113 break;
9115 if (current_function_decl == 0)
9116 ctx = comp_unit_die;
9117 else
9118 ctx = lookup_decl_die (current_function_decl);
9120 /* If we weren't able to find a context, it's most likely the case
9121 that we are processing the return type of the function. So
9122 make a SAVE_EXPR to point to it and have the limbo DIE code
9123 find the proper die. The save_expr function doesn't always
9124 make a SAVE_EXPR, so do it ourselves. */
9125 if (ctx == 0)
9126 bound = build (SAVE_EXPR, TREE_TYPE (bound), bound,
9127 current_function_decl, NULL_TREE);
9129 decl_die = new_die (DW_TAG_variable, ctx, bound);
9130 add_AT_flag (decl_die, DW_AT_artificial, 1);
9131 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
9132 add_AT_loc (decl_die, DW_AT_location, loc);
9134 add_AT_die_ref (subrange_die, bound_attr, decl_die);
9135 break;
9140 /* Note that the block of subscript information for an array type also
9141 includes information about the element type of type given array type. */
9143 static void
9144 add_subscript_info (type_die, type)
9145 dw_die_ref type_die;
9146 tree type;
9148 #ifndef MIPS_DEBUGGING_INFO
9149 unsigned dimension_number;
9150 #endif
9151 tree lower, upper;
9152 dw_die_ref subrange_die;
9154 /* The GNU compilers represent multidimensional array types as sequences of
9155 one dimensional array types whose element types are themselves array
9156 types. Here we squish that down, so that each multidimensional array
9157 type gets only one array_type DIE in the Dwarf debugging info. The draft
9158 Dwarf specification say that we are allowed to do this kind of
9159 compression in C (because there is no difference between an array or
9160 arrays and a multidimensional array in C) but for other source languages
9161 (e.g. Ada) we probably shouldn't do this. */
9163 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
9164 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
9165 We work around this by disabling this feature. See also
9166 gen_array_type_die. */
9167 #ifndef MIPS_DEBUGGING_INFO
9168 for (dimension_number = 0;
9169 TREE_CODE (type) == ARRAY_TYPE;
9170 type = TREE_TYPE (type), dimension_number++)
9171 #endif
9173 tree domain = TYPE_DOMAIN (type);
9175 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
9176 and (in GNU C only) variable bounds. Handle all three forms
9177 here. */
9178 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
9179 if (domain)
9181 /* We have an array type with specified bounds. */
9182 lower = TYPE_MIN_VALUE (domain);
9183 upper = TYPE_MAX_VALUE (domain);
9185 /* define the index type. */
9186 if (TREE_TYPE (domain))
9188 /* ??? This is probably an Ada unnamed subrange type. Ignore the
9189 TREE_TYPE field. We can't emit debug info for this
9190 because it is an unnamed integral type. */
9191 if (TREE_CODE (domain) == INTEGER_TYPE
9192 && TYPE_NAME (domain) == NULL_TREE
9193 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
9194 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
9196 else
9197 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
9198 type_die);
9201 /* ??? If upper is NULL, the array has unspecified length,
9202 but it does have a lower bound. This happens with Fortran
9203 dimension arr(N:*)
9204 Since the debugger is definitely going to need to know N
9205 to produce useful results, go ahead and output the lower
9206 bound solo, and hope the debugger can cope. */
9208 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
9209 if (upper)
9210 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
9213 /* Otherwise we have an array type with an unspecified length. The
9214 DWARF-2 spec does not say how to handle this; let's just leave out the
9215 bounds. */
9219 static void
9220 add_byte_size_attribute (die, tree_node)
9221 dw_die_ref die;
9222 tree tree_node;
9224 unsigned size;
9226 switch (TREE_CODE (tree_node))
9228 case ERROR_MARK:
9229 size = 0;
9230 break;
9231 case ENUMERAL_TYPE:
9232 case RECORD_TYPE:
9233 case UNION_TYPE:
9234 case QUAL_UNION_TYPE:
9235 size = int_size_in_bytes (tree_node);
9236 break;
9237 case FIELD_DECL:
9238 /* For a data member of a struct or union, the DW_AT_byte_size is
9239 generally given as the number of bytes normally allocated for an
9240 object of the *declared* type of the member itself. This is true
9241 even for bit-fields. */
9242 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
9243 break;
9244 default:
9245 abort ();
9248 /* Note that `size' might be -1 when we get to this point. If it is, that
9249 indicates that the byte size of the entity in question is variable. We
9250 have no good way of expressing this fact in Dwarf at the present time,
9251 so just let the -1 pass on through. */
9252 add_AT_unsigned (die, DW_AT_byte_size, size);
9255 /* For a FIELD_DECL node which represents a bit-field, output an attribute
9256 which specifies the distance in bits from the highest order bit of the
9257 "containing object" for the bit-field to the highest order bit of the
9258 bit-field itself.
9260 For any given bit-field, the "containing object" is a hypothetical object
9261 (of some integral or enum type) within which the given bit-field lives. The
9262 type of this hypothetical "containing object" is always the same as the
9263 declared type of the individual bit-field itself. The determination of the
9264 exact location of the "containing object" for a bit-field is rather
9265 complicated. It's handled by the `field_byte_offset' function (above).
9267 Note that it is the size (in bytes) of the hypothetical "containing object"
9268 which will be given in the DW_AT_byte_size attribute for this bit-field.
9269 (See `byte_size_attribute' above). */
9271 static inline void
9272 add_bit_offset_attribute (die, decl)
9273 dw_die_ref die;
9274 tree decl;
9276 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
9277 tree type = DECL_BIT_FIELD_TYPE (decl);
9278 HOST_WIDE_INT bitpos_int;
9279 HOST_WIDE_INT highest_order_object_bit_offset;
9280 HOST_WIDE_INT highest_order_field_bit_offset;
9281 HOST_WIDE_INT unsigned bit_offset;
9283 /* Must be a field and a bit field. */
9284 if (!type
9285 || TREE_CODE (decl) != FIELD_DECL)
9286 abort ();
9288 /* We can't yet handle bit-fields whose offsets are variable, so if we
9289 encounter such things, just return without generating any attribute
9290 whatsoever. Likewise for variable or too large size. */
9291 if (! host_integerp (bit_position (decl), 0)
9292 || ! host_integerp (DECL_SIZE (decl), 1))
9293 return;
9295 bitpos_int = int_bit_position (decl);
9297 /* Note that the bit offset is always the distance (in bits) from the
9298 highest-order bit of the "containing object" to the highest-order bit of
9299 the bit-field itself. Since the "high-order end" of any object or field
9300 is different on big-endian and little-endian machines, the computation
9301 below must take account of these differences. */
9302 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
9303 highest_order_field_bit_offset = bitpos_int;
9305 if (! BYTES_BIG_ENDIAN)
9307 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
9308 highest_order_object_bit_offset += simple_type_size_in_bits (type);
9311 bit_offset
9312 = (! BYTES_BIG_ENDIAN
9313 ? highest_order_object_bit_offset - highest_order_field_bit_offset
9314 : highest_order_field_bit_offset - highest_order_object_bit_offset);
9316 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
9319 /* For a FIELD_DECL node which represents a bit field, output an attribute
9320 which specifies the length in bits of the given field. */
9322 static inline void
9323 add_bit_size_attribute (die, decl)
9324 dw_die_ref die;
9325 tree decl;
9327 /* Must be a field and a bit field. */
9328 if (TREE_CODE (decl) != FIELD_DECL
9329 || ! DECL_BIT_FIELD_TYPE (decl))
9330 abort ();
9332 if (host_integerp (DECL_SIZE (decl), 1))
9333 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
9336 /* If the compiled language is ANSI C, then add a 'prototyped'
9337 attribute, if arg types are given for the parameters of a function. */
9339 static inline void
9340 add_prototyped_attribute (die, func_type)
9341 dw_die_ref die;
9342 tree func_type;
9344 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
9345 && TYPE_ARG_TYPES (func_type) != NULL)
9346 add_AT_flag (die, DW_AT_prototyped, 1);
9349 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
9350 by looking in either the type declaration or object declaration
9351 equate table. */
9353 static inline void
9354 add_abstract_origin_attribute (die, origin)
9355 dw_die_ref die;
9356 tree origin;
9358 dw_die_ref origin_die = NULL;
9360 if (TREE_CODE (origin) != FUNCTION_DECL)
9362 /* We may have gotten separated from the block for the inlined
9363 function, if we're in an exception handler or some such; make
9364 sure that the abstract function has been written out.
9366 Doing this for nested functions is wrong, however; functions are
9367 distinct units, and our context might not even be inline. */
9368 tree fn = origin;
9370 if (TYPE_P (fn))
9371 fn = TYPE_STUB_DECL (fn);
9373 fn = decl_function_context (fn);
9374 if (fn)
9375 dwarf2out_abstract_function (fn);
9378 if (DECL_P (origin))
9379 origin_die = lookup_decl_die (origin);
9380 else if (TYPE_P (origin))
9381 origin_die = lookup_type_die (origin);
9383 if (origin_die == NULL)
9384 abort ();
9386 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
9389 /* We do not currently support the pure_virtual attribute. */
9391 static inline void
9392 add_pure_or_virtual_attribute (die, func_decl)
9393 dw_die_ref die;
9394 tree func_decl;
9396 if (DECL_VINDEX (func_decl))
9398 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
9400 if (host_integerp (DECL_VINDEX (func_decl), 0))
9401 add_AT_loc (die, DW_AT_vtable_elem_location,
9402 new_loc_descr (DW_OP_constu,
9403 tree_low_cst (DECL_VINDEX (func_decl), 0),
9404 0));
9406 /* GNU extension: Record what type this method came from originally. */
9407 if (debug_info_level > DINFO_LEVEL_TERSE)
9408 add_AT_die_ref (die, DW_AT_containing_type,
9409 lookup_type_die (DECL_CONTEXT (func_decl)));
9413 /* Add source coordinate attributes for the given decl. */
9415 static void
9416 add_src_coords_attributes (die, decl)
9417 dw_die_ref die;
9418 tree decl;
9420 unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
9422 add_AT_unsigned (die, DW_AT_decl_file, file_index);
9423 add_AT_unsigned (die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
9426 /* Add an DW_AT_name attribute and source coordinate attribute for the
9427 given decl, but only if it actually has a name. */
9429 static void
9430 add_name_and_src_coords_attributes (die, decl)
9431 dw_die_ref die;
9432 tree decl;
9434 tree decl_name;
9436 decl_name = DECL_NAME (decl);
9437 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
9439 add_name_attribute (die, dwarf2_name (decl, 0));
9440 if (! DECL_ARTIFICIAL (decl))
9441 add_src_coords_attributes (die, decl);
9443 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
9444 && TREE_PUBLIC (decl)
9445 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
9446 && !DECL_ABSTRACT (decl))
9447 add_AT_string (die, DW_AT_MIPS_linkage_name,
9448 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
9451 #ifdef VMS_DEBUGGING_INFO
9452 /* Get the function's name, as described by its RTL. This may be different
9453 from the DECL_NAME name used in the source file. */
9454 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
9456 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
9457 XEXP (DECL_RTL (decl), 0));
9458 VARRAY_PUSH_RTX (used_rtx_varray, XEXP (DECL_RTL (decl), 0));
9460 #endif
9463 /* Push a new declaration scope. */
9465 static void
9466 push_decl_scope (scope)
9467 tree scope;
9469 VARRAY_PUSH_TREE (decl_scope_table, scope);
9472 /* Pop a declaration scope. */
9474 static inline void
9475 pop_decl_scope ()
9477 if (VARRAY_ACTIVE_SIZE (decl_scope_table) <= 0)
9478 abort ();
9480 VARRAY_POP (decl_scope_table);
9483 /* Return the DIE for the scope that immediately contains this type.
9484 Non-named types get global scope. Named types nested in other
9485 types get their containing scope if it's open, or global scope
9486 otherwise. All other types (i.e. function-local named types) get
9487 the current active scope. */
9489 static dw_die_ref
9490 scope_die_for (t, context_die)
9491 tree t;
9492 dw_die_ref context_die;
9494 dw_die_ref scope_die = NULL;
9495 tree containing_scope;
9496 int i;
9498 /* Non-types always go in the current scope. */
9499 if (! TYPE_P (t))
9500 abort ();
9502 containing_scope = TYPE_CONTEXT (t);
9504 /* Ignore namespaces for the moment. */
9505 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
9506 containing_scope = NULL_TREE;
9508 /* Ignore function type "scopes" from the C frontend. They mean that
9509 a tagged type is local to a parmlist of a function declarator, but
9510 that isn't useful to DWARF. */
9511 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
9512 containing_scope = NULL_TREE;
9514 if (containing_scope == NULL_TREE)
9515 scope_die = comp_unit_die;
9516 else if (TYPE_P (containing_scope))
9518 /* For types, we can just look up the appropriate DIE. But
9519 first we check to see if we're in the middle of emitting it
9520 so we know where the new DIE should go. */
9521 for (i = VARRAY_ACTIVE_SIZE (decl_scope_table) - 1; i >= 0; --i)
9522 if (VARRAY_TREE (decl_scope_table, i) == containing_scope)
9523 break;
9525 if (i < 0)
9527 if (debug_info_level > DINFO_LEVEL_TERSE
9528 && !TREE_ASM_WRITTEN (containing_scope))
9529 abort ();
9531 /* If none of the current dies are suitable, we get file scope. */
9532 scope_die = comp_unit_die;
9534 else
9535 scope_die = lookup_type_die (containing_scope);
9537 else
9538 scope_die = context_die;
9540 return scope_die;
9543 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
9545 static inline int
9546 local_scope_p (context_die)
9547 dw_die_ref context_die;
9549 for (; context_die; context_die = context_die->die_parent)
9550 if (context_die->die_tag == DW_TAG_inlined_subroutine
9551 || context_die->die_tag == DW_TAG_subprogram)
9552 return 1;
9554 return 0;
9557 /* Returns nonzero if CONTEXT_DIE is a class. */
9559 static inline int
9560 class_scope_p (context_die)
9561 dw_die_ref context_die;
9563 return (context_die
9564 && (context_die->die_tag == DW_TAG_structure_type
9565 || context_die->die_tag == DW_TAG_union_type));
9568 /* Many forms of DIEs require a "type description" attribute. This
9569 routine locates the proper "type descriptor" die for the type given
9570 by 'type', and adds an DW_AT_type attribute below the given die. */
9572 static void
9573 add_type_attribute (object_die, type, decl_const, decl_volatile, context_die)
9574 dw_die_ref object_die;
9575 tree type;
9576 int decl_const;
9577 int decl_volatile;
9578 dw_die_ref context_die;
9580 enum tree_code code = TREE_CODE (type);
9581 dw_die_ref type_die = NULL;
9583 /* ??? If this type is an unnamed subrange type of an integral or
9584 floating-point type, use the inner type. This is because we have no
9585 support for unnamed types in base_type_die. This can happen if this is
9586 an Ada subrange type. Correct solution is emit a subrange type die. */
9587 if ((code == INTEGER_TYPE || code == REAL_TYPE)
9588 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
9589 type = TREE_TYPE (type), code = TREE_CODE (type);
9591 if (code == ERROR_MARK
9592 /* Handle a special case. For functions whose return type is void, we
9593 generate *no* type attribute. (Note that no object may have type
9594 `void', so this only applies to function return types). */
9595 || code == VOID_TYPE)
9596 return;
9598 type_die = modified_type_die (type,
9599 decl_const || TYPE_READONLY (type),
9600 decl_volatile || TYPE_VOLATILE (type),
9601 context_die);
9603 if (type_die != NULL)
9604 add_AT_die_ref (object_die, DW_AT_type, type_die);
9607 /* Given a tree pointer to a struct, class, union, or enum type node, return
9608 a pointer to the (string) tag name for the given type, or zero if the type
9609 was declared without a tag. */
9611 static const char *
9612 type_tag (type)
9613 tree type;
9615 const char *name = 0;
9617 if (TYPE_NAME (type) != 0)
9619 tree t = 0;
9621 /* Find the IDENTIFIER_NODE for the type name. */
9622 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
9623 t = TYPE_NAME (type);
9625 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
9626 a TYPE_DECL node, regardless of whether or not a `typedef' was
9627 involved. */
9628 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9629 && ! DECL_IGNORED_P (TYPE_NAME (type)))
9630 t = DECL_NAME (TYPE_NAME (type));
9632 /* Now get the name as a string, or invent one. */
9633 if (t != 0)
9634 name = IDENTIFIER_POINTER (t);
9637 return (name == 0 || *name == '\0') ? 0 : name;
9640 /* Return the type associated with a data member, make a special check
9641 for bit field types. */
9643 static inline tree
9644 member_declared_type (member)
9645 tree member;
9647 return (DECL_BIT_FIELD_TYPE (member)
9648 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
9651 /* Get the decl's label, as described by its RTL. This may be different
9652 from the DECL_NAME name used in the source file. */
9654 #if 0
9655 static const char *
9656 decl_start_label (decl)
9657 tree decl;
9659 rtx x;
9660 const char *fnname;
9662 x = DECL_RTL (decl);
9663 if (GET_CODE (x) != MEM)
9664 abort ();
9666 x = XEXP (x, 0);
9667 if (GET_CODE (x) != SYMBOL_REF)
9668 abort ();
9670 fnname = XSTR (x, 0);
9671 return fnname;
9673 #endif
9675 /* These routines generate the internal representation of the DIE's for
9676 the compilation unit. Debugging information is collected by walking
9677 the declaration trees passed in from dwarf2out_decl(). */
9679 static void
9680 gen_array_type_die (type, context_die)
9681 tree type;
9682 dw_die_ref context_die;
9684 dw_die_ref scope_die = scope_die_for (type, context_die);
9685 dw_die_ref array_die;
9686 tree element_type;
9688 /* ??? The SGI dwarf reader fails for array of array of enum types unless
9689 the inner array type comes before the outer array type. Thus we must
9690 call gen_type_die before we call new_die. See below also. */
9691 #ifdef MIPS_DEBUGGING_INFO
9692 gen_type_die (TREE_TYPE (type), context_die);
9693 #endif
9695 array_die = new_die (DW_TAG_array_type, scope_die, type);
9697 #if 0
9698 /* We default the array ordering. SDB will probably do
9699 the right things even if DW_AT_ordering is not present. It's not even
9700 an issue until we start to get into multidimensional arrays anyway. If
9701 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
9702 then we'll have to put the DW_AT_ordering attribute back in. (But if
9703 and when we find out that we need to put these in, we will only do so
9704 for multidimensional arrays. */
9705 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
9706 #endif
9708 #ifdef MIPS_DEBUGGING_INFO
9709 /* The SGI compilers handle arrays of unknown bound by setting
9710 AT_declaration and not emitting any subrange DIEs. */
9711 if (! TYPE_DOMAIN (type))
9712 add_AT_unsigned (array_die, DW_AT_declaration, 1);
9713 else
9714 #endif
9715 add_subscript_info (array_die, type);
9717 add_name_attribute (array_die, type_tag (type));
9718 equate_type_number_to_die (type, array_die);
9720 /* Add representation of the type of the elements of this array type. */
9721 element_type = TREE_TYPE (type);
9723 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
9724 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
9725 We work around this by disabling this feature. See also
9726 add_subscript_info. */
9727 #ifndef MIPS_DEBUGGING_INFO
9728 while (TREE_CODE (element_type) == ARRAY_TYPE)
9729 element_type = TREE_TYPE (element_type);
9731 gen_type_die (element_type, context_die);
9732 #endif
9734 add_type_attribute (array_die, element_type, 0, 0, context_die);
9737 static void
9738 gen_set_type_die (type, context_die)
9739 tree type;
9740 dw_die_ref context_die;
9742 dw_die_ref type_die
9743 = new_die (DW_TAG_set_type, scope_die_for (type, context_die), type);
9745 equate_type_number_to_die (type, type_die);
9746 add_type_attribute (type_die, TREE_TYPE (type), 0, 0, context_die);
9749 #if 0
9750 static void
9751 gen_entry_point_die (decl, context_die)
9752 tree decl;
9753 dw_die_ref context_die;
9755 tree origin = decl_ultimate_origin (decl);
9756 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
9758 if (origin != NULL)
9759 add_abstract_origin_attribute (decl_die, origin);
9760 else
9762 add_name_and_src_coords_attributes (decl_die, decl);
9763 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
9764 0, 0, context_die);
9767 if (DECL_ABSTRACT (decl))
9768 equate_decl_number_to_die (decl, decl_die);
9769 else
9770 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
9772 #endif
9774 /* Walk through the list of incomplete types again, trying once more to
9775 emit full debugging info for them. */
9777 static void
9778 retry_incomplete_types ()
9780 int i;
9782 for (i = VARRAY_ACTIVE_SIZE (incomplete_types) - 1; i >= 0; i--)
9783 gen_type_die (VARRAY_TREE (incomplete_types, i), comp_unit_die);
9786 /* Generate a DIE to represent an inlined instance of an enumeration type. */
9788 static void
9789 gen_inlined_enumeration_type_die (type, context_die)
9790 tree type;
9791 dw_die_ref context_die;
9793 dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
9795 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
9796 be incomplete and such types are not marked. */
9797 add_abstract_origin_attribute (type_die, type);
9800 /* Generate a DIE to represent an inlined instance of a structure type. */
9802 static void
9803 gen_inlined_structure_type_die (type, context_die)
9804 tree type;
9805 dw_die_ref context_die;
9807 dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
9809 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
9810 be incomplete and such types are not marked. */
9811 add_abstract_origin_attribute (type_die, type);
9814 /* Generate a DIE to represent an inlined instance of a union type. */
9816 static void
9817 gen_inlined_union_type_die (type, context_die)
9818 tree type;
9819 dw_die_ref context_die;
9821 dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
9823 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
9824 be incomplete and such types are not marked. */
9825 add_abstract_origin_attribute (type_die, type);
9828 /* Generate a DIE to represent an enumeration type. Note that these DIEs
9829 include all of the information about the enumeration values also. Each
9830 enumerated type name/value is listed as a child of the enumerated type
9831 DIE. */
9833 static void
9834 gen_enumeration_type_die (type, context_die)
9835 tree type;
9836 dw_die_ref context_die;
9838 dw_die_ref type_die = lookup_type_die (type);
9840 if (type_die == NULL)
9842 type_die = new_die (DW_TAG_enumeration_type,
9843 scope_die_for (type, context_die), type);
9844 equate_type_number_to_die (type, type_die);
9845 add_name_attribute (type_die, type_tag (type));
9847 else if (! TYPE_SIZE (type))
9848 return;
9849 else
9850 remove_AT (type_die, DW_AT_declaration);
9852 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
9853 given enum type is incomplete, do not generate the DW_AT_byte_size
9854 attribute or the DW_AT_element_list attribute. */
9855 if (TYPE_SIZE (type))
9857 tree link;
9859 TREE_ASM_WRITTEN (type) = 1;
9860 add_byte_size_attribute (type_die, type);
9861 if (TYPE_STUB_DECL (type) != NULL_TREE)
9862 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
9864 /* If the first reference to this type was as the return type of an
9865 inline function, then it may not have a parent. Fix this now. */
9866 if (type_die->die_parent == NULL)
9867 add_child_die (scope_die_for (type, context_die), type_die);
9869 for (link = TYPE_FIELDS (type);
9870 link != NULL; link = TREE_CHAIN (link))
9872 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
9874 add_name_attribute (enum_die,
9875 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
9877 if (host_integerp (TREE_VALUE (link), 0))
9879 if (tree_int_cst_sgn (TREE_VALUE (link)) < 0)
9880 add_AT_int (enum_die, DW_AT_const_value,
9881 tree_low_cst (TREE_VALUE (link), 0));
9882 else
9883 add_AT_unsigned (enum_die, DW_AT_const_value,
9884 tree_low_cst (TREE_VALUE (link), 0));
9888 else
9889 add_AT_flag (type_die, DW_AT_declaration, 1);
9892 /* Generate a DIE to represent either a real live formal parameter decl or to
9893 represent just the type of some formal parameter position in some function
9894 type.
9896 Note that this routine is a bit unusual because its argument may be a
9897 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
9898 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
9899 node. If it's the former then this function is being called to output a
9900 DIE to represent a formal parameter object (or some inlining thereof). If
9901 it's the latter, then this function is only being called to output a
9902 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
9903 argument type of some subprogram type. */
9905 static dw_die_ref
9906 gen_formal_parameter_die (node, context_die)
9907 tree node;
9908 dw_die_ref context_die;
9910 dw_die_ref parm_die
9911 = new_die (DW_TAG_formal_parameter, context_die, node);
9912 tree origin;
9914 switch (TREE_CODE_CLASS (TREE_CODE (node)))
9916 case 'd':
9917 origin = decl_ultimate_origin (node);
9918 if (origin != NULL)
9919 add_abstract_origin_attribute (parm_die, origin);
9920 else
9922 add_name_and_src_coords_attributes (parm_die, node);
9923 add_type_attribute (parm_die, TREE_TYPE (node),
9924 TREE_READONLY (node),
9925 TREE_THIS_VOLATILE (node),
9926 context_die);
9927 if (DECL_ARTIFICIAL (node))
9928 add_AT_flag (parm_die, DW_AT_artificial, 1);
9931 equate_decl_number_to_die (node, parm_die);
9932 if (! DECL_ABSTRACT (node))
9933 add_location_or_const_value_attribute (parm_die, node);
9935 break;
9937 case 't':
9938 /* We were called with some kind of a ..._TYPE node. */
9939 add_type_attribute (parm_die, node, 0, 0, context_die);
9940 break;
9942 default:
9943 abort ();
9946 return parm_die;
9949 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
9950 at the end of an (ANSI prototyped) formal parameters list. */
9952 static void
9953 gen_unspecified_parameters_die (decl_or_type, context_die)
9954 tree decl_or_type;
9955 dw_die_ref context_die;
9957 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
9960 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
9961 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
9962 parameters as specified in some function type specification (except for
9963 those which appear as part of a function *definition*). */
9965 static void
9966 gen_formal_types_die (function_or_method_type, context_die)
9967 tree function_or_method_type;
9968 dw_die_ref context_die;
9970 tree link;
9971 tree formal_type = NULL;
9972 tree first_parm_type;
9973 tree arg;
9975 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
9977 arg = DECL_ARGUMENTS (function_or_method_type);
9978 function_or_method_type = TREE_TYPE (function_or_method_type);
9980 else
9981 arg = NULL_TREE;
9983 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
9985 /* Make our first pass over the list of formal parameter types and output a
9986 DW_TAG_formal_parameter DIE for each one. */
9987 for (link = first_parm_type; link; )
9989 dw_die_ref parm_die;
9991 formal_type = TREE_VALUE (link);
9992 if (formal_type == void_type_node)
9993 break;
9995 /* Output a (nameless) DIE to represent the formal parameter itself. */
9996 parm_die = gen_formal_parameter_die (formal_type, context_die);
9997 if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
9998 && link == first_parm_type)
9999 || (arg && DECL_ARTIFICIAL (arg)))
10000 add_AT_flag (parm_die, DW_AT_artificial, 1);
10002 link = TREE_CHAIN (link);
10003 if (arg)
10004 arg = TREE_CHAIN (arg);
10007 /* If this function type has an ellipsis, add a
10008 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
10009 if (formal_type != void_type_node)
10010 gen_unspecified_parameters_die (function_or_method_type, context_die);
10012 /* Make our second (and final) pass over the list of formal parameter types
10013 and output DIEs to represent those types (as necessary). */
10014 for (link = TYPE_ARG_TYPES (function_or_method_type);
10015 link && TREE_VALUE (link);
10016 link = TREE_CHAIN (link))
10017 gen_type_die (TREE_VALUE (link), context_die);
10020 /* We want to generate the DIE for TYPE so that we can generate the
10021 die for MEMBER, which has been defined; we will need to refer back
10022 to the member declaration nested within TYPE. If we're trying to
10023 generate minimal debug info for TYPE, processing TYPE won't do the
10024 trick; we need to attach the member declaration by hand. */
10026 static void
10027 gen_type_die_for_member (type, member, context_die)
10028 tree type, member;
10029 dw_die_ref context_die;
10031 gen_type_die (type, context_die);
10033 /* If we're trying to avoid duplicate debug info, we may not have
10034 emitted the member decl for this function. Emit it now. */
10035 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
10036 && ! lookup_decl_die (member))
10038 if (decl_ultimate_origin (member))
10039 abort ();
10041 push_decl_scope (type);
10042 if (TREE_CODE (member) == FUNCTION_DECL)
10043 gen_subprogram_die (member, lookup_type_die (type));
10044 else
10045 gen_variable_die (member, lookup_type_die (type));
10047 pop_decl_scope ();
10051 /* Generate the DWARF2 info for the "abstract" instance of a function which we
10052 may later generate inlined and/or out-of-line instances of. */
10054 static void
10055 dwarf2out_abstract_function (decl)
10056 tree decl;
10058 dw_die_ref old_die;
10059 tree save_fn;
10060 tree context;
10061 int was_abstract = DECL_ABSTRACT (decl);
10063 /* Make sure we have the actual abstract inline, not a clone. */
10064 decl = DECL_ORIGIN (decl);
10066 old_die = lookup_decl_die (decl);
10067 if (old_die && get_AT_unsigned (old_die, DW_AT_inline))
10068 /* We've already generated the abstract instance. */
10069 return;
10071 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
10072 we don't get confused by DECL_ABSTRACT. */
10073 if (debug_info_level > DINFO_LEVEL_TERSE)
10075 context = decl_class_context (decl);
10076 if (context)
10077 gen_type_die_for_member
10078 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
10081 /* Pretend we've just finished compiling this function. */
10082 save_fn = current_function_decl;
10083 current_function_decl = decl;
10085 set_decl_abstract_flags (decl, 1);
10086 dwarf2out_decl (decl);
10087 if (! was_abstract)
10088 set_decl_abstract_flags (decl, 0);
10090 current_function_decl = save_fn;
10093 /* Generate a DIE to represent a declared function (either file-scope or
10094 block-local). */
10096 static void
10097 gen_subprogram_die (decl, context_die)
10098 tree decl;
10099 dw_die_ref context_die;
10101 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10102 tree origin = decl_ultimate_origin (decl);
10103 dw_die_ref subr_die;
10104 rtx fp_reg;
10105 tree fn_arg_types;
10106 tree outer_scope;
10107 dw_die_ref old_die = lookup_decl_die (decl);
10108 int declaration = (current_function_decl != decl
10109 || class_scope_p (context_die));
10111 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
10112 started to generate the abstract instance of an inline, decided to output
10113 its containing class, and proceeded to emit the declaration of the inline
10114 from the member list for the class. If so, DECLARATION takes priority;
10115 we'll get back to the abstract instance when done with the class. */
10117 /* The class-scope declaration DIE must be the primary DIE. */
10118 if (origin && declaration && class_scope_p (context_die))
10120 origin = NULL;
10121 if (old_die)
10122 abort ();
10125 if (origin != NULL)
10127 if (declaration && ! local_scope_p (context_die))
10128 abort ();
10130 /* Fixup die_parent for the abstract instance of a nested
10131 inline function. */
10132 if (old_die && old_die->die_parent == NULL)
10133 add_child_die (context_die, old_die);
10135 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
10136 add_abstract_origin_attribute (subr_die, origin);
10138 else if (old_die)
10140 unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
10142 if (!get_AT_flag (old_die, DW_AT_declaration)
10143 /* We can have a normal definition following an inline one in the
10144 case of redefinition of GNU C extern inlines.
10145 It seems reasonable to use AT_specification in this case. */
10146 && !get_AT_unsigned (old_die, DW_AT_inline))
10148 /* ??? This can happen if there is a bug in the program, for
10149 instance, if it has duplicate function definitions. Ideally,
10150 we should detect this case and ignore it. For now, if we have
10151 already reported an error, any error at all, then assume that
10152 we got here because of an input error, not a dwarf2 bug. */
10153 if (errorcount)
10154 return;
10155 abort ();
10158 /* If the definition comes from the same place as the declaration,
10159 maybe use the old DIE. We always want the DIE for this function
10160 that has the *_pc attributes to be under comp_unit_die so the
10161 debugger can find it. We also need to do this for abstract
10162 instances of inlines, since the spec requires the out-of-line copy
10163 to have the same parent. For local class methods, this doesn't
10164 apply; we just use the old DIE. */
10165 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
10166 && (DECL_ARTIFICIAL (decl)
10167 || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
10168 && (get_AT_unsigned (old_die, DW_AT_decl_line)
10169 == (unsigned) DECL_SOURCE_LINE (decl)))))
10171 subr_die = old_die;
10173 /* Clear out the declaration attribute and the parm types. */
10174 remove_AT (subr_die, DW_AT_declaration);
10175 remove_children (subr_die);
10177 else
10179 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
10180 add_AT_die_ref (subr_die, DW_AT_specification, old_die);
10181 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
10182 add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
10183 if (get_AT_unsigned (old_die, DW_AT_decl_line)
10184 != (unsigned) DECL_SOURCE_LINE (decl))
10185 add_AT_unsigned
10186 (subr_die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
10189 else
10191 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
10193 if (TREE_PUBLIC (decl))
10194 add_AT_flag (subr_die, DW_AT_external, 1);
10196 add_name_and_src_coords_attributes (subr_die, decl);
10197 if (debug_info_level > DINFO_LEVEL_TERSE)
10199 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
10200 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
10201 0, 0, context_die);
10204 add_pure_or_virtual_attribute (subr_die, decl);
10205 if (DECL_ARTIFICIAL (decl))
10206 add_AT_flag (subr_die, DW_AT_artificial, 1);
10208 if (TREE_PROTECTED (decl))
10209 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
10210 else if (TREE_PRIVATE (decl))
10211 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
10214 if (declaration)
10216 if (!old_die || !get_AT_unsigned (old_die, DW_AT_inline))
10218 add_AT_flag (subr_die, DW_AT_declaration, 1);
10220 /* The first time we see a member function, it is in the context of
10221 the class to which it belongs. We make sure of this by emitting
10222 the class first. The next time is the definition, which is
10223 handled above. The two may come from the same source text. */
10224 if (DECL_CONTEXT (decl) || DECL_ABSTRACT (decl))
10225 equate_decl_number_to_die (decl, subr_die);
10228 else if (DECL_ABSTRACT (decl))
10230 if (DECL_INLINE (decl) && !flag_no_inline)
10232 /* ??? Checking DECL_DEFER_OUTPUT is correct for static
10233 inline functions, but not for extern inline functions.
10234 We can't get this completely correct because information
10235 about whether the function was declared inline is not
10236 saved anywhere. */
10237 if (DECL_DEFER_OUTPUT (decl))
10238 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
10239 else
10240 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
10242 else
10243 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
10245 equate_decl_number_to_die (decl, subr_die);
10247 else if (!DECL_EXTERNAL (decl))
10249 if (!old_die || !get_AT_unsigned (old_die, DW_AT_inline))
10250 equate_decl_number_to_die (decl, subr_die);
10252 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
10253 current_funcdef_number);
10254 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
10255 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10256 current_funcdef_number);
10257 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
10259 add_pubname (decl, subr_die);
10260 add_arange (decl, subr_die);
10262 #ifdef MIPS_DEBUGGING_INFO
10263 /* Add a reference to the FDE for this routine. */
10264 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
10265 #endif
10267 /* Define the "frame base" location for this routine. We use the
10268 frame pointer or stack pointer registers, since the RTL for local
10269 variables is relative to one of them. */
10270 fp_reg
10271 = frame_pointer_needed ? hard_frame_pointer_rtx : stack_pointer_rtx;
10272 add_AT_loc (subr_die, DW_AT_frame_base, reg_loc_descriptor (fp_reg));
10274 #if 0
10275 /* ??? This fails for nested inline functions, because context_display
10276 is not part of the state saved/restored for inline functions. */
10277 if (current_function_needs_context)
10278 add_AT_location_description (subr_die, DW_AT_static_link,
10279 lookup_static_chain (decl));
10280 #endif
10283 /* Now output descriptions of the arguments for this function. This gets
10284 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
10285 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
10286 `...' at the end of the formal parameter list. In order to find out if
10287 there was a trailing ellipsis or not, we must instead look at the type
10288 associated with the FUNCTION_DECL. This will be a node of type
10289 FUNCTION_TYPE. If the chain of type nodes hanging off of this
10290 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
10291 an ellipsis at the end. */
10293 /* In the case where we are describing a mere function declaration, all we
10294 need to do here (and all we *can* do here) is to describe the *types* of
10295 its formal parameters. */
10296 if (debug_info_level <= DINFO_LEVEL_TERSE)
10298 else if (declaration)
10299 gen_formal_types_die (decl, subr_die);
10300 else
10302 /* Generate DIEs to represent all known formal parameters */
10303 tree arg_decls = DECL_ARGUMENTS (decl);
10304 tree parm;
10306 /* When generating DIEs, generate the unspecified_parameters DIE
10307 instead if we come across the arg "__builtin_va_alist" */
10308 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
10309 if (TREE_CODE (parm) == PARM_DECL)
10311 if (DECL_NAME (parm)
10312 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
10313 "__builtin_va_alist"))
10314 gen_unspecified_parameters_die (parm, subr_die);
10315 else
10316 gen_decl_die (parm, subr_die);
10319 /* Decide whether we need an unspecified_parameters DIE at the end.
10320 There are 2 more cases to do this for: 1) the ansi ... declaration -
10321 this is detectable when the end of the arg list is not a
10322 void_type_node 2) an unprototyped function declaration (not a
10323 definition). This just means that we have no info about the
10324 parameters at all. */
10325 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
10326 if (fn_arg_types != NULL)
10328 /* this is the prototyped case, check for ... */
10329 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
10330 gen_unspecified_parameters_die (decl, subr_die);
10332 else if (DECL_INITIAL (decl) == NULL_TREE)
10333 gen_unspecified_parameters_die (decl, subr_die);
10336 /* Output Dwarf info for all of the stuff within the body of the function
10337 (if it has one - it may be just a declaration). */
10338 outer_scope = DECL_INITIAL (decl);
10340 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
10341 a function. This BLOCK actually represents the outermost binding contour
10342 for the function, i.e. the contour in which the function's formal
10343 parameters and labels get declared. Curiously, it appears that the front
10344 end doesn't actually put the PARM_DECL nodes for the current function onto
10345 the BLOCK_VARS list for this outer scope, but are strung off of the
10346 DECL_ARGUMENTS list for the function instead.
10348 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
10349 the LABEL_DECL nodes for the function however, and we output DWARF info
10350 for those in decls_for_scope. Just within the `outer_scope' there will be
10351 a BLOCK node representing the function's outermost pair of curly braces,
10352 and any blocks used for the base and member initializers of a C++
10353 constructor function. */
10354 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
10356 current_function_has_inlines = 0;
10357 decls_for_scope (outer_scope, subr_die, 0);
10359 #if 0 && defined (MIPS_DEBUGGING_INFO)
10360 if (current_function_has_inlines)
10362 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
10363 if (! comp_unit_has_inlines)
10365 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
10366 comp_unit_has_inlines = 1;
10369 #endif
10373 /* Generate a DIE to represent a declared data object. */
10375 static void
10376 gen_variable_die (decl, context_die)
10377 tree decl;
10378 dw_die_ref context_die;
10380 tree origin = decl_ultimate_origin (decl);
10381 dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
10383 dw_die_ref old_die = lookup_decl_die (decl);
10384 int declaration = (DECL_EXTERNAL (decl)
10385 || class_scope_p (context_die));
10387 if (origin != NULL)
10388 add_abstract_origin_attribute (var_die, origin);
10390 /* Loop unrolling can create multiple blocks that refer to the same
10391 static variable, so we must test for the DW_AT_declaration flag.
10393 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
10394 copy decls and set the DECL_ABSTRACT flag on them instead of
10395 sharing them.
10397 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
10398 else if (old_die && TREE_STATIC (decl)
10399 && get_AT_flag (old_die, DW_AT_declaration) == 1)
10401 /* This is a definition of a C++ class level static. */
10402 add_AT_die_ref (var_die, DW_AT_specification, old_die);
10403 if (DECL_NAME (decl))
10405 unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
10407 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
10408 add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
10410 if (get_AT_unsigned (old_die, DW_AT_decl_line)
10411 != (unsigned) DECL_SOURCE_LINE (decl))
10413 add_AT_unsigned (var_die, DW_AT_decl_line,
10414 DECL_SOURCE_LINE (decl));
10417 else
10419 add_name_and_src_coords_attributes (var_die, decl);
10420 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
10421 TREE_THIS_VOLATILE (decl), context_die);
10423 if (TREE_PUBLIC (decl))
10424 add_AT_flag (var_die, DW_AT_external, 1);
10426 if (DECL_ARTIFICIAL (decl))
10427 add_AT_flag (var_die, DW_AT_artificial, 1);
10429 if (TREE_PROTECTED (decl))
10430 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
10431 else if (TREE_PRIVATE (decl))
10432 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
10435 if (declaration)
10436 add_AT_flag (var_die, DW_AT_declaration, 1);
10438 if (class_scope_p (context_die) || DECL_ABSTRACT (decl))
10439 equate_decl_number_to_die (decl, var_die);
10441 if (! declaration && ! DECL_ABSTRACT (decl))
10443 add_location_or_const_value_attribute (var_die, decl);
10444 add_pubname (decl, var_die);
10446 else
10447 tree_add_const_value_attribute (var_die, decl);
10450 /* Generate a DIE to represent a label identifier. */
10452 static void
10453 gen_label_die (decl, context_die)
10454 tree decl;
10455 dw_die_ref context_die;
10457 tree origin = decl_ultimate_origin (decl);
10458 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
10459 rtx insn;
10460 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10462 if (origin != NULL)
10463 add_abstract_origin_attribute (lbl_die, origin);
10464 else
10465 add_name_and_src_coords_attributes (lbl_die, decl);
10467 if (DECL_ABSTRACT (decl))
10468 equate_decl_number_to_die (decl, lbl_die);
10469 else
10471 insn = DECL_RTL (decl);
10473 /* Deleted labels are programmer specified labels which have been
10474 eliminated because of various optimisations. We still emit them
10475 here so that it is possible to put breakpoints on them. */
10476 if (GET_CODE (insn) == CODE_LABEL
10477 || ((GET_CODE (insn) == NOTE
10478 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL)))
10480 /* When optimization is enabled (via -O) some parts of the compiler
10481 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
10482 represent source-level labels which were explicitly declared by
10483 the user. This really shouldn't be happening though, so catch
10484 it if it ever does happen. */
10485 if (INSN_DELETED_P (insn))
10486 abort ();
10488 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
10489 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
10494 /* Generate a DIE for a lexical block. */
10496 static void
10497 gen_lexical_block_die (stmt, context_die, depth)
10498 tree stmt;
10499 dw_die_ref context_die;
10500 int depth;
10502 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
10503 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10505 if (! BLOCK_ABSTRACT (stmt))
10507 if (BLOCK_FRAGMENT_CHAIN (stmt))
10509 tree chain;
10511 add_AT_range_list (stmt_die, DW_AT_ranges, add_ranges (stmt));
10513 chain = BLOCK_FRAGMENT_CHAIN (stmt);
10516 add_ranges (chain);
10517 chain = BLOCK_FRAGMENT_CHAIN (chain);
10519 while (chain);
10520 add_ranges (NULL);
10522 else
10524 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
10525 BLOCK_NUMBER (stmt));
10526 add_AT_lbl_id (stmt_die, DW_AT_low_pc, label);
10527 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
10528 BLOCK_NUMBER (stmt));
10529 add_AT_lbl_id (stmt_die, DW_AT_high_pc, label);
10533 decls_for_scope (stmt, stmt_die, depth);
10536 /* Generate a DIE for an inlined subprogram. */
10538 static void
10539 gen_inlined_subroutine_die (stmt, context_die, depth)
10540 tree stmt;
10541 dw_die_ref context_die;
10542 int depth;
10544 if (! BLOCK_ABSTRACT (stmt))
10546 dw_die_ref subr_die
10547 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
10548 tree decl = block_ultimate_origin (stmt);
10549 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10551 /* Emit info for the abstract instance first, if we haven't yet. */
10552 dwarf2out_abstract_function (decl);
10554 add_abstract_origin_attribute (subr_die, decl);
10555 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
10556 BLOCK_NUMBER (stmt));
10557 add_AT_lbl_id (subr_die, DW_AT_low_pc, label);
10558 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
10559 BLOCK_NUMBER (stmt));
10560 add_AT_lbl_id (subr_die, DW_AT_high_pc, label);
10561 decls_for_scope (stmt, subr_die, depth);
10562 current_function_has_inlines = 1;
10566 /* Generate a DIE for a field in a record, or structure. */
10568 static void
10569 gen_field_die (decl, context_die)
10570 tree decl;
10571 dw_die_ref context_die;
10573 dw_die_ref decl_die = new_die (DW_TAG_member, context_die, decl);
10575 add_name_and_src_coords_attributes (decl_die, decl);
10576 add_type_attribute (decl_die, member_declared_type (decl),
10577 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
10578 context_die);
10580 if (DECL_BIT_FIELD_TYPE (decl))
10582 add_byte_size_attribute (decl_die, decl);
10583 add_bit_size_attribute (decl_die, decl);
10584 add_bit_offset_attribute (decl_die, decl);
10587 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
10588 add_data_member_location_attribute (decl_die, decl);
10590 if (DECL_ARTIFICIAL (decl))
10591 add_AT_flag (decl_die, DW_AT_artificial, 1);
10593 if (TREE_PROTECTED (decl))
10594 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
10595 else if (TREE_PRIVATE (decl))
10596 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
10599 #if 0
10600 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
10601 Use modified_type_die instead.
10602 We keep this code here just in case these types of DIEs may be needed to
10603 represent certain things in other languages (e.g. Pascal) someday. */
10605 static void
10606 gen_pointer_type_die (type, context_die)
10607 tree type;
10608 dw_die_ref context_die;
10610 dw_die_ref ptr_die
10611 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
10613 equate_type_number_to_die (type, ptr_die);
10614 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
10615 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
10618 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
10619 Use modified_type_die instead.
10620 We keep this code here just in case these types of DIEs may be needed to
10621 represent certain things in other languages (e.g. Pascal) someday. */
10623 static void
10624 gen_reference_type_die (type, context_die)
10625 tree type;
10626 dw_die_ref context_die;
10628 dw_die_ref ref_die
10629 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
10631 equate_type_number_to_die (type, ref_die);
10632 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
10633 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
10635 #endif
10637 /* Generate a DIE for a pointer to a member type. */
10639 static void
10640 gen_ptr_to_mbr_type_die (type, context_die)
10641 tree type;
10642 dw_die_ref context_die;
10644 dw_die_ref ptr_die
10645 = new_die (DW_TAG_ptr_to_member_type,
10646 scope_die_for (type, context_die), type);
10648 equate_type_number_to_die (type, ptr_die);
10649 add_AT_die_ref (ptr_die, DW_AT_containing_type,
10650 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
10651 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
10654 /* Generate the DIE for the compilation unit. */
10656 static dw_die_ref
10657 gen_compile_unit_die (filename)
10658 const char *filename;
10660 dw_die_ref die;
10661 char producer[250];
10662 const char *wd = getpwd ();
10663 const char *language_string = lang_hooks.name;
10664 int language;
10666 die = new_die (DW_TAG_compile_unit, NULL, NULL);
10667 add_name_attribute (die, filename);
10669 if (wd != NULL && filename[0] != DIR_SEPARATOR)
10670 add_AT_string (die, DW_AT_comp_dir, wd);
10672 sprintf (producer, "%s %s", language_string, version_string);
10674 #ifdef MIPS_DEBUGGING_INFO
10675 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
10676 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
10677 not appear in the producer string, the debugger reaches the conclusion
10678 that the object file is stripped and has no debugging information.
10679 To get the MIPS/SGI debugger to believe that there is debugging
10680 information in the object file, we add a -g to the producer string. */
10681 if (debug_info_level > DINFO_LEVEL_TERSE)
10682 strcat (producer, " -g");
10683 #endif
10685 add_AT_string (die, DW_AT_producer, producer);
10687 if (strcmp (language_string, "GNU C++") == 0)
10688 language = DW_LANG_C_plus_plus;
10689 else if (strcmp (language_string, "GNU Ada") == 0)
10690 language = DW_LANG_Ada83;
10691 else if (strcmp (language_string, "GNU F77") == 0)
10692 language = DW_LANG_Fortran77;
10693 else if (strcmp (language_string, "GNU Pascal") == 0)
10694 language = DW_LANG_Pascal83;
10695 else if (strcmp (language_string, "GNU Java") == 0)
10696 language = DW_LANG_Java;
10697 else if (flag_traditional)
10698 language = DW_LANG_C;
10699 else
10700 language = DW_LANG_C89;
10702 add_AT_unsigned (die, DW_AT_language, language);
10703 return die;
10706 /* Generate a DIE for a string type. */
10708 static void
10709 gen_string_type_die (type, context_die)
10710 tree type;
10711 dw_die_ref context_die;
10713 dw_die_ref type_die
10714 = new_die (DW_TAG_string_type, scope_die_for (type, context_die), type);
10716 equate_type_number_to_die (type, type_die);
10718 /* ??? Fudge the string length attribute for now.
10719 TODO: add string length info. */
10720 #if 0
10721 string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
10722 bound_representation (upper_bound, 0, 'u');
10723 #endif
10726 /* Generate the DIE for a base class. */
10728 static void
10729 gen_inheritance_die (binfo, context_die)
10730 tree binfo;
10731 dw_die_ref context_die;
10733 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
10735 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
10736 add_data_member_location_attribute (die, binfo);
10738 if (TREE_VIA_VIRTUAL (binfo))
10739 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10741 if (TREE_VIA_PUBLIC (binfo))
10742 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
10743 else if (TREE_VIA_PROTECTED (binfo))
10744 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
10747 /* Generate a DIE for a class member. */
10749 static void
10750 gen_member_die (type, context_die)
10751 tree type;
10752 dw_die_ref context_die;
10754 tree member;
10755 dw_die_ref child;
10757 /* If this is not an incomplete type, output descriptions of each of its
10758 members. Note that as we output the DIEs necessary to represent the
10759 members of this record or union type, we will also be trying to output
10760 DIEs to represent the *types* of those members. However the `type'
10761 function (above) will specifically avoid generating type DIEs for member
10762 types *within* the list of member DIEs for this (containing) type except
10763 for those types (of members) which are explicitly marked as also being
10764 members of this (containing) type themselves. The g++ front- end can
10765 force any given type to be treated as a member of some other (containing)
10766 type by setting the TYPE_CONTEXT of the given (member) type to point to
10767 the TREE node representing the appropriate (containing) type. */
10769 /* First output info about the base classes. */
10770 if (TYPE_BINFO (type) && TYPE_BINFO_BASETYPES (type))
10772 tree bases = TYPE_BINFO_BASETYPES (type);
10773 int n_bases = TREE_VEC_LENGTH (bases);
10774 int i;
10776 for (i = 0; i < n_bases; i++)
10777 gen_inheritance_die (TREE_VEC_ELT (bases, i), context_die);
10780 /* Now output info about the data members and type members. */
10781 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
10783 /* If we thought we were generating minimal debug info for TYPE
10784 and then changed our minds, some of the member declarations
10785 may have already been defined. Don't define them again, but
10786 do put them in the right order. */
10788 child = lookup_decl_die (member);
10789 if (child)
10790 splice_child_die (context_die, child);
10791 else
10792 gen_decl_die (member, context_die);
10795 /* Now output info about the function members (if any). */
10796 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
10798 /* Don't include clones in the member list. */
10799 if (DECL_ABSTRACT_ORIGIN (member))
10800 continue;
10802 child = lookup_decl_die (member);
10803 if (child)
10804 splice_child_die (context_die, child);
10805 else
10806 gen_decl_die (member, context_die);
10810 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
10811 is set, we pretend that the type was never defined, so we only get the
10812 member DIEs needed by later specification DIEs. */
10814 static void
10815 gen_struct_or_union_type_die (type, context_die)
10816 tree type;
10817 dw_die_ref context_die;
10819 dw_die_ref type_die = lookup_type_die (type);
10820 dw_die_ref scope_die = 0;
10821 int nested = 0;
10822 int complete = (TYPE_SIZE (type)
10823 && (! TYPE_STUB_DECL (type)
10824 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
10826 if (type_die && ! complete)
10827 return;
10829 if (TYPE_CONTEXT (type) != NULL_TREE
10830 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type)))
10831 nested = 1;
10833 scope_die = scope_die_for (type, context_die);
10835 if (! type_die || (nested && scope_die == comp_unit_die))
10836 /* First occurrence of type or toplevel definition of nested class. */
10838 dw_die_ref old_die = type_die;
10840 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
10841 ? DW_TAG_structure_type : DW_TAG_union_type,
10842 scope_die, type);
10843 equate_type_number_to_die (type, type_die);
10844 if (old_die)
10845 add_AT_die_ref (type_die, DW_AT_specification, old_die);
10846 else
10847 add_name_attribute (type_die, type_tag (type));
10849 else
10850 remove_AT (type_die, DW_AT_declaration);
10852 /* If this type has been completed, then give it a byte_size attribute and
10853 then give a list of members. */
10854 if (complete)
10856 /* Prevent infinite recursion in cases where the type of some member of
10857 this type is expressed in terms of this type itself. */
10858 TREE_ASM_WRITTEN (type) = 1;
10859 add_byte_size_attribute (type_die, type);
10860 if (TYPE_STUB_DECL (type) != NULL_TREE)
10861 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
10863 /* If the first reference to this type was as the return type of an
10864 inline function, then it may not have a parent. Fix this now. */
10865 if (type_die->die_parent == NULL)
10866 add_child_die (scope_die, type_die);
10868 push_decl_scope (type);
10869 gen_member_die (type, type_die);
10870 pop_decl_scope ();
10872 /* GNU extension: Record what type our vtable lives in. */
10873 if (TYPE_VFIELD (type))
10875 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
10877 gen_type_die (vtype, context_die);
10878 add_AT_die_ref (type_die, DW_AT_containing_type,
10879 lookup_type_die (vtype));
10882 else
10884 add_AT_flag (type_die, DW_AT_declaration, 1);
10886 /* We don't need to do this for function-local types. */
10887 if (TYPE_STUB_DECL (type)
10888 && ! decl_function_context (TYPE_STUB_DECL (type)))
10889 VARRAY_PUSH_TREE (incomplete_types, type);
10893 /* Generate a DIE for a subroutine _type_. */
10895 static void
10896 gen_subroutine_type_die (type, context_die)
10897 tree type;
10898 dw_die_ref context_die;
10900 tree return_type = TREE_TYPE (type);
10901 dw_die_ref subr_die
10902 = new_die (DW_TAG_subroutine_type,
10903 scope_die_for (type, context_die), type);
10905 equate_type_number_to_die (type, subr_die);
10906 add_prototyped_attribute (subr_die, type);
10907 add_type_attribute (subr_die, return_type, 0, 0, context_die);
10908 gen_formal_types_die (type, subr_die);
10911 /* Generate a DIE for a type definition */
10913 static void
10914 gen_typedef_die (decl, context_die)
10915 tree decl;
10916 dw_die_ref context_die;
10918 dw_die_ref type_die;
10919 tree origin;
10921 if (TREE_ASM_WRITTEN (decl))
10922 return;
10924 TREE_ASM_WRITTEN (decl) = 1;
10925 type_die = new_die (DW_TAG_typedef, context_die, decl);
10926 origin = decl_ultimate_origin (decl);
10927 if (origin != NULL)
10928 add_abstract_origin_attribute (type_die, origin);
10929 else
10931 tree type;
10933 add_name_and_src_coords_attributes (type_die, decl);
10934 if (DECL_ORIGINAL_TYPE (decl))
10936 type = DECL_ORIGINAL_TYPE (decl);
10938 if (type == TREE_TYPE (decl))
10939 abort ();
10940 else
10941 equate_type_number_to_die (TREE_TYPE (decl), type_die);
10943 else
10944 type = TREE_TYPE (decl);
10946 add_type_attribute (type_die, type, TREE_READONLY (decl),
10947 TREE_THIS_VOLATILE (decl), context_die);
10950 if (DECL_ABSTRACT (decl))
10951 equate_decl_number_to_die (decl, type_die);
10954 /* Generate a type description DIE. */
10956 static void
10957 gen_type_die (type, context_die)
10958 tree type;
10959 dw_die_ref context_die;
10961 int need_pop;
10963 if (type == NULL_TREE || type == error_mark_node)
10964 return;
10966 /* We are going to output a DIE to represent the unqualified version of
10967 this type (i.e. without any const or volatile qualifiers) so get the
10968 main variant (i.e. the unqualified version) of this type now. */
10969 type = type_main_variant (type);
10971 if (TREE_ASM_WRITTEN (type))
10972 return;
10974 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10975 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
10977 TREE_ASM_WRITTEN (type) = 1;
10978 gen_decl_die (TYPE_NAME (type), context_die);
10979 return;
10982 switch (TREE_CODE (type))
10984 case ERROR_MARK:
10985 break;
10987 case POINTER_TYPE:
10988 case REFERENCE_TYPE:
10989 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
10990 ensures that the gen_type_die recursion will terminate even if the
10991 type is recursive. Recursive types are possible in Ada. */
10992 /* ??? We could perhaps do this for all types before the switch
10993 statement. */
10994 TREE_ASM_WRITTEN (type) = 1;
10996 /* For these types, all that is required is that we output a DIE (or a
10997 set of DIEs) to represent the "basis" type. */
10998 gen_type_die (TREE_TYPE (type), context_die);
10999 break;
11001 case OFFSET_TYPE:
11002 /* This code is used for C++ pointer-to-data-member types.
11003 Output a description of the relevant class type. */
11004 gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
11006 /* Output a description of the type of the object pointed to. */
11007 gen_type_die (TREE_TYPE (type), context_die);
11009 /* Now output a DIE to represent this pointer-to-data-member type
11010 itself. */
11011 gen_ptr_to_mbr_type_die (type, context_die);
11012 break;
11014 case SET_TYPE:
11015 gen_type_die (TYPE_DOMAIN (type), context_die);
11016 gen_set_type_die (type, context_die);
11017 break;
11019 case FILE_TYPE:
11020 gen_type_die (TREE_TYPE (type), context_die);
11021 abort (); /* No way to represent these in Dwarf yet! */
11022 break;
11024 case FUNCTION_TYPE:
11025 /* Force out return type (in case it wasn't forced out already). */
11026 gen_type_die (TREE_TYPE (type), context_die);
11027 gen_subroutine_type_die (type, context_die);
11028 break;
11030 case METHOD_TYPE:
11031 /* Force out return type (in case it wasn't forced out already). */
11032 gen_type_die (TREE_TYPE (type), context_die);
11033 gen_subroutine_type_die (type, context_die);
11034 break;
11036 case ARRAY_TYPE:
11037 if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
11039 gen_type_die (TREE_TYPE (type), context_die);
11040 gen_string_type_die (type, context_die);
11042 else
11043 gen_array_type_die (type, context_die);
11044 break;
11046 case VECTOR_TYPE:
11047 gen_type_die (TYPE_DEBUG_REPRESENTATION_TYPE (type), context_die);
11048 break;
11050 case ENUMERAL_TYPE:
11051 case RECORD_TYPE:
11052 case UNION_TYPE:
11053 case QUAL_UNION_TYPE:
11054 /* If this is a nested type whose containing class hasn't been written
11055 out yet, writing it out will cover this one, too. This does not apply
11056 to instantiations of member class templates; they need to be added to
11057 the containing class as they are generated. FIXME: This hurts the
11058 idea of combining type decls from multiple TUs, since we can't predict
11059 what set of template instantiations we'll get. */
11060 if (TYPE_CONTEXT (type)
11061 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
11062 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
11064 gen_type_die (TYPE_CONTEXT (type), context_die);
11066 if (TREE_ASM_WRITTEN (type))
11067 return;
11069 /* If that failed, attach ourselves to the stub. */
11070 push_decl_scope (TYPE_CONTEXT (type));
11071 context_die = lookup_type_die (TYPE_CONTEXT (type));
11072 need_pop = 1;
11074 else
11075 need_pop = 0;
11077 if (TREE_CODE (type) == ENUMERAL_TYPE)
11078 gen_enumeration_type_die (type, context_die);
11079 else
11080 gen_struct_or_union_type_die (type, context_die);
11082 if (need_pop)
11083 pop_decl_scope ();
11085 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
11086 it up if it is ever completed. gen_*_type_die will set it for us
11087 when appropriate. */
11088 return;
11090 case VOID_TYPE:
11091 case INTEGER_TYPE:
11092 case REAL_TYPE:
11093 case COMPLEX_TYPE:
11094 case BOOLEAN_TYPE:
11095 case CHAR_TYPE:
11096 /* No DIEs needed for fundamental types. */
11097 break;
11099 case LANG_TYPE:
11100 /* No Dwarf representation currently defined. */
11101 break;
11103 default:
11104 abort ();
11107 TREE_ASM_WRITTEN (type) = 1;
11110 /* Generate a DIE for a tagged type instantiation. */
11112 static void
11113 gen_tagged_type_instantiation_die (type, context_die)
11114 tree type;
11115 dw_die_ref context_die;
11117 if (type == NULL_TREE || type == error_mark_node)
11118 return;
11120 /* We are going to output a DIE to represent the unqualified version of
11121 this type (i.e. without any const or volatile qualifiers) so make sure
11122 that we have the main variant (i.e. the unqualified version) of this
11123 type now. */
11124 if (type != type_main_variant (type))
11125 abort ();
11127 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
11128 an instance of an unresolved type. */
11130 switch (TREE_CODE (type))
11132 case ERROR_MARK:
11133 break;
11135 case ENUMERAL_TYPE:
11136 gen_inlined_enumeration_type_die (type, context_die);
11137 break;
11139 case RECORD_TYPE:
11140 gen_inlined_structure_type_die (type, context_die);
11141 break;
11143 case UNION_TYPE:
11144 case QUAL_UNION_TYPE:
11145 gen_inlined_union_type_die (type, context_die);
11146 break;
11148 default:
11149 abort ();
11153 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
11154 things which are local to the given block. */
11156 static void
11157 gen_block_die (stmt, context_die, depth)
11158 tree stmt;
11159 dw_die_ref context_die;
11160 int depth;
11162 int must_output_die = 0;
11163 tree origin;
11164 tree decl;
11165 enum tree_code origin_code;
11167 /* Ignore blocks never really used to make RTL. */
11168 if (stmt == NULL_TREE || !TREE_USED (stmt)
11169 || (!TREE_ASM_WRITTEN (stmt) && !BLOCK_ABSTRACT (stmt)))
11170 return;
11172 /* If the block is one fragment of a non-contiguous block, do not
11173 process the variables, since they will have been done by the
11174 origin block. Do process subblocks. */
11175 if (BLOCK_FRAGMENT_ORIGIN (stmt))
11177 tree sub;
11179 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
11180 gen_block_die (sub, context_die, depth + 1);
11182 return;
11185 /* Determine the "ultimate origin" of this block. This block may be an
11186 inlined instance of an inlined instance of inline function, so we have
11187 to trace all of the way back through the origin chain to find out what
11188 sort of node actually served as the original seed for the creation of
11189 the current block. */
11190 origin = block_ultimate_origin (stmt);
11191 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
11193 /* Determine if we need to output any Dwarf DIEs at all to represent this
11194 block. */
11195 if (origin_code == FUNCTION_DECL)
11196 /* The outer scopes for inlinings *must* always be represented. We
11197 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
11198 must_output_die = 1;
11199 else
11201 /* In the case where the current block represents an inlining of the
11202 "body block" of an inline function, we must *NOT* output any DIE for
11203 this block because we have already output a DIE to represent the whole
11204 inlined function scope and the "body block" of any function doesn't
11205 really represent a different scope according to ANSI C rules. So we
11206 check here to make sure that this block does not represent a "body
11207 block inlining" before trying to set the MUST_OUTPUT_DIE flag. */
11208 if (! is_body_block (origin ? origin : stmt))
11210 /* Determine if this block directly contains any "significant"
11211 local declarations which we will need to output DIEs for. */
11212 if (debug_info_level > DINFO_LEVEL_TERSE)
11213 /* We are not in terse mode so *any* local declaration counts
11214 as being a "significant" one. */
11215 must_output_die = (BLOCK_VARS (stmt) != NULL);
11216 else
11217 /* We are in terse mode, so only local (nested) function
11218 definitions count as "significant" local declarations. */
11219 for (decl = BLOCK_VARS (stmt);
11220 decl != NULL; decl = TREE_CHAIN (decl))
11221 if (TREE_CODE (decl) == FUNCTION_DECL
11222 && DECL_INITIAL (decl))
11224 must_output_die = 1;
11225 break;
11230 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
11231 DIE for any block which contains no significant local declarations at
11232 all. Rather, in such cases we just call `decls_for_scope' so that any
11233 needed Dwarf info for any sub-blocks will get properly generated. Note
11234 that in terse mode, our definition of what constitutes a "significant"
11235 local declaration gets restricted to include only inlined function
11236 instances and local (nested) function definitions. */
11237 if (must_output_die)
11239 if (origin_code == FUNCTION_DECL)
11240 gen_inlined_subroutine_die (stmt, context_die, depth);
11241 else
11242 gen_lexical_block_die (stmt, context_die, depth);
11244 else
11245 decls_for_scope (stmt, context_die, depth);
11248 /* Generate all of the decls declared within a given scope and (recursively)
11249 all of its sub-blocks. */
11251 static void
11252 decls_for_scope (stmt, context_die, depth)
11253 tree stmt;
11254 dw_die_ref context_die;
11255 int depth;
11257 tree decl;
11258 tree subblocks;
11260 /* Ignore blocks never really used to make RTL. */
11261 if (stmt == NULL_TREE || ! TREE_USED (stmt))
11262 return;
11264 /* Output the DIEs to represent all of the data objects and typedefs
11265 declared directly within this block but not within any nested
11266 sub-blocks. Also, nested function and tag DIEs have been
11267 generated with a parent of NULL; fix that up now. */
11268 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
11270 dw_die_ref die;
11272 if (TREE_CODE (decl) == FUNCTION_DECL)
11273 die = lookup_decl_die (decl);
11274 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
11275 die = lookup_type_die (TREE_TYPE (decl));
11276 else
11277 die = NULL;
11279 if (die != NULL && die->die_parent == NULL)
11280 add_child_die (context_die, die);
11281 else
11282 gen_decl_die (decl, context_die);
11285 /* Output the DIEs to represent all sub-blocks (and the items declared
11286 therein) of this block. */
11287 for (subblocks = BLOCK_SUBBLOCKS (stmt);
11288 subblocks != NULL;
11289 subblocks = BLOCK_CHAIN (subblocks))
11290 gen_block_die (subblocks, context_die, depth + 1);
11293 /* Is this a typedef we can avoid emitting? */
11295 static inline int
11296 is_redundant_typedef (decl)
11297 tree decl;
11299 if (TYPE_DECL_IS_STUB (decl))
11300 return 1;
11302 if (DECL_ARTIFICIAL (decl)
11303 && DECL_CONTEXT (decl)
11304 && is_tagged_type (DECL_CONTEXT (decl))
11305 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
11306 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
11307 /* Also ignore the artificial member typedef for the class name. */
11308 return 1;
11310 return 0;
11313 /* Generate Dwarf debug information for a decl described by DECL. */
11315 static void
11316 gen_decl_die (decl, context_die)
11317 tree decl;
11318 dw_die_ref context_die;
11320 tree origin;
11322 if (DECL_P (decl) && DECL_IGNORED_P (decl))
11323 return;
11325 switch (TREE_CODE (decl))
11327 case ERROR_MARK:
11328 break;
11330 case CONST_DECL:
11331 /* The individual enumerators of an enum type get output when we output
11332 the Dwarf representation of the relevant enum type itself. */
11333 break;
11335 case FUNCTION_DECL:
11336 /* Don't output any DIEs to represent mere function declarations,
11337 unless they are class members or explicit block externs. */
11338 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
11339 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
11340 break;
11342 /* If we're emitting a clone, emit info for the abstract instance. */
11343 if (DECL_ORIGIN (decl) != decl)
11344 dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
11346 /* If we're emitting an out-of-line copy of an inline function,
11347 emit info for the abstract instance and set up to refer to it. */
11348 else if (DECL_INLINE (decl) && ! DECL_ABSTRACT (decl)
11349 && ! class_scope_p (context_die)
11350 /* dwarf2out_abstract_function won't emit a die if this is just
11351 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
11352 that case, because that works only if we have a die. */
11353 && DECL_INITIAL (decl) != NULL_TREE)
11355 dwarf2out_abstract_function (decl);
11356 set_decl_origin_self (decl);
11359 /* Otherwise we're emitting the primary DIE for this decl. */
11360 else if (debug_info_level > DINFO_LEVEL_TERSE)
11362 /* Before we describe the FUNCTION_DECL itself, make sure that we
11363 have described its return type. */
11364 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
11366 /* And its virtual context. */
11367 if (DECL_VINDEX (decl) != NULL_TREE)
11368 gen_type_die (DECL_CONTEXT (decl), context_die);
11370 /* And its containing type. */
11371 origin = decl_class_context (decl);
11372 if (origin != NULL_TREE)
11373 gen_type_die_for_member (origin, decl, context_die);
11376 /* Now output a DIE to represent the function itself. */
11377 gen_subprogram_die (decl, context_die);
11378 break;
11380 case TYPE_DECL:
11381 /* If we are in terse mode, don't generate any DIEs to represent any
11382 actual typedefs. */
11383 if (debug_info_level <= DINFO_LEVEL_TERSE)
11384 break;
11386 /* In the special case of a TYPE_DECL node representing the declaration
11387 of some type tag, if the given TYPE_DECL is marked as having been
11388 instantiated from some other (original) TYPE_DECL node (e.g. one which
11389 was generated within the original definition of an inline function) we
11390 have to generate a special (abbreviated) DW_TAG_structure_type,
11391 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. */
11392 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
11394 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
11395 break;
11398 if (is_redundant_typedef (decl))
11399 gen_type_die (TREE_TYPE (decl), context_die);
11400 else
11401 /* Output a DIE to represent the typedef itself. */
11402 gen_typedef_die (decl, context_die);
11403 break;
11405 case LABEL_DECL:
11406 if (debug_info_level >= DINFO_LEVEL_NORMAL)
11407 gen_label_die (decl, context_die);
11408 break;
11410 case VAR_DECL:
11411 /* If we are in terse mode, don't generate any DIEs to represent any
11412 variable declarations or definitions. */
11413 if (debug_info_level <= DINFO_LEVEL_TERSE)
11414 break;
11416 /* Output any DIEs that are needed to specify the type of this data
11417 object. */
11418 gen_type_die (TREE_TYPE (decl), context_die);
11420 /* And its containing type. */
11421 origin = decl_class_context (decl);
11422 if (origin != NULL_TREE)
11423 gen_type_die_for_member (origin, decl, context_die);
11425 /* Now output the DIE to represent the data object itself. This gets
11426 complicated because of the possibility that the VAR_DECL really
11427 represents an inlined instance of a formal parameter for an inline
11428 function. */
11429 origin = decl_ultimate_origin (decl);
11430 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
11431 gen_formal_parameter_die (decl, context_die);
11432 else
11433 gen_variable_die (decl, context_die);
11434 break;
11436 case FIELD_DECL:
11437 /* Ignore the nameless fields that are used to skip bits but handle C++
11438 anonymous unions. */
11439 if (DECL_NAME (decl) != NULL_TREE
11440 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE)
11442 gen_type_die (member_declared_type (decl), context_die);
11443 gen_field_die (decl, context_die);
11445 break;
11447 case PARM_DECL:
11448 gen_type_die (TREE_TYPE (decl), context_die);
11449 gen_formal_parameter_die (decl, context_die);
11450 break;
11452 case NAMESPACE_DECL:
11453 /* Ignore for now. */
11454 break;
11456 default:
11457 abort ();
11461 static void
11462 mark_limbo_die_list (ptr)
11463 void *ptr ATTRIBUTE_UNUSED;
11465 limbo_die_node *node;
11466 for (node = limbo_die_list; node ; node = node->next)
11467 ggc_mark_tree (node->created_for);
11470 /* Add Ada "use" clause information for SGI Workshop debugger. */
11472 void
11473 dwarf2out_add_library_unit_info (filename, context_list)
11474 const char *filename;
11475 const char *context_list;
11477 unsigned int file_index;
11479 if (filename != NULL)
11481 dw_die_ref unit_die = new_die (DW_TAG_module, comp_unit_die, NULL);
11482 tree context_list_decl
11483 = build_decl (LABEL_DECL, get_identifier (context_list),
11484 void_type_node);
11486 TREE_PUBLIC (context_list_decl) = TRUE;
11487 add_name_attribute (unit_die, context_list);
11488 file_index = lookup_filename (filename);
11489 add_AT_unsigned (unit_die, DW_AT_decl_file, file_index);
11490 add_pubname (context_list_decl, unit_die);
11494 /* Output debug information for global decl DECL. Called from toplev.c after
11495 compilation proper has finished. */
11497 static void
11498 dwarf2out_global_decl (decl)
11499 tree decl;
11501 /* Output DWARF2 information for file-scope tentative data object
11502 declarations, file-scope (extern) function declarations (which had no
11503 corresponding body) and file-scope tagged type declarations and
11504 definitions which have not yet been forced out. */
11505 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
11506 dwarf2out_decl (decl);
11509 /* Write the debugging output for DECL. */
11511 void
11512 dwarf2out_decl (decl)
11513 tree decl;
11515 dw_die_ref context_die = comp_unit_die;
11517 switch (TREE_CODE (decl))
11519 case ERROR_MARK:
11520 return;
11522 case FUNCTION_DECL:
11523 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration of a
11524 builtin function. Explicit programmer-supplied declarations of
11525 these same functions should NOT be ignored however. */
11526 if (DECL_EXTERNAL (decl) && DECL_BUILT_IN (decl))
11527 return;
11529 /* What we would really like to do here is to filter out all mere
11530 file-scope declarations of file-scope functions which are never
11531 referenced later within this translation unit (and keep all of ones
11532 that *are* referenced later on) but we aren't clairvoyant, so we have
11533 no idea which functions will be referenced in the future (i.e. later
11534 on within the current translation unit). So here we just ignore all
11535 file-scope function declarations which are not also definitions. If
11536 and when the debugger needs to know something about these functions,
11537 it will have to hunt around and find the DWARF information associated
11538 with the definition of the function.
11540 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
11541 nodes represent definitions and which ones represent mere
11542 declarations. We have to check DECL_INITIAL instead. That's because
11543 the C front-end supports some weird semantics for "extern inline"
11544 function definitions. These can get inlined within the current
11545 translation unit (an thus, we need to generate Dwarf info for their
11546 abstract instances so that the Dwarf info for the concrete inlined
11547 instances can have something to refer to) but the compiler never
11548 generates any out-of-lines instances of such things (despite the fact
11549 that they *are* definitions).
11551 The important point is that the C front-end marks these "extern
11552 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
11553 them anyway. Note that the C++ front-end also plays some similar games
11554 for inline function definitions appearing within include files which
11555 also contain `#pragma interface' pragmas. */
11556 if (DECL_INITIAL (decl) == NULL_TREE)
11557 return;
11559 /* If we're a nested function, initially use a parent of NULL; if we're
11560 a plain function, this will be fixed up in decls_for_scope. If
11561 we're a method, it will be ignored, since we already have a DIE. */
11562 if (decl_function_context (decl))
11563 context_die = NULL;
11564 break;
11566 case VAR_DECL:
11567 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
11568 declaration and if the declaration was never even referenced from
11569 within this entire compilation unit. We suppress these DIEs in
11570 order to save space in the .debug section (by eliminating entries
11571 which are probably useless). Note that we must not suppress
11572 block-local extern declarations (whether used or not) because that
11573 would screw-up the debugger's name lookup mechanism and cause it to
11574 miss things which really ought to be in scope at a given point. */
11575 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
11576 return;
11578 /* If we are in terse mode, don't generate any DIEs to represent any
11579 variable declarations or definitions. */
11580 if (debug_info_level <= DINFO_LEVEL_TERSE)
11581 return;
11582 break;
11584 case TYPE_DECL:
11585 /* Don't emit stubs for types unless they are needed by other DIEs. */
11586 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
11587 return;
11589 /* Don't bother trying to generate any DIEs to represent any of the
11590 normal built-in types for the language we are compiling. */
11591 if (DECL_SOURCE_LINE (decl) == 0)
11593 /* OK, we need to generate one for `bool' so GDB knows what type
11594 comparisons have. */
11595 if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
11596 == DW_LANG_C_plus_plus)
11597 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
11598 && ! DECL_IGNORED_P (decl))
11599 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
11601 return;
11604 /* If we are in terse mode, don't generate any DIEs for types. */
11605 if (debug_info_level <= DINFO_LEVEL_TERSE)
11606 return;
11608 /* If we're a function-scope tag, initially use a parent of NULL;
11609 this will be fixed up in decls_for_scope. */
11610 if (decl_function_context (decl))
11611 context_die = NULL;
11613 break;
11615 default:
11616 return;
11619 gen_decl_die (decl, context_die);
11622 /* Output a marker (i.e. a label) for the beginning of the generated code for
11623 a lexical block. */
11625 static void
11626 dwarf2out_begin_block (line, blocknum)
11627 unsigned int line ATTRIBUTE_UNUSED;
11628 unsigned int blocknum;
11630 function_section (current_function_decl);
11631 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
11634 /* Output a marker (i.e. a label) for the end of the generated code for a
11635 lexical block. */
11637 static void
11638 dwarf2out_end_block (line, blocknum)
11639 unsigned int line ATTRIBUTE_UNUSED;
11640 unsigned int blocknum;
11642 function_section (current_function_decl);
11643 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
11646 /* Returns nonzero if it is appropriate not to emit any debugging
11647 information for BLOCK, because it doesn't contain any instructions.
11649 Don't allow this for blocks with nested functions or local classes
11650 as we would end up with orphans, and in the presence of scheduling
11651 we may end up calling them anyway. */
11653 static bool
11654 dwarf2out_ignore_block (block)
11655 tree block;
11657 tree decl;
11659 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
11660 if (TREE_CODE (decl) == FUNCTION_DECL
11661 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
11662 return 0;
11664 return 1;
11667 /* Lookup FILE_NAME (in the list of filenames that we know about here in
11668 dwarf2out.c) and return its "index". The index of each (known) filename is
11669 just a unique number which is associated with only that one filename. We
11670 need such numbers for the sake of generating labels (in the .debug_sfnames
11671 section) and references to those files numbers (in the .debug_srcinfo
11672 and.debug_macinfo sections). If the filename given as an argument is not
11673 found in our current list, add it to the list and assign it the next
11674 available unique index number. In order to speed up searches, we remember
11675 the index of the filename was looked up last. This handles the majority of
11676 all searches. */
11678 static unsigned
11679 lookup_filename (file_name)
11680 const char *file_name;
11682 unsigned i;
11684 /* ??? Why isn't DECL_SOURCE_FILE left null instead. */
11685 if (strcmp (file_name, "<internal>") == 0
11686 || strcmp (file_name, "<built-in>") == 0)
11687 return 0;
11689 /* Check to see if the file name that was searched on the previous
11690 call matches this file name. If so, return the index. */
11691 if (file_table.last_lookup_index != 0)
11692 if (0 == strcmp (file_name,
11693 file_table.table[file_table.last_lookup_index]))
11694 return file_table.last_lookup_index;
11696 /* Didn't match the previous lookup, search the table */
11697 for (i = 1; i < file_table.in_use; i++)
11698 if (strcmp (file_name, file_table.table[i]) == 0)
11700 file_table.last_lookup_index = i;
11701 return i;
11704 /* Prepare to add a new table entry by making sure there is enough space in
11705 the table to do so. If not, expand the current table. */
11706 if (i == file_table.allocated)
11708 file_table.allocated = i + FILE_TABLE_INCREMENT;
11709 file_table.table = (char **)
11710 xrealloc (file_table.table, file_table.allocated * sizeof (char *));
11713 /* Add the new entry to the end of the filename table. */
11714 file_table.table[i] = xstrdup (file_name);
11715 file_table.in_use = i + 1;
11716 file_table.last_lookup_index = i;
11718 if (DWARF2_ASM_LINE_DEBUG_INFO)
11719 fprintf (asm_out_file, "\t.file %u \"%s\"\n", i, file_name);
11721 return i;
11724 static void
11725 init_file_table ()
11727 /* Allocate the initial hunk of the file_table. */
11728 file_table.table = (char **) xcalloc (FILE_TABLE_INCREMENT, sizeof (char *));
11729 file_table.allocated = FILE_TABLE_INCREMENT;
11731 /* Skip the first entry - file numbers begin at 1. */
11732 file_table.in_use = 1;
11733 file_table.last_lookup_index = 0;
11736 /* Output a label to mark the beginning of a source code line entry
11737 and record information relating to this source line, in
11738 'line_info_table' for later output of the .debug_line section. */
11740 static void
11741 dwarf2out_source_line (line, filename)
11742 unsigned int line;
11743 const char *filename;
11745 if (debug_info_level >= DINFO_LEVEL_NORMAL)
11747 function_section (current_function_decl);
11749 /* If requested, emit something human-readable. */
11750 if (flag_debug_asm)
11751 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
11752 filename, line);
11754 if (DWARF2_ASM_LINE_DEBUG_INFO)
11756 unsigned file_num = lookup_filename (filename);
11758 /* Emit the .loc directive understood by GNU as. */
11759 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
11761 /* Indicate that line number info exists. */
11762 line_info_table_in_use++;
11764 /* Indicate that multiple line number tables exist. */
11765 if (DECL_SECTION_NAME (current_function_decl))
11766 separate_line_info_table_in_use++;
11768 else if (DECL_SECTION_NAME (current_function_decl))
11770 dw_separate_line_info_ref line_info;
11771 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, SEPARATE_LINE_CODE_LABEL,
11772 separate_line_info_table_in_use);
11774 /* expand the line info table if necessary */
11775 if (separate_line_info_table_in_use
11776 == separate_line_info_table_allocated)
11778 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
11779 separate_line_info_table
11780 = (dw_separate_line_info_ref)
11781 xrealloc (separate_line_info_table,
11782 separate_line_info_table_allocated
11783 * sizeof (dw_separate_line_info_entry));
11786 /* Add the new entry at the end of the line_info_table. */
11787 line_info
11788 = &separate_line_info_table[separate_line_info_table_in_use++];
11789 line_info->dw_file_num = lookup_filename (filename);
11790 line_info->dw_line_num = line;
11791 line_info->function = current_funcdef_number;
11793 else
11795 dw_line_info_ref line_info;
11797 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, LINE_CODE_LABEL,
11798 line_info_table_in_use);
11800 /* Expand the line info table if necessary. */
11801 if (line_info_table_in_use == line_info_table_allocated)
11803 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
11804 line_info_table
11805 = (dw_line_info_ref)
11806 xrealloc (line_info_table,
11807 (line_info_table_allocated
11808 * sizeof (dw_line_info_entry)));
11811 /* Add the new entry at the end of the line_info_table. */
11812 line_info = &line_info_table[line_info_table_in_use++];
11813 line_info->dw_file_num = lookup_filename (filename);
11814 line_info->dw_line_num = line;
11819 /* Record the beginning of a new source file. */
11821 static void
11822 dwarf2out_start_source_file (lineno, filename)
11823 unsigned int lineno;
11824 const char *filename;
11826 if (flag_eliminate_dwarf2_dups)
11828 /* Record the beginning of the file for break_out_includes. */
11829 dw_die_ref bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
11830 add_AT_string (bincl_die, DW_AT_name, filename);
11833 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
11835 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
11836 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
11837 dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
11838 lineno);
11839 dw2_asm_output_data_uleb128 (lookup_filename (filename),
11840 "Filename we just started");
11844 /* Record the end of a source file. */
11846 static void
11847 dwarf2out_end_source_file (lineno)
11848 unsigned int lineno ATTRIBUTE_UNUSED;
11850 if (flag_eliminate_dwarf2_dups)
11851 /* Record the end of the file for break_out_includes. */
11852 new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
11854 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
11856 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
11857 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
11861 /* Called from debug_define in toplev.c. The `buffer' parameter contains
11862 the tail part of the directive line, i.e. the part which is past the
11863 initial whitespace, #, whitespace, directive-name, whitespace part. */
11865 static void
11866 dwarf2out_define (lineno, buffer)
11867 unsigned lineno ATTRIBUTE_UNUSED;
11868 const char *buffer ATTRIBUTE_UNUSED;
11870 static int initialized = 0;
11871 if (!initialized)
11873 dwarf2out_start_source_file (0, primary_filename);
11874 initialized = 1;
11877 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
11879 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
11880 dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
11881 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
11882 dw2_asm_output_nstring (buffer, -1, "The macro");
11886 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
11887 the tail part of the directive line, i.e. the part which is past the
11888 initial whitespace, #, whitespace, directive-name, whitespace part. */
11890 static void
11891 dwarf2out_undef (lineno, buffer)
11892 unsigned lineno ATTRIBUTE_UNUSED;
11893 const char *buffer ATTRIBUTE_UNUSED;
11895 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
11897 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
11898 dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
11899 dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
11900 dw2_asm_output_nstring (buffer, -1, "The macro");
11904 /* Set up for Dwarf output at the start of compilation. */
11906 static void
11907 dwarf2out_init (main_input_filename)
11908 const char *main_input_filename;
11910 init_file_table ();
11912 /* Remember the name of the primary input file. */
11913 primary_filename = main_input_filename;
11915 /* Add it to the file table first, under the assumption that we'll
11916 be emitting line number data for it first, which avoids having
11917 to add an initial DW_LNS_set_file. */
11918 lookup_filename (main_input_filename);
11920 /* Allocate the initial hunk of the decl_die_table. */
11921 decl_die_table
11922 = (dw_die_ref *) xcalloc (DECL_DIE_TABLE_INCREMENT, sizeof (dw_die_ref));
11923 decl_die_table_allocated = DECL_DIE_TABLE_INCREMENT;
11924 decl_die_table_in_use = 0;
11926 /* Allocate the initial hunk of the decl_scope_table. */
11927 VARRAY_TREE_INIT (decl_scope_table, 256, "decl_scope_table");
11928 ggc_add_tree_varray_root (&decl_scope_table, 1);
11930 /* Allocate the initial hunk of the abbrev_die_table. */
11931 abbrev_die_table
11932 = (dw_die_ref *) xcalloc (ABBREV_DIE_TABLE_INCREMENT,
11933 sizeof (dw_die_ref));
11934 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
11935 /* Zero-th entry is allocated, but unused */
11936 abbrev_die_table_in_use = 1;
11938 /* Allocate the initial hunk of the line_info_table. */
11939 line_info_table
11940 = (dw_line_info_ref) xcalloc (LINE_INFO_TABLE_INCREMENT,
11941 sizeof (dw_line_info_entry));
11942 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
11944 /* Zero-th entry is allocated, but unused */
11945 line_info_table_in_use = 1;
11947 /* Generate the initial DIE for the .debug section. Note that the (string)
11948 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
11949 will (typically) be a relative pathname and that this pathname should be
11950 taken as being relative to the directory from which the compiler was
11951 invoked when the given (base) source file was compiled. */
11952 comp_unit_die = gen_compile_unit_die (main_input_filename);
11954 VARRAY_TREE_INIT (incomplete_types, 64, "incomplete_types");
11955 ggc_add_tree_varray_root (&incomplete_types, 1);
11957 VARRAY_RTX_INIT (used_rtx_varray, 32, "used_rtx_varray");
11958 ggc_add_rtx_varray_root (&used_rtx_varray, 1);
11960 ggc_add_root (&limbo_die_list, 1, 1, mark_limbo_die_list);
11962 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
11963 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
11964 DEBUG_ABBREV_SECTION_LABEL, 0);
11965 if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
11966 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
11967 else
11968 strcpy (text_section_label, stripattributes (TEXT_SECTION_NAME));
11970 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
11971 DEBUG_INFO_SECTION_LABEL, 0);
11972 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
11973 DEBUG_LINE_SECTION_LABEL, 0);
11974 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
11975 DEBUG_RANGES_SECTION_LABEL, 0);
11976 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
11977 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
11978 named_section_flags (DEBUG_INFO_SECTION, SECTION_DEBUG);
11979 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11980 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
11981 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
11983 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
11985 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
11986 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
11987 DEBUG_MACINFO_SECTION_LABEL, 0);
11988 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
11991 if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
11993 text_section ();
11994 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
11998 /* Allocate a string in .debug_str hash table. */
12000 static hashnode
12001 indirect_string_alloc (tab)
12002 hash_table *tab ATTRIBUTE_UNUSED;
12004 struct indirect_string_node *node;
12006 node = xmalloc (sizeof (struct indirect_string_node));
12007 node->refcount = 0;
12008 node->form = 0;
12009 node->label = NULL;
12011 return (hashnode) node;
12014 /* A helper function for dwarf2out_finish called through
12015 ht_forall. Emit one queued .debug_str string. */
12017 static int
12018 output_indirect_string (pfile, h, v)
12019 struct cpp_reader *pfile ATTRIBUTE_UNUSED;
12020 hashnode h;
12021 const PTR v ATTRIBUTE_UNUSED;
12023 struct indirect_string_node *node = (struct indirect_string_node *) h;
12025 if (node->form == DW_FORM_strp)
12027 named_section_flags (DEBUG_STR_SECTION, DEBUG_STR_SECTION_FLAGS);
12028 ASM_OUTPUT_LABEL (asm_out_file, node->label);
12029 assemble_string ((const char *) HT_STR (&node->id),
12030 HT_LEN (&node->id) + 1);
12033 return 1;
12036 /* Output stuff that dwarf requires at the end of every file,
12037 and generate the DWARF-2 debugging info. */
12039 static void
12040 dwarf2out_finish (input_filename)
12041 const char *input_filename ATTRIBUTE_UNUSED;
12043 limbo_die_node *node, *next_node;
12044 dw_die_ref die = 0;
12046 /* Traverse the limbo die list, and add parent/child links. The only
12047 dies without parents that should be here are concrete instances of
12048 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
12049 For concrete instances, we can get the parent die from the abstract
12050 instance. */
12051 for (node = limbo_die_list; node; node = next_node)
12053 next_node = node->next;
12054 die = node->die;
12056 if (die->die_parent == NULL)
12058 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
12059 tree context;
12061 if (origin)
12062 add_child_die (origin->die_parent, die);
12063 else if (die == comp_unit_die)
12065 /* If this was an expression for a bound involved in a function
12066 return type, it may be a SAVE_EXPR for which we weren't able
12067 to find a DIE previously. So try now. */
12068 else if (node->created_for
12069 && TREE_CODE (node->created_for) == SAVE_EXPR
12070 && 0 != (origin = (lookup_decl_die
12071 (SAVE_EXPR_CONTEXT
12072 (node->created_for)))))
12073 add_child_die (origin, die);
12074 else if (errorcount > 0 || sorrycount > 0)
12075 /* It's OK to be confused by errors in the input. */
12076 add_child_die (comp_unit_die, die);
12077 else if (node->created_for
12078 && ((DECL_P (node->created_for)
12079 && (context = DECL_CONTEXT (node->created_for)))
12080 || (TYPE_P (node->created_for)
12081 && (context = TYPE_CONTEXT (node->created_for))))
12082 && TREE_CODE (context) == FUNCTION_DECL)
12084 /* In certain situations, the lexical block containing a
12085 nested function can be optimized away, which results
12086 in the nested function die being orphaned. Likewise
12087 with the return type of that nested function. Force
12088 this to be a child of the containing function. */
12089 origin = lookup_decl_die (context);
12090 if (! origin)
12091 abort ();
12092 add_child_die (origin, die);
12094 else
12095 abort ();
12098 free (node);
12101 limbo_die_list = NULL;
12103 /* Walk through the list of incomplete types again, trying once more to
12104 emit full debugging info for them. */
12105 retry_incomplete_types ();
12107 /* We need to reverse all the dies before break_out_includes, or
12108 we'll see the end of an include file before the beginning. */
12109 reverse_all_dies (comp_unit_die);
12111 /* Generate separate CUs for each of the include files we've seen.
12112 They will go into limbo_die_list. */
12113 if (flag_eliminate_dwarf2_dups)
12114 break_out_includes (comp_unit_die);
12116 /* Traverse the DIE's and add add sibling attributes to those DIE's
12117 that have children. */
12118 add_sibling_attributes (comp_unit_die);
12119 for (node = limbo_die_list; node; node = node->next)
12120 add_sibling_attributes (node->die);
12122 /* Output a terminator label for the .text section. */
12123 text_section ();
12124 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, TEXT_END_LABEL, 0);
12126 /* Output the source line correspondence table. We must do this
12127 even if there is no line information. Otherwise, on an empty
12128 translation unit, we will generate a present, but empty,
12129 .debug_info section. IRIX 6.5 `nm' will then complain when
12130 examining the file. */
12131 if (! DWARF2_ASM_LINE_DEBUG_INFO)
12133 named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
12134 output_line_info ();
12137 /* Output location list section if necessary. */
12138 if (have_location_lists)
12140 /* Output the location lists info. */
12141 named_section_flags (DEBUG_LOC_SECTION, SECTION_DEBUG);
12142 ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
12143 DEBUG_LOC_SECTION_LABEL, 0);
12144 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
12145 output_location_lists (die);
12146 have_location_lists = 0;
12149 /* We can only use the low/high_pc attributes if all of the code was
12150 in .text. */
12151 if (separate_line_info_table_in_use == 0)
12153 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
12154 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
12157 /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
12158 "base address". Use zero so that these addresses become absolute. */
12159 else if (have_location_lists || ranges_table_in_use)
12160 add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
12162 if (debug_info_level >= DINFO_LEVEL_NORMAL)
12163 add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
12164 debug_line_section_label);
12166 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
12167 add_AT_lbl_offset (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
12169 /* Output all of the compilation units. We put the main one last so that
12170 the offsets are available to output_pubnames. */
12171 for (node = limbo_die_list; node; node = node->next)
12172 output_comp_unit (node->die);
12174 output_comp_unit (comp_unit_die);
12176 /* Output the abbreviation table. */
12177 named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
12178 output_abbrev_section ();
12180 /* Output public names table if necessary. */
12181 if (pubname_table_in_use)
12183 named_section_flags (DEBUG_PUBNAMES_SECTION, SECTION_DEBUG);
12184 output_pubnames ();
12187 /* Output the address range information. We only put functions in the arange
12188 table, so don't write it out if we don't have any. */
12189 if (fde_table_in_use)
12191 named_section_flags (DEBUG_ARANGES_SECTION, SECTION_DEBUG);
12192 output_aranges ();
12195 /* Output ranges section if necessary. */
12196 if (ranges_table_in_use)
12198 named_section_flags (DEBUG_RANGES_SECTION, SECTION_DEBUG);
12199 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
12200 output_ranges ();
12203 /* Have to end the primary source file. */
12204 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
12206 named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
12207 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
12210 /* If we emitted any DW_FORM_strp form attribute, output the string
12211 table too. */
12212 if (debug_str_hash)
12213 ht_forall (debug_str_hash, output_indirect_string, NULL);
12215 #endif /* DWARF2_DEBUGGING_INFO || DWARF2_UNWIND_INFO */