* aclocal.m4 (gcc_AC_CHECK_DECL): Before attempting the test,
[official-gcc.git] / gcc / dwarf2out.c
blobed4970cbf39bf0f96cf2a977fd3eab23a8f3ddc3
1 /* Output Dwarf2 format symbol table information from the GNU C compiler.
2 Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001
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 GNU CC.
10 GNU CC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
15 GNU CC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GNU CC; see the file COPYING. If not, write to
22 the Free Software Foundation, 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
25 /* TODO: Implement .debug_str handling, and share entries somehow.
26 Emit .debug_line header even when there are no functions, since
27 the file numbers are used by .debug_info. Alternately, leave
28 out locations for types and decls.
29 Avoid talking about ctors and op= for PODs.
30 Factor out common prologue sequences into multiple CIEs. */
32 /* The first part of this file deals with the DWARF 2 frame unwind
33 information, which is also used by the GCC efficient exception handling
34 mechanism. The second part, controlled only by an #ifdef
35 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
36 information. */
38 #include "config.h"
39 #include "system.h"
40 #include "tree.h"
41 #include "flags.h"
42 #include "rtl.h"
43 #include "hard-reg-set.h"
44 #include "regs.h"
45 #include "insn-config.h"
46 #include "reload.h"
47 #include "output.h"
48 #include "expr.h"
49 #include "except.h"
50 #include "dwarf2.h"
51 #include "dwarf2out.h"
52 #include "toplev.h"
53 #include "varray.h"
54 #include "ggc.h"
55 #include "md5.h"
56 #include "tm_p.h"
58 /* DWARF2 Abbreviation Glossary:
59 CFA = Canonical Frame Address
60 a fixed address on the stack which identifies a call frame.
61 We define it to be the value of SP just before the call insn.
62 The CFA register and offset, which may change during the course
63 of the function, are used to calculate its value at runtime.
64 CFI = Call Frame Instruction
65 an instruction for the DWARF2 abstract machine
66 CIE = Common Information Entry
67 information describing information common to one or more FDEs
68 DIE = Debugging Information Entry
69 FDE = Frame Description Entry
70 information describing the stack call frame, in particular,
71 how to restore registers
73 DW_CFA_... = DWARF2 CFA call frame instruction
74 DW_TAG_... = DWARF2 DIE tag */
76 /* Decide whether we want to emit frame unwind information for the current
77 translation unit. */
79 int
80 dwarf2out_do_frame ()
82 return (write_symbols == DWARF2_DEBUG
83 #ifdef DWARF2_FRAME_INFO
84 || DWARF2_FRAME_INFO
85 #endif
86 #ifdef DWARF2_UNWIND_INFO
87 || flag_unwind_tables
88 || (flag_exceptions && ! exceptions_via_longjmp)
89 #endif
93 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
95 /* How to start an assembler comment. */
96 #ifndef ASM_COMMENT_START
97 #define ASM_COMMENT_START ";#"
98 #endif
100 typedef struct dw_cfi_struct *dw_cfi_ref;
101 typedef struct dw_fde_struct *dw_fde_ref;
102 typedef union dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
104 /* Call frames are described using a sequence of Call Frame
105 Information instructions. The register number, offset
106 and address fields are provided as possible operands;
107 their use is selected by the opcode field. */
109 typedef union dw_cfi_oprnd_struct
111 unsigned long dw_cfi_reg_num;
112 long int dw_cfi_offset;
113 const char *dw_cfi_addr;
114 struct dw_loc_descr_struct *dw_cfi_loc;
116 dw_cfi_oprnd;
118 typedef struct dw_cfi_struct
120 dw_cfi_ref dw_cfi_next;
121 enum dwarf_call_frame_info dw_cfi_opc;
122 dw_cfi_oprnd dw_cfi_oprnd1;
123 dw_cfi_oprnd dw_cfi_oprnd2;
125 dw_cfi_node;
127 /* This is how we define the location of the CFA. We use to handle it
128 as REG + OFFSET all the time, but now it can be more complex.
129 It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
130 Instead of passing around REG and OFFSET, we pass a copy
131 of this structure. */
132 typedef struct cfa_loc
134 unsigned long reg;
135 long offset;
136 long base_offset;
137 int indirect; /* 1 if CFA is accessed via a dereference. */
138 } dw_cfa_location;
140 /* All call frame descriptions (FDE's) in the GCC generated DWARF
141 refer to a single Common Information Entry (CIE), defined at
142 the beginning of the .debug_frame section. This used of a single
143 CIE obviates the need to keep track of multiple CIE's
144 in the DWARF generation routines below. */
146 typedef struct dw_fde_struct
148 const char *dw_fde_begin;
149 const char *dw_fde_current_label;
150 const char *dw_fde_end;
151 dw_cfi_ref dw_fde_cfi;
152 int nothrow;
154 dw_fde_node;
156 /* Maximum size (in bytes) of an artificially generated label. */
157 #define MAX_ARTIFICIAL_LABEL_BYTES 30
159 /* The size of the target's pointer type. */
160 #ifndef PTR_SIZE
161 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
162 #endif
164 /* The size of addresses as they appear in the Dwarf 2 data.
165 Some architectures use word addresses to refer to code locations,
166 but Dwarf 2 info always uses byte addresses. On such machines,
167 Dwarf 2 addresses need to be larger than the architecture's
168 pointers. */
169 #ifndef DWARF2_ADDR_SIZE
170 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
171 #endif
173 /* The size in bytes of a DWARF field indicating an offset or length
174 relative to a debug info section, specified to be 4 bytes in the
175 DWARF-2 specification. The SGI/MIPS ABI defines it to be the same
176 as PTR_SIZE. */
178 #ifndef DWARF_OFFSET_SIZE
179 #define DWARF_OFFSET_SIZE 4
180 #endif
182 #define DWARF_VERSION 2
184 /* Round SIZE up to the nearest BOUNDARY. */
185 #define DWARF_ROUND(SIZE,BOUNDARY) \
186 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
188 /* Offsets recorded in opcodes are a multiple of this alignment factor. */
189 #ifndef DWARF_CIE_DATA_ALIGNMENT
190 #ifdef STACK_GROWS_DOWNWARD
191 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
192 #else
193 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
194 #endif
195 #endif /* not DWARF_CIE_DATA_ALIGNMENT */
197 /* A pointer to the base of a table that contains frame description
198 information for each routine. */
199 static dw_fde_ref fde_table;
201 /* Number of elements currently allocated for fde_table. */
202 static unsigned fde_table_allocated;
204 /* Number of elements in fde_table currently in use. */
205 static unsigned fde_table_in_use;
207 /* Size (in elements) of increments by which we may expand the
208 fde_table. */
209 #define FDE_TABLE_INCREMENT 256
211 /* A list of call frame insns for the CIE. */
212 static dw_cfi_ref cie_cfi_head;
214 /* The number of the current function definition for which debugging
215 information is being generated. These numbers range from 1 up to the
216 maximum number of function definitions contained within the current
217 compilation unit. These numbers are used to create unique label id's
218 unique to each function definition. */
219 static unsigned current_funcdef_number = 0;
221 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
222 attribute that accelerates the lookup of the FDE associated
223 with the subprogram. This variable holds the table index of the FDE
224 associated with the current function (body) definition. */
225 static unsigned current_funcdef_fde;
227 /* Forward declarations for functions defined in this file. */
229 static char *stripattributes PARAMS ((const char *));
230 static const char *dwarf_cfi_name PARAMS ((unsigned));
231 static dw_cfi_ref new_cfi PARAMS ((void));
232 static void add_cfi PARAMS ((dw_cfi_ref *, dw_cfi_ref));
233 static unsigned long size_of_uleb128 PARAMS ((unsigned long));
234 static unsigned long size_of_sleb128 PARAMS ((long));
235 static void output_uleb128 PARAMS ((unsigned long));
236 static void output_sleb128 PARAMS ((long));
237 static void add_fde_cfi PARAMS ((const char *, dw_cfi_ref));
238 static void lookup_cfa_1 PARAMS ((dw_cfi_ref, dw_cfa_location *));
239 static void lookup_cfa PARAMS ((dw_cfa_location *));
240 static void reg_save PARAMS ((const char *, unsigned,
241 unsigned, long));
242 static void initial_return_save PARAMS ((rtx));
243 static long stack_adjust_offset PARAMS ((rtx));
244 static void output_cfi PARAMS ((dw_cfi_ref, dw_fde_ref));
245 static void output_call_frame_info PARAMS ((int));
246 static void dwarf2out_stack_adjust PARAMS ((rtx));
247 static void dwarf2out_frame_debug_expr PARAMS ((rtx, const char *));
249 /* Support for complex CFA locations. */
250 static void output_cfa_loc PARAMS ((dw_cfi_ref));
251 static void get_cfa_from_loc_descr PARAMS ((dw_cfa_location *,
252 struct dw_loc_descr_struct *));
253 static struct dw_loc_descr_struct *build_cfa_loc
254 PARAMS ((dw_cfa_location *));
255 static void def_cfa_1 PARAMS ((const char *, dw_cfa_location *));
257 /* Definitions of defaults for assembler-dependent names of various
258 pseudo-ops and section names.
259 Theses may be overridden in the tm.h file (if necessary) for a particular
260 assembler. */
262 #ifdef OBJECT_FORMAT_ELF
263 #ifndef UNALIGNED_SHORT_ASM_OP
264 #define UNALIGNED_SHORT_ASM_OP "\t.2byte\t"
265 #endif
266 #ifndef UNALIGNED_INT_ASM_OP
267 #define UNALIGNED_INT_ASM_OP "\t.4byte\t"
268 #endif
269 #ifndef UNALIGNED_DOUBLE_INT_ASM_OP
270 #define UNALIGNED_DOUBLE_INT_ASM_OP "\t.8byte\t"
271 #endif
272 #endif /* OBJECT_FORMAT_ELF */
274 #ifndef ASM_BYTE_OP
275 #define ASM_BYTE_OP "\t.byte\t"
276 #endif
278 /* Data and reference forms for relocatable data. */
279 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
280 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
282 /* Pseudo-op for defining a new section. */
283 #ifndef SECTION_ASM_OP
284 #define SECTION_ASM_OP "\t.section\t"
285 #endif
287 /* The default format used by the ASM_OUTPUT_SECTION macro (see below) to
288 print the SECTION_ASM_OP and the section name. The default here works for
289 almost all svr4 assemblers, except for the sparc, where the section name
290 must be enclosed in double quotes. (See sparcv4.h). */
291 #ifndef SECTION_FORMAT
292 #ifdef PUSHSECTION_FORMAT
293 #define SECTION_FORMAT PUSHSECTION_FORMAT
294 #else
295 #define SECTION_FORMAT "%s%s\n"
296 #endif
297 #endif
299 #ifndef FRAME_SECTION
300 #define FRAME_SECTION ".debug_frame"
301 #endif
303 #ifndef FUNC_BEGIN_LABEL
304 #define FUNC_BEGIN_LABEL "LFB"
305 #endif
306 #ifndef FUNC_END_LABEL
307 #define FUNC_END_LABEL "LFE"
308 #endif
309 #define CIE_AFTER_SIZE_LABEL "LSCIE"
310 #define CIE_END_LABEL "LECIE"
311 #define CIE_LENGTH_LABEL "LLCIE"
312 #define FDE_AFTER_SIZE_LABEL "LSFDE"
313 #define FDE_END_LABEL "LEFDE"
314 #define FDE_LENGTH_LABEL "LLFDE"
315 #define DIE_LABEL_PREFIX "DW"
317 /* Definitions of defaults for various types of primitive assembly language
318 output operations. These may be overridden from within the tm.h file,
319 but typically, that is unnecessary. */
321 #ifndef ASM_OUTPUT_SECTION
322 #define ASM_OUTPUT_SECTION(FILE, SECTION) \
323 fprintf ((FILE), SECTION_FORMAT, SECTION_ASM_OP, SECTION)
324 #endif
326 #ifndef ASM_OUTPUT_DWARF_DATA1
327 #define ASM_OUTPUT_DWARF_DATA1(FILE,VALUE) \
328 fprintf ((FILE), "%s0x%x", ASM_BYTE_OP, (unsigned) (VALUE))
329 #endif
331 #ifndef ASM_OUTPUT_DWARF_DELTA1
332 #define ASM_OUTPUT_DWARF_DELTA1(FILE,LABEL1,LABEL2) \
333 do { fprintf ((FILE), "%s", ASM_BYTE_OP); \
334 assemble_name (FILE, LABEL1); \
335 fprintf (FILE, "-"); \
336 assemble_name (FILE, LABEL2); \
337 } while (0)
338 #endif
340 #ifdef UNALIGNED_INT_ASM_OP
342 #ifndef UNALIGNED_OFFSET_ASM_OP
343 #define UNALIGNED_OFFSET_ASM_OP \
344 (DWARF_OFFSET_SIZE == 8 ? UNALIGNED_DOUBLE_INT_ASM_OP : UNALIGNED_INT_ASM_OP)
345 #endif
347 #ifndef UNALIGNED_WORD_ASM_OP
348 #define UNALIGNED_WORD_ASM_OP \
349 ((DWARF2_ADDR_SIZE) == 8 ? UNALIGNED_DOUBLE_INT_ASM_OP \
350 : (DWARF2_ADDR_SIZE) == 2 ? UNALIGNED_SHORT_ASM_OP \
351 : UNALIGNED_INT_ASM_OP)
352 #endif
354 #ifndef ASM_OUTPUT_DWARF_DELTA2
355 #define ASM_OUTPUT_DWARF_DELTA2(FILE,LABEL1,LABEL2) \
356 do { fprintf ((FILE), "%s", UNALIGNED_SHORT_ASM_OP); \
357 assemble_name (FILE, LABEL1); \
358 fprintf (FILE, "-"); \
359 assemble_name (FILE, LABEL2); \
360 } while (0)
361 #endif
363 #ifndef ASM_OUTPUT_DWARF_DELTA4
364 #define ASM_OUTPUT_DWARF_DELTA4(FILE,LABEL1,LABEL2) \
365 do { fprintf ((FILE), "%s", UNALIGNED_INT_ASM_OP); \
366 assemble_name (FILE, LABEL1); \
367 fprintf (FILE, "-"); \
368 assemble_name (FILE, LABEL2); \
369 } while (0)
370 #endif
372 #ifndef ASM_OUTPUT_DWARF_DELTA
373 #define ASM_OUTPUT_DWARF_DELTA(FILE,LABEL1,LABEL2) \
374 do { fprintf ((FILE), "%s", UNALIGNED_OFFSET_ASM_OP); \
375 assemble_name (FILE, LABEL1); \
376 fprintf (FILE, "-"); \
377 assemble_name (FILE, LABEL2); \
378 } while (0)
379 #endif
381 #ifndef ASM_OUTPUT_DWARF_ADDR_DELTA
382 #define ASM_OUTPUT_DWARF_ADDR_DELTA(FILE,LABEL1,LABEL2) \
383 do { fprintf ((FILE), "%s", UNALIGNED_WORD_ASM_OP); \
384 assemble_name (FILE, LABEL1); \
385 fprintf (FILE, "-"); \
386 assemble_name (FILE, LABEL2); \
387 } while (0)
388 #endif
390 #ifndef ASM_OUTPUT_DWARF_ADDR
391 #define ASM_OUTPUT_DWARF_ADDR(FILE,LABEL) \
392 do { fprintf ((FILE), "%s", UNALIGNED_WORD_ASM_OP); \
393 assemble_name (FILE, LABEL); \
394 } while (0)
395 #endif
397 #ifndef ASM_OUTPUT_DWARF_ADDR_CONST
398 #define ASM_OUTPUT_DWARF_ADDR_CONST(FILE,RTX) \
399 do { \
400 fprintf ((FILE), "%s", UNALIGNED_WORD_ASM_OP); \
401 output_addr_const ((FILE), (RTX)); \
402 } while (0)
403 #endif
405 #ifndef ASM_OUTPUT_DWARF_OFFSET4
406 #define ASM_OUTPUT_DWARF_OFFSET4(FILE,LABEL) \
407 do { fprintf ((FILE), "%s", UNALIGNED_INT_ASM_OP); \
408 assemble_name (FILE, LABEL); \
409 } while (0)
410 #endif
412 #ifndef ASM_OUTPUT_DWARF_OFFSET
413 #define ASM_OUTPUT_DWARF_OFFSET(FILE,LABEL) \
414 do { fprintf ((FILE), "%s", UNALIGNED_OFFSET_ASM_OP); \
415 assemble_name (FILE, LABEL); \
416 } while (0)
417 #endif
419 #ifndef ASM_OUTPUT_DWARF_DATA2
420 #define ASM_OUTPUT_DWARF_DATA2(FILE,VALUE) \
421 fprintf ((FILE), "%s0x%x", UNALIGNED_SHORT_ASM_OP, (unsigned) (VALUE))
422 #endif
424 #ifndef ASM_OUTPUT_DWARF_DATA4
425 #define ASM_OUTPUT_DWARF_DATA4(FILE,VALUE) \
426 fprintf ((FILE), "%s0x%x", UNALIGNED_INT_ASM_OP, (unsigned) (VALUE))
427 #endif
429 #ifndef ASM_OUTPUT_DWARF_DATA8
430 #define ASM_OUTPUT_DWARF_DATA8(FILE,VALUE) \
431 fprintf ((FILE), "%s0x%lx", UNALIGNED_DOUBLE_INT_ASM_OP, \
432 (unsigned long) (VALUE))
433 #endif
435 #ifndef ASM_OUTPUT_DWARF_DATA
436 #define ASM_OUTPUT_DWARF_DATA(FILE,VALUE) \
437 fprintf ((FILE), "%s0x%lx", UNALIGNED_OFFSET_ASM_OP, \
438 (unsigned long) (VALUE))
439 #endif
441 #ifndef ASM_OUTPUT_DWARF_ADDR_DATA
442 #define ASM_OUTPUT_DWARF_ADDR_DATA(FILE,VALUE) \
443 fprintf ((FILE), "%s0x%lx", UNALIGNED_WORD_ASM_OP, \
444 (unsigned long) (VALUE))
445 #endif
447 #ifndef ASM_OUTPUT_DWARF_CONST_DOUBLE
448 #define ASM_OUTPUT_DWARF_CONST_DOUBLE(FILE,HIGH_VALUE,LOW_VALUE) \
449 do { \
450 if (WORDS_BIG_ENDIAN) \
452 fprintf ((FILE), "%s0x%lx\n", UNALIGNED_INT_ASM_OP, (HIGH_VALUE));\
453 fprintf ((FILE), "%s0x%lx", UNALIGNED_INT_ASM_OP, (LOW_VALUE));\
455 else \
457 fprintf ((FILE), "%s0x%lx\n", UNALIGNED_INT_ASM_OP, (LOW_VALUE)); \
458 fprintf ((FILE), "%s0x%lx", UNALIGNED_INT_ASM_OP, (HIGH_VALUE)); \
460 } while (0)
461 #endif
463 #else /* UNALIGNED_INT_ASM_OP */
465 /* We don't have unaligned support, let's hope the normal output works for
466 .debug_frame. But we know it won't work for .debug_info. */
468 #ifdef DWARF2_DEBUGGING_INFO
469 #error DWARF2_DEBUGGING_INFO requires UNALIGNED_INT_ASM_OP.
470 #endif
472 #ifndef ASM_OUTPUT_DWARF_ADDR
473 #define ASM_OUTPUT_DWARF_ADDR(FILE,LABEL) \
474 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, LABEL), DWARF2_ADDR_SIZE, 1)
475 #endif
477 #ifndef ASM_OUTPUT_DWARF_ADDR_CONST
478 #define ASM_OUTPUT_DWARF_ADDR_CONST(FILE,RTX) ASM_OUTPUT_DWARF_ADDR (FILE,RTX)
479 #endif
481 #ifndef ASM_OUTPUT_DWARF_OFFSET4
482 #define ASM_OUTPUT_DWARF_OFFSET4(FILE,LABEL) \
483 assemble_integer (gen_rtx_SYMBOL_REF (SImode, LABEL), 4, 1)
484 #endif
486 #ifndef ASM_OUTPUT_DWARF_OFFSET
487 #define ASM_OUTPUT_DWARF_OFFSET(FILE,LABEL) \
488 assemble_integer (gen_rtx_SYMBOL_REF (SImode, LABEL), 4, 1)
489 #endif
491 #ifndef ASM_OUTPUT_DWARF_DELTA2
492 #define ASM_OUTPUT_DWARF_DELTA2(FILE,LABEL1,LABEL2) \
493 assemble_integer (gen_rtx_MINUS (HImode, \
494 gen_rtx_SYMBOL_REF (Pmode, LABEL1), \
495 gen_rtx_SYMBOL_REF (Pmode, LABEL2)), \
496 2, 1)
497 #endif
499 #ifndef ASM_OUTPUT_DWARF_DELTA4
500 #define ASM_OUTPUT_DWARF_DELTA4(FILE,LABEL1,LABEL2) \
501 assemble_integer (gen_rtx_MINUS (SImode, \
502 gen_rtx_SYMBOL_REF (Pmode, LABEL1), \
503 gen_rtx_SYMBOL_REF (Pmode, LABEL2)), \
504 4, 1)
505 #endif
507 #ifndef ASM_OUTPUT_DWARF_ADDR_DELTA
508 #define ASM_OUTPUT_DWARF_ADDR_DELTA(FILE,LABEL1,LABEL2) \
509 assemble_integer (gen_rtx_MINUS (Pmode, \
510 gen_rtx_SYMBOL_REF (Pmode, LABEL1), \
511 gen_rtx_SYMBOL_REF (Pmode, LABEL2)), \
512 DWARF2_ADDR_SIZE, 1)
513 #endif
515 #ifndef ASM_OUTPUT_DWARF_DELTA
516 #define ASM_OUTPUT_DWARF_DELTA(FILE,LABEL1,LABEL2) \
517 ASM_OUTPUT_DWARF_DELTA4 (FILE,LABEL1,LABEL2)
518 #endif
520 #ifndef ASM_OUTPUT_DWARF_DATA2
521 #define ASM_OUTPUT_DWARF_DATA2(FILE,VALUE) \
522 assemble_integer (GEN_INT (VALUE), 2, 1)
523 #endif
525 #ifndef ASM_OUTPUT_DWARF_DATA4
526 #define ASM_OUTPUT_DWARF_DATA4(FILE,VALUE) \
527 assemble_integer (GEN_INT (VALUE), 4, 1)
528 #endif
530 #endif /* UNALIGNED_INT_ASM_OP */
532 #ifdef SET_ASM_OP
533 #ifndef ASM_OUTPUT_DEFINE_LABEL_DIFFERENCE_SYMBOL
534 #define ASM_OUTPUT_DEFINE_LABEL_DIFFERENCE_SYMBOL(FILE, SY, HI, LO) \
535 do { \
536 fprintf (FILE, "%s", SET_ASM_OP); \
537 assemble_name (FILE, SY); \
538 fputc (',', FILE); \
539 assemble_name (FILE, HI); \
540 fputc ('-', FILE); \
541 assemble_name (FILE, LO); \
542 } while (0)
543 #endif
544 #endif /* SET_ASM_OP */
546 /* This is similar to the default ASM_OUTPUT_ASCII, except that no trailing
547 newline is produced. When flag_debug_asm is asserted, we add commentary
548 at the end of the line, so we must avoid output of a newline here. */
549 #ifndef ASM_OUTPUT_DWARF_NSTRING
550 #define ASM_OUTPUT_DWARF_NSTRING(FILE,P,SLEN) \
551 do { \
552 register int slen = (SLEN); \
553 register const char *p = (P); \
554 register int i; \
555 fprintf (FILE, "\t.ascii \""); \
556 for (i = 0; i < slen; i++) \
558 register int c = p[i]; \
559 if (c == '\"' || c == '\\') \
560 putc ('\\', FILE); \
561 if (ISPRINT(c)) \
562 putc (c, FILE); \
563 else \
565 fprintf (FILE, "\\%o", c); \
568 fprintf (FILE, "\\0\""); \
570 while (0)
571 #endif
572 #define ASM_OUTPUT_DWARF_STRING(FILE,P) \
573 ASM_OUTPUT_DWARF_NSTRING (FILE, P, strlen (P))
575 /* The DWARF 2 CFA column which tracks the return address. Normally this
576 is the column for PC, or the first column after all of the hard
577 registers. */
578 #ifndef DWARF_FRAME_RETURN_COLUMN
579 #ifdef PC_REGNUM
580 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGNUM (PC_REGNUM)
581 #else
582 #define DWARF_FRAME_RETURN_COLUMN DWARF_FRAME_REGISTERS
583 #endif
584 #endif
586 /* The mapping from gcc register number to DWARF 2 CFA column number. By
587 default, we just provide columns for all registers. */
588 #ifndef DWARF_FRAME_REGNUM
589 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
590 #endif
592 /* Hook used by __throw. */
595 expand_builtin_dwarf_fp_regnum ()
597 return GEN_INT (DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM));
600 /* The offset from the incoming value of %sp to the top of the stack frame
601 for the current function. */
602 #ifndef INCOMING_FRAME_SP_OFFSET
603 #define INCOMING_FRAME_SP_OFFSET 0
604 #endif
606 /* Return a pointer to a copy of the section string name S with all
607 attributes stripped off, and an asterisk prepended (for assemble_name). */
609 static inline char *
610 stripattributes (s)
611 const char *s;
613 char *stripped = xmalloc (strlen (s) + 2);
614 char *p = stripped;
616 *p++ = '*';
618 while (*s && *s != ',')
619 *p++ = *s++;
621 *p = '\0';
622 return stripped;
625 /* Generate code to initialize the register size table. */
627 void
628 expand_builtin_init_dwarf_reg_sizes (address)
629 tree address;
631 int i;
632 enum machine_mode mode = TYPE_MODE (char_type_node);
633 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, 0);
634 rtx mem = gen_rtx_MEM (mode, addr);
636 for (i = 0; i < DWARF_FRAME_REGISTERS; ++i)
638 int offset = DWARF_FRAME_REGNUM (i) * GET_MODE_SIZE (mode);
639 int size = GET_MODE_SIZE (reg_raw_mode[i]);
641 if (offset < 0)
642 continue;
644 emit_move_insn (change_address (mem, mode,
645 plus_constant (addr, offset)),
646 GEN_INT (size));
650 /* Convert a DWARF call frame info. operation to its string name */
652 static const char *
653 dwarf_cfi_name (cfi_opc)
654 register unsigned cfi_opc;
656 switch (cfi_opc)
658 case DW_CFA_advance_loc:
659 return "DW_CFA_advance_loc";
660 case DW_CFA_offset:
661 return "DW_CFA_offset";
662 case DW_CFA_restore:
663 return "DW_CFA_restore";
664 case DW_CFA_nop:
665 return "DW_CFA_nop";
666 case DW_CFA_set_loc:
667 return "DW_CFA_set_loc";
668 case DW_CFA_advance_loc1:
669 return "DW_CFA_advance_loc1";
670 case DW_CFA_advance_loc2:
671 return "DW_CFA_advance_loc2";
672 case DW_CFA_advance_loc4:
673 return "DW_CFA_advance_loc4";
674 case DW_CFA_offset_extended:
675 return "DW_CFA_offset_extended";
676 case DW_CFA_restore_extended:
677 return "DW_CFA_restore_extended";
678 case DW_CFA_undefined:
679 return "DW_CFA_undefined";
680 case DW_CFA_same_value:
681 return "DW_CFA_same_value";
682 case DW_CFA_register:
683 return "DW_CFA_register";
684 case DW_CFA_remember_state:
685 return "DW_CFA_remember_state";
686 case DW_CFA_restore_state:
687 return "DW_CFA_restore_state";
688 case DW_CFA_def_cfa:
689 return "DW_CFA_def_cfa";
690 case DW_CFA_def_cfa_register:
691 return "DW_CFA_def_cfa_register";
692 case DW_CFA_def_cfa_offset:
693 return "DW_CFA_def_cfa_offset";
694 case DW_CFA_def_cfa_expression:
695 return "DW_CFA_def_cfa_expression";
697 /* SGI/MIPS specific */
698 case DW_CFA_MIPS_advance_loc8:
699 return "DW_CFA_MIPS_advance_loc8";
701 /* GNU extensions */
702 case DW_CFA_GNU_window_save:
703 return "DW_CFA_GNU_window_save";
704 case DW_CFA_GNU_args_size:
705 return "DW_CFA_GNU_args_size";
706 case DW_CFA_GNU_negative_offset_extended:
707 return "DW_CFA_GNU_negative_offset_extended";
709 default:
710 return "DW_CFA_<unknown>";
714 /* Return a pointer to a newly allocated Call Frame Instruction. */
716 static inline dw_cfi_ref
717 new_cfi ()
719 register dw_cfi_ref cfi = (dw_cfi_ref) xmalloc (sizeof (dw_cfi_node));
721 cfi->dw_cfi_next = NULL;
722 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
723 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
725 return cfi;
728 /* Add a Call Frame Instruction to list of instructions. */
730 static inline void
731 add_cfi (list_head, cfi)
732 register dw_cfi_ref *list_head;
733 register dw_cfi_ref cfi;
735 register dw_cfi_ref *p;
737 /* Find the end of the chain. */
738 for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
741 *p = cfi;
744 /* Generate a new label for the CFI info to refer to. */
746 char *
747 dwarf2out_cfi_label ()
749 static char label[20];
750 static unsigned long label_num = 0;
752 ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", label_num++);
753 ASM_OUTPUT_LABEL (asm_out_file, label);
755 return label;
758 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
759 or to the CIE if LABEL is NULL. */
761 static void
762 add_fde_cfi (label, cfi)
763 register const char *label;
764 register dw_cfi_ref cfi;
766 if (label)
768 register dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
770 if (*label == 0)
771 label = dwarf2out_cfi_label ();
773 if (fde->dw_fde_current_label == NULL
774 || strcmp (label, fde->dw_fde_current_label) != 0)
776 register dw_cfi_ref xcfi;
778 fde->dw_fde_current_label = label = xstrdup (label);
780 /* Set the location counter to the new label. */
781 xcfi = new_cfi ();
782 xcfi->dw_cfi_opc = DW_CFA_advance_loc4;
783 xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
784 add_cfi (&fde->dw_fde_cfi, xcfi);
787 add_cfi (&fde->dw_fde_cfi, cfi);
790 else
791 add_cfi (&cie_cfi_head, cfi);
794 /* Subroutine of lookup_cfa. */
796 static inline void
797 lookup_cfa_1 (cfi, loc)
798 register dw_cfi_ref cfi;
799 register dw_cfa_location *loc;
801 switch (cfi->dw_cfi_opc)
803 case DW_CFA_def_cfa_offset:
804 loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
805 break;
806 case DW_CFA_def_cfa_register:
807 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
808 break;
809 case DW_CFA_def_cfa:
810 loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
811 loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
812 break;
813 case DW_CFA_def_cfa_expression:
814 get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
815 break;
816 default:
817 break;
821 /* Find the previous value for the CFA. */
823 static void
824 lookup_cfa (loc)
825 register dw_cfa_location *loc;
827 register dw_cfi_ref cfi;
829 loc->reg = (unsigned long) -1;
830 loc->offset = 0;
831 loc->indirect = 0;
832 loc->base_offset = 0;
834 for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
835 lookup_cfa_1 (cfi, loc);
837 if (fde_table_in_use)
839 register dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
840 for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
841 lookup_cfa_1 (cfi, loc);
845 /* The current rule for calculating the DWARF2 canonical frame address. */
846 dw_cfa_location cfa;
848 /* The register used for saving registers to the stack, and its offset
849 from the CFA. */
850 dw_cfa_location cfa_store;
852 /* The running total of the size of arguments pushed onto the stack. */
853 static long args_size;
855 /* The last args_size we actually output. */
856 static long old_args_size;
858 /* Entry point to update the canonical frame address (CFA).
859 LABEL is passed to add_fde_cfi. The value of CFA is now to be
860 calculated from REG+OFFSET. */
862 void
863 dwarf2out_def_cfa (label, reg, offset)
864 register const char *label;
865 unsigned reg;
866 long offset;
868 dw_cfa_location loc;
869 loc.indirect = 0;
870 loc.base_offset = 0;
871 loc.reg = reg;
872 loc.offset = offset;
873 def_cfa_1 (label, &loc);
876 /* This routine does the actual work. The CFA is now calculated from
877 the dw_cfa_location structure. */
878 static void
879 def_cfa_1 (label, loc_p)
880 register const char *label;
881 dw_cfa_location *loc_p;
883 register dw_cfi_ref cfi;
884 dw_cfa_location old_cfa, loc;
886 cfa = *loc_p;
887 loc = *loc_p;
889 if (cfa_store.reg == loc.reg && loc.indirect == 0)
890 cfa_store.offset = loc.offset;
892 loc.reg = DWARF_FRAME_REGNUM (loc.reg);
893 lookup_cfa (&old_cfa);
895 if (loc.reg == old_cfa.reg && loc.offset == old_cfa.offset &&
896 loc.indirect == old_cfa.indirect)
898 if (loc.indirect == 0
899 || loc.base_offset == old_cfa.base_offset)
900 /* Nothing changed so no need to issue any call frame
901 instructions. */
902 return;
905 cfi = new_cfi ();
907 if (loc.reg == old_cfa.reg && !loc.indirect)
909 /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction,
910 indicating the CFA register did not change but the offset
911 did. */
912 cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
913 cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
916 #ifndef MIPS_DEBUGGING_INFO /* SGI dbx thinks this means no offset. */
917 else if (loc.offset == old_cfa.offset && old_cfa.reg != (unsigned long) -1
918 && !loc.indirect)
920 /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
921 indicating the CFA register has changed to <register> but the
922 offset has not changed. */
923 cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
924 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
926 #endif
928 else if (loc.indirect == 0)
930 /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
931 indicating the CFA register has changed to <register> with
932 the specified offset. */
933 cfi->dw_cfi_opc = DW_CFA_def_cfa;
934 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
935 cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
937 else
939 /* Construct a DW_CFA_def_cfa_expression instruction to
940 calculate the CFA using a full location expression since no
941 register-offset pair is available. */
942 struct dw_loc_descr_struct *loc_list;
943 cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
944 loc_list = build_cfa_loc (&loc);
945 cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
948 add_fde_cfi (label, cfi);
951 /* Add the CFI for saving a register. REG is the CFA column number.
952 LABEL is passed to add_fde_cfi.
953 If SREG is -1, the register is saved at OFFSET from the CFA;
954 otherwise it is saved in SREG. */
956 static void
957 reg_save (label, reg, sreg, offset)
958 register const char *label;
959 register unsigned reg;
960 register unsigned sreg;
961 register long offset;
963 register dw_cfi_ref cfi = new_cfi ();
965 cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
967 /* The following comparison is correct. -1 is used to indicate that
968 the value isn't a register number. */
969 if (sreg == (unsigned int) -1)
971 if (reg & ~0x3f)
972 /* The register number won't fit in 6 bits, so we have to use
973 the long form. */
974 cfi->dw_cfi_opc = DW_CFA_offset_extended;
975 else
976 cfi->dw_cfi_opc = DW_CFA_offset;
978 #ifdef ENABLE_CHECKING
980 /* If we get an offset that is not a multiple of
981 DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
982 definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
983 description. */
984 long check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
986 if (check_offset * DWARF_CIE_DATA_ALIGNMENT != offset)
987 abort ();
989 #endif
990 offset /= DWARF_CIE_DATA_ALIGNMENT;
991 if (offset < 0)
993 cfi->dw_cfi_opc = DW_CFA_GNU_negative_offset_extended;
994 offset = -offset;
996 cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
998 else if (sreg == reg)
999 /* We could emit a DW_CFA_same_value in this case, but don't bother. */
1000 return;
1001 else
1003 cfi->dw_cfi_opc = DW_CFA_register;
1004 cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1007 add_fde_cfi (label, cfi);
1010 /* Add the CFI for saving a register window. LABEL is passed to reg_save.
1011 This CFI tells the unwinder that it needs to restore the window registers
1012 from the previous frame's window save area.
1014 ??? Perhaps we should note in the CIE where windows are saved (instead of
1015 assuming 0(cfa)) and what registers are in the window. */
1017 void
1018 dwarf2out_window_save (label)
1019 register const char *label;
1021 register dw_cfi_ref cfi = new_cfi ();
1022 cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1023 add_fde_cfi (label, cfi);
1026 /* Add a CFI to update the running total of the size of arguments
1027 pushed onto the stack. */
1029 void
1030 dwarf2out_args_size (label, size)
1031 const char *label;
1032 long size;
1034 register dw_cfi_ref cfi;
1036 if (size == old_args_size)
1037 return;
1038 old_args_size = size;
1040 cfi = new_cfi ();
1041 cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1042 cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1043 add_fde_cfi (label, cfi);
1046 /* Entry point for saving a register to the stack. REG is the GCC register
1047 number. LABEL and OFFSET are passed to reg_save. */
1049 void
1050 dwarf2out_reg_save (label, reg, offset)
1051 register const char *label;
1052 register unsigned reg;
1053 register long offset;
1055 reg_save (label, DWARF_FRAME_REGNUM (reg), -1, offset);
1058 /* Entry point for saving the return address in the stack.
1059 LABEL and OFFSET are passed to reg_save. */
1061 void
1062 dwarf2out_return_save (label, offset)
1063 register const char *label;
1064 register long offset;
1066 reg_save (label, DWARF_FRAME_RETURN_COLUMN, -1, offset);
1069 /* Entry point for saving the return address in a register.
1070 LABEL and SREG are passed to reg_save. */
1072 void
1073 dwarf2out_return_reg (label, sreg)
1074 register const char *label;
1075 register unsigned sreg;
1077 reg_save (label, DWARF_FRAME_RETURN_COLUMN, sreg, 0);
1080 /* Record the initial position of the return address. RTL is
1081 INCOMING_RETURN_ADDR_RTX. */
1083 static void
1084 initial_return_save (rtl)
1085 register rtx rtl;
1087 unsigned int reg = (unsigned int) -1;
1088 long offset = 0;
1090 switch (GET_CODE (rtl))
1092 case REG:
1093 /* RA is in a register. */
1094 reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1095 break;
1096 case MEM:
1097 /* RA is on the stack. */
1098 rtl = XEXP (rtl, 0);
1099 switch (GET_CODE (rtl))
1101 case REG:
1102 if (REGNO (rtl) != STACK_POINTER_REGNUM)
1103 abort ();
1104 offset = 0;
1105 break;
1106 case PLUS:
1107 if (REGNO (XEXP (rtl, 0)) != STACK_POINTER_REGNUM)
1108 abort ();
1109 offset = INTVAL (XEXP (rtl, 1));
1110 break;
1111 case MINUS:
1112 if (REGNO (XEXP (rtl, 0)) != STACK_POINTER_REGNUM)
1113 abort ();
1114 offset = -INTVAL (XEXP (rtl, 1));
1115 break;
1116 default:
1117 abort ();
1119 break;
1120 case PLUS:
1121 /* The return address is at some offset from any value we can
1122 actually load. For instance, on the SPARC it is in %i7+8. Just
1123 ignore the offset for now; it doesn't matter for unwinding frames. */
1124 if (GET_CODE (XEXP (rtl, 1)) != CONST_INT)
1125 abort ();
1126 initial_return_save (XEXP (rtl, 0));
1127 return;
1128 default:
1129 abort ();
1132 reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1135 /* Given a SET, calculate the amount of stack adjustment it
1136 contains. */
1138 static long
1139 stack_adjust_offset (pattern)
1140 rtx pattern;
1142 rtx src = SET_SRC (pattern);
1143 rtx dest = SET_DEST (pattern);
1144 long offset = 0;
1145 enum rtx_code code;
1147 if (dest == stack_pointer_rtx)
1149 /* (set (reg sp) (plus (reg sp) (const_int))) */
1150 code = GET_CODE (src);
1151 if (! (code == PLUS || code == MINUS)
1152 || XEXP (src, 0) != stack_pointer_rtx
1153 || GET_CODE (XEXP (src, 1)) != CONST_INT)
1154 return 0;
1156 offset = INTVAL (XEXP (src, 1));
1158 else if (GET_CODE (dest) == MEM)
1160 /* (set (mem (pre_dec (reg sp))) (foo)) */
1161 src = XEXP (dest, 0);
1162 code = GET_CODE (src);
1164 if (! (code == PRE_DEC || code == PRE_INC
1165 || code == PRE_MODIFY)
1166 || XEXP (src, 0) != stack_pointer_rtx)
1167 return 0;
1169 if (code == PRE_MODIFY)
1171 rtx val = XEXP (XEXP (src, 1), 1);
1172 /* We handle only adjustments by constant amount. */
1173 if (GET_CODE (XEXP (src, 1)) != PLUS ||
1174 GET_CODE (val) != CONST_INT)
1175 abort();
1176 offset = -INTVAL (val);
1178 else offset = GET_MODE_SIZE (GET_MODE (dest));
1180 else
1181 return 0;
1183 if (code == PLUS || code == PRE_INC)
1184 offset = -offset;
1186 return offset;
1189 /* Check INSN to see if it looks like a push or a stack adjustment, and
1190 make a note of it if it does. EH uses this information to find out how
1191 much extra space it needs to pop off the stack. */
1193 static void
1194 dwarf2out_stack_adjust (insn)
1195 rtx insn;
1197 long offset;
1198 const char *label;
1200 if (! asynchronous_exceptions && GET_CODE (insn) == CALL_INSN)
1202 /* Extract the size of the args from the CALL rtx itself. */
1204 insn = PATTERN (insn);
1205 if (GET_CODE (insn) == PARALLEL)
1206 insn = XVECEXP (insn, 0, 0);
1207 if (GET_CODE (insn) == SET)
1208 insn = SET_SRC (insn);
1209 if (GET_CODE (insn) != CALL)
1210 abort ();
1211 dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1212 return;
1215 /* If only calls can throw, and we have a frame pointer,
1216 save up adjustments until we see the CALL_INSN. */
1217 else if (! asynchronous_exceptions
1218 && cfa.reg != STACK_POINTER_REGNUM)
1219 return;
1221 if (GET_CODE (insn) == BARRIER)
1223 /* When we see a BARRIER, we know to reset args_size to 0. Usually
1224 the compiler will have already emitted a stack adjustment, but
1225 doesn't bother for calls to noreturn functions. */
1226 #ifdef STACK_GROWS_DOWNWARD
1227 offset = -args_size;
1228 #else
1229 offset = args_size;
1230 #endif
1232 else if (GET_CODE (PATTERN (insn)) == SET)
1234 offset = stack_adjust_offset (PATTERN (insn));
1236 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1237 || GET_CODE (PATTERN (insn)) == SEQUENCE)
1239 /* There may be stack adjustments inside compound insns. Search
1240 for them. */
1241 int j;
1243 offset = 0;
1244 for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
1246 rtx pattern = XVECEXP (PATTERN (insn), 0, j);
1247 if (GET_CODE (pattern) == SET)
1248 offset += stack_adjust_offset (pattern);
1251 else
1252 return;
1254 if (offset == 0)
1255 return;
1257 if (cfa.reg == STACK_POINTER_REGNUM)
1258 cfa.offset += offset;
1260 #ifndef STACK_GROWS_DOWNWARD
1261 offset = -offset;
1262 #endif
1263 args_size += offset;
1264 if (args_size < 0)
1265 args_size = 0;
1267 label = dwarf2out_cfi_label ();
1268 def_cfa_1 (label, &cfa);
1269 dwarf2out_args_size (label, args_size);
1272 /* A temporary register holding an integral value used in adjusting SP
1273 or setting up the store_reg. The "offset" field holds the integer
1274 value, not an offset. */
1275 dw_cfa_location cfa_temp;
1277 /* Record call frame debugging information for an expression EXPR,
1278 which either sets SP or FP (adjusting how we calculate the frame
1279 address) or saves a register to the stack. LABEL indicates the
1280 address of EXPR.
1282 This function encodes a state machine mapping rtxes to actions on
1283 cfa, cfa_store, and cfa_temp.reg. We describe these rules so
1284 users need not read the source code.
1286 The High-Level Picture
1288 Changes in the register we use to calculate the CFA: Currently we
1289 assume that if you copy the CFA register into another register, we
1290 should take the other one as the new CFA register; this seems to
1291 work pretty well. If it's wrong for some target, it's simple
1292 enough not to set RTX_FRAME_RELATED_P on the insn in question.
1294 Changes in the register we use for saving registers to the stack:
1295 This is usually SP, but not always. Again, we deduce that if you
1296 copy SP into another register (and SP is not the CFA register),
1297 then the new register is the one we will be using for register
1298 saves. This also seems to work.
1300 Register saves: There's not much guesswork about this one; if
1301 RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1302 register save, and the register used to calculate the destination
1303 had better be the one we think we're using for this purpose.
1305 Except: If the register being saved is the CFA register, and the
1306 offset is non-zero, we are saving the CFA, so we assume we have to
1307 use DW_CFA_def_cfa_expression. If the offset is 0, we assume that
1308 the intent is to save the value of SP from the previous frame.
1310 Invariants / Summaries of Rules
1312 cfa current rule for calculating the CFA. It usually
1313 consists of a register and an offset.
1314 cfa_store register used by prologue code to save things to the stack
1315 cfa_store.offset is the offset from the value of
1316 cfa_store.reg to the actual CFA
1317 cfa_temp register holding an integral value. cfa_temp.offset
1318 stores the value, which will be used to adjust the
1319 stack pointer.
1321 Rules 1- 4: Setting a register's value to cfa.reg or an expression
1322 with cfa.reg as the first operand changes the cfa.reg and its
1323 cfa.offset.
1325 Rules 6- 9: Set a non-cfa.reg register value to a constant or an
1326 expression yielding a constant. This sets cfa_temp.reg
1327 and cfa_temp.offset.
1329 Rule 5: Create a new register cfa_store used to save items to the
1330 stack.
1332 Rules 10-13: Save a register to the stack. Define offset as the
1333 difference of the original location and cfa_store's
1334 location.
1336 The Rules
1338 "{a,b}" indicates a choice of a xor b.
1339 "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1341 Rule 1:
1342 (set <reg1> <reg2>:cfa.reg)
1343 effects: cfa.reg = <REG1>
1344 cfa.offset unchanged
1346 Rule 2:
1347 (set sp ({minus,plus} {sp,fp}:cfa.reg {<const_int>,<reg>:cfa_temp.reg}))
1348 effects: cfa.reg = sp if fp used
1349 cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1350 cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1351 if cfa_store.reg==sp
1353 Rule 3:
1354 (set fp ({minus,plus} <reg>:cfa.reg <const_int>))
1355 effects: cfa.reg = fp
1356 cfa_offset += +/- <const_int>
1358 Rule 4:
1359 (set <reg1> (plus <reg2>:cfa.reg <const_int>))
1360 constraints: <reg1> != fp
1361 <reg1> != sp
1362 effects: cfa.reg = <reg1>
1364 Rule 5:
1365 (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1366 constraints: <reg1> != fp
1367 <reg1> != sp
1368 effects: cfa_store.reg = <reg1>
1369 cfa_store.offset = cfa.offset - cfa_temp.offset
1371 Rule 6:
1372 (set <reg> <const_int>)
1373 effects: cfa_temp.reg = <reg>
1374 cfa_temp.offset = <const_int>
1376 Rule 7:
1377 (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1378 effects: cfa_temp.reg = <reg1>
1379 cfa_temp.offset |= <const_int>
1381 Rule 8:
1382 (set <reg> (high <exp>))
1383 effects: none
1385 Rule 9:
1386 (set <reg> (lo_sum <exp> <const_int>))
1387 effects: cfa_temp.reg = <reg>
1388 cfa_temp.offset = <const_int>
1390 Rule 10:
1391 (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1392 effects: cfa_store.offset -= <const_int>
1393 cfa.offset = cfa_store.offset if cfa.reg == sp
1394 offset = -cfa_store.offset
1395 cfa.reg = sp
1396 cfa.base_offset = offset
1398 Rule 11:
1399 (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1400 effects: cfa_store.offset += -/+ mode_size(mem)
1401 cfa.offset = cfa_store.offset if cfa.reg == sp
1402 offset = -cfa_store.offset
1403 cfa.reg = sp
1404 cfa.base_offset = offset
1406 Rule 12:
1407 (set (mem ({minus,plus} <reg1>:cfa_store <const_int>)) <reg2>)
1408 effects: cfa_store.offset += -/+ <const_int>
1409 offset = -cfa_store.offset
1410 cfa.reg = <reg1
1411 cfa.base_offset = offset
1413 Rule 13:
1414 (set (mem <reg1>:cfa_store) <reg2>)
1415 effects: offset = -cfa_store.offset
1416 cfa.reg = <reg1>
1417 cfa.base_offset = offset */
1419 static void
1420 dwarf2out_frame_debug_expr (expr, label)
1421 rtx expr;
1422 const char *label;
1424 rtx src, dest;
1425 long offset;
1427 /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1428 the PARALLEL independently. The first element is always processed if
1429 it is a SET. This is for backward compatibility. Other elements
1430 are processed only if they are SETs and the RTX_FRAME_RELATED_P
1431 flag is set in them. */
1433 if (GET_CODE (expr) == PARALLEL
1434 || GET_CODE (expr) == SEQUENCE)
1436 int par_index;
1437 int limit = XVECLEN (expr, 0);
1439 for (par_index = 0; par_index < limit; par_index++)
1441 rtx x = XVECEXP (expr, 0, par_index);
1443 if (GET_CODE (x) == SET &&
1444 (RTX_FRAME_RELATED_P (x) || par_index == 0))
1445 dwarf2out_frame_debug_expr (x, label);
1447 return;
1450 if (GET_CODE (expr) != SET)
1451 abort ();
1453 src = SET_SRC (expr);
1454 dest = SET_DEST (expr);
1456 switch (GET_CODE (dest))
1458 case REG:
1459 /* Rule 1 */
1460 /* Update the CFA rule wrt SP or FP. Make sure src is
1461 relative to the current CFA register. */
1462 switch (GET_CODE (src))
1464 /* Setting FP from SP. */
1465 case REG:
1466 if (cfa.reg == (unsigned) REGNO (src))
1467 /* OK. */
1469 else
1470 abort ();
1472 /* We used to require that dest be either SP or FP, but the
1473 ARM copies SP to a temporary register, and from there to
1474 FP. So we just rely on the backends to only set
1475 RTX_FRAME_RELATED_P on appropriate insns. */
1476 cfa.reg = REGNO (dest);
1477 break;
1479 case PLUS:
1480 case MINUS:
1481 if (dest == stack_pointer_rtx)
1483 /* Rule 2 */
1484 /* Adjusting SP. */
1485 switch (GET_CODE (XEXP (src, 1)))
1487 case CONST_INT:
1488 offset = INTVAL (XEXP (src, 1));
1489 break;
1490 case REG:
1491 if ((unsigned) REGNO (XEXP (src, 1)) != cfa_temp.reg)
1492 abort ();
1493 offset = cfa_temp.offset;
1494 break;
1495 default:
1496 abort ();
1499 if (XEXP (src, 0) == hard_frame_pointer_rtx)
1501 /* Restoring SP from FP in the epilogue. */
1502 if (cfa.reg != (unsigned) HARD_FRAME_POINTER_REGNUM)
1503 abort ();
1504 cfa.reg = STACK_POINTER_REGNUM;
1506 else if (XEXP (src, 0) != stack_pointer_rtx)
1507 abort ();
1509 if (GET_CODE (src) == PLUS)
1510 offset = -offset;
1511 if (cfa.reg == STACK_POINTER_REGNUM)
1512 cfa.offset += offset;
1513 if (cfa_store.reg == STACK_POINTER_REGNUM)
1514 cfa_store.offset += offset;
1516 else if (dest == hard_frame_pointer_rtx)
1518 /* Rule 3 */
1519 /* Either setting the FP from an offset of the SP,
1520 or adjusting the FP */
1521 if (! frame_pointer_needed)
1522 abort ();
1524 if (GET_CODE (XEXP (src, 0)) == REG
1525 && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1526 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1528 offset = INTVAL (XEXP (src, 1));
1529 if (GET_CODE (src) == PLUS)
1530 offset = -offset;
1531 cfa.offset += offset;
1532 cfa.reg = HARD_FRAME_POINTER_REGNUM;
1534 else
1535 abort ();
1537 else
1539 if (GET_CODE (src) != PLUS)
1540 abort ();
1542 /* Rule 4 */
1543 if (GET_CODE (XEXP (src, 0)) == REG
1544 && REGNO (XEXP (src, 0)) == cfa.reg
1545 && GET_CODE (XEXP (src, 1)) == CONST_INT)
1547 /* Setting a temporary CFA register that will be copied
1548 into the FP later on. */
1549 offset = INTVAL (XEXP (src, 1));
1550 if (GET_CODE (src) == PLUS)
1551 offset = -offset;
1552 cfa.offset += offset;
1553 cfa.reg = REGNO (dest);
1555 /* Rule 5 */
1556 else
1558 /* Setting a scratch register that we will use instead
1559 of SP for saving registers to the stack. */
1560 if (XEXP (src, 1) != stack_pointer_rtx)
1561 abort ();
1562 if (GET_CODE (XEXP (src, 0)) != REG
1563 || (unsigned) REGNO (XEXP (src, 0)) != cfa_temp.reg)
1564 abort ();
1565 if (cfa.reg != STACK_POINTER_REGNUM)
1566 abort ();
1567 cfa_store.reg = REGNO (dest);
1568 cfa_store.offset = cfa.offset - cfa_temp.offset;
1571 break;
1573 /* Rule 6 */
1574 case CONST_INT:
1575 cfa_temp.reg = REGNO (dest);
1576 cfa_temp.offset = INTVAL (src);
1577 break;
1579 /* Rule 7 */
1580 case IOR:
1581 if (GET_CODE (XEXP (src, 0)) != REG
1582 || (unsigned) REGNO (XEXP (src, 0)) != cfa_temp.reg
1583 || GET_CODE (XEXP (src, 1)) != CONST_INT)
1584 abort ();
1585 if ((unsigned) REGNO (dest) != cfa_temp.reg)
1586 cfa_temp.reg = REGNO (dest);
1587 cfa_temp.offset |= INTVAL (XEXP (src, 1));
1588 break;
1590 default:
1591 abort ();
1593 def_cfa_1 (label, &cfa);
1594 break;
1596 /* Skip over HIGH, assuming it will be followed by a LO_SUM, which
1597 will fill in all of the bits. */
1598 /* Rule 8 */
1599 case HIGH:
1600 break;
1602 /* Rule 9 */
1603 case LO_SUM:
1604 if (GET_CODE (XEXP (src, 1)) != CONST_INT)
1605 abort ();
1606 cfa_temp.reg = REGNO (dest);
1607 cfa_temp.offset = INTVAL (XEXP (src, 1));
1608 break;
1610 case MEM:
1611 if (GET_CODE (src) != REG)
1612 abort ();
1614 /* Saving a register to the stack. Make sure dest is relative to the
1615 CFA register. */
1616 switch (GET_CODE (XEXP (dest, 0)))
1618 /* Rule 10 */
1619 /* With a push. */
1620 case PRE_MODIFY:
1621 /* We can't handle variable size modifications. */
1622 if (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1)) != CONST_INT)
1623 abort();
1624 offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1626 if (REGNO (XEXP (XEXP (dest, 0), 0)) != STACK_POINTER_REGNUM
1627 || cfa_store.reg != STACK_POINTER_REGNUM)
1628 abort ();
1629 cfa_store.offset += offset;
1630 if (cfa.reg == STACK_POINTER_REGNUM)
1631 cfa.offset = cfa_store.offset;
1633 offset = -cfa_store.offset;
1634 break;
1635 /* Rule 11 */
1636 case PRE_INC:
1637 case PRE_DEC:
1638 offset = GET_MODE_SIZE (GET_MODE (dest));
1639 if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1640 offset = -offset;
1642 if (REGNO (XEXP (XEXP (dest, 0), 0)) != STACK_POINTER_REGNUM
1643 || cfa_store.reg != STACK_POINTER_REGNUM)
1644 abort ();
1645 cfa_store.offset += offset;
1646 if (cfa.reg == STACK_POINTER_REGNUM)
1647 cfa.offset = cfa_store.offset;
1649 offset = -cfa_store.offset;
1650 break;
1652 /* Rule 12 */
1653 /* With an offset. */
1654 case PLUS:
1655 case MINUS:
1656 if (GET_CODE (XEXP (XEXP (dest, 0), 1)) != CONST_INT)
1657 abort ();
1658 offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1659 if (GET_CODE (XEXP (dest, 0)) == MINUS)
1660 offset = -offset;
1662 if (cfa_store.reg != (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)))
1663 abort ();
1664 offset -= cfa_store.offset;
1665 break;
1667 /* Rule 13 */
1668 /* Without an offset. */
1669 case REG:
1670 if (cfa_store.reg != (unsigned) REGNO (XEXP (dest, 0)))
1671 abort ();
1672 offset = -cfa_store.offset;
1673 break;
1675 default:
1676 abort ();
1679 if (REGNO (src) != STACK_POINTER_REGNUM
1680 && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1681 && (unsigned) REGNO (src) == cfa.reg)
1683 /* We're storing the current CFA reg into the stack. */
1685 if (cfa.offset == 0)
1687 /* If the source register is exactly the CFA, assume
1688 we're saving SP like any other register; this happens
1689 on the ARM. */
1691 def_cfa_1 (label, &cfa);
1692 dwarf2out_reg_save (label, STACK_POINTER_REGNUM, offset);
1693 break;
1695 else
1697 /* Otherwise, we'll need to look in the stack to
1698 calculate the CFA. */
1700 rtx x = XEXP (dest, 0);
1701 if (GET_CODE (x) != REG)
1702 x = XEXP (x, 0);
1703 if (GET_CODE (x) != REG)
1704 abort ();
1705 cfa.reg = (unsigned) REGNO (x);
1706 cfa.base_offset = offset;
1707 cfa.indirect = 1;
1708 def_cfa_1 (label, &cfa);
1709 break;
1713 def_cfa_1 (label, &cfa);
1714 dwarf2out_reg_save (label, REGNO (src), offset);
1715 break;
1717 default:
1718 abort ();
1722 /* Record call frame debugging information for INSN, which either
1723 sets SP or FP (adjusting how we calculate the frame address) or saves a
1724 register to the stack. If INSN is NULL_RTX, initialize our state. */
1726 void
1727 dwarf2out_frame_debug (insn)
1728 rtx insn;
1730 const char *label;
1731 rtx src;
1733 if (insn == NULL_RTX)
1735 /* Set up state for generating call frame debug info. */
1736 lookup_cfa (&cfa);
1737 if (cfa.reg != (unsigned long) DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM))
1738 abort ();
1739 cfa.reg = STACK_POINTER_REGNUM;
1740 cfa_store = cfa;
1741 cfa_temp.reg = -1;
1742 cfa_temp.offset = 0;
1743 return;
1746 if (! RTX_FRAME_RELATED_P (insn))
1748 dwarf2out_stack_adjust (insn);
1749 return;
1752 label = dwarf2out_cfi_label ();
1754 src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1755 if (src)
1756 insn = XEXP (src, 0);
1757 else
1758 insn = PATTERN (insn);
1760 dwarf2out_frame_debug_expr (insn, label);
1763 /* Return the size of an unsigned LEB128 quantity. */
1765 static inline unsigned long
1766 size_of_uleb128 (value)
1767 register unsigned long value;
1769 register unsigned long size = 0;
1770 register unsigned byte;
1774 byte = (value & 0x7f);
1775 value >>= 7;
1776 size += 1;
1778 while (value != 0);
1780 return size;
1783 /* Return the size of a signed LEB128 quantity. */
1785 static inline unsigned long
1786 size_of_sleb128 (value)
1787 register long value;
1789 register unsigned long size = 0;
1790 register unsigned byte;
1794 byte = (value & 0x7f);
1795 value >>= 7;
1796 size += 1;
1798 while (!(((value == 0) && ((byte & 0x40) == 0))
1799 || ((value == -1) && ((byte & 0x40) != 0))));
1801 return size;
1804 /* Output an unsigned LEB128 quantity. */
1806 static void
1807 output_uleb128 (value)
1808 register unsigned long value;
1810 unsigned long save_value = value;
1812 fprintf (asm_out_file, "%s", ASM_BYTE_OP);
1815 register unsigned byte = (value & 0x7f);
1816 value >>= 7;
1817 if (value != 0)
1818 /* More bytes to follow. */
1819 byte |= 0x80;
1821 fprintf (asm_out_file, "0x%x", byte);
1822 if (value != 0)
1823 fprintf (asm_out_file, ",");
1825 while (value != 0);
1827 if (flag_debug_asm)
1828 fprintf (asm_out_file, "\t%s ULEB128 0x%lx", ASM_COMMENT_START, save_value);
1831 /* Output an signed LEB128 quantity. */
1833 static void
1834 output_sleb128 (value)
1835 register long value;
1837 register int more;
1838 register unsigned byte;
1839 long save_value = value;
1841 fprintf (asm_out_file, "%s", ASM_BYTE_OP);
1844 byte = (value & 0x7f);
1845 /* arithmetic shift */
1846 value >>= 7;
1847 more = !((((value == 0) && ((byte & 0x40) == 0))
1848 || ((value == -1) && ((byte & 0x40) != 0))));
1849 if (more)
1850 byte |= 0x80;
1852 fprintf (asm_out_file, "0x%x", byte);
1853 if (more)
1854 fprintf (asm_out_file, ",");
1857 while (more);
1858 if (flag_debug_asm)
1859 fprintf (asm_out_file, "\t%s SLEB128 %ld", ASM_COMMENT_START, save_value);
1862 /* Output a Call Frame Information opcode and its operand(s). */
1864 static void
1865 output_cfi (cfi, fde)
1866 register dw_cfi_ref cfi;
1867 register dw_fde_ref fde;
1869 if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
1871 ASM_OUTPUT_DWARF_DATA1 (asm_out_file,
1872 cfi->dw_cfi_opc
1873 | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f));
1874 if (flag_debug_asm)
1875 fprintf (asm_out_file, "\t%s DW_CFA_advance_loc 0x%lx",
1876 ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
1877 fputc ('\n', asm_out_file);
1880 else if (cfi->dw_cfi_opc == DW_CFA_offset)
1882 ASM_OUTPUT_DWARF_DATA1 (asm_out_file,
1883 cfi->dw_cfi_opc
1884 | (cfi->dw_cfi_oprnd1.dw_cfi_reg_num & 0x3f));
1885 if (flag_debug_asm)
1886 fprintf (asm_out_file, "\t%s DW_CFA_offset, column 0x%lx",
1887 ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
1889 fputc ('\n', asm_out_file);
1890 output_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset);
1891 fputc ('\n', asm_out_file);
1893 else if (cfi->dw_cfi_opc == DW_CFA_restore)
1895 ASM_OUTPUT_DWARF_DATA1 (asm_out_file,
1896 cfi->dw_cfi_opc
1897 | (cfi->dw_cfi_oprnd1.dw_cfi_reg_num & 0x3f));
1898 if (flag_debug_asm)
1899 fprintf (asm_out_file, "\t%s DW_CFA_restore, column 0x%lx",
1900 ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
1902 fputc ('\n', asm_out_file);
1904 else
1906 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, cfi->dw_cfi_opc);
1907 if (flag_debug_asm)
1908 fprintf (asm_out_file, "\t%s %s", ASM_COMMENT_START,
1909 dwarf_cfi_name (cfi->dw_cfi_opc));
1911 fputc ('\n', asm_out_file);
1912 switch (cfi->dw_cfi_opc)
1914 case DW_CFA_set_loc:
1915 ASM_OUTPUT_DWARF_ADDR (asm_out_file, cfi->dw_cfi_oprnd1.dw_cfi_addr);
1916 fputc ('\n', asm_out_file);
1917 break;
1918 case DW_CFA_advance_loc1:
1919 ASM_OUTPUT_DWARF_DELTA1 (asm_out_file,
1920 cfi->dw_cfi_oprnd1.dw_cfi_addr,
1921 fde->dw_fde_current_label);
1922 fputc ('\n', asm_out_file);
1923 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1924 break;
1925 case DW_CFA_advance_loc2:
1926 ASM_OUTPUT_DWARF_DELTA2 (asm_out_file,
1927 cfi->dw_cfi_oprnd1.dw_cfi_addr,
1928 fde->dw_fde_current_label);
1929 fputc ('\n', asm_out_file);
1930 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1931 break;
1932 case DW_CFA_advance_loc4:
1933 ASM_OUTPUT_DWARF_DELTA4 (asm_out_file,
1934 cfi->dw_cfi_oprnd1.dw_cfi_addr,
1935 fde->dw_fde_current_label);
1936 fputc ('\n', asm_out_file);
1937 fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1938 break;
1939 #ifdef MIPS_DEBUGGING_INFO
1940 case DW_CFA_MIPS_advance_loc8:
1941 /* TODO: not currently implemented. */
1942 abort ();
1943 break;
1944 #endif
1945 case DW_CFA_offset_extended:
1946 case DW_CFA_GNU_negative_offset_extended:
1947 case DW_CFA_def_cfa:
1948 output_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
1949 fputc ('\n', asm_out_file);
1950 output_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset);
1951 fputc ('\n', asm_out_file);
1952 break;
1953 case DW_CFA_restore_extended:
1954 case DW_CFA_undefined:
1955 output_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
1956 fputc ('\n', asm_out_file);
1957 break;
1958 case DW_CFA_same_value:
1959 case DW_CFA_def_cfa_register:
1960 output_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
1961 fputc ('\n', asm_out_file);
1962 break;
1963 case DW_CFA_register:
1964 output_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
1965 fputc ('\n', asm_out_file);
1966 output_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
1967 fputc ('\n', asm_out_file);
1968 break;
1969 case DW_CFA_def_cfa_offset:
1970 output_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset);
1971 fputc ('\n', asm_out_file);
1972 break;
1973 case DW_CFA_GNU_window_save:
1974 break;
1975 case DW_CFA_GNU_args_size:
1976 output_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset);
1977 fputc ('\n', asm_out_file);
1978 break;
1979 case DW_CFA_def_cfa_expression:
1980 output_cfa_loc (cfi);
1981 break;
1982 default:
1983 break;
1988 /* Output the call frame information used to used to record information
1989 that relates to calculating the frame pointer, and records the
1990 location of saved registers. */
1992 static void
1993 output_call_frame_info (for_eh)
1994 int for_eh;
1996 register unsigned long i;
1997 register dw_fde_ref fde;
1998 register dw_cfi_ref cfi;
1999 char l1[20], l2[20];
2000 #ifdef ASM_OUTPUT_DEFINE_LABEL_DIFFERENCE_SYMBOL
2001 char ld[20];
2002 #endif
2004 /* Do we want to include a pointer to the exception table? */
2005 int eh_ptr = for_eh && exception_table_p ();
2007 /* If we don't have any functions we'll want to unwind out of, don't
2008 emit any EH unwind information. */
2009 if (for_eh)
2011 for (i = 0; i < fde_table_in_use; ++i)
2012 if (! fde_table[i].nothrow)
2013 goto found;
2014 return;
2015 found:;
2018 fputc ('\n', asm_out_file);
2020 /* We're going to be generating comments, so turn on app. */
2021 if (flag_debug_asm)
2022 app_enable ();
2024 if (for_eh)
2026 #ifdef EH_FRAME_SECTION
2027 EH_FRAME_SECTION ();
2028 #else
2029 tree label = get_file_function_name ('F');
2031 force_data_section ();
2032 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (DWARF2_ADDR_SIZE));
2033 ASM_GLOBALIZE_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2034 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2035 #endif
2036 assemble_label ("__FRAME_BEGIN__");
2038 else
2039 ASM_OUTPUT_SECTION (asm_out_file, FRAME_SECTION);
2041 /* Output the CIE. */
2042 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2043 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2044 #ifdef ASM_OUTPUT_DEFINE_LABEL_DIFFERENCE_SYMBOL
2045 ASM_GENERATE_INTERNAL_LABEL (ld, CIE_LENGTH_LABEL, for_eh);
2046 if (for_eh)
2047 ASM_OUTPUT_DWARF_OFFSET4 (asm_out_file, ld);
2048 else
2049 ASM_OUTPUT_DWARF_OFFSET (asm_out_file, ld);
2050 #else
2051 if (for_eh)
2052 ASM_OUTPUT_DWARF_DELTA4 (asm_out_file, l2, l1);
2053 else
2054 ASM_OUTPUT_DWARF_DELTA (asm_out_file, l2, l1);
2055 #endif
2056 if (flag_debug_asm)
2057 fprintf (asm_out_file, "\t%s Length of Common Information Entry",
2058 ASM_COMMENT_START);
2060 fputc ('\n', asm_out_file);
2061 ASM_OUTPUT_LABEL (asm_out_file, l1);
2063 if (for_eh)
2064 /* Now that the CIE pointer is PC-relative for EH,
2065 use 0 to identify the CIE. */
2066 ASM_OUTPUT_DWARF_DATA4 (asm_out_file, 0);
2067 else
2068 ASM_OUTPUT_DWARF_DATA4 (asm_out_file, DW_CIE_ID);
2070 if (flag_debug_asm)
2071 fprintf (asm_out_file, "\t%s CIE Identifier Tag", ASM_COMMENT_START);
2073 fputc ('\n', asm_out_file);
2074 if (! for_eh && DWARF_OFFSET_SIZE == 8)
2076 ASM_OUTPUT_DWARF_DATA4 (asm_out_file, DW_CIE_ID);
2077 fputc ('\n', asm_out_file);
2080 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_CIE_VERSION);
2081 if (flag_debug_asm)
2082 fprintf (asm_out_file, "\t%s CIE Version", ASM_COMMENT_START);
2084 fputc ('\n', asm_out_file);
2085 if (eh_ptr)
2087 /* The CIE contains a pointer to the exception region info for the
2088 frame. Make the augmentation string three bytes (including the
2089 trailing null) so the pointer is 4-byte aligned. The Solaris ld
2090 can't handle unaligned relocs. */
2091 if (flag_debug_asm)
2093 ASM_OUTPUT_DWARF_STRING (asm_out_file, "eh");
2094 fprintf (asm_out_file, "\t%s CIE Augmentation", ASM_COMMENT_START);
2096 else
2098 ASM_OUTPUT_ASCII (asm_out_file, "eh", 3);
2100 fputc ('\n', asm_out_file);
2102 ASM_OUTPUT_DWARF_ADDR (asm_out_file, "__EXCEPTION_TABLE__");
2103 if (flag_debug_asm)
2104 fprintf (asm_out_file, "\t%s pointer to exception region info",
2105 ASM_COMMENT_START);
2107 else
2109 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
2110 if (flag_debug_asm)
2111 fprintf (asm_out_file, "\t%s CIE Augmentation (none)",
2112 ASM_COMMENT_START);
2115 fputc ('\n', asm_out_file);
2116 output_uleb128 (1);
2117 if (flag_debug_asm)
2118 fprintf (asm_out_file, " (CIE Code Alignment Factor)");
2120 fputc ('\n', asm_out_file);
2121 output_sleb128 (DWARF_CIE_DATA_ALIGNMENT);
2122 if (flag_debug_asm)
2123 fprintf (asm_out_file, " (CIE Data Alignment Factor)");
2125 fputc ('\n', asm_out_file);
2126 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DWARF_FRAME_RETURN_COLUMN);
2127 if (flag_debug_asm)
2128 fprintf (asm_out_file, "\t%s CIE RA Column", ASM_COMMENT_START);
2130 fputc ('\n', asm_out_file);
2132 for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2133 output_cfi (cfi, NULL);
2135 /* Pad the CIE out to an address sized boundary. */
2136 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (DWARF2_ADDR_SIZE));
2137 ASM_OUTPUT_LABEL (asm_out_file, l2);
2138 #ifdef ASM_OUTPUT_DEFINE_LABEL_DIFFERENCE_SYMBOL
2139 ASM_OUTPUT_DEFINE_LABEL_DIFFERENCE_SYMBOL (asm_out_file, ld, l2, l1);
2140 if (flag_debug_asm)
2141 fprintf (asm_out_file, "\t%s CIE Length Symbol", ASM_COMMENT_START);
2142 fputc ('\n', asm_out_file);
2143 #endif
2145 /* Loop through all of the FDE's. */
2146 for (i = 0; i < fde_table_in_use; ++i)
2148 fde = &fde_table[i];
2150 /* Don't emit EH unwind info for leaf functions. */
2151 if (for_eh && fde->nothrow)
2152 continue;
2154 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2155 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2156 #ifdef ASM_OUTPUT_DEFINE_LABEL_DIFFERENCE_SYMBOL
2157 ASM_GENERATE_INTERNAL_LABEL (ld, FDE_LENGTH_LABEL, for_eh + i * 2);
2158 if (for_eh)
2159 ASM_OUTPUT_DWARF_OFFSET4 (asm_out_file, ld);
2160 else
2161 ASM_OUTPUT_DWARF_OFFSET (asm_out_file, ld);
2162 #else
2163 if (for_eh)
2164 ASM_OUTPUT_DWARF_DELTA4 (asm_out_file, l2, l1);
2165 else
2166 ASM_OUTPUT_DWARF_DELTA (asm_out_file, l2, l1);
2167 #endif
2168 if (flag_debug_asm)
2169 fprintf (asm_out_file, "\t%s FDE Length", ASM_COMMENT_START);
2170 fputc ('\n', asm_out_file);
2171 ASM_OUTPUT_LABEL (asm_out_file, l1);
2173 /* ??? This always emits a 4 byte offset when for_eh is true, but it
2174 emits a target dependent sized offset when for_eh is not true.
2175 This inconsistency may confuse gdb. The only case where we need a
2176 non-4 byte offset is for the Irix6 N64 ABI, so we may lose SGI
2177 compatibility if we emit a 4 byte offset. We need a 4 byte offset
2178 though in order to be compatible with the dwarf_fde struct in frame.c.
2179 If the for_eh case is changed, then the struct in frame.c has
2180 to be adjusted appropriately. */
2181 if (for_eh)
2182 ASM_OUTPUT_DWARF_DELTA4 (asm_out_file, l1, "__FRAME_BEGIN__");
2183 else
2184 ASM_OUTPUT_DWARF_OFFSET (asm_out_file, stripattributes (FRAME_SECTION));
2185 if (flag_debug_asm)
2186 fprintf (asm_out_file, "\t%s FDE CIE offset", ASM_COMMENT_START);
2188 fputc ('\n', asm_out_file);
2189 ASM_OUTPUT_DWARF_ADDR (asm_out_file, fde->dw_fde_begin);
2190 if (flag_debug_asm)
2191 fprintf (asm_out_file, "\t%s FDE initial location", ASM_COMMENT_START);
2193 fputc ('\n', asm_out_file);
2194 ASM_OUTPUT_DWARF_ADDR_DELTA (asm_out_file,
2195 fde->dw_fde_end, fde->dw_fde_begin);
2196 if (flag_debug_asm)
2197 fprintf (asm_out_file, "\t%s FDE address range", ASM_COMMENT_START);
2199 fputc ('\n', asm_out_file);
2201 /* Loop through the Call Frame Instructions associated with
2202 this FDE. */
2203 fde->dw_fde_current_label = fde->dw_fde_begin;
2204 for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2205 output_cfi (cfi, fde);
2207 /* Pad the FDE out to an address sized boundary. */
2208 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (DWARF2_ADDR_SIZE));
2209 ASM_OUTPUT_LABEL (asm_out_file, l2);
2210 #ifdef ASM_OUTPUT_DEFINE_LABEL_DIFFERENCE_SYMBOL
2211 ASM_OUTPUT_DEFINE_LABEL_DIFFERENCE_SYMBOL (asm_out_file, ld, l2, l1);
2212 if (flag_debug_asm)
2213 fprintf (asm_out_file, "\t%s FDE Length Symbol", ASM_COMMENT_START);
2214 fputc ('\n', asm_out_file);
2215 #endif
2217 #ifndef EH_FRAME_SECTION
2218 if (for_eh)
2220 /* Emit terminating zero for table. */
2221 ASM_OUTPUT_DWARF_DATA4 (asm_out_file, 0);
2222 fputc ('\n', asm_out_file);
2224 #endif
2225 #ifdef MIPS_DEBUGGING_INFO
2226 /* Work around Irix 6 assembler bug whereby labels at the end of a section
2227 get a value of 0. Putting .align 0 after the label fixes it. */
2228 ASM_OUTPUT_ALIGN (asm_out_file, 0);
2229 #endif
2231 /* Turn off app to make assembly quicker. */
2232 if (flag_debug_asm)
2233 app_disable ();
2236 /* Output a marker (i.e. a label) for the beginning of a function, before
2237 the prologue. */
2239 void
2240 dwarf2out_begin_prologue ()
2242 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2243 register dw_fde_ref fde;
2245 ++current_funcdef_number;
2247 function_section (current_function_decl);
2248 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2249 current_funcdef_number);
2250 ASM_OUTPUT_LABEL (asm_out_file, label);
2251 current_function_func_begin_label = get_identifier (label);
2253 /* Expand the fde table if necessary. */
2254 if (fde_table_in_use == fde_table_allocated)
2256 fde_table_allocated += FDE_TABLE_INCREMENT;
2257 fde_table
2258 = (dw_fde_ref) xrealloc (fde_table,
2259 fde_table_allocated * sizeof (dw_fde_node));
2262 /* Record the FDE associated with this function. */
2263 current_funcdef_fde = fde_table_in_use;
2265 /* Add the new FDE at the end of the fde_table. */
2266 fde = &fde_table[fde_table_in_use++];
2267 fde->dw_fde_begin = xstrdup (label);
2268 fde->dw_fde_current_label = NULL;
2269 fde->dw_fde_end = NULL;
2270 fde->dw_fde_cfi = NULL;
2271 fde->nothrow = current_function_nothrow;
2273 args_size = old_args_size = 0;
2276 /* Output a marker (i.e. a label) for the absolute end of the generated code
2277 for a function definition. This gets called *after* the epilogue code has
2278 been generated. */
2280 void
2281 dwarf2out_end_epilogue ()
2283 dw_fde_ref fde;
2284 char label[MAX_ARTIFICIAL_LABEL_BYTES];
2286 /* Output a label to mark the endpoint of the code generated for this
2287 function. */
2288 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL, current_funcdef_number);
2289 ASM_OUTPUT_LABEL (asm_out_file, label);
2290 fde = &fde_table[fde_table_in_use - 1];
2291 fde->dw_fde_end = xstrdup (label);
2294 void
2295 dwarf2out_frame_init ()
2297 /* Allocate the initial hunk of the fde_table. */
2298 fde_table = (dw_fde_ref) xcalloc (FDE_TABLE_INCREMENT, sizeof (dw_fde_node));
2299 fde_table_allocated = FDE_TABLE_INCREMENT;
2300 fde_table_in_use = 0;
2302 /* Generate the CFA instructions common to all FDE's. Do it now for the
2303 sake of lookup_cfa. */
2305 #ifdef DWARF2_UNWIND_INFO
2306 /* On entry, the Canonical Frame Address is at SP. */
2307 dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2308 initial_return_save (INCOMING_RETURN_ADDR_RTX);
2309 #endif
2312 void
2313 dwarf2out_frame_finish ()
2315 /* Output call frame information. */
2316 #ifdef MIPS_DEBUGGING_INFO
2317 if (write_symbols == DWARF2_DEBUG)
2318 output_call_frame_info (0);
2319 if (flag_unwind_tables || (flag_exceptions && ! exceptions_via_longjmp))
2320 output_call_frame_info (1);
2321 #else
2322 if (write_symbols == DWARF2_DEBUG
2323 || flag_unwind_tables || (flag_exceptions && ! exceptions_via_longjmp))
2324 output_call_frame_info (1);
2325 #endif
2328 /* And now, the subset of the debugging information support code necessary
2329 for emitting location expressions. */
2331 typedef struct dw_val_struct *dw_val_ref;
2332 typedef struct die_struct *dw_die_ref;
2333 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2335 /* Each DIE may have a series of attribute/value pairs. Values
2336 can take on several forms. The forms that are used in this
2337 implementation are listed below. */
2339 typedef enum
2341 dw_val_class_addr,
2342 dw_val_class_loc,
2343 dw_val_class_const,
2344 dw_val_class_unsigned_const,
2345 dw_val_class_long_long,
2346 dw_val_class_float,
2347 dw_val_class_flag,
2348 dw_val_class_die_ref,
2349 dw_val_class_fde_ref,
2350 dw_val_class_lbl_id,
2351 dw_val_class_lbl_offset,
2352 dw_val_class_str
2354 dw_val_class;
2356 /* Describe a double word constant value. */
2357 /* ??? Every instance of long_long in the code really means CONST_DOUBLE. */
2359 typedef struct dw_long_long_struct
2361 unsigned long hi;
2362 unsigned long low;
2364 dw_long_long_const;
2366 /* Describe a floating point constant value. */
2368 typedef struct dw_fp_struct
2370 long *array;
2371 unsigned length;
2373 dw_float_const;
2375 /* The dw_val_node describes an attribute's value, as it is
2376 represented internally. */
2378 typedef struct dw_val_struct
2380 dw_val_class val_class;
2381 union
2383 rtx val_addr;
2384 dw_loc_descr_ref val_loc;
2385 long int val_int;
2386 long unsigned val_unsigned;
2387 dw_long_long_const val_long_long;
2388 dw_float_const val_float;
2389 struct {
2390 dw_die_ref die;
2391 int external;
2392 } val_die_ref;
2393 unsigned val_fde_index;
2394 char *val_str;
2395 char *val_lbl_id;
2396 unsigned char val_flag;
2400 dw_val_node;
2402 /* Locations in memory are described using a sequence of stack machine
2403 operations. */
2405 typedef struct dw_loc_descr_struct
2407 dw_loc_descr_ref dw_loc_next;
2408 enum dwarf_location_atom dw_loc_opc;
2409 dw_val_node dw_loc_oprnd1;
2410 dw_val_node dw_loc_oprnd2;
2411 int dw_loc_addr;
2413 dw_loc_descr_node;
2415 static const char *dwarf_stack_op_name PARAMS ((unsigned));
2416 static dw_loc_descr_ref new_loc_descr PARAMS ((enum dwarf_location_atom,
2417 unsigned long,
2418 unsigned long));
2419 static void add_loc_descr PARAMS ((dw_loc_descr_ref *,
2420 dw_loc_descr_ref));
2421 static unsigned long size_of_loc_descr PARAMS ((dw_loc_descr_ref));
2422 static unsigned long size_of_locs PARAMS ((dw_loc_descr_ref));
2423 static void output_loc_operands PARAMS ((dw_loc_descr_ref));
2424 static void output_loc_sequence PARAMS ((dw_loc_descr_ref));
2426 /* Convert a DWARF stack opcode into its string name. */
2428 static const char *
2429 dwarf_stack_op_name (op)
2430 register unsigned op;
2432 switch (op)
2434 case DW_OP_addr:
2435 return "DW_OP_addr";
2436 case DW_OP_deref:
2437 return "DW_OP_deref";
2438 case DW_OP_const1u:
2439 return "DW_OP_const1u";
2440 case DW_OP_const1s:
2441 return "DW_OP_const1s";
2442 case DW_OP_const2u:
2443 return "DW_OP_const2u";
2444 case DW_OP_const2s:
2445 return "DW_OP_const2s";
2446 case DW_OP_const4u:
2447 return "DW_OP_const4u";
2448 case DW_OP_const4s:
2449 return "DW_OP_const4s";
2450 case DW_OP_const8u:
2451 return "DW_OP_const8u";
2452 case DW_OP_const8s:
2453 return "DW_OP_const8s";
2454 case DW_OP_constu:
2455 return "DW_OP_constu";
2456 case DW_OP_consts:
2457 return "DW_OP_consts";
2458 case DW_OP_dup:
2459 return "DW_OP_dup";
2460 case DW_OP_drop:
2461 return "DW_OP_drop";
2462 case DW_OP_over:
2463 return "DW_OP_over";
2464 case DW_OP_pick:
2465 return "DW_OP_pick";
2466 case DW_OP_swap:
2467 return "DW_OP_swap";
2468 case DW_OP_rot:
2469 return "DW_OP_rot";
2470 case DW_OP_xderef:
2471 return "DW_OP_xderef";
2472 case DW_OP_abs:
2473 return "DW_OP_abs";
2474 case DW_OP_and:
2475 return "DW_OP_and";
2476 case DW_OP_div:
2477 return "DW_OP_div";
2478 case DW_OP_minus:
2479 return "DW_OP_minus";
2480 case DW_OP_mod:
2481 return "DW_OP_mod";
2482 case DW_OP_mul:
2483 return "DW_OP_mul";
2484 case DW_OP_neg:
2485 return "DW_OP_neg";
2486 case DW_OP_not:
2487 return "DW_OP_not";
2488 case DW_OP_or:
2489 return "DW_OP_or";
2490 case DW_OP_plus:
2491 return "DW_OP_plus";
2492 case DW_OP_plus_uconst:
2493 return "DW_OP_plus_uconst";
2494 case DW_OP_shl:
2495 return "DW_OP_shl";
2496 case DW_OP_shr:
2497 return "DW_OP_shr";
2498 case DW_OP_shra:
2499 return "DW_OP_shra";
2500 case DW_OP_xor:
2501 return "DW_OP_xor";
2502 case DW_OP_bra:
2503 return "DW_OP_bra";
2504 case DW_OP_eq:
2505 return "DW_OP_eq";
2506 case DW_OP_ge:
2507 return "DW_OP_ge";
2508 case DW_OP_gt:
2509 return "DW_OP_gt";
2510 case DW_OP_le:
2511 return "DW_OP_le";
2512 case DW_OP_lt:
2513 return "DW_OP_lt";
2514 case DW_OP_ne:
2515 return "DW_OP_ne";
2516 case DW_OP_skip:
2517 return "DW_OP_skip";
2518 case DW_OP_lit0:
2519 return "DW_OP_lit0";
2520 case DW_OP_lit1:
2521 return "DW_OP_lit1";
2522 case DW_OP_lit2:
2523 return "DW_OP_lit2";
2524 case DW_OP_lit3:
2525 return "DW_OP_lit3";
2526 case DW_OP_lit4:
2527 return "DW_OP_lit4";
2528 case DW_OP_lit5:
2529 return "DW_OP_lit5";
2530 case DW_OP_lit6:
2531 return "DW_OP_lit6";
2532 case DW_OP_lit7:
2533 return "DW_OP_lit7";
2534 case DW_OP_lit8:
2535 return "DW_OP_lit8";
2536 case DW_OP_lit9:
2537 return "DW_OP_lit9";
2538 case DW_OP_lit10:
2539 return "DW_OP_lit10";
2540 case DW_OP_lit11:
2541 return "DW_OP_lit11";
2542 case DW_OP_lit12:
2543 return "DW_OP_lit12";
2544 case DW_OP_lit13:
2545 return "DW_OP_lit13";
2546 case DW_OP_lit14:
2547 return "DW_OP_lit14";
2548 case DW_OP_lit15:
2549 return "DW_OP_lit15";
2550 case DW_OP_lit16:
2551 return "DW_OP_lit16";
2552 case DW_OP_lit17:
2553 return "DW_OP_lit17";
2554 case DW_OP_lit18:
2555 return "DW_OP_lit18";
2556 case DW_OP_lit19:
2557 return "DW_OP_lit19";
2558 case DW_OP_lit20:
2559 return "DW_OP_lit20";
2560 case DW_OP_lit21:
2561 return "DW_OP_lit21";
2562 case DW_OP_lit22:
2563 return "DW_OP_lit22";
2564 case DW_OP_lit23:
2565 return "DW_OP_lit23";
2566 case DW_OP_lit24:
2567 return "DW_OP_lit24";
2568 case DW_OP_lit25:
2569 return "DW_OP_lit25";
2570 case DW_OP_lit26:
2571 return "DW_OP_lit26";
2572 case DW_OP_lit27:
2573 return "DW_OP_lit27";
2574 case DW_OP_lit28:
2575 return "DW_OP_lit28";
2576 case DW_OP_lit29:
2577 return "DW_OP_lit29";
2578 case DW_OP_lit30:
2579 return "DW_OP_lit30";
2580 case DW_OP_lit31:
2581 return "DW_OP_lit31";
2582 case DW_OP_reg0:
2583 return "DW_OP_reg0";
2584 case DW_OP_reg1:
2585 return "DW_OP_reg1";
2586 case DW_OP_reg2:
2587 return "DW_OP_reg2";
2588 case DW_OP_reg3:
2589 return "DW_OP_reg3";
2590 case DW_OP_reg4:
2591 return "DW_OP_reg4";
2592 case DW_OP_reg5:
2593 return "DW_OP_reg5";
2594 case DW_OP_reg6:
2595 return "DW_OP_reg6";
2596 case DW_OP_reg7:
2597 return "DW_OP_reg7";
2598 case DW_OP_reg8:
2599 return "DW_OP_reg8";
2600 case DW_OP_reg9:
2601 return "DW_OP_reg9";
2602 case DW_OP_reg10:
2603 return "DW_OP_reg10";
2604 case DW_OP_reg11:
2605 return "DW_OP_reg11";
2606 case DW_OP_reg12:
2607 return "DW_OP_reg12";
2608 case DW_OP_reg13:
2609 return "DW_OP_reg13";
2610 case DW_OP_reg14:
2611 return "DW_OP_reg14";
2612 case DW_OP_reg15:
2613 return "DW_OP_reg15";
2614 case DW_OP_reg16:
2615 return "DW_OP_reg16";
2616 case DW_OP_reg17:
2617 return "DW_OP_reg17";
2618 case DW_OP_reg18:
2619 return "DW_OP_reg18";
2620 case DW_OP_reg19:
2621 return "DW_OP_reg19";
2622 case DW_OP_reg20:
2623 return "DW_OP_reg20";
2624 case DW_OP_reg21:
2625 return "DW_OP_reg21";
2626 case DW_OP_reg22:
2627 return "DW_OP_reg22";
2628 case DW_OP_reg23:
2629 return "DW_OP_reg23";
2630 case DW_OP_reg24:
2631 return "DW_OP_reg24";
2632 case DW_OP_reg25:
2633 return "DW_OP_reg25";
2634 case DW_OP_reg26:
2635 return "DW_OP_reg26";
2636 case DW_OP_reg27:
2637 return "DW_OP_reg27";
2638 case DW_OP_reg28:
2639 return "DW_OP_reg28";
2640 case DW_OP_reg29:
2641 return "DW_OP_reg29";
2642 case DW_OP_reg30:
2643 return "DW_OP_reg30";
2644 case DW_OP_reg31:
2645 return "DW_OP_reg31";
2646 case DW_OP_breg0:
2647 return "DW_OP_breg0";
2648 case DW_OP_breg1:
2649 return "DW_OP_breg1";
2650 case DW_OP_breg2:
2651 return "DW_OP_breg2";
2652 case DW_OP_breg3:
2653 return "DW_OP_breg3";
2654 case DW_OP_breg4:
2655 return "DW_OP_breg4";
2656 case DW_OP_breg5:
2657 return "DW_OP_breg5";
2658 case DW_OP_breg6:
2659 return "DW_OP_breg6";
2660 case DW_OP_breg7:
2661 return "DW_OP_breg7";
2662 case DW_OP_breg8:
2663 return "DW_OP_breg8";
2664 case DW_OP_breg9:
2665 return "DW_OP_breg9";
2666 case DW_OP_breg10:
2667 return "DW_OP_breg10";
2668 case DW_OP_breg11:
2669 return "DW_OP_breg11";
2670 case DW_OP_breg12:
2671 return "DW_OP_breg12";
2672 case DW_OP_breg13:
2673 return "DW_OP_breg13";
2674 case DW_OP_breg14:
2675 return "DW_OP_breg14";
2676 case DW_OP_breg15:
2677 return "DW_OP_breg15";
2678 case DW_OP_breg16:
2679 return "DW_OP_breg16";
2680 case DW_OP_breg17:
2681 return "DW_OP_breg17";
2682 case DW_OP_breg18:
2683 return "DW_OP_breg18";
2684 case DW_OP_breg19:
2685 return "DW_OP_breg19";
2686 case DW_OP_breg20:
2687 return "DW_OP_breg20";
2688 case DW_OP_breg21:
2689 return "DW_OP_breg21";
2690 case DW_OP_breg22:
2691 return "DW_OP_breg22";
2692 case DW_OP_breg23:
2693 return "DW_OP_breg23";
2694 case DW_OP_breg24:
2695 return "DW_OP_breg24";
2696 case DW_OP_breg25:
2697 return "DW_OP_breg25";
2698 case DW_OP_breg26:
2699 return "DW_OP_breg26";
2700 case DW_OP_breg27:
2701 return "DW_OP_breg27";
2702 case DW_OP_breg28:
2703 return "DW_OP_breg28";
2704 case DW_OP_breg29:
2705 return "DW_OP_breg29";
2706 case DW_OP_breg30:
2707 return "DW_OP_breg30";
2708 case DW_OP_breg31:
2709 return "DW_OP_breg31";
2710 case DW_OP_regx:
2711 return "DW_OP_regx";
2712 case DW_OP_fbreg:
2713 return "DW_OP_fbreg";
2714 case DW_OP_bregx:
2715 return "DW_OP_bregx";
2716 case DW_OP_piece:
2717 return "DW_OP_piece";
2718 case DW_OP_deref_size:
2719 return "DW_OP_deref_size";
2720 case DW_OP_xderef_size:
2721 return "DW_OP_xderef_size";
2722 case DW_OP_nop:
2723 return "DW_OP_nop";
2724 default:
2725 return "OP_<unknown>";
2729 /* Return a pointer to a newly allocated location description. Location
2730 descriptions are simple expression terms that can be strung
2731 together to form more complicated location (address) descriptions. */
2733 static inline dw_loc_descr_ref
2734 new_loc_descr (op, oprnd1, oprnd2)
2735 register enum dwarf_location_atom op;
2736 register unsigned long oprnd1;
2737 register unsigned long oprnd2;
2739 /* Use xcalloc here so we clear out all of the long_long constant in
2740 the union. */
2741 register dw_loc_descr_ref descr
2742 = (dw_loc_descr_ref) xcalloc (1, sizeof (dw_loc_descr_node));
2744 descr->dw_loc_opc = op;
2745 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
2746 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
2747 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
2748 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
2750 return descr;
2753 /* Add a location description term to a location description expression. */
2755 static inline void
2756 add_loc_descr (list_head, descr)
2757 register dw_loc_descr_ref *list_head;
2758 register dw_loc_descr_ref descr;
2760 register dw_loc_descr_ref *d;
2762 /* Find the end of the chain. */
2763 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
2766 *d = descr;
2769 /* Return the size of a location descriptor. */
2771 static unsigned long
2772 size_of_loc_descr (loc)
2773 register dw_loc_descr_ref loc;
2775 register unsigned long size = 1;
2777 switch (loc->dw_loc_opc)
2779 case DW_OP_addr:
2780 size += DWARF2_ADDR_SIZE;
2781 break;
2782 case DW_OP_const1u:
2783 case DW_OP_const1s:
2784 size += 1;
2785 break;
2786 case DW_OP_const2u:
2787 case DW_OP_const2s:
2788 size += 2;
2789 break;
2790 case DW_OP_const4u:
2791 case DW_OP_const4s:
2792 size += 4;
2793 break;
2794 case DW_OP_const8u:
2795 case DW_OP_const8s:
2796 size += 8;
2797 break;
2798 case DW_OP_constu:
2799 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2800 break;
2801 case DW_OP_consts:
2802 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2803 break;
2804 case DW_OP_pick:
2805 size += 1;
2806 break;
2807 case DW_OP_plus_uconst:
2808 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2809 break;
2810 case DW_OP_skip:
2811 case DW_OP_bra:
2812 size += 2;
2813 break;
2814 case DW_OP_breg0:
2815 case DW_OP_breg1:
2816 case DW_OP_breg2:
2817 case DW_OP_breg3:
2818 case DW_OP_breg4:
2819 case DW_OP_breg5:
2820 case DW_OP_breg6:
2821 case DW_OP_breg7:
2822 case DW_OP_breg8:
2823 case DW_OP_breg9:
2824 case DW_OP_breg10:
2825 case DW_OP_breg11:
2826 case DW_OP_breg12:
2827 case DW_OP_breg13:
2828 case DW_OP_breg14:
2829 case DW_OP_breg15:
2830 case DW_OP_breg16:
2831 case DW_OP_breg17:
2832 case DW_OP_breg18:
2833 case DW_OP_breg19:
2834 case DW_OP_breg20:
2835 case DW_OP_breg21:
2836 case DW_OP_breg22:
2837 case DW_OP_breg23:
2838 case DW_OP_breg24:
2839 case DW_OP_breg25:
2840 case DW_OP_breg26:
2841 case DW_OP_breg27:
2842 case DW_OP_breg28:
2843 case DW_OP_breg29:
2844 case DW_OP_breg30:
2845 case DW_OP_breg31:
2846 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2847 break;
2848 case DW_OP_regx:
2849 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2850 break;
2851 case DW_OP_fbreg:
2852 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2853 break;
2854 case DW_OP_bregx:
2855 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2856 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
2857 break;
2858 case DW_OP_piece:
2859 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2860 break;
2861 case DW_OP_deref_size:
2862 case DW_OP_xderef_size:
2863 size += 1;
2864 break;
2865 default:
2866 break;
2869 return size;
2872 /* Return the size of a series of location descriptors. */
2874 static unsigned long
2875 size_of_locs (loc)
2876 register dw_loc_descr_ref loc;
2878 register unsigned long size = 0;
2880 for (; loc != NULL; loc = loc->dw_loc_next)
2882 loc->dw_loc_addr = size;
2883 size += size_of_loc_descr (loc);
2886 return size;
2889 /* Output location description stack opcode's operands (if any). */
2891 static void
2892 output_loc_operands (loc)
2893 register dw_loc_descr_ref loc;
2895 register dw_val_ref val1 = &loc->dw_loc_oprnd1;
2896 register dw_val_ref val2 = &loc->dw_loc_oprnd2;
2898 switch (loc->dw_loc_opc)
2900 #ifdef DWARF2_DEBUGGING_INFO
2901 case DW_OP_addr:
2902 ASM_OUTPUT_DWARF_ADDR_CONST (asm_out_file, val1->v.val_addr);
2903 fputc ('\n', asm_out_file);
2904 break;
2905 case DW_OP_const2u:
2906 case DW_OP_const2s:
2907 ASM_OUTPUT_DWARF_DATA2 (asm_out_file, val1->v.val_int);
2908 fputc ('\n', asm_out_file);
2909 break;
2910 case DW_OP_const4u:
2911 case DW_OP_const4s:
2912 ASM_OUTPUT_DWARF_DATA4 (asm_out_file, val1->v.val_int);
2913 fputc ('\n', asm_out_file);
2914 break;
2915 case DW_OP_const8u:
2916 case DW_OP_const8s:
2917 abort ();
2918 fputc ('\n', asm_out_file);
2919 break;
2920 case DW_OP_skip:
2921 case DW_OP_bra:
2923 int offset;
2925 if (val1->val_class == dw_val_class_loc)
2926 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2927 else
2928 abort ();
2930 ASM_OUTPUT_DWARF_DATA2 (asm_out_file, offset);
2931 fputc ('\n', asm_out_file);
2933 break;
2934 #else
2935 case DW_OP_addr:
2936 case DW_OP_const2u:
2937 case DW_OP_const2s:
2938 case DW_OP_const4u:
2939 case DW_OP_const4s:
2940 case DW_OP_const8u:
2941 case DW_OP_const8s:
2942 case DW_OP_skip:
2943 case DW_OP_bra:
2944 /* We currently don't make any attempt to make sure these are
2945 aligned properly like we do for the main unwind info, so
2946 don't support emitting things larger than a byte if we're
2947 only doing unwinding. */
2948 abort ();
2949 #endif
2950 case DW_OP_const1u:
2951 case DW_OP_const1s:
2952 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, val1->v.val_flag);
2953 fputc ('\n', asm_out_file);
2954 break;
2955 case DW_OP_constu:
2956 output_uleb128 (val1->v.val_unsigned);
2957 fputc ('\n', asm_out_file);
2958 break;
2959 case DW_OP_consts:
2960 output_sleb128 (val1->v.val_int);
2961 fputc ('\n', asm_out_file);
2962 break;
2963 case DW_OP_pick:
2964 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, val1->v.val_int);
2965 fputc ('\n', asm_out_file);
2966 break;
2967 case DW_OP_plus_uconst:
2968 output_uleb128 (val1->v.val_unsigned);
2969 fputc ('\n', asm_out_file);
2970 break;
2971 case DW_OP_breg0:
2972 case DW_OP_breg1:
2973 case DW_OP_breg2:
2974 case DW_OP_breg3:
2975 case DW_OP_breg4:
2976 case DW_OP_breg5:
2977 case DW_OP_breg6:
2978 case DW_OP_breg7:
2979 case DW_OP_breg8:
2980 case DW_OP_breg9:
2981 case DW_OP_breg10:
2982 case DW_OP_breg11:
2983 case DW_OP_breg12:
2984 case DW_OP_breg13:
2985 case DW_OP_breg14:
2986 case DW_OP_breg15:
2987 case DW_OP_breg16:
2988 case DW_OP_breg17:
2989 case DW_OP_breg18:
2990 case DW_OP_breg19:
2991 case DW_OP_breg20:
2992 case DW_OP_breg21:
2993 case DW_OP_breg22:
2994 case DW_OP_breg23:
2995 case DW_OP_breg24:
2996 case DW_OP_breg25:
2997 case DW_OP_breg26:
2998 case DW_OP_breg27:
2999 case DW_OP_breg28:
3000 case DW_OP_breg29:
3001 case DW_OP_breg30:
3002 case DW_OP_breg31:
3003 output_sleb128 (val1->v.val_int);
3004 fputc ('\n', asm_out_file);
3005 break;
3006 case DW_OP_regx:
3007 output_uleb128 (val1->v.val_unsigned);
3008 fputc ('\n', asm_out_file);
3009 break;
3010 case DW_OP_fbreg:
3011 output_sleb128 (val1->v.val_int);
3012 fputc ('\n', asm_out_file);
3013 break;
3014 case DW_OP_bregx:
3015 output_uleb128 (val1->v.val_unsigned);
3016 fputc ('\n', asm_out_file);
3017 output_sleb128 (val2->v.val_int);
3018 fputc ('\n', asm_out_file);
3019 break;
3020 case DW_OP_piece:
3021 output_uleb128 (val1->v.val_unsigned);
3022 fputc ('\n', asm_out_file);
3023 break;
3024 case DW_OP_deref_size:
3025 case DW_OP_xderef_size:
3026 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, val1->v.val_flag);
3027 fputc ('\n', asm_out_file);
3028 break;
3029 default:
3030 /* Other codes have no operands. */
3031 break;
3035 /* Output a sequence of location operations. */
3037 static void
3038 output_loc_sequence (loc)
3039 dw_loc_descr_ref loc;
3041 for (; loc != NULL; loc = loc->dw_loc_next)
3043 /* Output the opcode. */
3044 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, loc->dw_loc_opc);
3045 if (flag_debug_asm)
3046 fprintf (asm_out_file, "\t%s %s", ASM_COMMENT_START,
3047 dwarf_stack_op_name (loc->dw_loc_opc));
3049 fputc ('\n', asm_out_file);
3051 /* Output the operand(s) (if any). */
3052 output_loc_operands (loc);
3056 /* This routine will generate the correct assembly data for a location
3057 description based on a cfi entry with a complex address. */
3059 static void
3060 output_cfa_loc (cfi)
3061 dw_cfi_ref cfi;
3063 dw_loc_descr_ref loc;
3064 unsigned long size;
3066 /* Output the size of the block. */
3067 loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3068 size = size_of_locs (loc);
3069 output_uleb128 (size);
3070 fputc ('\n', asm_out_file);
3072 /* Now output the operations themselves. */
3073 output_loc_sequence (loc);
3076 /* This function builds a dwarf location descriptor seqeunce from
3077 a dw_cfa_location. */
3079 static struct dw_loc_descr_struct *
3080 build_cfa_loc (cfa)
3081 dw_cfa_location *cfa;
3083 struct dw_loc_descr_struct *head, *tmp;
3085 if (cfa->indirect == 0)
3086 abort ();
3088 if (cfa->base_offset)
3090 if (cfa->reg <= 31)
3091 head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3092 else
3093 head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3095 else if (cfa->reg <= 31)
3096 head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3097 else
3098 head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3099 head->dw_loc_oprnd1.val_class = dw_val_class_const;
3100 tmp = new_loc_descr (DW_OP_deref, 0, 0);
3101 add_loc_descr (&head, tmp);
3102 if (cfa->offset != 0)
3104 tmp = new_loc_descr (DW_OP_plus_uconst, cfa->offset, 0);
3105 add_loc_descr (&head, tmp);
3107 return head;
3110 /* This function fills in aa dw_cfa_location structure from a
3111 dwarf location descriptor sequence. */
3113 static void
3114 get_cfa_from_loc_descr (cfa, loc)
3115 dw_cfa_location *cfa;
3116 struct dw_loc_descr_struct *loc;
3118 struct dw_loc_descr_struct *ptr;
3119 cfa->offset = 0;
3120 cfa->base_offset = 0;
3121 cfa->indirect = 0;
3122 cfa->reg = -1;
3124 for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3126 enum dwarf_location_atom op = ptr->dw_loc_opc;
3127 switch (op)
3129 case DW_OP_reg0:
3130 case DW_OP_reg1:
3131 case DW_OP_reg2:
3132 case DW_OP_reg3:
3133 case DW_OP_reg4:
3134 case DW_OP_reg5:
3135 case DW_OP_reg6:
3136 case DW_OP_reg7:
3137 case DW_OP_reg8:
3138 case DW_OP_reg9:
3139 case DW_OP_reg10:
3140 case DW_OP_reg11:
3141 case DW_OP_reg12:
3142 case DW_OP_reg13:
3143 case DW_OP_reg14:
3144 case DW_OP_reg15:
3145 case DW_OP_reg16:
3146 case DW_OP_reg17:
3147 case DW_OP_reg18:
3148 case DW_OP_reg19:
3149 case DW_OP_reg20:
3150 case DW_OP_reg21:
3151 case DW_OP_reg22:
3152 case DW_OP_reg23:
3153 case DW_OP_reg24:
3154 case DW_OP_reg25:
3155 case DW_OP_reg26:
3156 case DW_OP_reg27:
3157 case DW_OP_reg28:
3158 case DW_OP_reg29:
3159 case DW_OP_reg30:
3160 case DW_OP_reg31:
3161 cfa->reg = op - DW_OP_reg0;
3162 break;
3163 case DW_OP_regx:
3164 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3165 break;
3166 case DW_OP_breg0:
3167 case DW_OP_breg1:
3168 case DW_OP_breg2:
3169 case DW_OP_breg3:
3170 case DW_OP_breg4:
3171 case DW_OP_breg5:
3172 case DW_OP_breg6:
3173 case DW_OP_breg7:
3174 case DW_OP_breg8:
3175 case DW_OP_breg9:
3176 case DW_OP_breg10:
3177 case DW_OP_breg11:
3178 case DW_OP_breg12:
3179 case DW_OP_breg13:
3180 case DW_OP_breg14:
3181 case DW_OP_breg15:
3182 case DW_OP_breg16:
3183 case DW_OP_breg17:
3184 case DW_OP_breg18:
3185 case DW_OP_breg19:
3186 case DW_OP_breg20:
3187 case DW_OP_breg21:
3188 case DW_OP_breg22:
3189 case DW_OP_breg23:
3190 case DW_OP_breg24:
3191 case DW_OP_breg25:
3192 case DW_OP_breg26:
3193 case DW_OP_breg27:
3194 case DW_OP_breg28:
3195 case DW_OP_breg29:
3196 case DW_OP_breg30:
3197 case DW_OP_breg31:
3198 cfa->reg = op - DW_OP_breg0;
3199 cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3200 break;
3201 case DW_OP_bregx:
3202 cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3203 cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3204 break;
3205 case DW_OP_deref:
3206 cfa->indirect = 1;
3207 break;
3208 case DW_OP_plus_uconst:
3209 cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3210 break;
3211 default:
3212 internal_error ("DW_LOC_OP %s not implememnted\n",
3213 dwarf_stack_op_name (ptr->dw_loc_opc));
3217 #endif /* .debug_frame support */
3219 /* And now, the support for symbolic debugging information. */
3220 #ifdef DWARF2_DEBUGGING_INFO
3222 /* NOTE: In the comments in this file, many references are made to
3223 "Debugging Information Entries". This term is abbreviated as `DIE'
3224 throughout the remainder of this file. */
3226 /* An internal representation of the DWARF output is built, and then
3227 walked to generate the DWARF debugging info. The walk of the internal
3228 representation is done after the entire program has been compiled.
3229 The types below are used to describe the internal representation. */
3231 /* Various DIE's use offsets relative to the beginning of the
3232 .debug_info section to refer to each other. */
3234 typedef long int dw_offset;
3236 /* Define typedefs here to avoid circular dependencies. */
3238 typedef struct dw_attr_struct *dw_attr_ref;
3239 typedef struct dw_line_info_struct *dw_line_info_ref;
3240 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3241 typedef struct pubname_struct *pubname_ref;
3242 typedef dw_die_ref *arange_ref;
3244 /* Each entry in the line_info_table maintains the file and
3245 line number associated with the label generated for that
3246 entry. The label gives the PC value associated with
3247 the line number entry. */
3249 typedef struct dw_line_info_struct
3251 unsigned long dw_file_num;
3252 unsigned long dw_line_num;
3254 dw_line_info_entry;
3256 /* Line information for functions in separate sections; each one gets its
3257 own sequence. */
3258 typedef struct dw_separate_line_info_struct
3260 unsigned long dw_file_num;
3261 unsigned long dw_line_num;
3262 unsigned long function;
3264 dw_separate_line_info_entry;
3266 /* Each DIE attribute has a field specifying the attribute kind,
3267 a link to the next attribute in the chain, and an attribute value.
3268 Attributes are typically linked below the DIE they modify. */
3270 typedef struct dw_attr_struct
3272 enum dwarf_attribute dw_attr;
3273 dw_attr_ref dw_attr_next;
3274 dw_val_node dw_attr_val;
3276 dw_attr_node;
3278 /* The Debugging Information Entry (DIE) structure */
3280 typedef struct die_struct
3282 enum dwarf_tag die_tag;
3283 char *die_symbol;
3284 dw_attr_ref die_attr;
3285 dw_die_ref die_parent;
3286 dw_die_ref die_child;
3287 dw_die_ref die_sib;
3288 dw_offset die_offset;
3289 unsigned long die_abbrev;
3290 int die_mark;
3292 die_node;
3294 /* The pubname structure */
3296 typedef struct pubname_struct
3298 dw_die_ref die;
3299 char *name;
3301 pubname_entry;
3303 /* The limbo die list structure. */
3304 typedef struct limbo_die_struct
3306 dw_die_ref die;
3307 struct limbo_die_struct *next;
3309 limbo_die_node;
3311 /* How to start an assembler comment. */
3312 #ifndef ASM_COMMENT_START
3313 #define ASM_COMMENT_START ";#"
3314 #endif
3316 /* Define a macro which returns non-zero for a TYPE_DECL which was
3317 implicitly generated for a tagged type.
3319 Note that unlike the gcc front end (which generates a NULL named
3320 TYPE_DECL node for each complete tagged type, each array type, and
3321 each function type node created) the g++ front end generates a
3322 _named_ TYPE_DECL node for each tagged type node created.
3323 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3324 generate a DW_TAG_typedef DIE for them. */
3326 #define TYPE_DECL_IS_STUB(decl) \
3327 (DECL_NAME (decl) == NULL_TREE \
3328 || (DECL_ARTIFICIAL (decl) \
3329 && is_tagged_type (TREE_TYPE (decl)) \
3330 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3331 /* This is necessary for stub decls that \
3332 appear in nested inline functions. */ \
3333 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3334 && (decl_ultimate_origin (decl) \
3335 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3337 /* Information concerning the compilation unit's programming
3338 language, and compiler version. */
3340 extern int flag_traditional;
3342 /* Fixed size portion of the DWARF compilation unit header. */
3343 #define DWARF_COMPILE_UNIT_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 3)
3345 /* Fixed size portion of debugging line information prolog. */
3346 #define DWARF_LINE_PROLOG_HEADER_SIZE 5
3348 /* Fixed size portion of public names info. */
3349 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3351 /* Fixed size portion of the address range info. */
3352 #define DWARF_ARANGES_HEADER_SIZE \
3353 (DWARF_ROUND (2 * DWARF_OFFSET_SIZE + 4, DWARF2_ADDR_SIZE * 2) \
3354 - DWARF_OFFSET_SIZE)
3356 /* Size of padding portion in the address range info. It must be
3357 aligned to twice the pointer size. */
3358 #define DWARF_ARANGES_PAD_SIZE \
3359 (DWARF_ROUND (2 * DWARF_OFFSET_SIZE + 4, DWARF2_ADDR_SIZE * 2) \
3360 - (2 * DWARF_OFFSET_SIZE + 4))
3362 /* Use assembler line directives if available. */
3363 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3364 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3365 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3366 #else
3367 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3368 #endif
3369 #endif
3371 /* Define the architecture-dependent minimum instruction length (in bytes).
3372 In this implementation of DWARF, this field is used for information
3373 purposes only. Since GCC generates assembly language, we have
3374 no a priori knowledge of how many instruction bytes are generated
3375 for each source line, and therefore can use only the DW_LNE_set_address
3376 and DW_LNS_fixed_advance_pc line information commands. */
3378 #ifndef DWARF_LINE_MIN_INSTR_LENGTH
3379 #define DWARF_LINE_MIN_INSTR_LENGTH 4
3380 #endif
3382 /* Minimum line offset in a special line info. opcode.
3383 This value was chosen to give a reasonable range of values. */
3384 #define DWARF_LINE_BASE -10
3386 /* First special line opcde - leave room for the standard opcodes. */
3387 #define DWARF_LINE_OPCODE_BASE 10
3389 /* Range of line offsets in a special line info. opcode. */
3390 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3392 /* Flag that indicates the initial value of the is_stmt_start flag.
3393 In the present implementation, we do not mark any lines as
3394 the beginning of a source statement, because that information
3395 is not made available by the GCC front-end. */
3396 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3398 /* This location is used by calc_die_sizes() to keep track
3399 the offset of each DIE within the .debug_info section. */
3400 static unsigned long next_die_offset;
3402 /* Record the root of the DIE's built for the current compilation unit. */
3403 static dw_die_ref comp_unit_die;
3405 /* A list of DIEs with a NULL parent waiting to be relocated. */
3406 static limbo_die_node *limbo_die_list = 0;
3408 /* Structure used by lookup_filename to manage sets of filenames. */
3409 struct file_table
3411 char **table;
3412 unsigned allocated;
3413 unsigned in_use;
3414 unsigned last_lookup_index;
3417 /* Size (in elements) of increments by which we may expand the filename
3418 table. */
3419 #define FILE_TABLE_INCREMENT 64
3421 /* Filenames referenced by declarations this compilation unit. */
3422 static struct file_table decl_file_table;
3424 /* Filenames referenced by line numbers in this compilation unit. */
3425 static struct file_table line_file_table;
3427 /* Local pointer to the name of the main input file. Initialized in
3428 dwarf2out_init. */
3429 static const char *primary_filename;
3431 /* A pointer to the base of a table of references to DIE's that describe
3432 declarations. The table is indexed by DECL_UID() which is a unique
3433 number identifying each decl. */
3434 static dw_die_ref *decl_die_table;
3436 /* Number of elements currently allocated for the decl_die_table. */
3437 static unsigned decl_die_table_allocated;
3439 /* Number of elements in decl_die_table currently in use. */
3440 static unsigned decl_die_table_in_use;
3442 /* Size (in elements) of increments by which we may expand the
3443 decl_die_table. */
3444 #define DECL_DIE_TABLE_INCREMENT 256
3446 /* A pointer to the base of a table of references to declaration
3447 scopes. This table is a display which tracks the nesting
3448 of declaration scopes at the current scope and containing
3449 scopes. This table is used to find the proper place to
3450 define type declaration DIE's. */
3451 static tree *decl_scope_table;
3453 /* Number of elements currently allocated for the decl_scope_table. */
3454 static int decl_scope_table_allocated;
3456 /* Current level of nesting of declaration scopes. */
3457 static int decl_scope_depth;
3459 /* Size (in elements) of increments by which we may expand the
3460 decl_scope_table. */
3461 #define DECL_SCOPE_TABLE_INCREMENT 64
3463 /* A pointer to the base of a list of references to DIE's that
3464 are uniquely identified by their tag, presence/absence of
3465 children DIE's, and list of attribute/value pairs. */
3466 static dw_die_ref *abbrev_die_table;
3468 /* Number of elements currently allocated for abbrev_die_table. */
3469 static unsigned abbrev_die_table_allocated;
3471 /* Number of elements in type_die_table currently in use. */
3472 static unsigned abbrev_die_table_in_use;
3474 /* Size (in elements) of increments by which we may expand the
3475 abbrev_die_table. */
3476 #define ABBREV_DIE_TABLE_INCREMENT 256
3478 /* A pointer to the base of a table that contains line information
3479 for each source code line in .text in the compilation unit. */
3480 static dw_line_info_ref line_info_table;
3482 /* Number of elements currently allocated for line_info_table. */
3483 static unsigned line_info_table_allocated;
3485 /* Number of elements in separate_line_info_table currently in use. */
3486 static unsigned separate_line_info_table_in_use;
3488 /* A pointer to the base of a table that contains line information
3489 for each source code line outside of .text in the compilation unit. */
3490 static dw_separate_line_info_ref separate_line_info_table;
3492 /* Number of elements currently allocated for separate_line_info_table. */
3493 static unsigned separate_line_info_table_allocated;
3495 /* Number of elements in line_info_table currently in use. */
3496 static unsigned line_info_table_in_use;
3498 /* Size (in elements) of increments by which we may expand the
3499 line_info_table. */
3500 #define LINE_INFO_TABLE_INCREMENT 1024
3502 /* A pointer to the base of a table that contains a list of publicly
3503 accessible names. */
3504 static pubname_ref pubname_table;
3506 /* Number of elements currently allocated for pubname_table. */
3507 static unsigned pubname_table_allocated;
3509 /* Number of elements in pubname_table currently in use. */
3510 static unsigned pubname_table_in_use;
3512 /* Size (in elements) of increments by which we may expand the
3513 pubname_table. */
3514 #define PUBNAME_TABLE_INCREMENT 64
3516 /* A pointer to the base of a table that contains a list of publicly
3517 accessible names. */
3518 static arange_ref arange_table;
3520 /* Number of elements currently allocated for arange_table. */
3521 static unsigned arange_table_allocated;
3523 /* Number of elements in arange_table currently in use. */
3524 static unsigned arange_table_in_use;
3526 /* Size (in elements) of increments by which we may expand the
3527 arange_table. */
3528 #define ARANGE_TABLE_INCREMENT 64
3530 /* A pointer to the base of a list of incomplete types which might be
3531 completed at some later time. */
3533 static tree *incomplete_types_list;
3535 /* Number of elements currently allocated for the incomplete_types_list. */
3536 static unsigned incomplete_types_allocated;
3538 /* Number of elements of incomplete_types_list currently in use. */
3539 static unsigned incomplete_types;
3541 /* Size (in elements) of increments by which we may expand the incomplete
3542 types list. Actually, a single hunk of space of this size should
3543 be enough for most typical programs. */
3544 #define INCOMPLETE_TYPES_INCREMENT 64
3546 /* Record whether the function being analyzed contains inlined functions. */
3547 static int current_function_has_inlines;
3548 #if 0 && defined (MIPS_DEBUGGING_INFO)
3549 static int comp_unit_has_inlines;
3550 #endif
3552 /* Array of RTXes referenced by the debugging information, which therefore
3553 must be kept around forever. We do this rather than perform GC on
3554 the dwarf info because almost all of the dwarf info lives forever, and
3555 it's easier to support non-GC frontends this way. */
3556 static varray_type used_rtx_varray;
3558 /* Forward declarations for functions defined in this file. */
3560 static int is_pseudo_reg PARAMS ((rtx));
3561 static tree type_main_variant PARAMS ((tree));
3562 static int is_tagged_type PARAMS ((tree));
3563 static const char *dwarf_tag_name PARAMS ((unsigned));
3564 static const char *dwarf_attr_name PARAMS ((unsigned));
3565 static const char *dwarf_form_name PARAMS ((unsigned));
3566 #if 0
3567 static const char *dwarf_type_encoding_name PARAMS ((unsigned));
3568 #endif
3569 static tree decl_ultimate_origin PARAMS ((tree));
3570 static tree block_ultimate_origin PARAMS ((tree));
3571 static tree decl_class_context PARAMS ((tree));
3572 static void add_dwarf_attr PARAMS ((dw_die_ref, dw_attr_ref));
3573 static void add_AT_flag PARAMS ((dw_die_ref,
3574 enum dwarf_attribute,
3575 unsigned));
3576 static void add_AT_int PARAMS ((dw_die_ref,
3577 enum dwarf_attribute, long));
3578 static void add_AT_unsigned PARAMS ((dw_die_ref,
3579 enum dwarf_attribute,
3580 unsigned long));
3581 static void add_AT_long_long PARAMS ((dw_die_ref,
3582 enum dwarf_attribute,
3583 unsigned long,
3584 unsigned long));
3585 static void add_AT_float PARAMS ((dw_die_ref,
3586 enum dwarf_attribute,
3587 unsigned, long *));
3588 static void add_AT_string PARAMS ((dw_die_ref,
3589 enum dwarf_attribute,
3590 const char *));
3591 static void add_AT_die_ref PARAMS ((dw_die_ref,
3592 enum dwarf_attribute,
3593 dw_die_ref));
3594 static void add_AT_fde_ref PARAMS ((dw_die_ref,
3595 enum dwarf_attribute,
3596 unsigned));
3597 static void add_AT_loc PARAMS ((dw_die_ref,
3598 enum dwarf_attribute,
3599 dw_loc_descr_ref));
3600 static void add_AT_addr PARAMS ((dw_die_ref,
3601 enum dwarf_attribute,
3602 rtx));
3603 static void add_AT_lbl_id PARAMS ((dw_die_ref,
3604 enum dwarf_attribute,
3605 const char *));
3606 static void add_AT_lbl_offset PARAMS ((dw_die_ref,
3607 enum dwarf_attribute,
3608 const char *));
3609 static dw_attr_ref get_AT PARAMS ((dw_die_ref,
3610 enum dwarf_attribute));
3611 static const char *get_AT_low_pc PARAMS ((dw_die_ref));
3612 static const char *get_AT_hi_pc PARAMS ((dw_die_ref));
3613 static const char *get_AT_string PARAMS ((dw_die_ref,
3614 enum dwarf_attribute));
3615 static int get_AT_flag PARAMS ((dw_die_ref,
3616 enum dwarf_attribute));
3617 static unsigned get_AT_unsigned PARAMS ((dw_die_ref,
3618 enum dwarf_attribute));
3619 static inline dw_die_ref get_AT_ref PARAMS ((dw_die_ref,
3620 enum dwarf_attribute));
3621 static int is_c_family PARAMS ((void));
3622 static int is_java PARAMS ((void));
3623 static int is_fortran PARAMS ((void));
3624 static void remove_AT PARAMS ((dw_die_ref,
3625 enum dwarf_attribute));
3626 static void remove_children PARAMS ((dw_die_ref));
3627 static void add_child_die PARAMS ((dw_die_ref, dw_die_ref));
3628 static dw_die_ref new_die PARAMS ((enum dwarf_tag, dw_die_ref));
3629 static dw_die_ref lookup_type_die PARAMS ((tree));
3630 static void equate_type_number_to_die PARAMS ((tree, dw_die_ref));
3631 static dw_die_ref lookup_decl_die PARAMS ((tree));
3632 static void equate_decl_number_to_die PARAMS ((tree, dw_die_ref));
3633 static void print_spaces PARAMS ((FILE *));
3634 static void print_die PARAMS ((dw_die_ref, FILE *));
3635 static void print_dwarf_line_table PARAMS ((FILE *));
3636 static void reverse_die_lists PARAMS ((dw_die_ref));
3637 static void reverse_all_dies PARAMS ((dw_die_ref));
3638 static dw_die_ref push_new_compile_unit PARAMS ((dw_die_ref, dw_die_ref));
3639 static dw_die_ref pop_compile_unit PARAMS ((dw_die_ref));
3640 static void loc_checksum PARAMS ((dw_loc_descr_ref, struct md5_ctx *));
3641 static void attr_checksum PARAMS ((dw_attr_ref, struct md5_ctx *));
3642 static void die_checksum PARAMS ((dw_die_ref, struct md5_ctx *));
3643 static void compute_section_prefix PARAMS ((dw_die_ref));
3644 static int is_type_die PARAMS ((dw_die_ref));
3645 static int is_comdat_die PARAMS ((dw_die_ref));
3646 static int is_symbol_die PARAMS ((dw_die_ref));
3647 static char *gen_internal_sym PARAMS ((void));
3648 static void assign_symbol_names PARAMS ((dw_die_ref));
3649 static void break_out_includes PARAMS ((dw_die_ref));
3650 static void add_sibling_attributes PARAMS ((dw_die_ref));
3651 static void build_abbrev_table PARAMS ((dw_die_ref));
3652 static unsigned long size_of_string PARAMS ((const char *));
3653 static int constant_size PARAMS ((long unsigned));
3654 static unsigned long size_of_die PARAMS ((dw_die_ref));
3655 static void calc_die_sizes PARAMS ((dw_die_ref));
3656 static void mark_dies PARAMS ((dw_die_ref));
3657 static void unmark_dies PARAMS ((dw_die_ref));
3658 static unsigned long size_of_line_prolog PARAMS ((void));
3659 static unsigned long size_of_pubnames PARAMS ((void));
3660 static unsigned long size_of_aranges PARAMS ((void));
3661 static enum dwarf_form value_format PARAMS ((dw_attr_ref));
3662 static void output_value_format PARAMS ((dw_attr_ref));
3663 static void output_abbrev_section PARAMS ((void));
3664 static void output_die_symbol PARAMS ((dw_die_ref));
3665 static void output_symbolic_ref PARAMS ((dw_die_ref));
3666 static void output_die PARAMS ((dw_die_ref));
3667 static void output_compilation_unit_header PARAMS ((void));
3668 static void output_comp_unit PARAMS ((dw_die_ref));
3669 static const char *dwarf2_name PARAMS ((tree, int));
3670 static void add_pubname PARAMS ((tree, dw_die_ref));
3671 static void output_pubnames PARAMS ((void));
3672 static void add_arange PARAMS ((tree, dw_die_ref));
3673 static void output_aranges PARAMS ((void));
3674 static void output_line_info PARAMS ((void));
3675 static void output_file_names PARAMS ((void));
3676 static dw_die_ref base_type_die PARAMS ((tree));
3677 static tree root_type PARAMS ((tree));
3678 static int is_base_type PARAMS ((tree));
3679 static dw_die_ref modified_type_die PARAMS ((tree, int, int, dw_die_ref));
3680 static int type_is_enum PARAMS ((tree));
3681 static unsigned int reg_number PARAMS ((rtx));
3682 static dw_loc_descr_ref reg_loc_descriptor PARAMS ((rtx));
3683 static dw_loc_descr_ref int_loc_descriptor PARAMS ((HOST_WIDE_INT));
3684 static dw_loc_descr_ref based_loc_descr PARAMS ((unsigned, long));
3685 static int is_based_loc PARAMS ((rtx));
3686 static dw_loc_descr_ref mem_loc_descriptor PARAMS ((rtx, enum machine_mode mode));
3687 static dw_loc_descr_ref concat_loc_descriptor PARAMS ((rtx, rtx));
3688 static dw_loc_descr_ref loc_descriptor PARAMS ((rtx));
3689 static dw_loc_descr_ref loc_descriptor_from_tree PARAMS ((tree, int));
3690 static HOST_WIDE_INT ceiling PARAMS ((HOST_WIDE_INT, unsigned int));
3691 static tree field_type PARAMS ((tree));
3692 static unsigned int simple_type_align_in_bits PARAMS ((tree));
3693 static unsigned int simple_decl_align_in_bits PARAMS ((tree));
3694 static unsigned HOST_WIDE_INT simple_type_size_in_bits PARAMS ((tree));
3695 static HOST_WIDE_INT field_byte_offset PARAMS ((tree));
3696 static void add_AT_location_description PARAMS ((dw_die_ref,
3697 enum dwarf_attribute, rtx));
3698 static void add_data_member_location_attribute PARAMS ((dw_die_ref, tree));
3699 static void add_const_value_attribute PARAMS ((dw_die_ref, rtx));
3700 static rtx rtl_for_decl_location PARAMS ((tree));
3701 static void add_location_or_const_value_attribute PARAMS ((dw_die_ref, tree));
3702 static void tree_add_const_value_attribute PARAMS ((dw_die_ref, tree));
3703 static void add_name_attribute PARAMS ((dw_die_ref, const char *));
3704 static void add_bound_info PARAMS ((dw_die_ref,
3705 enum dwarf_attribute, tree));
3706 static void add_subscript_info PARAMS ((dw_die_ref, tree));
3707 static void add_byte_size_attribute PARAMS ((dw_die_ref, tree));
3708 static void add_bit_offset_attribute PARAMS ((dw_die_ref, tree));
3709 static void add_bit_size_attribute PARAMS ((dw_die_ref, tree));
3710 static void add_prototyped_attribute PARAMS ((dw_die_ref, tree));
3711 static void add_abstract_origin_attribute PARAMS ((dw_die_ref, tree));
3712 static void add_pure_or_virtual_attribute PARAMS ((dw_die_ref, tree));
3713 static void add_src_coords_attributes PARAMS ((dw_die_ref, tree));
3714 static void add_name_and_src_coords_attributes PARAMS ((dw_die_ref, tree));
3715 static void push_decl_scope PARAMS ((tree));
3716 static dw_die_ref scope_die_for PARAMS ((tree, dw_die_ref));
3717 static void pop_decl_scope PARAMS ((void));
3718 static void add_type_attribute PARAMS ((dw_die_ref, tree, int, int,
3719 dw_die_ref));
3720 static const char *type_tag PARAMS ((tree));
3721 static tree member_declared_type PARAMS ((tree));
3722 #if 0
3723 static const char *decl_start_label PARAMS ((tree));
3724 #endif
3725 static void gen_array_type_die PARAMS ((tree, dw_die_ref));
3726 static void gen_set_type_die PARAMS ((tree, dw_die_ref));
3727 #if 0
3728 static void gen_entry_point_die PARAMS ((tree, dw_die_ref));
3729 #endif
3730 static void gen_inlined_enumeration_type_die PARAMS ((tree, dw_die_ref));
3731 static void gen_inlined_structure_type_die PARAMS ((tree, dw_die_ref));
3732 static void gen_inlined_union_type_die PARAMS ((tree, dw_die_ref));
3733 static void gen_enumeration_type_die PARAMS ((tree, dw_die_ref));
3734 static dw_die_ref gen_formal_parameter_die PARAMS ((tree, dw_die_ref));
3735 static void gen_unspecified_parameters_die PARAMS ((tree, dw_die_ref));
3736 static void gen_formal_types_die PARAMS ((tree, dw_die_ref));
3737 static void gen_subprogram_die PARAMS ((tree, dw_die_ref));
3738 static void gen_variable_die PARAMS ((tree, dw_die_ref));
3739 static void gen_label_die PARAMS ((tree, dw_die_ref));
3740 static void gen_lexical_block_die PARAMS ((tree, dw_die_ref, int));
3741 static void gen_inlined_subroutine_die PARAMS ((tree, dw_die_ref, int));
3742 static void gen_field_die PARAMS ((tree, dw_die_ref));
3743 static void gen_ptr_to_mbr_type_die PARAMS ((tree, dw_die_ref));
3744 static dw_die_ref gen_compile_unit_die PARAMS ((const char *));
3745 static void gen_string_type_die PARAMS ((tree, dw_die_ref));
3746 static void gen_inheritance_die PARAMS ((tree, dw_die_ref));
3747 static void gen_member_die PARAMS ((tree, dw_die_ref));
3748 static void gen_struct_or_union_type_die PARAMS ((tree, dw_die_ref));
3749 static void gen_subroutine_type_die PARAMS ((tree, dw_die_ref));
3750 static void gen_typedef_die PARAMS ((tree, dw_die_ref));
3751 static void gen_type_die PARAMS ((tree, dw_die_ref));
3752 static void gen_tagged_type_instantiation_die PARAMS ((tree, dw_die_ref));
3753 static void gen_block_die PARAMS ((tree, dw_die_ref, int));
3754 static void decls_for_scope PARAMS ((tree, dw_die_ref, int));
3755 static int is_redundant_typedef PARAMS ((tree));
3756 static void gen_decl_die PARAMS ((tree, dw_die_ref));
3757 static unsigned lookup_filename PARAMS ((struct file_table *,
3758 const char *));
3759 static void init_file_table PARAMS ((struct file_table *));
3760 static void add_incomplete_type PARAMS ((tree));
3761 static void retry_incomplete_types PARAMS ((void));
3762 static void gen_type_die_for_member PARAMS ((tree, tree, dw_die_ref));
3763 static rtx save_rtx PARAMS ((rtx));
3764 static void splice_child_die PARAMS ((dw_die_ref, dw_die_ref));
3765 static int file_info_cmp PARAMS ((const void *, const void *));
3767 /* Section names used to hold DWARF debugging information. */
3768 #ifndef DEBUG_INFO_SECTION
3769 #define DEBUG_INFO_SECTION ".debug_info"
3770 #endif
3771 #ifndef ABBREV_SECTION
3772 #define ABBREV_SECTION ".debug_abbrev"
3773 #endif
3774 #ifndef ARANGES_SECTION
3775 #define ARANGES_SECTION ".debug_aranges"
3776 #endif
3777 #ifndef DW_MACINFO_SECTION
3778 #define DW_MACINFO_SECTION ".debug_macinfo"
3779 #endif
3780 #ifndef DEBUG_LINE_SECTION
3781 #define DEBUG_LINE_SECTION ".debug_line"
3782 #endif
3783 #ifndef LOC_SECTION
3784 #define LOC_SECTION ".debug_loc"
3785 #endif
3786 #ifndef PUBNAMES_SECTION
3787 #define PUBNAMES_SECTION ".debug_pubnames"
3788 #endif
3789 #ifndef STR_SECTION
3790 #define STR_SECTION ".debug_str"
3791 #endif
3793 /* Standard ELF section names for compiled code and data. */
3794 #ifndef TEXT_SECTION
3795 #define TEXT_SECTION ".text"
3796 #endif
3797 #ifndef DATA_SECTION
3798 #define DATA_SECTION ".data"
3799 #endif
3800 #ifndef BSS_SECTION
3801 #define BSS_SECTION ".bss"
3802 #endif
3804 /* Labels we insert at beginning sections we can reference instead of
3805 the section names themselves. */
3807 #ifndef TEXT_SECTION_LABEL
3808 #define TEXT_SECTION_LABEL "Ltext"
3809 #endif
3810 #ifndef DEBUG_LINE_SECTION_LABEL
3811 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3812 #endif
3813 #ifndef DEBUG_INFO_SECTION_LABEL
3814 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3815 #endif
3816 #ifndef ABBREV_SECTION_LABEL
3817 #define ABBREV_SECTION_LABEL "Ldebug_abbrev"
3818 #endif
3820 /* Definitions of defaults for formats and names of various special
3821 (artificial) labels which may be generated within this file (when the -g
3822 options is used and DWARF_DEBUGGING_INFO is in effect.
3823 If necessary, these may be overridden from within the tm.h file, but
3824 typically, overriding these defaults is unnecessary. */
3826 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3827 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3828 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3829 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3830 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3832 #ifndef TEXT_END_LABEL
3833 #define TEXT_END_LABEL "Letext"
3834 #endif
3835 #ifndef DATA_END_LABEL
3836 #define DATA_END_LABEL "Ledata"
3837 #endif
3838 #ifndef BSS_END_LABEL
3839 #define BSS_END_LABEL "Lebss"
3840 #endif
3841 #ifndef BLOCK_BEGIN_LABEL
3842 #define BLOCK_BEGIN_LABEL "LBB"
3843 #endif
3844 #ifndef BLOCK_END_LABEL
3845 #define BLOCK_END_LABEL "LBE"
3846 #endif
3847 #ifndef BODY_BEGIN_LABEL
3848 #define BODY_BEGIN_LABEL "Lbb"
3849 #endif
3850 #ifndef BODY_END_LABEL
3851 #define BODY_END_LABEL "Lbe"
3852 #endif
3853 #ifndef LINE_CODE_LABEL
3854 #define LINE_CODE_LABEL "LM"
3855 #endif
3856 #ifndef SEPARATE_LINE_CODE_LABEL
3857 #define SEPARATE_LINE_CODE_LABEL "LSM"
3858 #endif
3860 /* We allow a language front-end to designate a function that is to be
3861 called to "demangle" any name before it it put into a DIE. */
3863 static const char *(*demangle_name_func) PARAMS ((const char *));
3865 void
3866 dwarf2out_set_demangle_name_func (func)
3867 const char *(*func) PARAMS ((const char *));
3869 demangle_name_func = func;
3872 /* Return an rtx like ORIG which lives forever. If we're doing GC,
3873 that means adding it to used_rtx_varray. If not, that means making
3874 a copy on the permanent_obstack. */
3876 static rtx
3877 save_rtx (orig)
3878 register rtx orig;
3880 VARRAY_PUSH_RTX (used_rtx_varray, orig);
3882 return orig;
3885 /* Test if rtl node points to a pseudo register. */
3887 static inline int
3888 is_pseudo_reg (rtl)
3889 register rtx rtl;
3891 return ((GET_CODE (rtl) == REG && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3892 || (GET_CODE (rtl) == SUBREG
3893 && REGNO (XEXP (rtl, 0)) >= FIRST_PSEUDO_REGISTER));
3896 /* Return a reference to a type, with its const and volatile qualifiers
3897 removed. */
3899 static inline tree
3900 type_main_variant (type)
3901 register tree type;
3903 type = TYPE_MAIN_VARIANT (type);
3905 /* There really should be only one main variant among any group of variants
3906 of a given type (and all of the MAIN_VARIANT values for all members of
3907 the group should point to that one type) but sometimes the C front-end
3908 messes this up for array types, so we work around that bug here. */
3910 if (TREE_CODE (type) == ARRAY_TYPE)
3911 while (type != TYPE_MAIN_VARIANT (type))
3912 type = TYPE_MAIN_VARIANT (type);
3914 return type;
3917 /* Return non-zero if the given type node represents a tagged type. */
3919 static inline int
3920 is_tagged_type (type)
3921 register tree type;
3923 register enum tree_code code = TREE_CODE (type);
3925 return (code == RECORD_TYPE || code == UNION_TYPE
3926 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3929 /* Convert a DIE tag into its string name. */
3931 static const char *
3932 dwarf_tag_name (tag)
3933 register unsigned tag;
3935 switch (tag)
3937 case DW_TAG_padding:
3938 return "DW_TAG_padding";
3939 case DW_TAG_array_type:
3940 return "DW_TAG_array_type";
3941 case DW_TAG_class_type:
3942 return "DW_TAG_class_type";
3943 case DW_TAG_entry_point:
3944 return "DW_TAG_entry_point";
3945 case DW_TAG_enumeration_type:
3946 return "DW_TAG_enumeration_type";
3947 case DW_TAG_formal_parameter:
3948 return "DW_TAG_formal_parameter";
3949 case DW_TAG_imported_declaration:
3950 return "DW_TAG_imported_declaration";
3951 case DW_TAG_label:
3952 return "DW_TAG_label";
3953 case DW_TAG_lexical_block:
3954 return "DW_TAG_lexical_block";
3955 case DW_TAG_member:
3956 return "DW_TAG_member";
3957 case DW_TAG_pointer_type:
3958 return "DW_TAG_pointer_type";
3959 case DW_TAG_reference_type:
3960 return "DW_TAG_reference_type";
3961 case DW_TAG_compile_unit:
3962 return "DW_TAG_compile_unit";
3963 case DW_TAG_string_type:
3964 return "DW_TAG_string_type";
3965 case DW_TAG_structure_type:
3966 return "DW_TAG_structure_type";
3967 case DW_TAG_subroutine_type:
3968 return "DW_TAG_subroutine_type";
3969 case DW_TAG_typedef:
3970 return "DW_TAG_typedef";
3971 case DW_TAG_union_type:
3972 return "DW_TAG_union_type";
3973 case DW_TAG_unspecified_parameters:
3974 return "DW_TAG_unspecified_parameters";
3975 case DW_TAG_variant:
3976 return "DW_TAG_variant";
3977 case DW_TAG_common_block:
3978 return "DW_TAG_common_block";
3979 case DW_TAG_common_inclusion:
3980 return "DW_TAG_common_inclusion";
3981 case DW_TAG_inheritance:
3982 return "DW_TAG_inheritance";
3983 case DW_TAG_inlined_subroutine:
3984 return "DW_TAG_inlined_subroutine";
3985 case DW_TAG_module:
3986 return "DW_TAG_module";
3987 case DW_TAG_ptr_to_member_type:
3988 return "DW_TAG_ptr_to_member_type";
3989 case DW_TAG_set_type:
3990 return "DW_TAG_set_type";
3991 case DW_TAG_subrange_type:
3992 return "DW_TAG_subrange_type";
3993 case DW_TAG_with_stmt:
3994 return "DW_TAG_with_stmt";
3995 case DW_TAG_access_declaration:
3996 return "DW_TAG_access_declaration";
3997 case DW_TAG_base_type:
3998 return "DW_TAG_base_type";
3999 case DW_TAG_catch_block:
4000 return "DW_TAG_catch_block";
4001 case DW_TAG_const_type:
4002 return "DW_TAG_const_type";
4003 case DW_TAG_constant:
4004 return "DW_TAG_constant";
4005 case DW_TAG_enumerator:
4006 return "DW_TAG_enumerator";
4007 case DW_TAG_file_type:
4008 return "DW_TAG_file_type";
4009 case DW_TAG_friend:
4010 return "DW_TAG_friend";
4011 case DW_TAG_namelist:
4012 return "DW_TAG_namelist";
4013 case DW_TAG_namelist_item:
4014 return "DW_TAG_namelist_item";
4015 case DW_TAG_packed_type:
4016 return "DW_TAG_packed_type";
4017 case DW_TAG_subprogram:
4018 return "DW_TAG_subprogram";
4019 case DW_TAG_template_type_param:
4020 return "DW_TAG_template_type_param";
4021 case DW_TAG_template_value_param:
4022 return "DW_TAG_template_value_param";
4023 case DW_TAG_thrown_type:
4024 return "DW_TAG_thrown_type";
4025 case DW_TAG_try_block:
4026 return "DW_TAG_try_block";
4027 case DW_TAG_variant_part:
4028 return "DW_TAG_variant_part";
4029 case DW_TAG_variable:
4030 return "DW_TAG_variable";
4031 case DW_TAG_volatile_type:
4032 return "DW_TAG_volatile_type";
4033 case DW_TAG_MIPS_loop:
4034 return "DW_TAG_MIPS_loop";
4035 case DW_TAG_format_label:
4036 return "DW_TAG_format_label";
4037 case DW_TAG_function_template:
4038 return "DW_TAG_function_template";
4039 case DW_TAG_class_template:
4040 return "DW_TAG_class_template";
4041 case DW_TAG_GNU_BINCL:
4042 return "DW_TAG_GNU_BINCL";
4043 case DW_TAG_GNU_EINCL:
4044 return "DW_TAG_GNU_EINCL";
4045 default:
4046 return "DW_TAG_<unknown>";
4050 /* Convert a DWARF attribute code into its string name. */
4052 static const char *
4053 dwarf_attr_name (attr)
4054 register unsigned attr;
4056 switch (attr)
4058 case DW_AT_sibling:
4059 return "DW_AT_sibling";
4060 case DW_AT_location:
4061 return "DW_AT_location";
4062 case DW_AT_name:
4063 return "DW_AT_name";
4064 case DW_AT_ordering:
4065 return "DW_AT_ordering";
4066 case DW_AT_subscr_data:
4067 return "DW_AT_subscr_data";
4068 case DW_AT_byte_size:
4069 return "DW_AT_byte_size";
4070 case DW_AT_bit_offset:
4071 return "DW_AT_bit_offset";
4072 case DW_AT_bit_size:
4073 return "DW_AT_bit_size";
4074 case DW_AT_element_list:
4075 return "DW_AT_element_list";
4076 case DW_AT_stmt_list:
4077 return "DW_AT_stmt_list";
4078 case DW_AT_low_pc:
4079 return "DW_AT_low_pc";
4080 case DW_AT_high_pc:
4081 return "DW_AT_high_pc";
4082 case DW_AT_language:
4083 return "DW_AT_language";
4084 case DW_AT_member:
4085 return "DW_AT_member";
4086 case DW_AT_discr:
4087 return "DW_AT_discr";
4088 case DW_AT_discr_value:
4089 return "DW_AT_discr_value";
4090 case DW_AT_visibility:
4091 return "DW_AT_visibility";
4092 case DW_AT_import:
4093 return "DW_AT_import";
4094 case DW_AT_string_length:
4095 return "DW_AT_string_length";
4096 case DW_AT_common_reference:
4097 return "DW_AT_common_reference";
4098 case DW_AT_comp_dir:
4099 return "DW_AT_comp_dir";
4100 case DW_AT_const_value:
4101 return "DW_AT_const_value";
4102 case DW_AT_containing_type:
4103 return "DW_AT_containing_type";
4104 case DW_AT_default_value:
4105 return "DW_AT_default_value";
4106 case DW_AT_inline:
4107 return "DW_AT_inline";
4108 case DW_AT_is_optional:
4109 return "DW_AT_is_optional";
4110 case DW_AT_lower_bound:
4111 return "DW_AT_lower_bound";
4112 case DW_AT_producer:
4113 return "DW_AT_producer";
4114 case DW_AT_prototyped:
4115 return "DW_AT_prototyped";
4116 case DW_AT_return_addr:
4117 return "DW_AT_return_addr";
4118 case DW_AT_start_scope:
4119 return "DW_AT_start_scope";
4120 case DW_AT_stride_size:
4121 return "DW_AT_stride_size";
4122 case DW_AT_upper_bound:
4123 return "DW_AT_upper_bound";
4124 case DW_AT_abstract_origin:
4125 return "DW_AT_abstract_origin";
4126 case DW_AT_accessibility:
4127 return "DW_AT_accessibility";
4128 case DW_AT_address_class:
4129 return "DW_AT_address_class";
4130 case DW_AT_artificial:
4131 return "DW_AT_artificial";
4132 case DW_AT_base_types:
4133 return "DW_AT_base_types";
4134 case DW_AT_calling_convention:
4135 return "DW_AT_calling_convention";
4136 case DW_AT_count:
4137 return "DW_AT_count";
4138 case DW_AT_data_member_location:
4139 return "DW_AT_data_member_location";
4140 case DW_AT_decl_column:
4141 return "DW_AT_decl_column";
4142 case DW_AT_decl_file:
4143 return "DW_AT_decl_file";
4144 case DW_AT_decl_line:
4145 return "DW_AT_decl_line";
4146 case DW_AT_declaration:
4147 return "DW_AT_declaration";
4148 case DW_AT_discr_list:
4149 return "DW_AT_discr_list";
4150 case DW_AT_encoding:
4151 return "DW_AT_encoding";
4152 case DW_AT_external:
4153 return "DW_AT_external";
4154 case DW_AT_frame_base:
4155 return "DW_AT_frame_base";
4156 case DW_AT_friend:
4157 return "DW_AT_friend";
4158 case DW_AT_identifier_case:
4159 return "DW_AT_identifier_case";
4160 case DW_AT_macro_info:
4161 return "DW_AT_macro_info";
4162 case DW_AT_namelist_items:
4163 return "DW_AT_namelist_items";
4164 case DW_AT_priority:
4165 return "DW_AT_priority";
4166 case DW_AT_segment:
4167 return "DW_AT_segment";
4168 case DW_AT_specification:
4169 return "DW_AT_specification";
4170 case DW_AT_static_link:
4171 return "DW_AT_static_link";
4172 case DW_AT_type:
4173 return "DW_AT_type";
4174 case DW_AT_use_location:
4175 return "DW_AT_use_location";
4176 case DW_AT_variable_parameter:
4177 return "DW_AT_variable_parameter";
4178 case DW_AT_virtuality:
4179 return "DW_AT_virtuality";
4180 case DW_AT_vtable_elem_location:
4181 return "DW_AT_vtable_elem_location";
4183 case DW_AT_MIPS_fde:
4184 return "DW_AT_MIPS_fde";
4185 case DW_AT_MIPS_loop_begin:
4186 return "DW_AT_MIPS_loop_begin";
4187 case DW_AT_MIPS_tail_loop_begin:
4188 return "DW_AT_MIPS_tail_loop_begin";
4189 case DW_AT_MIPS_epilog_begin:
4190 return "DW_AT_MIPS_epilog_begin";
4191 case DW_AT_MIPS_loop_unroll_factor:
4192 return "DW_AT_MIPS_loop_unroll_factor";
4193 case DW_AT_MIPS_software_pipeline_depth:
4194 return "DW_AT_MIPS_software_pipeline_depth";
4195 case DW_AT_MIPS_linkage_name:
4196 return "DW_AT_MIPS_linkage_name";
4197 case DW_AT_MIPS_stride:
4198 return "DW_AT_MIPS_stride";
4199 case DW_AT_MIPS_abstract_name:
4200 return "DW_AT_MIPS_abstract_name";
4201 case DW_AT_MIPS_clone_origin:
4202 return "DW_AT_MIPS_clone_origin";
4203 case DW_AT_MIPS_has_inlines:
4204 return "DW_AT_MIPS_has_inlines";
4206 case DW_AT_sf_names:
4207 return "DW_AT_sf_names";
4208 case DW_AT_src_info:
4209 return "DW_AT_src_info";
4210 case DW_AT_mac_info:
4211 return "DW_AT_mac_info";
4212 case DW_AT_src_coords:
4213 return "DW_AT_src_coords";
4214 case DW_AT_body_begin:
4215 return "DW_AT_body_begin";
4216 case DW_AT_body_end:
4217 return "DW_AT_body_end";
4218 default:
4219 return "DW_AT_<unknown>";
4223 /* Convert a DWARF value form code into its string name. */
4225 static const char *
4226 dwarf_form_name (form)
4227 register unsigned form;
4229 switch (form)
4231 case DW_FORM_addr:
4232 return "DW_FORM_addr";
4233 case DW_FORM_block2:
4234 return "DW_FORM_block2";
4235 case DW_FORM_block4:
4236 return "DW_FORM_block4";
4237 case DW_FORM_data2:
4238 return "DW_FORM_data2";
4239 case DW_FORM_data4:
4240 return "DW_FORM_data4";
4241 case DW_FORM_data8:
4242 return "DW_FORM_data8";
4243 case DW_FORM_string:
4244 return "DW_FORM_string";
4245 case DW_FORM_block:
4246 return "DW_FORM_block";
4247 case DW_FORM_block1:
4248 return "DW_FORM_block1";
4249 case DW_FORM_data1:
4250 return "DW_FORM_data1";
4251 case DW_FORM_flag:
4252 return "DW_FORM_flag";
4253 case DW_FORM_sdata:
4254 return "DW_FORM_sdata";
4255 case DW_FORM_strp:
4256 return "DW_FORM_strp";
4257 case DW_FORM_udata:
4258 return "DW_FORM_udata";
4259 case DW_FORM_ref_addr:
4260 return "DW_FORM_ref_addr";
4261 case DW_FORM_ref1:
4262 return "DW_FORM_ref1";
4263 case DW_FORM_ref2:
4264 return "DW_FORM_ref2";
4265 case DW_FORM_ref4:
4266 return "DW_FORM_ref4";
4267 case DW_FORM_ref8:
4268 return "DW_FORM_ref8";
4269 case DW_FORM_ref_udata:
4270 return "DW_FORM_ref_udata";
4271 case DW_FORM_indirect:
4272 return "DW_FORM_indirect";
4273 default:
4274 return "DW_FORM_<unknown>";
4278 /* Convert a DWARF type code into its string name. */
4280 #if 0
4281 static const char *
4282 dwarf_type_encoding_name (enc)
4283 register unsigned enc;
4285 switch (enc)
4287 case DW_ATE_address:
4288 return "DW_ATE_address";
4289 case DW_ATE_boolean:
4290 return "DW_ATE_boolean";
4291 case DW_ATE_complex_float:
4292 return "DW_ATE_complex_float";
4293 case DW_ATE_float:
4294 return "DW_ATE_float";
4295 case DW_ATE_signed:
4296 return "DW_ATE_signed";
4297 case DW_ATE_signed_char:
4298 return "DW_ATE_signed_char";
4299 case DW_ATE_unsigned:
4300 return "DW_ATE_unsigned";
4301 case DW_ATE_unsigned_char:
4302 return "DW_ATE_unsigned_char";
4303 default:
4304 return "DW_ATE_<unknown>";
4307 #endif
4309 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4310 instance of an inlined instance of a decl which is local to an inline
4311 function, so we have to trace all of the way back through the origin chain
4312 to find out what sort of node actually served as the original seed for the
4313 given block. */
4315 static tree
4316 decl_ultimate_origin (decl)
4317 register tree decl;
4319 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4320 nodes in the function to point to themselves; ignore that if
4321 we're trying to output the abstract instance of this function. */
4322 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4323 return NULL_TREE;
4325 #ifdef ENABLE_CHECKING
4326 if (DECL_FROM_INLINE (DECL_ORIGIN (decl)))
4327 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4328 most distant ancestor, this should never happen. */
4329 abort ();
4330 #endif
4332 return DECL_ABSTRACT_ORIGIN (decl);
4335 /* Determine the "ultimate origin" of a block. The block may be an inlined
4336 instance of an inlined instance of a block which is local to an inline
4337 function, so we have to trace all of the way back through the origin chain
4338 to find out what sort of node actually served as the original seed for the
4339 given block. */
4341 static tree
4342 block_ultimate_origin (block)
4343 register tree block;
4345 register tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4347 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4348 nodes in the function to point to themselves; ignore that if
4349 we're trying to output the abstract instance of this function. */
4350 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4351 return NULL_TREE;
4353 if (immediate_origin == NULL_TREE)
4354 return NULL_TREE;
4355 else
4357 register tree ret_val;
4358 register tree lookahead = immediate_origin;
4362 ret_val = lookahead;
4363 lookahead = (TREE_CODE (ret_val) == BLOCK)
4364 ? BLOCK_ABSTRACT_ORIGIN (ret_val)
4365 : NULL;
4367 while (lookahead != NULL && lookahead != ret_val);
4369 return ret_val;
4373 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4374 of a virtual function may refer to a base class, so we check the 'this'
4375 parameter. */
4377 static tree
4378 decl_class_context (decl)
4379 tree decl;
4381 tree context = NULL_TREE;
4383 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4384 context = DECL_CONTEXT (decl);
4385 else
4386 context = TYPE_MAIN_VARIANT
4387 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4389 if (context && !TYPE_P (context))
4390 context = NULL_TREE;
4392 return context;
4395 /* Add an attribute/value pair to a DIE. We build the lists up in reverse
4396 addition order, and correct that in reverse_all_dies. */
4398 static inline void
4399 add_dwarf_attr (die, attr)
4400 register dw_die_ref die;
4401 register dw_attr_ref attr;
4403 if (die != NULL && attr != NULL)
4405 attr->dw_attr_next = die->die_attr;
4406 die->die_attr = attr;
4410 static inline dw_val_class AT_class PARAMS ((dw_attr_ref));
4411 static inline dw_val_class
4412 AT_class (a)
4413 dw_attr_ref a;
4415 return a->dw_attr_val.val_class;
4418 /* Add a flag value attribute to a DIE. */
4420 static inline void
4421 add_AT_flag (die, attr_kind, flag)
4422 register dw_die_ref die;
4423 register enum dwarf_attribute attr_kind;
4424 register unsigned flag;
4426 register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4428 attr->dw_attr_next = NULL;
4429 attr->dw_attr = attr_kind;
4430 attr->dw_attr_val.val_class = dw_val_class_flag;
4431 attr->dw_attr_val.v.val_flag = flag;
4432 add_dwarf_attr (die, attr);
4435 static inline unsigned AT_flag PARAMS ((dw_attr_ref));
4436 static inline unsigned
4437 AT_flag (a)
4438 register dw_attr_ref a;
4440 if (a && AT_class (a) == dw_val_class_flag)
4441 return a->dw_attr_val.v.val_flag;
4443 abort ();
4446 /* Add a signed integer attribute value to a DIE. */
4448 static inline void
4449 add_AT_int (die, attr_kind, int_val)
4450 register dw_die_ref die;
4451 register enum dwarf_attribute attr_kind;
4452 register long int int_val;
4454 register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4456 attr->dw_attr_next = NULL;
4457 attr->dw_attr = attr_kind;
4458 attr->dw_attr_val.val_class = dw_val_class_const;
4459 attr->dw_attr_val.v.val_int = int_val;
4460 add_dwarf_attr (die, attr);
4463 static inline long int AT_int PARAMS ((dw_attr_ref));
4464 static inline long int
4465 AT_int (a)
4466 register dw_attr_ref a;
4468 if (a && AT_class (a) == dw_val_class_const)
4469 return a->dw_attr_val.v.val_int;
4471 abort ();
4474 /* Add an unsigned integer attribute value to a DIE. */
4476 static inline void
4477 add_AT_unsigned (die, attr_kind, unsigned_val)
4478 register dw_die_ref die;
4479 register enum dwarf_attribute attr_kind;
4480 register unsigned long unsigned_val;
4482 register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4484 attr->dw_attr_next = NULL;
4485 attr->dw_attr = attr_kind;
4486 attr->dw_attr_val.val_class = dw_val_class_unsigned_const;
4487 attr->dw_attr_val.v.val_unsigned = unsigned_val;
4488 add_dwarf_attr (die, attr);
4491 static inline unsigned long AT_unsigned PARAMS ((dw_attr_ref));
4492 static inline unsigned long
4493 AT_unsigned (a)
4494 register dw_attr_ref a;
4496 if (a && AT_class (a) == dw_val_class_unsigned_const)
4497 return a->dw_attr_val.v.val_unsigned;
4499 abort ();
4502 /* Add an unsigned double integer attribute value to a DIE. */
4504 static inline void
4505 add_AT_long_long (die, attr_kind, val_hi, val_low)
4506 register dw_die_ref die;
4507 register enum dwarf_attribute attr_kind;
4508 register unsigned long val_hi;
4509 register unsigned long val_low;
4511 register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4513 attr->dw_attr_next = NULL;
4514 attr->dw_attr = attr_kind;
4515 attr->dw_attr_val.val_class = dw_val_class_long_long;
4516 attr->dw_attr_val.v.val_long_long.hi = val_hi;
4517 attr->dw_attr_val.v.val_long_long.low = val_low;
4518 add_dwarf_attr (die, attr);
4521 /* Add a floating point attribute value to a DIE and return it. */
4523 static inline void
4524 add_AT_float (die, attr_kind, length, array)
4525 register dw_die_ref die;
4526 register enum dwarf_attribute attr_kind;
4527 register unsigned length;
4528 register long *array;
4530 register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4532 attr->dw_attr_next = NULL;
4533 attr->dw_attr = attr_kind;
4534 attr->dw_attr_val.val_class = dw_val_class_float;
4535 attr->dw_attr_val.v.val_float.length = length;
4536 attr->dw_attr_val.v.val_float.array = array;
4537 add_dwarf_attr (die, attr);
4540 /* Add a string attribute value to a DIE. */
4542 static inline void
4543 add_AT_string (die, attr_kind, str)
4544 register dw_die_ref die;
4545 register enum dwarf_attribute attr_kind;
4546 register const char *str;
4548 register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4550 attr->dw_attr_next = NULL;
4551 attr->dw_attr = attr_kind;
4552 attr->dw_attr_val.val_class = dw_val_class_str;
4553 attr->dw_attr_val.v.val_str = xstrdup (str);
4554 add_dwarf_attr (die, attr);
4557 static inline const char *AT_string PARAMS ((dw_attr_ref));
4558 static inline const char *
4559 AT_string (a)
4560 register dw_attr_ref a;
4562 if (a && AT_class (a) == dw_val_class_str)
4563 return a->dw_attr_val.v.val_str;
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 register dw_die_ref die;
4573 register enum dwarf_attribute attr_kind;
4574 register dw_die_ref targ_die;
4576 register 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 AT_ref PARAMS ((dw_attr_ref));
4587 static inline dw_die_ref
4588 AT_ref (a)
4589 register dw_attr_ref a;
4591 if (a && AT_class (a) == dw_val_class_die_ref)
4592 return a->dw_attr_val.v.val_die_ref.die;
4594 abort ();
4597 static inline int AT_ref_external PARAMS ((dw_attr_ref));
4598 static inline int
4599 AT_ref_external (a)
4600 register dw_attr_ref a;
4602 if (a && AT_class (a) == dw_val_class_die_ref)
4603 return a->dw_attr_val.v.val_die_ref.external;
4605 return 0;
4608 static inline void set_AT_ref_external PARAMS ((dw_attr_ref, int));
4609 static inline void
4610 set_AT_ref_external (a, i)
4611 register dw_attr_ref a;
4612 int i;
4614 if (a && AT_class (a) == dw_val_class_die_ref)
4615 a->dw_attr_val.v.val_die_ref.external = i;
4616 else
4617 abort ();
4620 /* Add an FDE reference attribute value to a DIE. */
4622 static inline void
4623 add_AT_fde_ref (die, attr_kind, targ_fde)
4624 register dw_die_ref die;
4625 register enum dwarf_attribute attr_kind;
4626 register unsigned targ_fde;
4628 register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4630 attr->dw_attr_next = NULL;
4631 attr->dw_attr = attr_kind;
4632 attr->dw_attr_val.val_class = dw_val_class_fde_ref;
4633 attr->dw_attr_val.v.val_fde_index = targ_fde;
4634 add_dwarf_attr (die, attr);
4637 /* Add a location description attribute value to a DIE. */
4639 static inline void
4640 add_AT_loc (die, attr_kind, loc)
4641 register dw_die_ref die;
4642 register enum dwarf_attribute attr_kind;
4643 register dw_loc_descr_ref loc;
4645 register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4647 attr->dw_attr_next = NULL;
4648 attr->dw_attr = attr_kind;
4649 attr->dw_attr_val.val_class = dw_val_class_loc;
4650 attr->dw_attr_val.v.val_loc = loc;
4651 add_dwarf_attr (die, attr);
4654 static inline dw_loc_descr_ref AT_loc PARAMS ((dw_attr_ref));
4655 static inline dw_loc_descr_ref
4656 AT_loc (a)
4657 register dw_attr_ref a;
4659 if (a && AT_class (a) == dw_val_class_loc)
4660 return a->dw_attr_val.v.val_loc;
4662 abort ();
4665 /* Add an address constant attribute value to a DIE. */
4667 static inline void
4668 add_AT_addr (die, attr_kind, addr)
4669 register dw_die_ref die;
4670 register enum dwarf_attribute attr_kind;
4671 rtx addr;
4673 register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4675 attr->dw_attr_next = NULL;
4676 attr->dw_attr = attr_kind;
4677 attr->dw_attr_val.val_class = dw_val_class_addr;
4678 attr->dw_attr_val.v.val_addr = addr;
4679 add_dwarf_attr (die, attr);
4682 static inline rtx AT_addr PARAMS ((dw_attr_ref));
4683 static inline rtx
4684 AT_addr (a)
4685 register dw_attr_ref a;
4687 if (a && AT_class (a) == dw_val_class_addr)
4688 return a->dw_attr_val.v.val_addr;
4690 abort ();
4693 /* Add a label identifier attribute value to a DIE. */
4695 static inline void
4696 add_AT_lbl_id (die, attr_kind, lbl_id)
4697 register dw_die_ref die;
4698 register enum dwarf_attribute attr_kind;
4699 register const char *lbl_id;
4701 register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4703 attr->dw_attr_next = NULL;
4704 attr->dw_attr = attr_kind;
4705 attr->dw_attr_val.val_class = dw_val_class_lbl_id;
4706 attr->dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4707 add_dwarf_attr (die, attr);
4710 /* Add a section offset attribute value to a DIE. */
4712 static inline void
4713 add_AT_lbl_offset (die, attr_kind, label)
4714 register dw_die_ref die;
4715 register enum dwarf_attribute attr_kind;
4716 register const char *label;
4718 register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4720 attr->dw_attr_next = NULL;
4721 attr->dw_attr = attr_kind;
4722 attr->dw_attr_val.val_class = dw_val_class_lbl_offset;
4723 attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
4724 add_dwarf_attr (die, attr);
4727 static inline const char *AT_lbl PARAMS ((dw_attr_ref));
4728 static inline const char *
4729 AT_lbl (a)
4730 register dw_attr_ref a;
4732 if (a && (AT_class (a) == dw_val_class_lbl_id
4733 || AT_class (a) == dw_val_class_lbl_offset))
4734 return a->dw_attr_val.v.val_lbl_id;
4736 abort ();
4739 /* Get the attribute of type attr_kind. */
4741 static inline dw_attr_ref
4742 get_AT (die, attr_kind)
4743 register dw_die_ref die;
4744 register enum dwarf_attribute attr_kind;
4746 register dw_attr_ref a;
4747 register dw_die_ref spec = NULL;
4749 if (die != NULL)
4751 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
4753 if (a->dw_attr == attr_kind)
4754 return a;
4756 if (a->dw_attr == DW_AT_specification
4757 || a->dw_attr == DW_AT_abstract_origin)
4758 spec = AT_ref (a);
4761 if (spec)
4762 return get_AT (spec, attr_kind);
4765 return NULL;
4768 /* Return the "low pc" attribute value, typically associated with
4769 a subprogram DIE. Return null if the "low pc" attribute is
4770 either not prsent, or if it cannot be represented as an
4771 assembler label identifier. */
4773 static inline const char *
4774 get_AT_low_pc (die)
4775 register dw_die_ref die;
4777 register dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4778 return a ? AT_lbl (a) : NULL;
4781 /* Return the "high pc" attribute value, typically associated with
4782 a subprogram DIE. Return null if the "high pc" attribute is
4783 either not prsent, or if it cannot be represented as an
4784 assembler label identifier. */
4786 static inline const char *
4787 get_AT_hi_pc (die)
4788 register dw_die_ref die;
4790 register dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4791 return a ? AT_lbl (a) : NULL;
4794 /* Return the value of the string attribute designated by ATTR_KIND, or
4795 NULL if it is not present. */
4797 static inline const char *
4798 get_AT_string (die, attr_kind)
4799 register dw_die_ref die;
4800 register enum dwarf_attribute attr_kind;
4802 register dw_attr_ref a = get_AT (die, attr_kind);
4803 return a ? AT_string (a) : NULL;
4806 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4807 if it is not present. */
4809 static inline int
4810 get_AT_flag (die, attr_kind)
4811 register dw_die_ref die;
4812 register enum dwarf_attribute attr_kind;
4814 register dw_attr_ref a = get_AT (die, attr_kind);
4815 return a ? AT_flag (a) : 0;
4818 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4819 if it is not present. */
4821 static inline unsigned
4822 get_AT_unsigned (die, attr_kind)
4823 register dw_die_ref die;
4824 register enum dwarf_attribute attr_kind;
4826 register dw_attr_ref a = get_AT (die, attr_kind);
4827 return a ? AT_unsigned (a) : 0;
4830 static inline dw_die_ref
4831 get_AT_ref (die, attr_kind)
4832 dw_die_ref die;
4833 register enum dwarf_attribute attr_kind;
4835 register dw_attr_ref a = get_AT (die, attr_kind);
4836 return a ? AT_ref (a) : NULL;
4839 static inline int
4840 is_c_family ()
4842 register unsigned lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
4844 return (lang == DW_LANG_C || lang == DW_LANG_C89
4845 || lang == DW_LANG_C_plus_plus);
4848 static inline int
4849 is_fortran ()
4851 register unsigned lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
4853 return (lang == DW_LANG_Fortran77 || lang == DW_LANG_Fortran90);
4856 static inline int
4857 is_java ()
4859 register unsigned lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
4861 return (lang == DW_LANG_Java);
4864 /* Free up the memory used by A. */
4866 static inline void free_AT PARAMS ((dw_attr_ref));
4867 static inline void
4868 free_AT (a)
4869 dw_attr_ref a;
4871 switch (AT_class (a))
4873 case dw_val_class_str:
4874 case dw_val_class_lbl_id:
4875 case dw_val_class_lbl_offset:
4876 free (a->dw_attr_val.v.val_str);
4877 break;
4879 case dw_val_class_float:
4880 free (a->dw_attr_val.v.val_float.array);
4881 break;
4883 default:
4884 break;
4887 free (a);
4890 /* Remove the specified attribute if present. */
4892 static void
4893 remove_AT (die, attr_kind)
4894 register dw_die_ref die;
4895 register enum dwarf_attribute attr_kind;
4897 register dw_attr_ref *p;
4898 register dw_attr_ref removed = NULL;
4900 if (die != NULL)
4902 for (p = &(die->die_attr); *p; p = &((*p)->dw_attr_next))
4903 if ((*p)->dw_attr == attr_kind)
4905 removed = *p;
4906 *p = (*p)->dw_attr_next;
4907 break;
4910 if (removed != 0)
4911 free_AT (removed);
4915 /* Free up the memory used by DIE. */
4917 static inline void free_die PARAMS ((dw_die_ref));
4918 static inline void
4919 free_die (die)
4920 dw_die_ref die;
4922 remove_children (die);
4923 free (die);
4926 /* Discard the children of this DIE. */
4928 static void
4929 remove_children (die)
4930 register dw_die_ref die;
4932 register dw_die_ref child_die = die->die_child;
4934 die->die_child = NULL;
4936 while (child_die != NULL)
4938 register dw_die_ref tmp_die = child_die;
4939 register dw_attr_ref a;
4941 child_die = child_die->die_sib;
4943 for (a = tmp_die->die_attr; a != NULL;)
4945 register dw_attr_ref tmp_a = a;
4947 a = a->dw_attr_next;
4948 free_AT (tmp_a);
4951 free_die (tmp_die);
4955 /* Add a child DIE below its parent. We build the lists up in reverse
4956 addition order, and correct that in reverse_all_dies. */
4958 static inline void
4959 add_child_die (die, child_die)
4960 register dw_die_ref die;
4961 register dw_die_ref child_die;
4963 if (die != NULL && child_die != NULL)
4965 if (die == child_die)
4966 abort ();
4967 child_die->die_parent = die;
4968 child_die->die_sib = die->die_child;
4969 die->die_child = child_die;
4973 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4974 is the specification, to the front of PARENT's list of children. */
4976 static void
4977 splice_child_die (parent, child)
4978 dw_die_ref parent, child;
4980 dw_die_ref *p;
4982 /* We want the declaration DIE from inside the class, not the
4983 specification DIE at toplevel. */
4984 if (child->die_parent != parent)
4986 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4987 if (tmp)
4988 child = tmp;
4991 if (child->die_parent != parent
4992 && child->die_parent != get_AT_ref (parent, DW_AT_specification))
4993 abort ();
4995 for (p = &(child->die_parent->die_child); *p; p = &((*p)->die_sib))
4996 if (*p == child)
4998 *p = child->die_sib;
4999 break;
5002 child->die_sib = parent->die_child;
5003 parent->die_child = child;
5006 /* Return a pointer to a newly created DIE node. */
5008 static inline dw_die_ref
5009 new_die (tag_value, parent_die)
5010 register enum dwarf_tag tag_value;
5011 register dw_die_ref parent_die;
5013 register dw_die_ref die = (dw_die_ref) xcalloc (1, sizeof (die_node));
5015 die->die_tag = tag_value;
5017 if (parent_die != NULL)
5018 add_child_die (parent_die, die);
5019 else
5021 limbo_die_node *limbo_node;
5023 limbo_node = (limbo_die_node *) xmalloc (sizeof (limbo_die_node));
5024 limbo_node->die = die;
5025 limbo_node->next = limbo_die_list;
5026 limbo_die_list = limbo_node;
5029 return die;
5032 /* Return the DIE associated with the given type specifier. */
5034 static inline dw_die_ref
5035 lookup_type_die (type)
5036 register tree type;
5038 if (TREE_CODE (type) == VECTOR_TYPE)
5039 type = TYPE_DEBUG_REPRESENTATION_TYPE (type);
5040 return (dw_die_ref) TYPE_SYMTAB_POINTER (type);
5043 /* Equate a DIE to a given type specifier. */
5045 static inline void
5046 equate_type_number_to_die (type, type_die)
5047 register tree type;
5048 register dw_die_ref type_die;
5050 TYPE_SYMTAB_POINTER (type) = (char *) type_die;
5053 /* Return the DIE associated with a given declaration. */
5055 static inline dw_die_ref
5056 lookup_decl_die (decl)
5057 register tree decl;
5059 register unsigned decl_id = DECL_UID (decl);
5061 return (decl_id < decl_die_table_in_use
5062 ? decl_die_table[decl_id] : NULL);
5065 /* Equate a DIE to a particular declaration. */
5067 static void
5068 equate_decl_number_to_die (decl, decl_die)
5069 register tree decl;
5070 register dw_die_ref decl_die;
5072 register unsigned decl_id = DECL_UID (decl);
5073 register unsigned num_allocated;
5075 if (decl_id >= decl_die_table_allocated)
5077 num_allocated
5078 = ((decl_id + 1 + DECL_DIE_TABLE_INCREMENT - 1)
5079 / DECL_DIE_TABLE_INCREMENT)
5080 * DECL_DIE_TABLE_INCREMENT;
5082 decl_die_table
5083 = (dw_die_ref *) xrealloc (decl_die_table,
5084 sizeof (dw_die_ref) * num_allocated);
5086 memset ((char *) &decl_die_table[decl_die_table_allocated], 0,
5087 (num_allocated - decl_die_table_allocated) * sizeof (dw_die_ref));
5088 decl_die_table_allocated = num_allocated;
5091 if (decl_id >= decl_die_table_in_use)
5092 decl_die_table_in_use = (decl_id + 1);
5094 decl_die_table[decl_id] = decl_die;
5097 /* Keep track of the number of spaces used to indent the
5098 output of the debugging routines that print the structure of
5099 the DIE internal representation. */
5100 static int print_indent;
5102 /* Indent the line the number of spaces given by print_indent. */
5104 static inline void
5105 print_spaces (outfile)
5106 FILE *outfile;
5108 fprintf (outfile, "%*s", print_indent, "");
5111 /* Print the information associated with a given DIE, and its children.
5112 This routine is a debugging aid only. */
5114 static void
5115 print_die (die, outfile)
5116 dw_die_ref die;
5117 FILE *outfile;
5119 register dw_attr_ref a;
5120 register dw_die_ref c;
5122 print_spaces (outfile);
5123 fprintf (outfile, "DIE %4lu: %s\n",
5124 die->die_offset, dwarf_tag_name (die->die_tag));
5125 print_spaces (outfile);
5126 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5127 fprintf (outfile, " offset: %lu\n", die->die_offset);
5129 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5131 print_spaces (outfile);
5132 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5134 switch (AT_class (a))
5136 case dw_val_class_addr:
5137 fprintf (outfile, "address");
5138 break;
5139 case dw_val_class_loc:
5140 fprintf (outfile, "location descriptor");
5141 break;
5142 case dw_val_class_const:
5143 fprintf (outfile, "%ld", AT_int (a));
5144 break;
5145 case dw_val_class_unsigned_const:
5146 fprintf (outfile, "%lu", AT_unsigned (a));
5147 break;
5148 case dw_val_class_long_long:
5149 fprintf (outfile, "constant (%lu,%lu)",
5150 a->dw_attr_val.v.val_long_long.hi,
5151 a->dw_attr_val.v.val_long_long.low);
5152 break;
5153 case dw_val_class_float:
5154 fprintf (outfile, "floating-point constant");
5155 break;
5156 case dw_val_class_flag:
5157 fprintf (outfile, "%u", AT_flag (a));
5158 break;
5159 case dw_val_class_die_ref:
5160 if (AT_ref (a) != NULL)
5162 if (AT_ref (a)->die_symbol)
5163 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5164 else
5165 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5167 else
5168 fprintf (outfile, "die -> <null>");
5169 break;
5170 case dw_val_class_lbl_id:
5171 case dw_val_class_lbl_offset:
5172 fprintf (outfile, "label: %s", AT_lbl (a));
5173 break;
5174 case dw_val_class_str:
5175 if (AT_string (a) != NULL)
5176 fprintf (outfile, "\"%s\"", AT_string (a));
5177 else
5178 fprintf (outfile, "<null>");
5179 break;
5180 default:
5181 break;
5184 fprintf (outfile, "\n");
5187 if (die->die_child != NULL)
5189 print_indent += 4;
5190 for (c = die->die_child; c != NULL; c = c->die_sib)
5191 print_die (c, outfile);
5193 print_indent -= 4;
5195 if (print_indent == 0)
5196 fprintf (outfile, "\n");
5199 /* Print the contents of the source code line number correspondence table.
5200 This routine is a debugging aid only. */
5202 static void
5203 print_dwarf_line_table (outfile)
5204 FILE *outfile;
5206 register unsigned i;
5207 register dw_line_info_ref line_info;
5209 fprintf (outfile, "\n\nDWARF source line information\n");
5210 for (i = 1; i < line_info_table_in_use; ++i)
5212 line_info = &line_info_table[i];
5213 fprintf (outfile, "%5d: ", i);
5214 fprintf (outfile, "%-20s", line_file_table.table[line_info->dw_file_num]);
5215 fprintf (outfile, "%6ld", line_info->dw_line_num);
5216 fprintf (outfile, "\n");
5219 fprintf (outfile, "\n\n");
5222 /* Print the information collected for a given DIE. */
5224 void
5225 debug_dwarf_die (die)
5226 dw_die_ref die;
5228 print_die (die, stderr);
5231 /* Print all DWARF information collected for the compilation unit.
5232 This routine is a debugging aid only. */
5234 void
5235 debug_dwarf ()
5237 print_indent = 0;
5238 print_die (comp_unit_die, stderr);
5239 if (! DWARF2_ASM_LINE_DEBUG_INFO)
5240 print_dwarf_line_table (stderr);
5243 /* We build up the lists of children and attributes by pushing new ones
5244 onto the beginning of the list. Reverse the lists for DIE so that
5245 they are in order of addition. */
5247 static void
5248 reverse_die_lists (die)
5249 register dw_die_ref die;
5251 register dw_die_ref c, cp, cn;
5252 register dw_attr_ref a, ap, an;
5254 for (a = die->die_attr, ap = 0; a; a = an)
5256 an = a->dw_attr_next;
5257 a->dw_attr_next = ap;
5258 ap = a;
5260 die->die_attr = ap;
5262 for (c = die->die_child, cp = 0; c; c = cn)
5264 cn = c->die_sib;
5265 c->die_sib = cp;
5266 cp = c;
5268 die->die_child = cp;
5271 /* reverse_die_lists only reverses the single die you pass it. Since
5272 we used to reverse all dies in add_sibling_attributes, which runs
5273 through all the dies, it would reverse all the dies. Now, however,
5274 since we don't call reverse_die_lists in add_sibling_attributes, we
5275 need a routine to recursively reverse all the dies. This is that
5276 routine. */
5278 static void
5279 reverse_all_dies (die)
5280 register dw_die_ref die;
5282 register dw_die_ref c;
5284 reverse_die_lists (die);
5286 for (c = die->die_child; c; c = c->die_sib)
5287 reverse_all_dies (c);
5290 /* Start a new compilation unit DIE for an include file. OLD_UNIT is
5291 the CU for the enclosing include file, if any. BINCL_DIE is the
5292 DW_TAG_GNU_BINCL DIE that marks the start of the DIEs for this
5293 include file. */
5295 static dw_die_ref
5296 push_new_compile_unit (old_unit, bincl_die)
5297 dw_die_ref old_unit, bincl_die;
5299 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5300 dw_die_ref new_unit = gen_compile_unit_die (filename);
5301 new_unit->die_sib = old_unit;
5302 return new_unit;
5305 /* Close an include-file CU and reopen the enclosing one. */
5307 static dw_die_ref
5308 pop_compile_unit (old_unit)
5309 dw_die_ref old_unit;
5311 dw_die_ref new_unit = old_unit->die_sib;
5312 old_unit->die_sib = NULL;
5313 return new_unit;
5316 #define PROCESS(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5317 #define PROCESS_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5319 /* Calculate the checksum of a location expression. */
5321 static inline void
5322 loc_checksum (loc, ctx)
5323 dw_loc_descr_ref loc;
5324 struct md5_ctx *ctx;
5326 PROCESS (loc->dw_loc_opc);
5327 PROCESS (loc->dw_loc_oprnd1);
5328 PROCESS (loc->dw_loc_oprnd2);
5331 /* Calculate the checksum of an attribute. */
5333 static void
5334 attr_checksum (at, ctx)
5335 dw_attr_ref at;
5336 struct md5_ctx *ctx;
5338 dw_loc_descr_ref loc;
5339 rtx r;
5341 PROCESS (at->dw_attr);
5343 /* We don't care about differences in file numbering. */
5344 if (at->dw_attr == DW_AT_decl_file
5345 /* Or that this was compiled with a different compiler snapshot; if
5346 the output is the same, that's what matters. */
5347 || at->dw_attr == DW_AT_producer)
5348 return;
5350 switch (AT_class (at))
5352 case dw_val_class_const:
5353 PROCESS (at->dw_attr_val.v.val_int);
5354 break;
5355 case dw_val_class_unsigned_const:
5356 PROCESS (at->dw_attr_val.v.val_unsigned);
5357 break;
5358 case dw_val_class_long_long:
5359 PROCESS (at->dw_attr_val.v.val_long_long);
5360 break;
5361 case dw_val_class_float:
5362 PROCESS (at->dw_attr_val.v.val_float);
5363 break;
5364 case dw_val_class_flag:
5365 PROCESS (at->dw_attr_val.v.val_flag);
5366 break;
5368 case dw_val_class_str:
5369 PROCESS_STRING (AT_string (at));
5370 break;
5371 case dw_val_class_addr:
5372 r = AT_addr (at);
5373 switch (GET_CODE (r))
5375 case SYMBOL_REF:
5376 PROCESS_STRING (XSTR (r, 0));
5377 break;
5379 default:
5380 abort ();
5382 break;
5384 case dw_val_class_loc:
5385 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5386 loc_checksum (loc, ctx);
5387 break;
5389 case dw_val_class_die_ref:
5390 if (AT_ref (at)->die_offset)
5391 PROCESS (AT_ref (at)->die_offset);
5392 /* FIXME else use target die name or something. */
5394 case dw_val_class_fde_ref:
5395 case dw_val_class_lbl_id:
5396 case dw_val_class_lbl_offset:
5398 default:
5399 break;
5403 /* Calculate the checksum of a DIE. */
5405 static void
5406 die_checksum (die, ctx)
5407 dw_die_ref die;
5408 struct md5_ctx *ctx;
5410 dw_die_ref c;
5411 dw_attr_ref a;
5413 PROCESS (die->die_tag);
5415 for (a = die->die_attr; a; a = a->dw_attr_next)
5416 attr_checksum (a, ctx);
5418 for (c = die->die_child; c; c = c->die_sib)
5419 die_checksum (c, ctx);
5422 #undef PROCESS
5423 #undef PROCESS_STRING
5425 /* The prefix to attach to symbols on DIEs in the current comdat debug
5426 info section. */
5427 static char *comdat_symbol_id;
5429 /* The index of the current symbol within the current comdat CU. */
5430 static unsigned int comdat_symbol_number;
5432 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
5433 children, and set comdat_symbol_id accordingly. */
5435 static void
5436 compute_section_prefix (unit_die)
5437 dw_die_ref unit_die;
5439 char *p, *name;
5440 int i;
5441 unsigned char checksum[16];
5442 struct md5_ctx ctx;
5444 md5_init_ctx (&ctx);
5445 die_checksum (unit_die, &ctx);
5446 md5_finish_ctx (&ctx, checksum);
5448 p = file_name_nondirectory (get_AT_string (unit_die, DW_AT_name));
5449 name = (char *) alloca (strlen (p) + 64);
5450 sprintf (name, "%s.", p);
5452 clean_symbol_name (name);
5454 p = name + strlen (name);
5455 for (i = 0; i < 4; ++i)
5457 sprintf (p, "%.2x", checksum[i]);
5458 p += 2;
5461 comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
5462 comdat_symbol_number = 0;
5465 /* Returns nonzero iff DIE represents a type, in the sense of TYPE_P. */
5467 static int
5468 is_type_die (die)
5469 dw_die_ref die;
5471 switch (die->die_tag)
5473 case DW_TAG_array_type:
5474 case DW_TAG_class_type:
5475 case DW_TAG_enumeration_type:
5476 case DW_TAG_pointer_type:
5477 case DW_TAG_reference_type:
5478 case DW_TAG_string_type:
5479 case DW_TAG_structure_type:
5480 case DW_TAG_subroutine_type:
5481 case DW_TAG_union_type:
5482 case DW_TAG_ptr_to_member_type:
5483 case DW_TAG_set_type:
5484 case DW_TAG_subrange_type:
5485 case DW_TAG_base_type:
5486 case DW_TAG_const_type:
5487 case DW_TAG_file_type:
5488 case DW_TAG_packed_type:
5489 case DW_TAG_volatile_type:
5490 return 1;
5491 default:
5492 return 0;
5496 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
5497 Basically, we want to choose the bits that are likely to be shared between
5498 compilations (types) and leave out the bits that are specific to individual
5499 compilations (functions). */
5501 static int
5502 is_comdat_die (c)
5503 dw_die_ref c;
5505 #if 1
5506 /* I think we want to leave base types and __vtbl_ptr_type in the
5507 main CU, as we do for stabs. The advantage is a greater
5508 likelihood of sharing between objects that don't include headers
5509 in the same order (and therefore would put the base types in a
5510 different comdat). jason 8/28/00 */
5511 if (c->die_tag == DW_TAG_base_type)
5512 return 0;
5514 if (c->die_tag == DW_TAG_pointer_type
5515 || c->die_tag == DW_TAG_reference_type
5516 || c->die_tag == DW_TAG_const_type
5517 || c->die_tag == DW_TAG_volatile_type)
5519 dw_die_ref t = get_AT_ref (c, DW_AT_type);
5520 return t ? is_comdat_die (t) : 0;
5522 #endif
5524 return is_type_die (c);
5527 /* Returns 1 iff C is the sort of DIE that might be referred to from another
5528 compilation unit. */
5530 static int
5531 is_symbol_die (c)
5532 dw_die_ref c;
5534 if (is_type_die (c))
5535 return 1;
5536 if (get_AT (c, DW_AT_declaration)
5537 && ! get_AT (c, DW_AT_specification))
5538 return 1;
5539 return 0;
5542 static char *
5543 gen_internal_sym ()
5545 char buf[256];
5546 static int label_num;
5547 ASM_GENERATE_INTERNAL_LABEL (buf, "LDIE", label_num++);
5548 return xstrdup (buf);
5551 /* Assign symbols to all worthy DIEs under DIE. */
5553 static void
5554 assign_symbol_names (die)
5555 register dw_die_ref die;
5557 register dw_die_ref c;
5559 if (is_symbol_die (die))
5561 if (comdat_symbol_id)
5563 char *p = alloca (strlen (comdat_symbol_id) + 64);
5564 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
5565 comdat_symbol_id, comdat_symbol_number++);
5566 die->die_symbol = xstrdup (p);
5568 else
5569 die->die_symbol = gen_internal_sym ();
5572 for (c = die->die_child; c != NULL; c = c->die_sib)
5573 assign_symbol_names (c);
5576 /* Traverse the DIE (which is always comp_unit_die), and set up
5577 additional compilation units for each of the include files we see
5578 bracketed by BINCL/EINCL. */
5580 static void
5581 break_out_includes (die)
5582 register dw_die_ref die;
5584 dw_die_ref *ptr;
5585 register dw_die_ref unit = NULL;
5586 limbo_die_node *node;
5588 for (ptr = &(die->die_child); *ptr; )
5590 register dw_die_ref c = *ptr;
5592 if (c->die_tag == DW_TAG_GNU_BINCL
5593 || c->die_tag == DW_TAG_GNU_EINCL
5594 || (unit && is_comdat_die (c)))
5596 /* This DIE is for a secondary CU; remove it from the main one. */
5597 *ptr = c->die_sib;
5599 if (c->die_tag == DW_TAG_GNU_BINCL)
5601 unit = push_new_compile_unit (unit, c);
5602 free_die (c);
5604 else if (c->die_tag == DW_TAG_GNU_EINCL)
5606 unit = pop_compile_unit (unit);
5607 free_die (c);
5609 else
5610 add_child_die (unit, c);
5612 else
5614 /* Leave this DIE in the main CU. */
5615 ptr = &(c->die_sib);
5616 continue;
5620 #if 0
5621 /* We can only use this in debugging, since the frontend doesn't check
5622 to make sure that we leave every include file we enter. */
5623 if (unit != NULL)
5624 abort ();
5625 #endif
5627 assign_symbol_names (die);
5628 for (node = limbo_die_list; node; node = node->next)
5630 compute_section_prefix (node->die);
5631 assign_symbol_names (node->die);
5635 /* Traverse the DIE and add a sibling attribute if it may have the
5636 effect of speeding up access to siblings. To save some space,
5637 avoid generating sibling attributes for DIE's without children. */
5639 static void
5640 add_sibling_attributes (die)
5641 register dw_die_ref die;
5643 register dw_die_ref c;
5645 if (die->die_tag != DW_TAG_compile_unit
5646 && die->die_sib && die->die_child != NULL)
5647 /* Add the sibling link to the front of the attribute list. */
5648 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
5650 for (c = die->die_child; c != NULL; c = c->die_sib)
5651 add_sibling_attributes (c);
5654 /* The format of each DIE (and its attribute value pairs)
5655 is encoded in an abbreviation table. This routine builds the
5656 abbreviation table and assigns a unique abbreviation id for
5657 each abbreviation entry. The children of each die are visited
5658 recursively. */
5660 static void
5661 build_abbrev_table (die)
5662 register dw_die_ref die;
5664 register unsigned long abbrev_id;
5665 register unsigned long n_alloc;
5666 register dw_die_ref c;
5667 register dw_attr_ref d_attr, a_attr;
5669 /* Scan the DIE references, and mark as external any that refer to
5670 DIEs from other CUs (i.e. those which are not marked). */
5671 for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
5673 if (AT_class (d_attr) == dw_val_class_die_ref
5674 && AT_ref (d_attr)->die_mark == 0)
5676 if (AT_ref (d_attr)->die_symbol == 0)
5677 abort ();
5678 set_AT_ref_external (d_attr, 1);
5682 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
5684 register dw_die_ref abbrev = abbrev_die_table[abbrev_id];
5686 if (abbrev->die_tag == die->die_tag)
5688 if ((abbrev->die_child != NULL) == (die->die_child != NULL))
5690 a_attr = abbrev->die_attr;
5691 d_attr = die->die_attr;
5693 while (a_attr != NULL && d_attr != NULL)
5695 if ((a_attr->dw_attr != d_attr->dw_attr)
5696 || (value_format (a_attr) != value_format (d_attr)))
5697 break;
5699 a_attr = a_attr->dw_attr_next;
5700 d_attr = d_attr->dw_attr_next;
5703 if (a_attr == NULL && d_attr == NULL)
5704 break;
5709 if (abbrev_id >= abbrev_die_table_in_use)
5711 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
5713 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
5714 abbrev_die_table
5715 = (dw_die_ref *) xrealloc (abbrev_die_table,
5716 sizeof (dw_die_ref) * n_alloc);
5718 memset ((char *) &abbrev_die_table[abbrev_die_table_allocated], 0,
5719 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
5720 abbrev_die_table_allocated = n_alloc;
5723 ++abbrev_die_table_in_use;
5724 abbrev_die_table[abbrev_id] = die;
5727 die->die_abbrev = abbrev_id;
5728 for (c = die->die_child; c != NULL; c = c->die_sib)
5729 build_abbrev_table (c);
5732 /* Return the size of a string, including the null byte.
5734 This used to treat backslashes as escapes, and hence they were not included
5735 in the count. However, that conflicts with what ASM_OUTPUT_ASCII does,
5736 which treats a backslash as a backslash, escaping it if necessary, and hence
5737 we must include them in the count. */
5739 static unsigned long
5740 size_of_string (str)
5741 register const char *str;
5743 return strlen (str) + 1;
5746 /* Return the power-of-two number of bytes necessary to represent VALUE. */
5748 static int
5749 constant_size (value)
5750 long unsigned value;
5752 int log;
5754 if (value == 0)
5755 log = 0;
5756 else
5757 log = floor_log2 (value);
5759 log = log / 8;
5760 log = 1 << (floor_log2 (log) + 1);
5762 return log;
5765 /* Return the size of a DIE, as it is represented in the
5766 .debug_info section. */
5768 static unsigned long
5769 size_of_die (die)
5770 register dw_die_ref die;
5772 register unsigned long size = 0;
5773 register dw_attr_ref a;
5775 size += size_of_uleb128 (die->die_abbrev);
5776 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5778 switch (AT_class (a))
5780 case dw_val_class_addr:
5781 size += DWARF2_ADDR_SIZE;
5782 break;
5783 case dw_val_class_loc:
5785 register unsigned long lsize = size_of_locs (AT_loc (a));
5787 /* Block length. */
5788 size += constant_size (lsize);
5789 size += lsize;
5791 break;
5792 case dw_val_class_const:
5793 size += size_of_sleb128 (AT_int (a));
5794 break;
5795 case dw_val_class_unsigned_const:
5796 size += constant_size (AT_unsigned (a));
5797 break;
5798 case dw_val_class_long_long:
5799 size += 1 + 8; /* block */
5800 break;
5801 case dw_val_class_float:
5802 size += 1 + a->dw_attr_val.v.val_float.length * 4; /* block */
5803 break;
5804 case dw_val_class_flag:
5805 size += 1;
5806 break;
5807 case dw_val_class_die_ref:
5808 size += DWARF_OFFSET_SIZE;
5809 break;
5810 case dw_val_class_fde_ref:
5811 size += DWARF_OFFSET_SIZE;
5812 break;
5813 case dw_val_class_lbl_id:
5814 size += DWARF2_ADDR_SIZE;
5815 break;
5816 case dw_val_class_lbl_offset:
5817 size += DWARF_OFFSET_SIZE;
5818 break;
5819 case dw_val_class_str:
5820 size += size_of_string (AT_string (a));
5821 break;
5822 default:
5823 abort ();
5827 return size;
5830 /* Size the debugging information associated with a given DIE.
5831 Visits the DIE's children recursively. Updates the global
5832 variable next_die_offset, on each time through. Uses the
5833 current value of next_die_offset to update the die_offset
5834 field in each DIE. */
5836 static void
5837 calc_die_sizes (die)
5838 dw_die_ref die;
5840 register dw_die_ref c;
5841 die->die_offset = next_die_offset;
5842 next_die_offset += size_of_die (die);
5844 for (c = die->die_child; c != NULL; c = c->die_sib)
5845 calc_die_sizes (c);
5847 if (die->die_child != NULL)
5848 /* Count the null byte used to terminate sibling lists. */
5849 next_die_offset += 1;
5852 /* Set the marks for a die and its children. We do this so
5853 that we know whether or not a reference needs to use FORM_ref_addr; only
5854 DIEs in the same CU will be marked. We used to clear out the offset
5855 and use that as the flag, but ran into ordering problems. */
5857 static void
5858 mark_dies (die)
5859 dw_die_ref die;
5861 register dw_die_ref c;
5862 die->die_mark = 1;
5863 for (c = die->die_child; c; c = c->die_sib)
5864 mark_dies (c);
5867 /* Clear the marks for a die and its children. */
5869 static void
5870 unmark_dies (die)
5871 dw_die_ref die;
5873 register dw_die_ref c;
5874 die->die_mark = 0;
5875 for (c = die->die_child; c; c = c->die_sib)
5876 unmark_dies (c);
5879 /* Return the size of the line information prolog generated for the
5880 compilation unit. */
5882 static unsigned long
5883 size_of_line_prolog ()
5885 register unsigned long size;
5886 register unsigned long ft_index;
5888 size = DWARF_LINE_PROLOG_HEADER_SIZE;
5890 /* Count the size of the table giving number of args for each
5891 standard opcode. */
5892 size += DWARF_LINE_OPCODE_BASE - 1;
5894 /* Include directory table is empty (at present). Count only the
5895 null byte used to terminate the table. */
5896 size += 1;
5898 for (ft_index = 1; ft_index < decl_file_table.in_use; ++ft_index)
5900 /* File name entry. */
5901 size += size_of_string (decl_file_table.table[ft_index]);
5903 /* Include directory index. */
5904 size += size_of_uleb128 (0);
5906 /* Modification time. */
5907 size += size_of_uleb128 (0);
5909 /* File length in bytes. */
5910 size += size_of_uleb128 (0);
5913 /* Count the file table terminator. */
5914 size += 1;
5915 return size;
5918 /* Return the size of the .debug_pubnames table generated for the
5919 compilation unit. */
5921 static unsigned long
5922 size_of_pubnames ()
5924 register unsigned long size;
5925 register unsigned i;
5927 size = DWARF_PUBNAMES_HEADER_SIZE;
5928 for (i = 0; i < pubname_table_in_use; ++i)
5930 register pubname_ref p = &pubname_table[i];
5931 size += DWARF_OFFSET_SIZE + size_of_string (p->name);
5934 size += DWARF_OFFSET_SIZE;
5935 return size;
5938 /* Return the size of the information in the .debug_aranges section. */
5940 static unsigned long
5941 size_of_aranges ()
5943 register unsigned long size;
5945 size = DWARF_ARANGES_HEADER_SIZE;
5947 /* Count the address/length pair for this compilation unit. */
5948 size += 2 * DWARF2_ADDR_SIZE;
5949 size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
5951 /* Count the two zero words used to terminated the address range table. */
5952 size += 2 * DWARF2_ADDR_SIZE;
5953 return size;
5956 /* Select the encoding of an attribute value. */
5958 static enum dwarf_form
5959 value_format (a)
5960 dw_attr_ref a;
5962 switch (a->dw_attr_val.val_class)
5964 case dw_val_class_addr:
5965 return DW_FORM_addr;
5966 case dw_val_class_loc:
5967 switch (constant_size (size_of_locs (AT_loc (a))))
5969 case 1:
5970 return DW_FORM_block1;
5971 case 2:
5972 return DW_FORM_block2;
5973 default:
5974 abort ();
5976 case dw_val_class_const:
5977 return DW_FORM_sdata;
5978 case dw_val_class_unsigned_const:
5979 switch (constant_size (AT_unsigned (a)))
5981 case 1:
5982 return DW_FORM_data1;
5983 case 2:
5984 return DW_FORM_data2;
5985 case 4:
5986 return DW_FORM_data4;
5987 case 8:
5988 return DW_FORM_data8;
5989 default:
5990 abort ();
5992 case dw_val_class_long_long:
5993 return DW_FORM_block1;
5994 case dw_val_class_float:
5995 return DW_FORM_block1;
5996 case dw_val_class_flag:
5997 return DW_FORM_flag;
5998 case dw_val_class_die_ref:
5999 if (AT_ref_external (a))
6000 return DW_FORM_ref_addr;
6001 else
6002 return DW_FORM_ref;
6003 case dw_val_class_fde_ref:
6004 return DW_FORM_data;
6005 case dw_val_class_lbl_id:
6006 return DW_FORM_addr;
6007 case dw_val_class_lbl_offset:
6008 return DW_FORM_data;
6009 case dw_val_class_str:
6010 return DW_FORM_string;
6011 default:
6012 abort ();
6016 /* Output the encoding of an attribute value. */
6018 static void
6019 output_value_format (a)
6020 dw_attr_ref a;
6022 enum dwarf_form form = value_format (a);
6024 output_uleb128 (form);
6025 if (flag_debug_asm)
6026 fprintf (asm_out_file, " (%s)", dwarf_form_name (form));
6028 fputc ('\n', asm_out_file);
6031 /* Output the .debug_abbrev section which defines the DIE abbreviation
6032 table. */
6034 static void
6035 output_abbrev_section ()
6037 unsigned long abbrev_id;
6039 dw_attr_ref a_attr;
6040 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6042 register dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6044 output_uleb128 (abbrev_id);
6045 if (flag_debug_asm)
6046 fprintf (asm_out_file, " (abbrev code)");
6048 fputc ('\n', asm_out_file);
6049 output_uleb128 (abbrev->die_tag);
6050 if (flag_debug_asm)
6051 fprintf (asm_out_file, " (TAG: %s)",
6052 dwarf_tag_name (abbrev->die_tag));
6054 fputc ('\n', asm_out_file);
6055 fprintf (asm_out_file, "%s0x%x", ASM_BYTE_OP,
6056 abbrev->die_child != NULL ? DW_children_yes : DW_children_no);
6058 if (flag_debug_asm)
6059 fprintf (asm_out_file, "\t%s %s",
6060 ASM_COMMENT_START,
6061 (abbrev->die_child != NULL
6062 ? "DW_children_yes" : "DW_children_no"));
6064 fputc ('\n', asm_out_file);
6066 for (a_attr = abbrev->die_attr; a_attr != NULL;
6067 a_attr = a_attr->dw_attr_next)
6069 output_uleb128 (a_attr->dw_attr);
6070 if (flag_debug_asm)
6071 fprintf (asm_out_file, " (%s)",
6072 dwarf_attr_name (a_attr->dw_attr));
6074 fputc ('\n', asm_out_file);
6075 output_value_format (a_attr);
6078 fprintf (asm_out_file, "%s0,0\n", ASM_BYTE_OP);
6081 /* Terminate the table. */
6082 fprintf (asm_out_file, "%s0\n", ASM_BYTE_OP);
6085 /* Output a symbol we can use to refer to this DIE from another CU. */
6087 static inline void
6088 output_die_symbol (die)
6089 register dw_die_ref die;
6091 char *sym = die->die_symbol;
6093 if (sym == 0)
6094 return;
6096 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6097 /* We make these global, not weak; if the target doesn't support
6098 .linkonce, it doesn't support combining the sections, so debugging
6099 will break. */
6100 ASM_GLOBALIZE_LABEL (asm_out_file, sym);
6101 ASM_OUTPUT_LABEL (asm_out_file, sym);
6104 /* Output a symbolic (i.e. FORM_ref_addr) reference to TARGET_DIE. */
6106 static inline void
6107 output_symbolic_ref (target_die)
6108 dw_die_ref target_die;
6110 char *sym = target_die->die_symbol;
6112 if (sym == 0)
6113 abort ();
6115 ASM_OUTPUT_DWARF_OFFSET (asm_out_file, sym);
6118 /* Output the DIE and its attributes. Called recursively to generate
6119 the definitions of each child DIE. */
6121 static void
6122 output_die (die)
6123 register dw_die_ref die;
6125 register dw_attr_ref a;
6126 register dw_die_ref c;
6127 register unsigned long size;
6129 /* If someone in another CU might refer to us, set up a symbol for
6130 them to point to. */
6131 if (die->die_symbol)
6132 output_die_symbol (die);
6134 output_uleb128 (die->die_abbrev);
6135 if (flag_debug_asm)
6136 fprintf (asm_out_file, " (DIE (0x%lx) %s)",
6137 die->die_offset, dwarf_tag_name (die->die_tag));
6139 fputc ('\n', asm_out_file);
6141 for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6143 switch (AT_class (a))
6145 case dw_val_class_addr:
6146 ASM_OUTPUT_DWARF_ADDR_CONST (asm_out_file, AT_addr (a));
6147 break;
6149 case dw_val_class_loc:
6150 size = size_of_locs (AT_loc (a));
6152 /* Output the block length for this list of location operations. */
6153 switch (constant_size (size))
6155 case 1:
6156 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, size);
6157 break;
6158 case 2:
6159 ASM_OUTPUT_DWARF_DATA2 (asm_out_file, size);
6160 break;
6161 default:
6162 abort ();
6165 if (flag_debug_asm)
6166 fprintf (asm_out_file, "\t%s %s",
6167 ASM_COMMENT_START, dwarf_attr_name (a->dw_attr));
6169 fputc ('\n', asm_out_file);
6171 output_loc_sequence (AT_loc (a));
6172 break;
6174 case dw_val_class_const:
6175 /* ??? It would be slightly more efficient to use a scheme like is
6176 used for unsigned constants below, but gdb 4.x does not sign
6177 extend. Gdb 5.x does sign extend. */
6178 output_sleb128 (AT_int (a));
6179 break;
6181 case dw_val_class_unsigned_const:
6182 switch (constant_size (AT_unsigned (a)))
6184 case 1:
6185 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, AT_unsigned (a));
6186 break;
6187 case 2:
6188 ASM_OUTPUT_DWARF_DATA2 (asm_out_file, AT_unsigned (a));
6189 break;
6190 case 4:
6191 ASM_OUTPUT_DWARF_DATA4 (asm_out_file, AT_unsigned (a));
6192 break;
6193 case 8:
6194 ASM_OUTPUT_DWARF_DATA8 (asm_out_file, AT_unsigned (a));
6195 break;
6196 default:
6197 abort ();
6199 break;
6201 case dw_val_class_long_long:
6202 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 8);
6203 if (flag_debug_asm)
6204 fprintf (asm_out_file, "\t%s %s",
6205 ASM_COMMENT_START, dwarf_attr_name (a->dw_attr));
6207 fputc ('\n', asm_out_file);
6208 ASM_OUTPUT_DWARF_CONST_DOUBLE (asm_out_file,
6209 a->dw_attr_val.v.val_long_long.hi,
6210 a->dw_attr_val.v.val_long_long.low);
6212 if (flag_debug_asm)
6213 fprintf (asm_out_file,
6214 "\t%s long long constant", ASM_COMMENT_START);
6216 fputc ('\n', asm_out_file);
6217 break;
6219 case dw_val_class_float:
6221 register unsigned int i;
6222 ASM_OUTPUT_DWARF_DATA1 (asm_out_file,
6223 a->dw_attr_val.v.val_float.length * 4);
6224 if (flag_debug_asm)
6225 fprintf (asm_out_file, "\t%s %s",
6226 ASM_COMMENT_START, dwarf_attr_name (a->dw_attr));
6228 fputc ('\n', asm_out_file);
6229 for (i = 0; i < a->dw_attr_val.v.val_float.length; ++i)
6231 ASM_OUTPUT_DWARF_DATA4 (asm_out_file,
6232 a->dw_attr_val.v.val_float.array[i]);
6233 if (flag_debug_asm)
6234 fprintf (asm_out_file, "\t%s fp constant word %u",
6235 ASM_COMMENT_START, i);
6237 fputc ('\n', asm_out_file);
6239 break;
6242 case dw_val_class_flag:
6243 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, AT_flag (a));
6244 break;
6246 case dw_val_class_die_ref:
6247 if (AT_ref_external (a))
6248 output_symbolic_ref (AT_ref (a));
6249 else if (AT_ref (a)->die_offset == 0)
6250 abort ();
6251 else
6252 ASM_OUTPUT_DWARF_DATA (asm_out_file, AT_ref (a)->die_offset);
6253 break;
6255 case dw_val_class_fde_ref:
6257 char l1[20];
6258 ASM_GENERATE_INTERNAL_LABEL
6259 (l1, FDE_AFTER_SIZE_LABEL, a->dw_attr_val.v.val_fde_index * 2);
6260 ASM_OUTPUT_DWARF_OFFSET (asm_out_file, l1);
6261 fprintf (asm_out_file, " - %d", DWARF_OFFSET_SIZE);
6263 break;
6265 case dw_val_class_lbl_id:
6266 ASM_OUTPUT_DWARF_ADDR (asm_out_file, AT_lbl (a));
6267 break;
6269 case dw_val_class_lbl_offset:
6270 ASM_OUTPUT_DWARF_OFFSET (asm_out_file, AT_lbl (a));
6271 break;
6273 case dw_val_class_str:
6274 if (flag_debug_asm)
6275 ASM_OUTPUT_DWARF_STRING (asm_out_file, AT_string (a));
6276 else
6277 ASM_OUTPUT_ASCII (asm_out_file, AT_string (a),
6278 (int) strlen (AT_string (a)) + 1);
6279 break;
6281 default:
6282 abort ();
6285 if (AT_class (a) != dw_val_class_loc
6286 && AT_class (a) != dw_val_class_long_long
6287 && AT_class (a) != dw_val_class_float)
6289 if (flag_debug_asm)
6290 fprintf (asm_out_file, "\t%s %s",
6291 ASM_COMMENT_START, dwarf_attr_name (a->dw_attr));
6293 fputc ('\n', asm_out_file);
6297 for (c = die->die_child; c != NULL; c = c->die_sib)
6298 output_die (c);
6300 if (die->die_child != NULL)
6302 /* Add null byte to terminate sibling list. */
6303 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
6304 if (flag_debug_asm)
6305 fprintf (asm_out_file, "\t%s end of children of DIE 0x%lx",
6306 ASM_COMMENT_START, die->die_offset);
6308 fputc ('\n', asm_out_file);
6312 /* Output the compilation unit that appears at the beginning of the
6313 .debug_info section, and precedes the DIE descriptions. */
6315 static void
6316 output_compilation_unit_header ()
6318 ASM_OUTPUT_DWARF_DATA (asm_out_file, next_die_offset - DWARF_OFFSET_SIZE);
6319 if (flag_debug_asm)
6320 fprintf (asm_out_file, "\t%s Length of Compilation Unit Info.",
6321 ASM_COMMENT_START);
6323 fputc ('\n', asm_out_file);
6324 ASM_OUTPUT_DWARF_DATA2 (asm_out_file, DWARF_VERSION);
6325 if (flag_debug_asm)
6326 fprintf (asm_out_file, "\t%s DWARF version number", ASM_COMMENT_START);
6328 fputc ('\n', asm_out_file);
6329 ASM_OUTPUT_DWARF_OFFSET (asm_out_file, abbrev_section_label);
6330 if (flag_debug_asm)
6331 fprintf (asm_out_file, "\t%s Offset Into Abbrev. Section",
6332 ASM_COMMENT_START);
6334 fputc ('\n', asm_out_file);
6335 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DWARF2_ADDR_SIZE);
6336 if (flag_debug_asm)
6337 fprintf (asm_out_file, "\t%s Pointer Size (in bytes)", ASM_COMMENT_START);
6339 fputc ('\n', asm_out_file);
6342 /* Output the compilation unit DIE and its children. */
6344 static void
6345 output_comp_unit (die)
6346 dw_die_ref die;
6348 const char *secname;
6350 if (die->die_child == 0)
6351 return;
6353 /* Mark all the DIEs in this CU so we know which get local refs. */
6354 mark_dies (die);
6356 build_abbrev_table (die);
6358 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
6359 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
6360 calc_die_sizes (die);
6362 if (die->die_symbol)
6364 char *tmp = (char *) alloca (strlen (die->die_symbol) + 24);
6365 sprintf (tmp, ".gnu.linkonce.wi.%s", die->die_symbol);
6366 secname = tmp;
6367 die->die_symbol = NULL;
6369 else
6370 secname = (const char *) DEBUG_INFO_SECTION;
6372 /* Output debugging information. */
6373 fputc ('\n', asm_out_file);
6374 ASM_OUTPUT_SECTION (asm_out_file, secname);
6375 output_compilation_unit_header ();
6376 output_die (die);
6378 /* Leave the marks on the main CU, so we can check them in
6379 output_pubnames. */
6380 if (die->die_symbol)
6381 unmark_dies (die);
6384 /* The DWARF2 pubname for a nested thingy looks like "A::f". The output
6385 of decl_printable_name for C++ looks like "A::f(int)". Let's drop the
6386 argument list, and maybe the scope. */
6388 static const char *
6389 dwarf2_name (decl, scope)
6390 tree decl;
6391 int scope;
6393 return (*decl_printable_name) (decl, scope ? 1 : 0);
6396 /* Add a new entry to .debug_pubnames if appropriate. */
6398 static void
6399 add_pubname (decl, die)
6400 tree decl;
6401 dw_die_ref die;
6403 pubname_ref p;
6405 if (! TREE_PUBLIC (decl))
6406 return;
6408 if (pubname_table_in_use == pubname_table_allocated)
6410 pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
6411 pubname_table = (pubname_ref) xrealloc
6412 (pubname_table, pubname_table_allocated * sizeof (pubname_entry));
6415 p = &pubname_table[pubname_table_in_use++];
6416 p->die = die;
6418 p->name = xstrdup (dwarf2_name (decl, 1));
6421 /* Output the public names table used to speed up access to externally
6422 visible names. For now, only generate entries for externally
6423 visible procedures. */
6425 static void
6426 output_pubnames ()
6428 register unsigned i;
6429 register unsigned long pubnames_length = size_of_pubnames ();
6431 ASM_OUTPUT_DWARF_DATA (asm_out_file, pubnames_length);
6433 if (flag_debug_asm)
6434 fprintf (asm_out_file, "\t%s Length of Public Names Info.",
6435 ASM_COMMENT_START);
6437 fputc ('\n', asm_out_file);
6438 ASM_OUTPUT_DWARF_DATA2 (asm_out_file, DWARF_VERSION);
6440 if (flag_debug_asm)
6441 fprintf (asm_out_file, "\t%s DWARF Version", ASM_COMMENT_START);
6443 fputc ('\n', asm_out_file);
6444 ASM_OUTPUT_DWARF_OFFSET (asm_out_file, debug_info_section_label);
6445 if (flag_debug_asm)
6446 fprintf (asm_out_file, "\t%s Offset of Compilation Unit Info.",
6447 ASM_COMMENT_START);
6449 fputc ('\n', asm_out_file);
6450 ASM_OUTPUT_DWARF_DATA (asm_out_file, next_die_offset);
6451 if (flag_debug_asm)
6452 fprintf (asm_out_file, "\t%s Compilation Unit Length", ASM_COMMENT_START);
6454 fputc ('\n', asm_out_file);
6455 for (i = 0; i < pubname_table_in_use; ++i)
6457 register pubname_ref pub = &pubname_table[i];
6459 /* We shouldn't see pubnames for DIEs outside of the main CU. */
6460 if (pub->die->die_mark == 0)
6461 abort ();
6463 ASM_OUTPUT_DWARF_DATA (asm_out_file, pub->die->die_offset);
6464 if (flag_debug_asm)
6465 fprintf (asm_out_file, "\t%s DIE offset", ASM_COMMENT_START);
6467 fputc ('\n', asm_out_file);
6469 if (flag_debug_asm)
6471 ASM_OUTPUT_DWARF_STRING (asm_out_file, pub->name);
6472 fprintf (asm_out_file, "%s external name", ASM_COMMENT_START);
6474 else
6476 ASM_OUTPUT_ASCII (asm_out_file, pub->name,
6477 (int) strlen (pub->name) + 1);
6480 fputc ('\n', asm_out_file);
6483 ASM_OUTPUT_DWARF_DATA (asm_out_file, 0);
6484 fputc ('\n', asm_out_file);
6487 /* Add a new entry to .debug_aranges if appropriate. */
6489 static void
6490 add_arange (decl, die)
6491 tree decl;
6492 dw_die_ref die;
6494 if (! DECL_SECTION_NAME (decl))
6495 return;
6497 if (arange_table_in_use == arange_table_allocated)
6499 arange_table_allocated += ARANGE_TABLE_INCREMENT;
6500 arange_table
6501 = (arange_ref) xrealloc (arange_table,
6502 arange_table_allocated * sizeof (dw_die_ref));
6505 arange_table[arange_table_in_use++] = die;
6508 /* Output the information that goes into the .debug_aranges table.
6509 Namely, define the beginning and ending address range of the
6510 text section generated for this compilation unit. */
6512 static void
6513 output_aranges ()
6515 register unsigned i;
6516 register unsigned long aranges_length = size_of_aranges ();
6518 ASM_OUTPUT_DWARF_DATA (asm_out_file, aranges_length);
6519 if (flag_debug_asm)
6520 fprintf (asm_out_file, "\t%s Length of Address Ranges Info.",
6521 ASM_COMMENT_START);
6523 fputc ('\n', asm_out_file);
6524 ASM_OUTPUT_DWARF_DATA2 (asm_out_file, DWARF_VERSION);
6525 if (flag_debug_asm)
6526 fprintf (asm_out_file, "\t%s DWARF Version", ASM_COMMENT_START);
6528 fputc ('\n', asm_out_file);
6529 ASM_OUTPUT_DWARF_OFFSET (asm_out_file, debug_info_section_label);
6530 if (flag_debug_asm)
6531 fprintf (asm_out_file, "\t%s Offset of Compilation Unit Info.",
6532 ASM_COMMENT_START);
6534 fputc ('\n', asm_out_file);
6535 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DWARF2_ADDR_SIZE);
6536 if (flag_debug_asm)
6537 fprintf (asm_out_file, "\t%s Size of Address", ASM_COMMENT_START);
6539 fputc ('\n', asm_out_file);
6540 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
6541 if (flag_debug_asm)
6542 fprintf (asm_out_file, "\t%s Size of Segment Descriptor",
6543 ASM_COMMENT_START);
6545 fputc ('\n', asm_out_file);
6547 /* We need to align to twice the pointer size here. */
6548 if (DWARF_ARANGES_PAD_SIZE)
6550 /* Pad using a 2 bytes word so that padding is correct
6551 for any pointer size. */
6552 ASM_OUTPUT_DWARF_DATA2 (asm_out_file, 0);
6553 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
6554 fprintf (asm_out_file, ",0");
6555 if (flag_debug_asm)
6556 fprintf (asm_out_file, "\t%s Pad to %d byte boundary",
6557 ASM_COMMENT_START, 2 * DWARF2_ADDR_SIZE);
6560 fputc ('\n', asm_out_file);
6561 ASM_OUTPUT_DWARF_ADDR (asm_out_file, text_section_label);
6562 if (flag_debug_asm)
6563 fprintf (asm_out_file, "\t%s Address", ASM_COMMENT_START);
6565 fputc ('\n', asm_out_file);
6566 ASM_OUTPUT_DWARF_ADDR_DELTA (asm_out_file, text_end_label,
6567 text_section_label);
6568 if (flag_debug_asm)
6569 fprintf (asm_out_file, "%s Length", ASM_COMMENT_START);
6571 fputc ('\n', asm_out_file);
6572 for (i = 0; i < arange_table_in_use; ++i)
6574 dw_die_ref die = arange_table[i];
6576 /* We shouldn't see aranges for DIEs outside of the main CU. */
6577 if (die->die_mark == 0)
6578 abort ();
6580 if (die->die_tag == DW_TAG_subprogram)
6581 ASM_OUTPUT_DWARF_ADDR (asm_out_file, get_AT_low_pc (die));
6582 else
6584 /* A static variable; extract the symbol from DW_AT_location.
6585 Note that this code isn't currently hit, as we only emit
6586 aranges for functions (jason 9/23/99). */
6588 dw_attr_ref a = get_AT (die, DW_AT_location);
6589 dw_loc_descr_ref loc;
6590 if (! a || AT_class (a) != dw_val_class_loc)
6591 abort ();
6593 loc = AT_loc (a);
6594 if (loc->dw_loc_opc != DW_OP_addr)
6595 abort ();
6597 ASM_OUTPUT_DWARF_ADDR_CONST (asm_out_file,
6598 loc->dw_loc_oprnd1.v.val_addr);
6601 if (flag_debug_asm)
6602 fprintf (asm_out_file, "\t%s Address", ASM_COMMENT_START);
6604 fputc ('\n', asm_out_file);
6605 if (die->die_tag == DW_TAG_subprogram)
6606 ASM_OUTPUT_DWARF_ADDR_DELTA (asm_out_file, get_AT_hi_pc (die),
6607 get_AT_low_pc (die));
6608 else
6609 ASM_OUTPUT_DWARF_ADDR_DATA (asm_out_file,
6610 get_AT_unsigned (die, DW_AT_byte_size));
6612 if (flag_debug_asm)
6613 fprintf (asm_out_file, "%s Length", ASM_COMMENT_START);
6615 fputc ('\n', asm_out_file);
6618 /* Output the terminator words. */
6619 ASM_OUTPUT_DWARF_ADDR_DATA (asm_out_file, 0);
6620 fputc ('\n', asm_out_file);
6621 ASM_OUTPUT_DWARF_ADDR_DATA (asm_out_file, 0);
6622 fputc ('\n', asm_out_file);
6626 /* Data structure containing information about input files. */
6627 struct file_info
6629 char *path; /* Complete file name. */
6630 char *fname; /* File name part. */
6631 int length; /* Length of entire string. */
6632 int file_idx; /* Index in input file table. */
6633 int dir_idx; /* Index in directory table. */
6636 /* Data structure containing information about directories with source
6637 files. */
6638 struct dir_info
6640 char *path; /* Path including directory name. */
6641 int length; /* Path length. */
6642 int prefix; /* Index of directory entry which is a prefix. */
6643 int nbytes; /* Total number of bytes in all file names excluding
6644 paths. */
6645 int count; /* Number of files in this directory. */
6646 int dir_idx; /* Index of directory used as base. */
6647 int used; /* Used in the end? */
6650 /* Callback function for file_info comparison. We sort by looking at
6651 the directories in the path. */
6652 static int
6653 file_info_cmp (p1, p2)
6654 const void *p1;
6655 const void *p2;
6657 const struct file_info *s1 = p1;
6658 const struct file_info *s2 = p2;
6659 unsigned char *cp1;
6660 unsigned char *cp2;
6662 /* Take care of file names without directories. */
6663 if (s1->path == s1->fname)
6664 return -1;
6665 else if (s2->path == s2->fname)
6666 return 1;
6668 cp1 = (unsigned char *) s1->path;
6669 cp2 = (unsigned char *) s2->path;
6671 while (1)
6673 ++cp1;
6674 ++cp2;
6675 /* Reached the end of the first path? */
6676 if (cp1 == (unsigned char *) s1->fname)
6677 /* It doesn't really matter in which order files from the
6678 same directory are sorted in. Therefore don't test for
6679 the second path reaching the end. */
6680 return -1;
6681 else if (cp2 == (unsigned char *) s2->fname)
6682 return 1;
6684 /* Character of current path component the same? */
6685 if (*cp1 != *cp2)
6686 return *cp1 - *cp2;
6690 /* Compute the maximum prefix of P2 appearing also in P1. Entire
6691 directory names must match. */
6692 static int prefix_of PARAMS ((struct dir_info *, struct dir_info *));
6693 static int
6694 prefix_of (p1, p2)
6695 struct dir_info *p1;
6696 struct dir_info *p2;
6698 char *s1 = p1->path;
6699 char *s2 = p2->path;
6700 int len = p1->length < p2->length ? p1->length : p2->length;
6702 while (*s1 == *s2 && s1 < p1->path + len)
6703 ++s1, ++s2;
6705 if (*s1 == '/' && *s2 == '/')
6706 /* The whole of P1 is the prefix. */
6707 return p1->length;
6709 /* Go back to the last directory component. */
6710 while (s1 > p1->path)
6711 if (*--s1 == '/')
6712 return s1 - p1->path + 1;
6714 return 0;
6717 /* Output the directory table and the file name table. We try to minimize
6718 the total amount of memory needed. A heuristic is used to avoid large
6719 slowdowns with many input files. */
6720 static void
6721 output_file_names ()
6723 struct file_info *files;
6724 struct dir_info *dirs;
6725 int *saved;
6726 int *savehere;
6727 int *backmap;
6728 int ndirs;
6729 int idx_offset;
6730 int i;
6731 int idx;
6733 /* Allocate the various arrays we need. */
6734 files = (struct file_info *) alloca (line_file_table.in_use
6735 * sizeof (struct file_info));
6736 dirs = (struct dir_info *) alloca (line_file_table.in_use * 2
6737 * sizeof (struct dir_info));
6739 /* Sort the file names. */
6740 for (i = 1; i < (int) line_file_table.in_use; ++i)
6742 char *f;
6744 /* Skip all leading "./". */
6745 f = line_file_table.table[i];
6746 while (f[0] == '.' && f[1] == '/')
6747 f += 2;
6749 /* Create a new array entry. */
6750 files[i].path = f;
6751 files[i].length = strlen (f);
6752 files[i].file_idx = i;
6754 /* Search for the file name part. */
6755 f = strrchr (f, '/');
6756 files[i].fname = f == NULL ? files[i].path : f + 1;
6758 qsort (files + 1, line_file_table.in_use - 1, sizeof (files[0]),
6759 file_info_cmp);
6761 /* Find all the different directories used. */
6762 dirs[0].path = files[1].path;
6763 dirs[0].length = files[1].fname - files[1].path;
6764 dirs[0].prefix = -1;
6765 dirs[0].nbytes = files[1].length - dirs[1].length + 1;
6766 dirs[0].count = 1;
6767 dirs[0].dir_idx = 0;
6768 dirs[0].used = 0;
6769 files[1].dir_idx = 0;
6770 ndirs = 1;
6772 for (i = 2; i < (int) line_file_table.in_use; ++i)
6773 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
6774 && memcmp (dirs[ndirs - 1].path, files[i].path,
6775 dirs[ndirs - 1].length) == 0)
6777 /* Same directory as last entry. */
6778 files[i].dir_idx = ndirs - 1;
6779 dirs[ndirs - 1].nbytes += files[i].length - dirs[ndirs - 1].length + 1;
6780 ++dirs[ndirs - 1].count;
6782 else
6784 int j;
6785 int max_idx;
6786 int max_len;
6788 /* This is a new directory. */
6789 dirs[ndirs].path = files[i].path;
6790 dirs[ndirs].length = files[i].fname - files[i].path;
6791 dirs[ndirs].nbytes = files[i].length - dirs[i].length + 1;
6792 dirs[ndirs].count = 1;
6793 dirs[ndirs].dir_idx = ndirs;
6794 dirs[ndirs].used = 0;
6795 files[i].dir_idx = ndirs;
6797 /* Search for a prefix. */
6798 max_len = 0;
6799 max_idx = 0;
6800 for (j = 0; j < ndirs; ++j)
6801 if (dirs[j].length > max_len)
6803 int this_len = prefix_of (&dirs[j], &dirs[ndirs]);
6805 if (this_len > max_len)
6807 max_len = this_len;
6808 max_idx = j;
6812 /* Remember the prefix. If this is a known prefix simply
6813 remember the index. Otherwise we will have to create an
6814 artificial entry. */
6815 if (max_len == dirs[max_idx].length)
6816 /* This is our prefix. */
6817 dirs[ndirs].prefix = max_idx;
6818 else if (max_len > 0)
6820 /* Create an entry without associated file. Since we have
6821 to keep the dirs array sorted (means, entries with paths
6822 which come first) we have to move the new entry in the
6823 place of the old one. */
6824 dirs[++ndirs] = dirs[max_idx];
6826 /* We don't have to set .path. */
6827 dirs[max_idx].length = max_len;
6828 dirs[max_idx].nbytes = 0;
6829 dirs[max_idx].count = 0;
6830 dirs[max_idx].dir_idx = ndirs;
6831 dirs[max_idx].used = 0;
6832 dirs[max_idx].prefix = dirs[ndirs].prefix;
6834 dirs[ndirs - 1].prefix = dirs[ndirs].prefix = max_idx;
6836 else
6837 dirs[ndirs].prefix = -1;
6839 ++ndirs;
6842 /* Now to the actual work. We have to find a subset of the
6843 directories which allow expressing the file name using references
6844 to the directory table with the least amount of characters. We
6845 do not do an exhaustive search where we would have to check out
6846 every combination of every single possible prefix. Instead we
6847 use a heuristic which provides nearly optimal results in most
6848 cases and never is much off. */
6849 saved = (int *) alloca (ndirs * sizeof (int));
6850 savehere = (int *) alloca (ndirs * sizeof (int));
6852 memset (saved, '\0', ndirs * sizeof (saved[0]));
6853 for (i = 0; i < ndirs; ++i)
6855 int j;
6856 int total;
6858 /* We can always safe some space for the current directory. But
6859 this does not mean it will be enough to justify adding the
6860 directory. */
6861 savehere[i] = dirs[i].length;
6862 total = (savehere[i] - saved[i]) * dirs[i].count;
6864 for (j = i + 1; j < ndirs; ++j)
6866 savehere[j] = 0;
6868 if (saved[j] < dirs[i].length)
6870 /* Determine whether the dirs[i] path is a prefix of the
6871 dirs[j] path. */
6872 int k;
6874 k = dirs[j].prefix;
6875 while (k != -1 && k != i)
6876 k = dirs[k].prefix;
6878 if (k == i)
6880 /* Yes it is. We can possibly safe some memory but
6881 writing the filenames in dirs[j] relative to
6882 dirs[i]. */
6883 savehere[j] = dirs[i].length;
6884 total += (savehere[j] - saved[j]) * dirs[j].count;
6889 /* Check whether we can safe enough to justify adding the dirs[i]
6890 directory. */
6891 if (total > dirs[i].length + 1)
6893 /* It's worthwhile adding. */
6894 for (j = i; j < ndirs; ++j)
6895 if (savehere[j] > 0)
6897 /* Remember how much we saved for this directory so far. */
6898 saved[j] = savehere[j];
6900 /* Remember the prefix directory. */
6901 dirs[j].dir_idx = i;
6906 /* We have to emit them in the order they appear in the line_file_table
6907 array since the index is used in the debug info generation. To
6908 do this efficiently we generate a back-mapping of the indices
6909 first. */
6910 backmap = (int *) alloca (line_file_table.in_use * sizeof (int));
6911 for (i = 1; i < (int) line_file_table.in_use; ++i)
6913 backmap[files[i].file_idx] = i;
6914 /* Mark this directory as used. */
6915 dirs[dirs[files[i].dir_idx].dir_idx].used = 1;
6918 /* That was it. We are ready to emit the information. First the
6919 directory name table. Here we have to make sure that the first
6920 actually emitted directory name has the index one. Zero is
6921 reserved for the current working directory. Make sure we do not
6922 confuse these indices with the one for the constructed table
6923 (even though most of the time they are identical). */
6924 idx = 1;
6925 idx_offset = dirs[0].length > 0 ? 1 : 0;
6926 for (i = 1 - idx_offset; i < ndirs; ++i)
6927 if (dirs[i].used != 0)
6929 dirs[i].used = idx++;
6931 if (flag_debug_asm)
6933 ASM_OUTPUT_DWARF_NSTRING (asm_out_file,
6934 dirs[i].path, dirs[i].length - 1);
6935 fprintf (asm_out_file, "%s Directory Entry: 0x%x\n",
6936 ASM_COMMENT_START, dirs[i].used);
6938 else
6940 ASM_OUTPUT_ASCII (asm_out_file, dirs[i].path, dirs[i].length - 1);
6941 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
6942 fputc ('\n', asm_out_file);
6945 /* Correct the index for the current working directory entry if it
6946 exists. */
6947 if (idx_offset == 0)
6948 dirs[0].used = 0;
6949 /* Terminate the directory name array. */
6950 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
6951 if (flag_debug_asm)
6952 fprintf (asm_out_file, "\t%s End directory table", ASM_COMMENT_START);
6953 fputc ('\n', asm_out_file);
6955 /* Now write all the file names. */
6956 for (i = 1; i < (int) line_file_table.in_use; ++i)
6958 int file_idx = backmap[i];
6959 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
6961 if (flag_debug_asm)
6963 ASM_OUTPUT_DWARF_STRING (asm_out_file,
6964 files[file_idx].path
6965 + dirs[dir_idx].length);
6966 fprintf (asm_out_file, "%s File Entry: 0x%x\n",
6967 ASM_COMMENT_START, i);
6969 else
6970 ASM_OUTPUT_ASCII (asm_out_file,
6971 files[file_idx].path + dirs[dir_idx].length,
6972 (files[file_idx].length
6973 - dirs[dir_idx].length) + 1);
6975 /* Include directory index. */
6976 output_uleb128 (dirs[dir_idx].used);
6977 fputc ('\n', asm_out_file);
6979 /* Modification time. */
6980 output_uleb128 (0);
6981 fputc ('\n', asm_out_file);
6983 /* File length in bytes. */
6984 output_uleb128 (0);
6985 fputc ('\n', asm_out_file);
6988 /* Terminate the file name table */
6989 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
6990 if (flag_debug_asm)
6991 fprintf (asm_out_file, "\t%s End file name table", ASM_COMMENT_START);
6992 fputc ('\n', asm_out_file);
6996 /* Output the source line number correspondence information. This
6997 information goes into the .debug_line section. */
6999 static void
7000 output_line_info ()
7002 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7003 char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7004 register unsigned opc;
7005 register unsigned n_op_args;
7006 register unsigned long lt_index;
7007 register unsigned long current_line;
7008 register long line_offset;
7009 register long line_delta;
7010 register unsigned long current_file;
7011 register unsigned long function;
7013 ASM_OUTPUT_DWARF_DELTA (asm_out_file, ".LTEND", ".LTSTART");
7014 if (flag_debug_asm)
7015 fprintf (asm_out_file, "\t%s Length of Source Line Info.",
7016 ASM_COMMENT_START);
7018 fputc ('\n', asm_out_file);
7019 ASM_OUTPUT_LABEL (asm_out_file, ".LTSTART");
7020 ASM_OUTPUT_DWARF_DATA2 (asm_out_file, DWARF_VERSION);
7021 if (flag_debug_asm)
7022 fprintf (asm_out_file, "\t%s DWARF Version", ASM_COMMENT_START);
7024 fputc ('\n', asm_out_file);
7025 ASM_OUTPUT_DWARF_DATA (asm_out_file, size_of_line_prolog ());
7026 if (flag_debug_asm)
7027 fprintf (asm_out_file, "\t%s Prolog Length", ASM_COMMENT_START);
7029 fputc ('\n', asm_out_file);
7030 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DWARF_LINE_MIN_INSTR_LENGTH);
7031 if (flag_debug_asm)
7032 fprintf (asm_out_file, "\t%s Minimum Instruction Length",
7033 ASM_COMMENT_START);
7035 fputc ('\n', asm_out_file);
7036 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DWARF_LINE_DEFAULT_IS_STMT_START);
7037 if (flag_debug_asm)
7038 fprintf (asm_out_file, "\t%s Default is_stmt_start flag",
7039 ASM_COMMENT_START);
7041 fputc ('\n', asm_out_file);
7042 fprintf (asm_out_file, "%s%d", ASM_BYTE_OP, DWARF_LINE_BASE);
7043 if (flag_debug_asm)
7044 fprintf (asm_out_file, "\t%s Line Base Value (Special Opcodes)",
7045 ASM_COMMENT_START);
7047 fputc ('\n', asm_out_file);
7048 fprintf (asm_out_file, "%s%u", ASM_BYTE_OP, DWARF_LINE_RANGE);
7049 if (flag_debug_asm)
7050 fprintf (asm_out_file, "\t%s Line Range Value (Special Opcodes)",
7051 ASM_COMMENT_START);
7053 fputc ('\n', asm_out_file);
7054 fprintf (asm_out_file, "%s%u", ASM_BYTE_OP, DWARF_LINE_OPCODE_BASE);
7055 if (flag_debug_asm)
7056 fprintf (asm_out_file, "\t%s Special Opcode Base", ASM_COMMENT_START);
7058 fputc ('\n', asm_out_file);
7059 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; ++opc)
7061 switch (opc)
7063 case DW_LNS_advance_pc:
7064 case DW_LNS_advance_line:
7065 case DW_LNS_set_file:
7066 case DW_LNS_set_column:
7067 case DW_LNS_fixed_advance_pc:
7068 n_op_args = 1;
7069 break;
7070 default:
7071 n_op_args = 0;
7072 break;
7074 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, n_op_args);
7075 if (flag_debug_asm)
7076 fprintf (asm_out_file, "\t%s opcode: 0x%x has %d args",
7077 ASM_COMMENT_START, opc, n_op_args);
7078 fputc ('\n', asm_out_file);
7081 /* Write out the information about the files we use. */
7082 output_file_names ();
7084 /* We used to set the address register to the first location in the text
7085 section here, but that didn't accomplish anything since we already
7086 have a line note for the opening brace of the first function. */
7088 /* Generate the line number to PC correspondence table, encoded as
7089 a series of state machine operations. */
7090 current_file = 1;
7091 current_line = 1;
7092 strcpy (prev_line_label, text_section_label);
7093 for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
7095 register dw_line_info_ref line_info = &line_info_table[lt_index];
7097 #if 0
7098 /* Disable this optimization for now; GDB wants to see two line notes
7099 at the beginning of a function so it can find the end of the
7100 prologue. */
7102 /* Don't emit anything for redundant notes. Just updating the
7103 address doesn't accomplish anything, because we already assume
7104 that anything after the last address is this line. */
7105 if (line_info->dw_line_num == current_line
7106 && line_info->dw_file_num == current_file)
7107 continue;
7108 #endif
7110 /* Emit debug info for the address of the current line, choosing
7111 the encoding that uses the least amount of space. */
7112 /* ??? Unfortunately, we have little choice here currently, and must
7113 always use the most general form. Gcc does not know the address
7114 delta itself, so we can't use DW_LNS_advance_pc. There are no known
7115 dwarf2 aware assemblers at this time, so we can't use any special
7116 pseudo ops that would allow the assembler to optimally encode this for
7117 us. Many ports do have length attributes which will give an upper
7118 bound on the address range. We could perhaps use length attributes
7119 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
7120 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
7121 if (0)
7123 /* This can handle deltas up to 0xffff. This takes 3 bytes. */
7124 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_fixed_advance_pc);
7125 if (flag_debug_asm)
7126 fprintf (asm_out_file, "\t%s DW_LNS_fixed_advance_pc",
7127 ASM_COMMENT_START);
7129 fputc ('\n', asm_out_file);
7130 ASM_OUTPUT_DWARF_DELTA2 (asm_out_file, line_label, prev_line_label);
7131 fputc ('\n', asm_out_file);
7133 else
7135 /* This can handle any delta. This takes
7136 4+DWARF2_ADDR_SIZE bytes. */
7137 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
7138 if (flag_debug_asm)
7139 fprintf (asm_out_file, "\t%s DW_LNE_set_address",
7140 ASM_COMMENT_START);
7141 fputc ('\n', asm_out_file);
7142 output_uleb128 (1 + DWARF2_ADDR_SIZE);
7143 fputc ('\n', asm_out_file);
7144 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNE_set_address);
7145 fputc ('\n', asm_out_file);
7146 ASM_OUTPUT_DWARF_ADDR (asm_out_file, line_label);
7147 fputc ('\n', asm_out_file);
7149 strcpy (prev_line_label, line_label);
7151 /* Emit debug info for the source file of the current line, if
7152 different from the previous line. */
7153 if (line_info->dw_file_num != current_file)
7155 current_file = line_info->dw_file_num;
7156 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_set_file);
7157 if (flag_debug_asm)
7158 fprintf (asm_out_file, "\t%s DW_LNS_set_file", ASM_COMMENT_START);
7160 fputc ('\n', asm_out_file);
7161 output_uleb128 (current_file);
7162 if (flag_debug_asm)
7163 fprintf (asm_out_file, " (\"%s\")",
7164 line_file_table.table[current_file]);
7166 fputc ('\n', asm_out_file);
7169 /* Emit debug info for the current line number, choosing the encoding
7170 that uses the least amount of space. */
7171 if (line_info->dw_line_num != current_line)
7173 line_offset = line_info->dw_line_num - current_line;
7174 line_delta = line_offset - DWARF_LINE_BASE;
7175 current_line = line_info->dw_line_num;
7176 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7178 /* This can handle deltas from -10 to 234, using the current
7179 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE. This
7180 takes 1 byte. */
7181 ASM_OUTPUT_DWARF_DATA1 (asm_out_file,
7182 DWARF_LINE_OPCODE_BASE + line_delta);
7183 if (flag_debug_asm)
7184 fprintf (asm_out_file,
7185 "\t%s line %ld", ASM_COMMENT_START, current_line);
7187 fputc ('\n', asm_out_file);
7189 else
7191 /* This can handle any delta. This takes at least 4 bytes,
7192 depending on the value being encoded. */
7193 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_advance_line);
7194 if (flag_debug_asm)
7195 fprintf (asm_out_file, "\t%s advance to line %ld",
7196 ASM_COMMENT_START, current_line);
7198 fputc ('\n', asm_out_file);
7199 output_sleb128 (line_offset);
7200 fputc ('\n', asm_out_file);
7201 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_copy);
7202 if (flag_debug_asm)
7203 fprintf (asm_out_file, "\t%s DW_LNS_copy", ASM_COMMENT_START);
7204 fputc ('\n', asm_out_file);
7207 else
7209 /* We still need to start a new row, so output a copy insn. */
7210 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_copy);
7211 if (flag_debug_asm)
7212 fprintf (asm_out_file, "\t%s DW_LNS_copy", ASM_COMMENT_START);
7213 fputc ('\n', asm_out_file);
7217 /* Emit debug info for the address of the end of the function. */
7218 if (0)
7220 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_fixed_advance_pc);
7221 if (flag_debug_asm)
7222 fprintf (asm_out_file, "\t%s DW_LNS_fixed_advance_pc",
7223 ASM_COMMENT_START);
7225 fputc ('\n', asm_out_file);
7226 ASM_OUTPUT_DWARF_DELTA2 (asm_out_file, text_end_label, prev_line_label);
7227 fputc ('\n', asm_out_file);
7229 else
7231 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
7232 if (flag_debug_asm)
7233 fprintf (asm_out_file, "\t%s DW_LNE_set_address", ASM_COMMENT_START);
7234 fputc ('\n', asm_out_file);
7235 output_uleb128 (1 + DWARF2_ADDR_SIZE);
7236 fputc ('\n', asm_out_file);
7237 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNE_set_address);
7238 fputc ('\n', asm_out_file);
7239 ASM_OUTPUT_DWARF_ADDR (asm_out_file, text_end_label);
7240 fputc ('\n', asm_out_file);
7243 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
7244 if (flag_debug_asm)
7245 fprintf (asm_out_file, "\t%s DW_LNE_end_sequence", ASM_COMMENT_START);
7247 fputc ('\n', asm_out_file);
7248 output_uleb128 (1);
7249 fputc ('\n', asm_out_file);
7250 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNE_end_sequence);
7251 fputc ('\n', asm_out_file);
7253 function = 0;
7254 current_file = 1;
7255 current_line = 1;
7256 for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
7258 register dw_separate_line_info_ref line_info
7259 = &separate_line_info_table[lt_index];
7261 #if 0
7262 /* Don't emit anything for redundant notes. */
7263 if (line_info->dw_line_num == current_line
7264 && line_info->dw_file_num == current_file
7265 && line_info->function == function)
7266 goto cont;
7267 #endif
7269 /* Emit debug info for the address of the current line. If this is
7270 a new function, or the first line of a function, then we need
7271 to handle it differently. */
7272 ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
7273 lt_index);
7274 if (function != line_info->function)
7276 function = line_info->function;
7278 /* Set the address register to the first line in the function */
7279 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
7280 if (flag_debug_asm)
7281 fprintf (asm_out_file, "\t%s DW_LNE_set_address",
7282 ASM_COMMENT_START);
7284 fputc ('\n', asm_out_file);
7285 output_uleb128 (1 + DWARF2_ADDR_SIZE);
7286 fputc ('\n', asm_out_file);
7287 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNE_set_address);
7288 fputc ('\n', asm_out_file);
7289 ASM_OUTPUT_DWARF_ADDR (asm_out_file, line_label);
7290 fputc ('\n', asm_out_file);
7292 else
7294 /* ??? See the DW_LNS_advance_pc comment above. */
7295 if (0)
7297 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_fixed_advance_pc);
7298 if (flag_debug_asm)
7299 fprintf (asm_out_file, "\t%s DW_LNS_fixed_advance_pc",
7300 ASM_COMMENT_START);
7302 fputc ('\n', asm_out_file);
7303 ASM_OUTPUT_DWARF_DELTA2 (asm_out_file, line_label,
7304 prev_line_label);
7305 fputc ('\n', asm_out_file);
7307 else
7309 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
7310 if (flag_debug_asm)
7311 fprintf (asm_out_file, "\t%s DW_LNE_set_address",
7312 ASM_COMMENT_START);
7313 fputc ('\n', asm_out_file);
7314 output_uleb128 (1 + DWARF2_ADDR_SIZE);
7315 fputc ('\n', asm_out_file);
7316 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNE_set_address);
7317 fputc ('\n', asm_out_file);
7318 ASM_OUTPUT_DWARF_ADDR (asm_out_file, line_label);
7319 fputc ('\n', asm_out_file);
7322 strcpy (prev_line_label, line_label);
7324 /* Emit debug info for the source file of the current line, if
7325 different from the previous line. */
7326 if (line_info->dw_file_num != current_file)
7328 current_file = line_info->dw_file_num;
7329 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_set_file);
7330 if (flag_debug_asm)
7331 fprintf (asm_out_file, "\t%s DW_LNS_set_file", ASM_COMMENT_START);
7333 fputc ('\n', asm_out_file);
7334 output_uleb128 (current_file);
7335 if (flag_debug_asm)
7336 fprintf (asm_out_file, " (\"%s\")",
7337 line_file_table.table[current_file]);
7339 fputc ('\n', asm_out_file);
7342 /* Emit debug info for the current line number, choosing the encoding
7343 that uses the least amount of space. */
7344 if (line_info->dw_line_num != current_line)
7346 line_offset = line_info->dw_line_num - current_line;
7347 line_delta = line_offset - DWARF_LINE_BASE;
7348 current_line = line_info->dw_line_num;
7349 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7351 ASM_OUTPUT_DWARF_DATA1 (asm_out_file,
7352 DWARF_LINE_OPCODE_BASE + line_delta);
7353 if (flag_debug_asm)
7354 fprintf (asm_out_file,
7355 "\t%s line %ld", ASM_COMMENT_START, current_line);
7357 fputc ('\n', asm_out_file);
7359 else
7361 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_advance_line);
7362 if (flag_debug_asm)
7363 fprintf (asm_out_file, "\t%s advance to line %ld",
7364 ASM_COMMENT_START, current_line);
7366 fputc ('\n', asm_out_file);
7367 output_sleb128 (line_offset);
7368 fputc ('\n', asm_out_file);
7369 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_copy);
7370 if (flag_debug_asm)
7371 fprintf (asm_out_file, "\t%s DW_LNS_copy", ASM_COMMENT_START);
7372 fputc ('\n', asm_out_file);
7375 else
7377 /* We still need to start a new row, so output a copy insn. */
7378 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_copy);
7379 if (flag_debug_asm)
7380 fprintf (asm_out_file, "\t%s DW_LNS_copy", ASM_COMMENT_START);
7381 fputc ('\n', asm_out_file);
7384 #if 0
7385 cont:
7386 #endif
7387 ++lt_index;
7389 /* If we're done with a function, end its sequence. */
7390 if (lt_index == separate_line_info_table_in_use
7391 || separate_line_info_table[lt_index].function != function)
7393 current_file = 1;
7394 current_line = 1;
7396 /* Emit debug info for the address of the end of the function. */
7397 ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
7398 if (0)
7400 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_fixed_advance_pc);
7401 if (flag_debug_asm)
7402 fprintf (asm_out_file, "\t%s DW_LNS_fixed_advance_pc",
7403 ASM_COMMENT_START);
7405 fputc ('\n', asm_out_file);
7406 ASM_OUTPUT_DWARF_DELTA2 (asm_out_file, line_label,
7407 prev_line_label);
7408 fputc ('\n', asm_out_file);
7410 else
7412 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
7413 if (flag_debug_asm)
7414 fprintf (asm_out_file, "\t%s DW_LNE_set_address",
7415 ASM_COMMENT_START);
7416 fputc ('\n', asm_out_file);
7417 output_uleb128 (1 + DWARF2_ADDR_SIZE);
7418 fputc ('\n', asm_out_file);
7419 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNE_set_address);
7420 fputc ('\n', asm_out_file);
7421 ASM_OUTPUT_DWARF_ADDR (asm_out_file, line_label);
7422 fputc ('\n', asm_out_file);
7425 /* Output the marker for the end of this sequence. */
7426 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
7427 if (flag_debug_asm)
7428 fprintf (asm_out_file, "\t%s DW_LNE_end_sequence",
7429 ASM_COMMENT_START);
7431 fputc ('\n', asm_out_file);
7432 output_uleb128 (1);
7433 fputc ('\n', asm_out_file);
7434 ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNE_end_sequence);
7435 fputc ('\n', asm_out_file);
7439 /* Output the marker for the end of the line number info. */
7440 ASM_OUTPUT_LABEL (asm_out_file, ".LTEND");
7443 /* Given a pointer to a tree node for some base type, return a pointer to
7444 a DIE that describes the given type.
7446 This routine must only be called for GCC type nodes that correspond to
7447 Dwarf base (fundamental) types. */
7449 static dw_die_ref
7450 base_type_die (type)
7451 register tree type;
7453 register dw_die_ref base_type_result;
7454 register const char *type_name;
7455 register enum dwarf_type encoding;
7456 register tree name = TYPE_NAME (type);
7458 if (TREE_CODE (type) == ERROR_MARK
7459 || TREE_CODE (type) == VOID_TYPE)
7460 return 0;
7462 if (name)
7464 if (TREE_CODE (name) == TYPE_DECL)
7465 name = DECL_NAME (name);
7467 type_name = IDENTIFIER_POINTER (name);
7469 else
7470 type_name = "__unknown__";
7472 switch (TREE_CODE (type))
7474 case INTEGER_TYPE:
7475 /* Carefully distinguish the C character types, without messing
7476 up if the language is not C. Note that we check only for the names
7477 that contain spaces; other names might occur by coincidence in other
7478 languages. */
7479 if (! (TYPE_PRECISION (type) == CHAR_TYPE_SIZE
7480 && (type == char_type_node
7481 || ! strcmp (type_name, "signed char")
7482 || ! strcmp (type_name, "unsigned char"))))
7484 if (TREE_UNSIGNED (type))
7485 encoding = DW_ATE_unsigned;
7486 else
7487 encoding = DW_ATE_signed;
7488 break;
7490 /* else fall through. */
7492 case CHAR_TYPE:
7493 /* GNU Pascal/Ada CHAR type. Not used in C. */
7494 if (TREE_UNSIGNED (type))
7495 encoding = DW_ATE_unsigned_char;
7496 else
7497 encoding = DW_ATE_signed_char;
7498 break;
7500 case REAL_TYPE:
7501 encoding = DW_ATE_float;
7502 break;
7504 /* Dwarf2 doesn't know anything about complex ints, so use
7505 a user defined type for it. */
7506 case COMPLEX_TYPE:
7507 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
7508 encoding = DW_ATE_complex_float;
7509 else
7510 encoding = DW_ATE_lo_user;
7511 break;
7513 case BOOLEAN_TYPE:
7514 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
7515 encoding = DW_ATE_boolean;
7516 break;
7518 default:
7519 abort (); /* No other TREE_CODEs are Dwarf fundamental types. */
7522 base_type_result = new_die (DW_TAG_base_type, comp_unit_die);
7523 if (demangle_name_func)
7524 type_name = (*demangle_name_func) (type_name);
7526 add_AT_string (base_type_result, DW_AT_name, type_name);
7527 add_AT_unsigned (base_type_result, DW_AT_byte_size,
7528 int_size_in_bytes (type));
7529 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
7531 return base_type_result;
7534 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
7535 the Dwarf "root" type for the given input type. The Dwarf "root" type of
7536 a given type is generally the same as the given type, except that if the
7537 given type is a pointer or reference type, then the root type of the given
7538 type is the root type of the "basis" type for the pointer or reference
7539 type. (This definition of the "root" type is recursive.) Also, the root
7540 type of a `const' qualified type or a `volatile' qualified type is the
7541 root type of the given type without the qualifiers. */
7543 static tree
7544 root_type (type)
7545 register tree type;
7547 if (TREE_CODE (type) == ERROR_MARK)
7548 return error_mark_node;
7550 switch (TREE_CODE (type))
7552 case ERROR_MARK:
7553 return error_mark_node;
7555 case POINTER_TYPE:
7556 case REFERENCE_TYPE:
7557 return type_main_variant (root_type (TREE_TYPE (type)));
7559 default:
7560 return type_main_variant (type);
7564 /* Given a pointer to an arbitrary ..._TYPE tree node, return non-zero if the
7565 given input type is a Dwarf "fundamental" type. Otherwise return null. */
7567 static inline int
7568 is_base_type (type)
7569 register tree type;
7571 switch (TREE_CODE (type))
7573 case ERROR_MARK:
7574 case VOID_TYPE:
7575 case INTEGER_TYPE:
7576 case REAL_TYPE:
7577 case COMPLEX_TYPE:
7578 case BOOLEAN_TYPE:
7579 case CHAR_TYPE:
7580 return 1;
7582 case SET_TYPE:
7583 case ARRAY_TYPE:
7584 case RECORD_TYPE:
7585 case UNION_TYPE:
7586 case QUAL_UNION_TYPE:
7587 case ENUMERAL_TYPE:
7588 case FUNCTION_TYPE:
7589 case METHOD_TYPE:
7590 case POINTER_TYPE:
7591 case REFERENCE_TYPE:
7592 case FILE_TYPE:
7593 case OFFSET_TYPE:
7594 case LANG_TYPE:
7595 case VECTOR_TYPE:
7596 return 0;
7598 default:
7599 abort ();
7602 return 0;
7605 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
7606 entry that chains various modifiers in front of the given type. */
7608 static dw_die_ref
7609 modified_type_die (type, is_const_type, is_volatile_type, context_die)
7610 register tree type;
7611 register int is_const_type;
7612 register int is_volatile_type;
7613 register dw_die_ref context_die;
7615 register enum tree_code code = TREE_CODE (type);
7616 register dw_die_ref mod_type_die = NULL;
7617 register dw_die_ref sub_die = NULL;
7618 register tree item_type = NULL;
7620 if (code != ERROR_MARK)
7622 type = build_type_variant (type, is_const_type, is_volatile_type);
7624 mod_type_die = lookup_type_die (type);
7625 if (mod_type_die)
7626 return mod_type_die;
7628 /* Handle C typedef types. */
7629 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
7630 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
7632 tree dtype = TREE_TYPE (TYPE_NAME (type));
7633 if (type == dtype)
7635 /* For a named type, use the typedef. */
7636 gen_type_die (type, context_die);
7637 mod_type_die = lookup_type_die (type);
7640 else if (is_const_type < TYPE_READONLY (dtype)
7641 || is_volatile_type < TYPE_VOLATILE (dtype))
7642 /* cv-unqualified version of named type. Just use the unnamed
7643 type to which it refers. */
7644 mod_type_die
7645 = modified_type_die (DECL_ORIGINAL_TYPE (TYPE_NAME (type)),
7646 is_const_type, is_volatile_type,
7647 context_die);
7648 /* Else cv-qualified version of named type; fall through. */
7651 if (mod_type_die)
7652 /* OK. */
7654 else if (is_const_type)
7656 mod_type_die = new_die (DW_TAG_const_type, comp_unit_die);
7657 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
7659 else if (is_volatile_type)
7661 mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die);
7662 sub_die = modified_type_die (type, 0, 0, context_die);
7664 else if (code == POINTER_TYPE)
7666 mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die);
7667 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
7668 #if 0
7669 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
7670 #endif
7671 item_type = TREE_TYPE (type);
7673 else if (code == REFERENCE_TYPE)
7675 mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die);
7676 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
7677 #if 0
7678 add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
7679 #endif
7680 item_type = TREE_TYPE (type);
7682 else if (is_base_type (type))
7683 mod_type_die = base_type_die (type);
7684 else
7686 gen_type_die (type, context_die);
7688 /* We have to get the type_main_variant here (and pass that to the
7689 `lookup_type_die' routine) because the ..._TYPE node we have
7690 might simply be a *copy* of some original type node (where the
7691 copy was created to help us keep track of typedef names) and
7692 that copy might have a different TYPE_UID from the original
7693 ..._TYPE node. */
7694 mod_type_die = lookup_type_die (type_main_variant (type));
7695 if (mod_type_die == NULL)
7696 abort ();
7700 equate_type_number_to_die (type, mod_type_die);
7701 if (item_type)
7702 /* We must do this after the equate_type_number_to_die call, in case
7703 this is a recursive type. This ensures that the modified_type_die
7704 recursion will terminate even if the type is recursive. Recursive
7705 types are possible in Ada. */
7706 sub_die = modified_type_die (item_type,
7707 TYPE_READONLY (item_type),
7708 TYPE_VOLATILE (item_type),
7709 context_die);
7711 if (sub_die != NULL)
7712 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
7714 return mod_type_die;
7717 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
7718 an enumerated type. */
7720 static inline int
7721 type_is_enum (type)
7722 register tree type;
7724 return TREE_CODE (type) == ENUMERAL_TYPE;
7727 /* Return the register number described by a given RTL node. */
7729 static unsigned int
7730 reg_number (rtl)
7731 register rtx rtl;
7733 register unsigned regno = REGNO (rtl);
7735 if (regno >= FIRST_PSEUDO_REGISTER)
7737 warning ("internal regno botch: regno = %d\n", regno);
7738 regno = 0;
7741 regno = DBX_REGISTER_NUMBER (regno);
7742 return regno;
7745 /* Return a location descriptor that designates a machine register. */
7747 static dw_loc_descr_ref
7748 reg_loc_descriptor (rtl)
7749 register rtx rtl;
7751 register dw_loc_descr_ref loc_result = NULL;
7752 register unsigned reg = reg_number (rtl);
7754 if (reg <= 31)
7755 loc_result = new_loc_descr (DW_OP_reg0 + reg, 0, 0);
7756 else
7757 loc_result = new_loc_descr (DW_OP_regx, reg, 0);
7759 return loc_result;
7762 /* Return a location descriptor that designates a constant. */
7764 static dw_loc_descr_ref
7765 int_loc_descriptor (i)
7766 HOST_WIDE_INT i;
7768 enum dwarf_location_atom op;
7770 /* Pick the smallest representation of a constant, rather than just
7771 defaulting to the LEB encoding. */
7772 if (i >= 0)
7774 if (i <= 31)
7775 op = DW_OP_lit0 + i;
7776 else if (i <= 0xff)
7777 op = DW_OP_const1u;
7778 else if (i <= 0xffff)
7779 op = DW_OP_const2u;
7780 else if (HOST_BITS_PER_WIDE_INT == 32
7781 || i <= 0xffffffff)
7782 op = DW_OP_const4u;
7783 else
7784 op = DW_OP_constu;
7786 else
7788 if (i >= -0x80)
7789 op = DW_OP_const1s;
7790 else if (i >= -0x8000)
7791 op = DW_OP_const2s;
7792 else if (HOST_BITS_PER_WIDE_INT == 32
7793 || i >= -0x80000000)
7794 op = DW_OP_const4s;
7795 else
7796 op = DW_OP_consts;
7799 return new_loc_descr (op, i, 0);
7802 /* Return a location descriptor that designates a base+offset location. */
7804 static dw_loc_descr_ref
7805 based_loc_descr (reg, offset)
7806 unsigned reg;
7807 long int offset;
7809 register dw_loc_descr_ref loc_result;
7810 /* For the "frame base", we use the frame pointer or stack pointer
7811 registers, since the RTL for local variables is relative to one of
7812 them. */
7813 register unsigned fp_reg = DBX_REGISTER_NUMBER (frame_pointer_needed
7814 ? HARD_FRAME_POINTER_REGNUM
7815 : STACK_POINTER_REGNUM);
7817 if (reg == fp_reg)
7818 loc_result = new_loc_descr (DW_OP_fbreg, offset, 0);
7819 else if (reg <= 31)
7820 loc_result = new_loc_descr (DW_OP_breg0 + reg, offset, 0);
7821 else
7822 loc_result = new_loc_descr (DW_OP_bregx, reg, offset);
7824 return loc_result;
7827 /* Return true if this RTL expression describes a base+offset calculation. */
7829 static inline int
7830 is_based_loc (rtl)
7831 register rtx rtl;
7833 return (GET_CODE (rtl) == PLUS
7834 && ((GET_CODE (XEXP (rtl, 0)) == REG
7835 && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
7838 /* The following routine converts the RTL for a variable or parameter
7839 (resident in memory) into an equivalent Dwarf representation of a
7840 mechanism for getting the address of that same variable onto the top of a
7841 hypothetical "address evaluation" stack.
7843 When creating memory location descriptors, we are effectively transforming
7844 the RTL for a memory-resident object into its Dwarf postfix expression
7845 equivalent. This routine recursively descends an RTL tree, turning
7846 it into Dwarf postfix code as it goes.
7848 MODE is the mode of the memory reference, needed to handle some
7849 autoincrement addressing modes. */
7851 static dw_loc_descr_ref
7852 mem_loc_descriptor (rtl, mode)
7853 register rtx rtl;
7854 enum machine_mode mode;
7856 dw_loc_descr_ref mem_loc_result = NULL;
7857 /* Note that for a dynamically sized array, the location we will generate a
7858 description of here will be the lowest numbered location which is
7859 actually within the array. That's *not* necessarily the same as the
7860 zeroth element of the array. */
7862 #ifdef ASM_SIMPLIFY_DWARF_ADDR
7863 rtl = ASM_SIMPLIFY_DWARF_ADDR (rtl);
7864 #endif
7866 switch (GET_CODE (rtl))
7868 case POST_INC:
7869 case POST_DEC:
7870 case POST_MODIFY:
7871 /* POST_INC and POST_DEC can be handled just like a SUBREG. So we
7872 just fall into the SUBREG code. */
7874 /* Fall through. */
7876 case SUBREG:
7877 /* The case of a subreg may arise when we have a local (register)
7878 variable or a formal (register) parameter which doesn't quite fill
7879 up an entire register. For now, just assume that it is
7880 legitimate to make the Dwarf info refer to the whole register which
7881 contains the given subreg. */
7882 rtl = XEXP (rtl, 0);
7884 /* Fall through. */
7886 case REG:
7887 /* Whenever a register number forms a part of the description of the
7888 method for calculating the (dynamic) address of a memory resident
7889 object, DWARF rules require the register number be referred to as
7890 a "base register". This distinction is not based in any way upon
7891 what category of register the hardware believes the given register
7892 belongs to. This is strictly DWARF terminology we're dealing with
7893 here. Note that in cases where the location of a memory-resident
7894 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
7895 OP_CONST (0)) the actual DWARF location descriptor that we generate
7896 may just be OP_BASEREG (basereg). This may look deceptively like
7897 the object in question was allocated to a register (rather than in
7898 memory) so DWARF consumers need to be aware of the subtle
7899 distinction between OP_REG and OP_BASEREG. */
7900 mem_loc_result = based_loc_descr (reg_number (rtl), 0);
7901 break;
7903 case MEM:
7904 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
7905 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
7906 break;
7908 case LABEL_REF:
7909 /* Some ports can transform a symbol ref into a label ref, because
7910 the symbol ref is too far away and has to be dumped into a constant
7911 pool. */
7912 case CONST:
7913 case SYMBOL_REF:
7914 mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
7915 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
7916 mem_loc_result->dw_loc_oprnd1.v.val_addr = save_rtx (rtl);
7917 break;
7919 case PRE_MODIFY:
7920 /* Extract the PLUS expression nested inside and fall into
7921 PLUS code bellow. */
7922 rtl = XEXP (rtl, 1);
7923 goto plus;
7925 case PRE_INC:
7926 case PRE_DEC:
7927 /* Turn these into a PLUS expression and fall into the PLUS code
7928 below. */
7929 rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
7930 GEN_INT (GET_CODE (rtl) == PRE_INC
7931 ? GET_MODE_UNIT_SIZE (mode)
7932 : -GET_MODE_UNIT_SIZE (mode)));
7934 /* Fall through. */
7936 case PLUS:
7937 plus:
7938 if (is_based_loc (rtl))
7939 mem_loc_result = based_loc_descr (reg_number (XEXP (rtl, 0)),
7940 INTVAL (XEXP (rtl, 1)));
7941 else
7943 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
7945 if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
7946 && INTVAL (XEXP (rtl, 1)) >= 0)
7948 add_loc_descr (&mem_loc_result,
7949 new_loc_descr (DW_OP_plus_uconst,
7950 INTVAL (XEXP (rtl, 1)), 0));
7952 else
7954 add_loc_descr (&mem_loc_result,
7955 mem_loc_descriptor (XEXP (rtl, 1), mode));
7956 add_loc_descr (&mem_loc_result,
7957 new_loc_descr (DW_OP_plus, 0, 0));
7960 break;
7962 case MULT:
7963 /* If a pseudo-reg is optimized away, it is possible for it to
7964 be replaced with a MEM containing a multiply. */
7965 add_loc_descr (&mem_loc_result,
7966 mem_loc_descriptor (XEXP (rtl, 0), mode));
7967 add_loc_descr (&mem_loc_result,
7968 mem_loc_descriptor (XEXP (rtl, 1), mode));
7969 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
7970 break;
7972 case CONST_INT:
7973 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
7974 break;
7976 default:
7977 abort ();
7980 return mem_loc_result;
7983 /* Return a descriptor that describes the concatenation of two locations.
7984 This is typically a complex variable. */
7986 static dw_loc_descr_ref
7987 concat_loc_descriptor (x0, x1)
7988 register rtx x0, x1;
7990 dw_loc_descr_ref cc_loc_result = NULL;
7992 if (!is_pseudo_reg (x0)
7993 && (GET_CODE (x0) != MEM || !is_pseudo_reg (XEXP (x0, 0))))
7994 add_loc_descr (&cc_loc_result, loc_descriptor (x0));
7995 add_loc_descr (&cc_loc_result,
7996 new_loc_descr (DW_OP_piece, GET_MODE_SIZE (GET_MODE (x0)), 0));
7998 if (!is_pseudo_reg (x1)
7999 && (GET_CODE (x1) != MEM || !is_pseudo_reg (XEXP (x1, 0))))
8000 add_loc_descr (&cc_loc_result, loc_descriptor (x1));
8001 add_loc_descr (&cc_loc_result,
8002 new_loc_descr (DW_OP_piece, GET_MODE_SIZE (GET_MODE (x1)), 0));
8004 return cc_loc_result;
8007 /* Output a proper Dwarf location descriptor for a variable or parameter
8008 which is either allocated in a register or in a memory location. For a
8009 register, we just generate an OP_REG and the register number. For a
8010 memory location we provide a Dwarf postfix expression describing how to
8011 generate the (dynamic) address of the object onto the address stack. */
8013 static dw_loc_descr_ref
8014 loc_descriptor (rtl)
8015 register rtx rtl;
8017 dw_loc_descr_ref loc_result = NULL;
8018 switch (GET_CODE (rtl))
8020 case SUBREG:
8021 /* The case of a subreg may arise when we have a local (register)
8022 variable or a formal (register) parameter which doesn't quite fill
8023 up an entire register. For now, just assume that it is
8024 legitimate to make the Dwarf info refer to the whole register which
8025 contains the given subreg. */
8026 rtl = XEXP (rtl, 0);
8028 /* Fall through. */
8030 case REG:
8031 loc_result = reg_loc_descriptor (rtl);
8032 break;
8034 case MEM:
8035 loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8036 break;
8038 case CONCAT:
8039 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
8040 break;
8042 default:
8043 abort ();
8046 return loc_result;
8049 /* Similar, but generate the descriptor from trees instead of rtl.
8050 This comes up particularly with variable length arrays. */
8052 static dw_loc_descr_ref
8053 loc_descriptor_from_tree (loc, addressp)
8054 tree loc;
8055 int addressp;
8057 dw_loc_descr_ref ret = NULL;
8058 int indirect_size = 0;
8059 int unsignedp = TREE_UNSIGNED (TREE_TYPE (loc));
8060 enum dwarf_location_atom op;
8062 /* ??? Most of the time we do not take proper care for sign/zero
8063 extending the values properly. Hopefully this won't be a real
8064 problem... */
8066 switch (TREE_CODE (loc))
8068 case ERROR_MARK:
8069 break;
8071 case WITH_RECORD_EXPR:
8072 /* This case involves extracting fields from an object to determine the
8073 position of other fields. We don't try to encode this here. The
8074 only user of this is Ada, which encodes the needed information using
8075 the names of types. */
8076 return ret;
8078 case VAR_DECL:
8079 case PARM_DECL:
8081 rtx rtl = rtl_for_decl_location (loc);
8082 enum machine_mode mode = DECL_MODE (loc);
8084 if (rtl == NULL_RTX)
8085 break;
8086 else if (CONSTANT_P (rtl))
8088 ret = new_loc_descr (DW_OP_addr, 0, 0);
8089 ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8090 ret->dw_loc_oprnd1.v.val_addr = rtl;
8091 indirect_size = GET_MODE_SIZE (mode);
8093 else
8095 if (GET_CODE (rtl) == MEM)
8097 indirect_size = GET_MODE_SIZE (mode);
8098 rtl = XEXP (rtl, 0);
8100 ret = mem_loc_descriptor (rtl, mode);
8103 break;
8105 case INDIRECT_REF:
8106 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8107 indirect_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (loc)));
8108 break;
8110 case NOP_EXPR:
8111 case CONVERT_EXPR:
8112 case NON_LVALUE_EXPR:
8113 case SAVE_EXPR:
8114 return loc_descriptor_from_tree (TREE_OPERAND (loc, 0), addressp);
8116 case COMPONENT_REF:
8117 case BIT_FIELD_REF:
8118 case ARRAY_REF:
8120 tree obj, offset;
8121 HOST_WIDE_INT bitsize, bitpos, bytepos;
8122 enum machine_mode mode;
8123 int volatilep;
8124 unsigned int alignment;
8126 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
8127 &unsignedp, &volatilep, &alignment);
8128 ret = loc_descriptor_from_tree (obj, 1);
8130 if (offset != NULL_TREE)
8132 /* Variable offset. */
8133 add_loc_descr (&ret, loc_descriptor_from_tree (offset, 0));
8134 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
8137 if (addressp)
8139 /* We cannot address anything not on a unit boundary. */
8140 if (bitpos % BITS_PER_UNIT != 0)
8141 abort ();
8143 else
8145 if (bitpos % BITS_PER_UNIT != 0
8146 || bitsize % BITS_PER_UNIT != 0)
8148 /* ??? We could handle this by loading and shifting etc.
8149 Wait until someone needs it before expending the effort. */
8150 abort ();
8153 indirect_size = bitsize / BITS_PER_UNIT;
8156 bytepos = bitpos / BITS_PER_UNIT;
8157 if (bytepos > 0)
8158 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
8159 else if (bytepos < 0)
8161 add_loc_descr (&ret, int_loc_descriptor (bytepos));
8162 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
8164 break;
8167 case INTEGER_CST:
8168 if (host_integerp (loc, 0))
8169 ret = int_loc_descriptor (tree_low_cst (loc, 0));
8170 break;
8172 case BIT_AND_EXPR:
8173 op = DW_OP_and;
8174 goto do_binop;
8175 case BIT_XOR_EXPR:
8176 op = DW_OP_xor;
8177 goto do_binop;
8178 case BIT_IOR_EXPR:
8179 op = DW_OP_or;
8180 goto do_binop;
8181 case TRUNC_DIV_EXPR:
8182 op = DW_OP_div;
8183 goto do_binop;
8184 case MINUS_EXPR:
8185 op = DW_OP_minus;
8186 goto do_binop;
8187 case TRUNC_MOD_EXPR:
8188 op = DW_OP_mod;
8189 goto do_binop;
8190 case MULT_EXPR:
8191 op = DW_OP_mul;
8192 goto do_binop;
8193 case LSHIFT_EXPR:
8194 op = DW_OP_shl;
8195 goto do_binop;
8196 case RSHIFT_EXPR:
8197 op = (unsignedp ? DW_OP_shr : DW_OP_shra);
8198 goto do_binop;
8199 case PLUS_EXPR:
8200 if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
8201 && host_integerp (TREE_OPERAND (loc, 1), 0))
8203 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8204 add_loc_descr (&ret,
8205 new_loc_descr (DW_OP_plus_uconst,
8206 tree_low_cst (TREE_OPERAND (loc, 1),
8208 0));
8209 break;
8211 op = DW_OP_plus;
8212 goto do_binop;
8213 case LE_EXPR:
8214 if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
8215 break;
8216 op = DW_OP_le;
8217 goto do_binop;
8218 case GE_EXPR:
8219 if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
8220 break;
8221 op = DW_OP_ge;
8222 goto do_binop;
8223 case LT_EXPR:
8224 if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
8225 break;
8226 op = DW_OP_lt;
8227 goto do_binop;
8228 case GT_EXPR:
8229 if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
8230 break;
8231 op = DW_OP_gt;
8232 goto do_binop;
8233 case EQ_EXPR:
8234 op = DW_OP_eq;
8235 goto do_binop;
8236 case NE_EXPR:
8237 op = DW_OP_ne;
8238 goto do_binop;
8240 do_binop:
8241 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8242 add_loc_descr (&ret, loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0));
8243 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
8244 break;
8246 case BIT_NOT_EXPR:
8247 op = DW_OP_not;
8248 goto do_unop;
8249 case ABS_EXPR:
8250 op = DW_OP_abs;
8251 goto do_unop;
8252 case NEGATE_EXPR:
8253 op = DW_OP_neg;
8254 goto do_unop;
8256 do_unop:
8257 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8258 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
8259 break;
8261 case MAX_EXPR:
8262 loc = build (COND_EXPR, TREE_TYPE (loc),
8263 build (LT_EXPR, integer_type_node,
8264 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
8265 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
8266 /* FALLTHRU */
8268 case COND_EXPR:
8270 dw_loc_descr_ref bra_node, jump_node, tmp;
8272 ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8273 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
8274 add_loc_descr (&ret, bra_node);
8276 tmp = loc_descriptor_from_tree (TREE_OPERAND (loc, 2), 0);
8277 add_loc_descr (&ret, tmp);
8278 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
8279 add_loc_descr (&ret, jump_node);
8281 tmp = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
8282 add_loc_descr (&ret, tmp);
8283 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
8284 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
8286 /* ??? Need a node to point the skip at. Use a nop. */
8287 tmp = new_loc_descr (DW_OP_nop, 0, 0);
8288 add_loc_descr (&ret, tmp);
8289 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
8290 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
8292 break;
8294 default:
8295 abort ();
8298 /* If we can't fill the request for an address, die. */
8299 if (addressp && indirect_size == 0)
8300 abort ();
8302 /* If we've got an address and don't want one, dereference. */
8303 if (!addressp && indirect_size > 0)
8305 if (indirect_size > DWARF2_ADDR_SIZE)
8306 abort ();
8307 if (indirect_size == DWARF2_ADDR_SIZE)
8308 op = DW_OP_deref;
8309 else
8310 op = DW_OP_deref_size;
8311 add_loc_descr (&ret, new_loc_descr (op, indirect_size, 0));
8314 return ret;
8317 /* Given a value, round it up to the lowest multiple of `boundary'
8318 which is not less than the value itself. */
8320 static inline HOST_WIDE_INT
8321 ceiling (value, boundary)
8322 HOST_WIDE_INT value;
8323 unsigned int boundary;
8325 return (((value + boundary - 1) / boundary) * boundary);
8328 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
8329 pointer to the declared type for the relevant field variable, or return
8330 `integer_type_node' if the given node turns out to be an
8331 ERROR_MARK node. */
8333 static inline tree
8334 field_type (decl)
8335 register tree decl;
8337 register tree type;
8339 if (TREE_CODE (decl) == ERROR_MARK)
8340 return integer_type_node;
8342 type = DECL_BIT_FIELD_TYPE (decl);
8343 if (type == NULL_TREE)
8344 type = TREE_TYPE (decl);
8346 return type;
8349 /* Given a pointer to a tree node, return the alignment in bits for
8350 it, or else return BITS_PER_WORD if the node actually turns out to
8351 be an ERROR_MARK node. */
8353 static inline unsigned
8354 simple_type_align_in_bits (type)
8355 register tree type;
8357 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
8360 static inline unsigned
8361 simple_decl_align_in_bits (decl)
8362 register tree decl;
8364 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
8367 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8368 node, return the size in bits for the type if it is a constant, or else
8369 return the alignment for the type if the type's size is not constant, or
8370 else return BITS_PER_WORD if the type actually turns out to be an
8371 ERROR_MARK node. */
8373 static inline unsigned HOST_WIDE_INT
8374 simple_type_size_in_bits (type)
8375 register tree type;
8377 tree type_size_tree;
8379 if (TREE_CODE (type) == ERROR_MARK)
8380 return BITS_PER_WORD;
8381 type_size_tree = TYPE_SIZE (type);
8383 if (type_size_tree == NULL_TREE)
8384 return 0;
8385 if (! host_integerp (type_size_tree, 1))
8386 return TYPE_ALIGN (type);
8387 return tree_low_cst (type_size_tree, 1);
8390 /* Given a pointer to what is assumed to be a FIELD_DECL node, compute and
8391 return the byte offset of the lowest addressed byte of the "containing
8392 object" for the given FIELD_DECL, or return 0 if we are unable to
8393 determine what that offset is, either because the argument turns out to
8394 be a pointer to an ERROR_MARK node, or because the offset is actually
8395 variable. (We can't handle the latter case just yet). */
8397 static HOST_WIDE_INT
8398 field_byte_offset (decl)
8399 register tree decl;
8401 unsigned int type_align_in_bits;
8402 unsigned int decl_align_in_bits;
8403 unsigned HOST_WIDE_INT type_size_in_bits;
8404 HOST_WIDE_INT object_offset_in_bits;
8405 HOST_WIDE_INT object_offset_in_bytes;
8406 tree type;
8407 tree field_size_tree;
8408 HOST_WIDE_INT bitpos_int;
8409 HOST_WIDE_INT deepest_bitpos;
8410 unsigned HOST_WIDE_INT field_size_in_bits;
8412 if (TREE_CODE (decl) == ERROR_MARK)
8413 return 0;
8415 if (TREE_CODE (decl) != FIELD_DECL)
8416 abort ();
8418 type = field_type (decl);
8419 field_size_tree = DECL_SIZE (decl);
8421 /* The size could be unspecified if there was an error, or for
8422 a flexible array member. */
8423 if (! field_size_tree)
8424 field_size_tree = bitsize_zero_node;
8426 /* We cannot yet cope with fields whose positions are variable, so
8427 for now, when we see such things, we simply return 0. Someday, we may
8428 be able to handle such cases, but it will be damn difficult. */
8429 if (! host_integerp (bit_position (decl), 0))
8430 return 0;
8432 bitpos_int = int_bit_position (decl);
8434 /* If we don't know the size of the field, pretend it's a full word. */
8435 if (host_integerp (field_size_tree, 1))
8436 field_size_in_bits = tree_low_cst (field_size_tree, 1);
8437 else
8438 field_size_in_bits = BITS_PER_WORD;
8440 type_size_in_bits = simple_type_size_in_bits (type);
8441 type_align_in_bits = simple_type_align_in_bits (type);
8442 decl_align_in_bits = simple_decl_align_in_bits (decl);
8444 /* Note that the GCC front-end doesn't make any attempt to keep track of
8445 the starting bit offset (relative to the start of the containing
8446 structure type) of the hypothetical "containing object" for a bit-
8447 field. Thus, when computing the byte offset value for the start of the
8448 "containing object" of a bit-field, we must deduce this information on
8449 our own. This can be rather tricky to do in some cases. For example,
8450 handling the following structure type definition when compiling for an
8451 i386/i486 target (which only aligns long long's to 32-bit boundaries)
8452 can be very tricky:
8454 struct S { int field1; long long field2:31; };
8456 Fortunately, there is a simple rule-of-thumb which can be
8457 used in such cases. When compiling for an i386/i486, GCC will allocate
8458 8 bytes for the structure shown above. It decides to do this based upon
8459 one simple rule for bit-field allocation. Quite simply, GCC allocates
8460 each "containing object" for each bit-field at the first (i.e. lowest
8461 addressed) legitimate alignment boundary (based upon the required
8462 minimum alignment for the declared type of the field) which it can
8463 possibly use, subject to the condition that there is still enough
8464 available space remaining in the containing object (when allocated at
8465 the selected point) to fully accommodate all of the bits of the
8466 bit-field itself. This simple rule makes it obvious why GCC allocates
8467 8 bytes for each object of the structure type shown above. When looking
8468 for a place to allocate the "containing object" for `field2', the
8469 compiler simply tries to allocate a 64-bit "containing object" at each
8470 successive 32-bit boundary (starting at zero) until it finds a place to
8471 allocate that 64- bit field such that at least 31 contiguous (and
8472 previously unallocated) bits remain within that selected 64 bit field.
8473 (As it turns out, for the example above, the compiler finds that it is
8474 OK to allocate the "containing object" 64-bit field at bit-offset zero
8475 within the structure type.) Here we attempt to work backwards from the
8476 limited set of facts we're given, and we try to deduce from those facts,
8477 where GCC must have believed that the containing object started (within
8478 the structure type). The value we deduce is then used (by the callers of
8479 this routine) to generate DW_AT_location and DW_AT_bit_offset attributes
8480 for fields (both bit-fields and, in the case of DW_AT_location, regular
8481 fields as well). */
8483 /* Figure out the bit-distance from the start of the structure to the
8484 "deepest" bit of the bit-field. */
8485 deepest_bitpos = bitpos_int + field_size_in_bits;
8487 /* This is the tricky part. Use some fancy footwork to deduce where the
8488 lowest addressed bit of the containing object must be. */
8489 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
8491 /* Round up to type_align by default. This works best for bitfields. */
8492 object_offset_in_bits += type_align_in_bits - 1;
8493 object_offset_in_bits /= type_align_in_bits;
8494 object_offset_in_bits *= type_align_in_bits;
8496 if (object_offset_in_bits > bitpos_int)
8498 /* Sigh, the decl must be packed. */
8499 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
8501 /* Round up to decl_align instead. */
8502 object_offset_in_bits += decl_align_in_bits - 1;
8503 object_offset_in_bits /= decl_align_in_bits;
8504 object_offset_in_bits *= decl_align_in_bits;
8507 object_offset_in_bytes = object_offset_in_bits / BITS_PER_UNIT;
8509 return object_offset_in_bytes;
8512 /* The following routines define various Dwarf attributes and any data
8513 associated with them. */
8515 /* Add a location description attribute value to a DIE.
8517 This emits location attributes suitable for whole variables and
8518 whole parameters. Note that the location attributes for struct fields are
8519 generated by the routine `data_member_location_attribute' below. */
8521 static void
8522 add_AT_location_description (die, attr_kind, rtl)
8523 dw_die_ref die;
8524 enum dwarf_attribute attr_kind;
8525 register rtx rtl;
8527 /* Handle a special case. If we are about to output a location descriptor
8528 for a variable or parameter which has been optimized out of existence,
8529 don't do that. A variable which has been optimized out
8530 of existence will have a DECL_RTL value which denotes a pseudo-reg.
8531 Currently, in some rare cases, variables can have DECL_RTL values which
8532 look like (MEM (REG pseudo-reg#)). These cases are due to bugs
8533 elsewhere in the compiler. We treat such cases as if the variable(s) in
8534 question had been optimized out of existence. */
8536 if (is_pseudo_reg (rtl)
8537 || (GET_CODE (rtl) == MEM
8538 && is_pseudo_reg (XEXP (rtl, 0)))
8539 /* This can happen for a PARM_DECL with a DECL_INCOMING_RTL which
8540 references the internal argument pointer (a pseudo) in a function
8541 where all references to the internal argument pointer were
8542 eliminated via the optimizers. */
8543 || (GET_CODE (rtl) == MEM
8544 && GET_CODE (XEXP (rtl, 0)) == PLUS
8545 && is_pseudo_reg (XEXP (XEXP (rtl, 0), 0)))
8546 || (GET_CODE (rtl) == CONCAT
8547 && is_pseudo_reg (XEXP (rtl, 0))
8548 && is_pseudo_reg (XEXP (rtl, 1))))
8549 return;
8551 add_AT_loc (die, attr_kind, loc_descriptor (rtl));
8554 /* Attach the specialized form of location attribute used for data
8555 members of struct and union types. In the special case of a
8556 FIELD_DECL node which represents a bit-field, the "offset" part
8557 of this special location descriptor must indicate the distance
8558 in bytes from the lowest-addressed byte of the containing struct
8559 or union type to the lowest-addressed byte of the "containing
8560 object" for the bit-field. (See the `field_byte_offset' function
8561 above).. For any given bit-field, the "containing object" is a
8562 hypothetical object (of some integral or enum type) within which
8563 the given bit-field lives. The type of this hypothetical
8564 "containing object" is always the same as the declared type of
8565 the individual bit-field itself (for GCC anyway... the DWARF
8566 spec doesn't actually mandate this). Note that it is the size
8567 (in bytes) of the hypothetical "containing object" which will
8568 be given in the DW_AT_byte_size attribute for this bit-field.
8569 (See the `byte_size_attribute' function below.) It is also used
8570 when calculating the value of the DW_AT_bit_offset attribute.
8571 (See the `bit_offset_attribute' function below). */
8573 static void
8574 add_data_member_location_attribute (die, decl)
8575 register dw_die_ref die;
8576 register tree decl;
8578 register unsigned long offset;
8579 register dw_loc_descr_ref loc_descr;
8580 register enum dwarf_location_atom op;
8582 if (TREE_CODE (decl) == TREE_VEC)
8583 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
8584 else
8585 offset = field_byte_offset (decl);
8587 /* The DWARF2 standard says that we should assume that the structure address
8588 is already on the stack, so we can specify a structure field address
8589 by using DW_OP_plus_uconst. */
8591 #ifdef MIPS_DEBUGGING_INFO
8592 /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst operator
8593 correctly. It works only if we leave the offset on the stack. */
8594 op = DW_OP_constu;
8595 #else
8596 op = DW_OP_plus_uconst;
8597 #endif
8599 loc_descr = new_loc_descr (op, offset, 0);
8600 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
8603 /* Attach an DW_AT_const_value attribute for a variable or a parameter which
8604 does not have a "location" either in memory or in a register. These
8605 things can arise in GNU C when a constant is passed as an actual parameter
8606 to an inlined function. They can also arise in C++ where declared
8607 constants do not necessarily get memory "homes". */
8609 static void
8610 add_const_value_attribute (die, rtl)
8611 register dw_die_ref die;
8612 register rtx rtl;
8614 switch (GET_CODE (rtl))
8616 case CONST_INT:
8617 /* Note that a CONST_INT rtx could represent either an integer or a
8618 floating-point constant. A CONST_INT is used whenever the constant
8619 will fit into a single word. In all such cases, the original mode
8620 of the constant value is wiped out, and the CONST_INT rtx is
8621 assigned VOIDmode. */
8622 add_AT_unsigned (die, DW_AT_const_value, (unsigned) INTVAL (rtl));
8623 break;
8625 case CONST_DOUBLE:
8626 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
8627 floating-point constant. A CONST_DOUBLE is used whenever the
8628 constant requires more than one word in order to be adequately
8629 represented. We output CONST_DOUBLEs as blocks. */
8631 register enum machine_mode mode = GET_MODE (rtl);
8633 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
8635 register unsigned length = GET_MODE_SIZE (mode) / 4;
8636 long *array = (long *) xmalloc (sizeof (long) * length);
8637 REAL_VALUE_TYPE rv;
8639 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
8640 switch (mode)
8642 case SFmode:
8643 REAL_VALUE_TO_TARGET_SINGLE (rv, array[0]);
8644 break;
8646 case DFmode:
8647 REAL_VALUE_TO_TARGET_DOUBLE (rv, array);
8648 break;
8650 case XFmode:
8651 case TFmode:
8652 REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, array);
8653 break;
8655 default:
8656 abort ();
8659 add_AT_float (die, DW_AT_const_value, length, array);
8661 else
8662 add_AT_long_long (die, DW_AT_const_value,
8663 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
8665 break;
8667 case CONST_STRING:
8668 add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
8669 break;
8671 case SYMBOL_REF:
8672 case LABEL_REF:
8673 case CONST:
8674 add_AT_addr (die, DW_AT_const_value, save_rtx (rtl));
8675 break;
8677 case PLUS:
8678 /* In cases where an inlined instance of an inline function is passed
8679 the address of an `auto' variable (which is local to the caller) we
8680 can get a situation where the DECL_RTL of the artificial local
8681 variable (for the inlining) which acts as a stand-in for the
8682 corresponding formal parameter (of the inline function) will look
8683 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
8684 exactly a compile-time constant expression, but it isn't the address
8685 of the (artificial) local variable either. Rather, it represents the
8686 *value* which the artificial local variable always has during its
8687 lifetime. We currently have no way to represent such quasi-constant
8688 values in Dwarf, so for now we just punt and generate nothing. */
8689 break;
8691 default:
8692 /* No other kinds of rtx should be possible here. */
8693 abort ();
8698 static rtx
8699 rtl_for_decl_location (decl)
8700 tree decl;
8702 register rtx rtl;
8704 /* Here we have to decide where we are going to say the parameter "lives"
8705 (as far as the debugger is concerned). We only have a couple of
8706 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
8708 DECL_RTL normally indicates where the parameter lives during most of the
8709 activation of the function. If optimization is enabled however, this
8710 could be either NULL or else a pseudo-reg. Both of those cases indicate
8711 that the parameter doesn't really live anywhere (as far as the code
8712 generation parts of GCC are concerned) during most of the function's
8713 activation. That will happen (for example) if the parameter is never
8714 referenced within the function.
8716 We could just generate a location descriptor here for all non-NULL
8717 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
8718 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
8719 where DECL_RTL is NULL or is a pseudo-reg.
8721 Note however that we can only get away with using DECL_INCOMING_RTL as
8722 a backup substitute for DECL_RTL in certain limited cases. In cases
8723 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
8724 we can be sure that the parameter was passed using the same type as it is
8725 declared to have within the function, and that its DECL_INCOMING_RTL
8726 points us to a place where a value of that type is passed.
8728 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
8729 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
8730 because in these cases DECL_INCOMING_RTL points us to a value of some
8731 type which is *different* from the type of the parameter itself. Thus,
8732 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
8733 such cases, the debugger would end up (for example) trying to fetch a
8734 `float' from a place which actually contains the first part of a
8735 `double'. That would lead to really incorrect and confusing
8736 output at debug-time.
8738 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
8739 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
8740 are a couple of exceptions however. On little-endian machines we can
8741 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
8742 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
8743 an integral type that is smaller than TREE_TYPE (decl). These cases arise
8744 when (on a little-endian machine) a non-prototyped function has a
8745 parameter declared to be of type `short' or `char'. In such cases,
8746 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
8747 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
8748 passed `int' value. If the debugger then uses that address to fetch
8749 a `short' or a `char' (on a little-endian machine) the result will be
8750 the correct data, so we allow for such exceptional cases below.
8752 Note that our goal here is to describe the place where the given formal
8753 parameter lives during most of the function's activation (i.e. between
8754 the end of the prologue and the start of the epilogue). We'll do that
8755 as best as we can. Note however that if the given formal parameter is
8756 modified sometime during the execution of the function, then a stack
8757 backtrace (at debug-time) will show the function as having been
8758 called with the *new* value rather than the value which was
8759 originally passed in. This happens rarely enough that it is not
8760 a major problem, but it *is* a problem, and I'd like to fix it.
8762 A future version of dwarf2out.c may generate two additional
8763 attributes for any given DW_TAG_formal_parameter DIE which will
8764 describe the "passed type" and the "passed location" for the
8765 given formal parameter in addition to the attributes we now
8766 generate to indicate the "declared type" and the "active
8767 location" for each parameter. This additional set of attributes
8768 could be used by debuggers for stack backtraces. Separately, note
8769 that sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be
8770 NULL also. This happens (for example) for inlined-instances of
8771 inline function formal parameters which are never referenced.
8772 This really shouldn't be happening. All PARM_DECL nodes should
8773 get valid non-NULL DECL_INCOMING_RTL values, but integrate.c
8774 doesn't currently generate these values for inlined instances of
8775 inline function parameters, so when we see such cases, we are
8776 just out-of-luck for the time being (until integrate.c
8777 gets fixed). */
8779 /* Use DECL_RTL as the "location" unless we find something better. */
8780 rtl = DECL_RTL (decl);
8782 if (TREE_CODE (decl) == PARM_DECL)
8784 if (rtl == NULL_RTX || is_pseudo_reg (rtl))
8786 tree declared_type = type_main_variant (TREE_TYPE (decl));
8787 tree passed_type = type_main_variant (DECL_ARG_TYPE (decl));
8789 /* This decl represents a formal parameter which was optimized out.
8790 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
8791 all* cases where (rtl == NULL_RTX) just below. */
8792 if (declared_type == passed_type)
8793 rtl = DECL_INCOMING_RTL (decl);
8794 else if (! BYTES_BIG_ENDIAN
8795 && TREE_CODE (declared_type) == INTEGER_TYPE
8796 && (GET_MODE_SIZE (TYPE_MODE (declared_type))
8797 <= GET_MODE_SIZE (TYPE_MODE (passed_type))))
8798 rtl = DECL_INCOMING_RTL (decl);
8801 /* If the parm was passed in registers, but lives on the stack, then
8802 make a big endian correction if the mode of the type of the
8803 parameter is not the same as the mode of the rtl. */
8804 /* ??? This is the same series of checks that are made in dbxout.c before
8805 we reach the big endian correction code there. It isn't clear if all
8806 of these checks are necessary here, but keeping them all is the safe
8807 thing to do. */
8808 else if (GET_CODE (rtl) == MEM
8809 && XEXP (rtl, 0) != const0_rtx
8810 && ! CONSTANT_P (XEXP (rtl, 0))
8811 /* Not passed in memory. */
8812 && GET_CODE (DECL_INCOMING_RTL (decl)) != MEM
8813 /* Not passed by invisible reference. */
8814 && (GET_CODE (XEXP (rtl, 0)) != REG
8815 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
8816 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
8817 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
8818 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
8819 #endif
8821 /* Big endian correction check. */
8822 && BYTES_BIG_ENDIAN
8823 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
8824 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
8825 < UNITS_PER_WORD))
8827 int offset = (UNITS_PER_WORD
8828 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
8829 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
8830 plus_constant (XEXP (rtl, 0), offset));
8834 if (rtl != NULL_RTX)
8836 rtl = eliminate_regs (rtl, 0, NULL_RTX);
8837 #ifdef LEAF_REG_REMAP
8838 if (current_function_uses_only_leaf_regs)
8839 leaf_renumber_regs_insn (rtl);
8840 #endif
8843 return rtl;
8846 /* Generate *either* an DW_AT_location attribute or else an DW_AT_const_value
8847 data attribute for a variable or a parameter. We generate the
8848 DW_AT_const_value attribute only in those cases where the given variable
8849 or parameter does not have a true "location" either in memory or in a
8850 register. This can happen (for example) when a constant is passed as an
8851 actual argument in a call to an inline function. (It's possible that
8852 these things can crop up in other ways also.) Note that one type of
8853 constant value which can be passed into an inlined function is a constant
8854 pointer. This can happen for example if an actual argument in an inlined
8855 function call evaluates to a compile-time constant address. */
8857 static void
8858 add_location_or_const_value_attribute (die, decl)
8859 register dw_die_ref die;
8860 register tree decl;
8862 register rtx rtl;
8864 if (TREE_CODE (decl) == ERROR_MARK)
8865 return;
8867 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != PARM_DECL)
8868 abort ();
8870 rtl = rtl_for_decl_location (decl);
8871 if (rtl == NULL_RTX)
8872 return;
8874 switch (GET_CODE (rtl))
8876 case ADDRESSOF:
8877 /* The address of a variable that was optimized away; don't emit
8878 anything. */
8879 break;
8881 case CONST_INT:
8882 case CONST_DOUBLE:
8883 case CONST_STRING:
8884 case SYMBOL_REF:
8885 case LABEL_REF:
8886 case CONST:
8887 case PLUS:
8888 /* DECL_RTL could be (plus (reg ...) (const_int ...)) */
8889 add_const_value_attribute (die, rtl);
8890 break;
8892 case MEM:
8893 case REG:
8894 case SUBREG:
8895 case CONCAT:
8896 add_AT_location_description (die, DW_AT_location, rtl);
8897 break;
8899 default:
8900 abort ();
8904 /* If we don't have a copy of this variable in memory for some reason (such
8905 as a C++ member constant that doesn't have an out-of-line definition),
8906 we should tell the debugger about the constant value. */
8908 static void
8909 tree_add_const_value_attribute (var_die, decl)
8910 dw_die_ref var_die;
8911 tree decl;
8913 tree init = DECL_INITIAL (decl);
8914 tree type = TREE_TYPE (decl);
8916 if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init
8917 && initializer_constant_valid_p (init, type) == null_pointer_node)
8918 /* OK */;
8919 else
8920 return;
8922 switch (TREE_CODE (type))
8924 case INTEGER_TYPE:
8925 if (host_integerp (init, 0))
8926 add_AT_unsigned (var_die, DW_AT_const_value,
8927 TREE_INT_CST_LOW (init));
8928 else
8929 add_AT_long_long (var_die, DW_AT_const_value,
8930 TREE_INT_CST_HIGH (init),
8931 TREE_INT_CST_LOW (init));
8932 break;
8934 default:;
8938 /* Generate an DW_AT_name attribute given some string value to be included as
8939 the value of the attribute. */
8941 static inline void
8942 add_name_attribute (die, name_string)
8943 register dw_die_ref die;
8944 register const char *name_string;
8946 if (name_string != NULL && *name_string != 0)
8948 if (demangle_name_func)
8949 name_string = (*demangle_name_func) (name_string);
8951 add_AT_string (die, DW_AT_name, name_string);
8955 /* Given a tree node describing an array bound (either lower or upper) output
8956 a representation for that bound. */
8958 static void
8959 add_bound_info (subrange_die, bound_attr, bound)
8960 register dw_die_ref subrange_die;
8961 register enum dwarf_attribute bound_attr;
8962 register tree bound;
8964 /* If this is an Ada unconstrained array type, then don't emit any debug
8965 info because the array bounds are unknown. They are parameterized when
8966 the type is instantiated. */
8967 if (contains_placeholder_p (bound))
8968 return;
8970 switch (TREE_CODE (bound))
8972 case ERROR_MARK:
8973 return;
8975 /* All fixed-bounds are represented by INTEGER_CST nodes. */
8976 case INTEGER_CST:
8977 if (! host_integerp (bound, 0)
8978 || (bound_attr == DW_AT_lower_bound
8979 && (((is_c_family () || is_java ()) && integer_zerop (bound))
8980 || (is_fortran () && integer_onep (bound)))))
8981 /* use the default */
8983 else
8984 add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
8985 break;
8987 case CONVERT_EXPR:
8988 case NOP_EXPR:
8989 case NON_LVALUE_EXPR:
8990 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
8991 break;
8993 case SAVE_EXPR:
8994 /* If optimization is turned on, the SAVE_EXPRs that describe how to
8995 access the upper bound values may be bogus. If they refer to a
8996 register, they may only describe how to get at these values at the
8997 points in the generated code right after they have just been
8998 computed. Worse yet, in the typical case, the upper bound values
8999 will not even *be* computed in the optimized code (though the
9000 number of elements will), so these SAVE_EXPRs are entirely
9001 bogus. In order to compensate for this fact, we check here to see
9002 if optimization is enabled, and if so, we don't add an attribute
9003 for the (unknown and unknowable) upper bound. This should not
9004 cause too much trouble for existing (stupid?) debuggers because
9005 they have to deal with empty upper bounds location descriptions
9006 anyway in order to be able to deal with incomplete array types.
9007 Of course an intelligent debugger (GDB?) should be able to
9008 comprehend that a missing upper bound specification in a array
9009 type used for a storage class `auto' local array variable
9010 indicates that the upper bound is both unknown (at compile- time)
9011 and unknowable (at run-time) due to optimization.
9013 We assume that a MEM rtx is safe because gcc wouldn't put the
9014 value there unless it was going to be used repeatedly in the
9015 function, i.e. for cleanups. */
9016 if (SAVE_EXPR_RTL (bound)
9017 && (! optimize || GET_CODE (SAVE_EXPR_RTL (bound)) == MEM))
9019 register dw_die_ref ctx = lookup_decl_die (current_function_decl);
9020 register dw_die_ref decl_die = new_die (DW_TAG_variable, ctx);
9021 register rtx loc = SAVE_EXPR_RTL (bound);
9023 /* If the RTL for the SAVE_EXPR is memory, handle the case where
9024 it references an outer function's frame. */
9026 if (GET_CODE (loc) == MEM)
9028 rtx new_addr = fix_lexical_addr (XEXP (loc, 0), bound);
9030 if (XEXP (loc, 0) != new_addr)
9031 loc = gen_rtx_MEM (GET_MODE (loc), new_addr);
9034 add_AT_flag (decl_die, DW_AT_artificial, 1);
9035 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
9036 add_AT_location_description (decl_die, DW_AT_location, loc);
9037 add_AT_die_ref (subrange_die, bound_attr, decl_die);
9040 /* Else leave out the attribute. */
9041 break;
9043 case VAR_DECL:
9044 case PARM_DECL:
9046 dw_die_ref decl_die = lookup_decl_die (bound);
9048 /* ??? Can this happen, or should the variable have been bound
9049 first? Probably it can, since I imagine that we try to create
9050 the types of parameters in the order in which they exist in
9051 the list, and won't have created a forward reference to a
9052 later parameter. */
9053 if (decl_die != NULL)
9054 add_AT_die_ref (subrange_die, bound_attr, decl_die);
9055 break;
9058 default:
9060 /* Otherwise try to create a stack operation procedure to
9061 evaluate the value of the array bound. */
9063 dw_die_ref ctx, decl_die;
9064 dw_loc_descr_ref loc;
9066 loc = loc_descriptor_from_tree (bound, 0);
9067 if (loc == NULL)
9068 break;
9070 ctx = lookup_decl_die (current_function_decl);
9072 decl_die = new_die (DW_TAG_variable, ctx);
9073 add_AT_flag (decl_die, DW_AT_artificial, 1);
9074 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
9075 add_AT_loc (decl_die, DW_AT_location, loc);
9077 add_AT_die_ref (subrange_die, bound_attr, decl_die);
9078 break;
9083 /* Note that the block of subscript information for an array type also
9084 includes information about the element type of type given array type. */
9086 static void
9087 add_subscript_info (type_die, type)
9088 register dw_die_ref type_die;
9089 register tree type;
9091 #ifndef MIPS_DEBUGGING_INFO
9092 register unsigned dimension_number;
9093 #endif
9094 register tree lower, upper;
9095 register dw_die_ref subrange_die;
9097 /* The GNU compilers represent multidimensional array types as sequences of
9098 one dimensional array types whose element types are themselves array
9099 types. Here we squish that down, so that each multidimensional array
9100 type gets only one array_type DIE in the Dwarf debugging info. The draft
9101 Dwarf specification say that we are allowed to do this kind of
9102 compression in C (because there is no difference between an array or
9103 arrays and a multidimensional array in C) but for other source languages
9104 (e.g. Ada) we probably shouldn't do this. */
9106 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
9107 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
9108 We work around this by disabling this feature. See also
9109 gen_array_type_die. */
9110 #ifndef MIPS_DEBUGGING_INFO
9111 for (dimension_number = 0;
9112 TREE_CODE (type) == ARRAY_TYPE;
9113 type = TREE_TYPE (type), dimension_number++)
9115 #endif
9116 register tree domain = TYPE_DOMAIN (type);
9118 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
9119 and (in GNU C only) variable bounds. Handle all three forms
9120 here. */
9121 subrange_die = new_die (DW_TAG_subrange_type, type_die);
9122 if (domain)
9124 /* We have an array type with specified bounds. */
9125 lower = TYPE_MIN_VALUE (domain);
9126 upper = TYPE_MAX_VALUE (domain);
9128 /* define the index type. */
9129 if (TREE_TYPE (domain))
9131 /* ??? This is probably an Ada unnamed subrange type. Ignore the
9132 TREE_TYPE field. We can't emit debug info for this
9133 because it is an unnamed integral type. */
9134 if (TREE_CODE (domain) == INTEGER_TYPE
9135 && TYPE_NAME (domain) == NULL_TREE
9136 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
9137 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
9139 else
9140 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
9141 type_die);
9144 /* ??? If upper is NULL, the array has unspecified length,
9145 but it does have a lower bound. This happens with Fortran
9146 dimension arr(N:*)
9147 Since the debugger is definitely going to need to know N
9148 to produce useful results, go ahead and output the lower
9149 bound solo, and hope the debugger can cope. */
9151 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
9152 if (upper)
9153 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
9155 else
9156 /* We have an array type with an unspecified length. The DWARF-2
9157 spec does not say how to handle this; let's just leave out the
9158 bounds. */
9161 #ifndef MIPS_DEBUGGING_INFO
9163 #endif
9166 static void
9167 add_byte_size_attribute (die, tree_node)
9168 dw_die_ref die;
9169 register tree tree_node;
9171 register unsigned size;
9173 switch (TREE_CODE (tree_node))
9175 case ERROR_MARK:
9176 size = 0;
9177 break;
9178 case ENUMERAL_TYPE:
9179 case RECORD_TYPE:
9180 case UNION_TYPE:
9181 case QUAL_UNION_TYPE:
9182 size = int_size_in_bytes (tree_node);
9183 break;
9184 case FIELD_DECL:
9185 /* For a data member of a struct or union, the DW_AT_byte_size is
9186 generally given as the number of bytes normally allocated for an
9187 object of the *declared* type of the member itself. This is true
9188 even for bit-fields. */
9189 size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
9190 break;
9191 default:
9192 abort ();
9195 /* Note that `size' might be -1 when we get to this point. If it is, that
9196 indicates that the byte size of the entity in question is variable. We
9197 have no good way of expressing this fact in Dwarf at the present time,
9198 so just let the -1 pass on through. */
9200 add_AT_unsigned (die, DW_AT_byte_size, size);
9203 /* For a FIELD_DECL node which represents a bit-field, output an attribute
9204 which specifies the distance in bits from the highest order bit of the
9205 "containing object" for the bit-field to the highest order bit of the
9206 bit-field itself.
9208 For any given bit-field, the "containing object" is a hypothetical
9209 object (of some integral or enum type) within which the given bit-field
9210 lives. The type of this hypothetical "containing object" is always the
9211 same as the declared type of the individual bit-field itself. The
9212 determination of the exact location of the "containing object" for a
9213 bit-field is rather complicated. It's handled by the
9214 `field_byte_offset' function (above).
9216 Note that it is the size (in bytes) of the hypothetical "containing object"
9217 which will be given in the DW_AT_byte_size attribute for this bit-field.
9218 (See `byte_size_attribute' above). */
9220 static inline void
9221 add_bit_offset_attribute (die, decl)
9222 register dw_die_ref die;
9223 register tree decl;
9225 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
9226 tree type = DECL_BIT_FIELD_TYPE (decl);
9227 HOST_WIDE_INT bitpos_int;
9228 HOST_WIDE_INT highest_order_object_bit_offset;
9229 HOST_WIDE_INT highest_order_field_bit_offset;
9230 HOST_WIDE_INT unsigned bit_offset;
9232 /* Must be a field and a bit field. */
9233 if (!type
9234 || TREE_CODE (decl) != FIELD_DECL)
9235 abort ();
9237 /* We can't yet handle bit-fields whose offsets are variable, so if we
9238 encounter such things, just return without generating any attribute
9239 whatsoever. Likewise for variable or too large size. */
9240 if (! host_integerp (bit_position (decl), 0)
9241 || ! host_integerp (DECL_SIZE (decl), 1))
9242 return;
9244 bitpos_int = int_bit_position (decl);
9246 /* Note that the bit offset is always the distance (in bits) from the
9247 highest-order bit of the "containing object" to the highest-order bit of
9248 the bit-field itself. Since the "high-order end" of any object or field
9249 is different on big-endian and little-endian machines, the computation
9250 below must take account of these differences. */
9251 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
9252 highest_order_field_bit_offset = bitpos_int;
9254 if (! BYTES_BIG_ENDIAN)
9256 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
9257 highest_order_object_bit_offset += simple_type_size_in_bits (type);
9260 bit_offset
9261 = (! BYTES_BIG_ENDIAN
9262 ? highest_order_object_bit_offset - highest_order_field_bit_offset
9263 : highest_order_field_bit_offset - highest_order_object_bit_offset);
9265 add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
9268 /* For a FIELD_DECL node which represents a bit field, output an attribute
9269 which specifies the length in bits of the given field. */
9271 static inline void
9272 add_bit_size_attribute (die, decl)
9273 register dw_die_ref die;
9274 register tree decl;
9276 /* Must be a field and a bit field. */
9277 if (TREE_CODE (decl) != FIELD_DECL
9278 || ! DECL_BIT_FIELD_TYPE (decl))
9279 abort ();
9281 if (host_integerp (DECL_SIZE (decl), 1))
9282 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
9285 /* If the compiled language is ANSI C, then add a 'prototyped'
9286 attribute, if arg types are given for the parameters of a function. */
9288 static inline void
9289 add_prototyped_attribute (die, func_type)
9290 register dw_die_ref die;
9291 register tree func_type;
9293 if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
9294 && TYPE_ARG_TYPES (func_type) != NULL)
9295 add_AT_flag (die, DW_AT_prototyped, 1);
9298 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
9299 by looking in either the type declaration or object declaration
9300 equate table. */
9302 static inline void
9303 add_abstract_origin_attribute (die, origin)
9304 register dw_die_ref die;
9305 register tree origin;
9307 dw_die_ref origin_die = NULL;
9309 if (TREE_CODE (origin) != FUNCTION_DECL)
9311 /* We may have gotten separated from the block for the inlined
9312 function, if we're in an exception handler or some such; make
9313 sure that the abstract function has been written out.
9315 Doing this for nested functions is wrong, however; functions are
9316 distinct units, and our context might not even be inline. */
9317 tree fn = origin;
9318 if (TYPE_P (fn))
9319 fn = TYPE_STUB_DECL (fn);
9320 fn = decl_function_context (fn);
9321 if (fn)
9322 dwarf2out_abstract_function (fn);
9325 if (DECL_P (origin))
9326 origin_die = lookup_decl_die (origin);
9327 else if (TYPE_P (origin))
9328 origin_die = lookup_type_die (origin);
9330 if (origin_die == NULL)
9331 abort ();
9333 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
9336 /* We do not currently support the pure_virtual attribute. */
9338 static inline void
9339 add_pure_or_virtual_attribute (die, func_decl)
9340 register dw_die_ref die;
9341 register tree func_decl;
9343 if (DECL_VINDEX (func_decl))
9345 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
9347 if (host_integerp (DECL_VINDEX (func_decl), 0))
9348 add_AT_loc (die, DW_AT_vtable_elem_location,
9349 new_loc_descr (DW_OP_constu,
9350 tree_low_cst (DECL_VINDEX (func_decl), 0),
9351 0));
9353 /* GNU extension: Record what type this method came from originally. */
9354 if (debug_info_level > DINFO_LEVEL_TERSE)
9355 add_AT_die_ref (die, DW_AT_containing_type,
9356 lookup_type_die (DECL_CONTEXT (func_decl)));
9360 /* Add source coordinate attributes for the given decl. */
9362 static void
9363 add_src_coords_attributes (die, decl)
9364 register dw_die_ref die;
9365 register tree decl;
9367 register unsigned file_index = lookup_filename (&decl_file_table,
9368 DECL_SOURCE_FILE (decl));
9370 add_AT_unsigned (die, DW_AT_decl_file, file_index);
9371 add_AT_unsigned (die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
9374 /* Add an DW_AT_name attribute and source coordinate attribute for the
9375 given decl, but only if it actually has a name. */
9377 static void
9378 add_name_and_src_coords_attributes (die, decl)
9379 register dw_die_ref die;
9380 register tree decl;
9382 register tree decl_name;
9384 decl_name = DECL_NAME (decl);
9385 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
9387 add_name_attribute (die, dwarf2_name (decl, 0));
9388 if (! DECL_ARTIFICIAL (decl))
9389 add_src_coords_attributes (die, decl);
9391 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
9392 && TREE_PUBLIC (decl)
9393 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
9394 add_AT_string (die, DW_AT_MIPS_linkage_name,
9395 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
9399 /* Push a new declaration scope. */
9401 static void
9402 push_decl_scope (scope)
9403 tree scope;
9405 /* Make room in the decl_scope_table, if necessary. */
9406 if (decl_scope_table_allocated == decl_scope_depth)
9408 decl_scope_table_allocated += DECL_SCOPE_TABLE_INCREMENT;
9409 decl_scope_table
9410 = (tree *) xrealloc (decl_scope_table,
9411 decl_scope_table_allocated * sizeof (tree));
9414 decl_scope_table[decl_scope_depth] = scope;
9415 decl_scope_depth++;
9418 /* Pop a declaration scope. */
9419 static inline void
9420 pop_decl_scope ()
9422 if (decl_scope_depth <= 0)
9423 abort ();
9424 --decl_scope_depth;
9427 /* Return the DIE for the scope that immediately contains this type.
9428 Non-named types get global scope. Named types nested in other
9429 types get their containing scope if it's open, or global scope
9430 otherwise. All other types (i.e. function-local named types) get
9431 the current active scope. */
9433 static dw_die_ref
9434 scope_die_for (t, context_die)
9435 register tree t;
9436 register dw_die_ref context_die;
9438 register dw_die_ref scope_die = NULL;
9439 register tree containing_scope;
9440 register int i;
9442 /* Non-types always go in the current scope. */
9443 if (! TYPE_P (t))
9444 abort ();
9446 containing_scope = TYPE_CONTEXT (t);
9448 /* Ignore namespaces for the moment. */
9449 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
9450 containing_scope = NULL_TREE;
9452 /* Ignore function type "scopes" from the C frontend. They mean that
9453 a tagged type is local to a parmlist of a function declarator, but
9454 that isn't useful to DWARF. */
9455 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
9456 containing_scope = NULL_TREE;
9458 if (containing_scope == NULL_TREE)
9459 scope_die = comp_unit_die;
9460 else if (TYPE_P (containing_scope))
9462 /* For types, we can just look up the appropriate DIE. But
9463 first we check to see if we're in the middle of emitting it
9464 so we know where the new DIE should go. */
9466 for (i = decl_scope_depth - 1; i >= 0; --i)
9467 if (decl_scope_table[i] == containing_scope)
9468 break;
9470 if (i < 0)
9472 if (debug_info_level > DINFO_LEVEL_TERSE
9473 && !TREE_ASM_WRITTEN (containing_scope))
9474 abort ();
9476 /* If none of the current dies are suitable, we get file scope. */
9477 scope_die = comp_unit_die;
9479 else
9480 scope_die = lookup_type_die (containing_scope);
9482 else
9483 scope_die = context_die;
9485 return scope_die;
9488 /* Returns nonzero iff CONTEXT_DIE is internal to a function. */
9490 static inline int local_scope_p PARAMS ((dw_die_ref));
9491 static inline int
9492 local_scope_p (context_die)
9493 dw_die_ref context_die;
9495 for (; context_die; context_die = context_die->die_parent)
9496 if (context_die->die_tag == DW_TAG_inlined_subroutine
9497 || context_die->die_tag == DW_TAG_subprogram)
9498 return 1;
9499 return 0;
9502 /* Returns nonzero iff CONTEXT_DIE is a class. */
9504 static inline int class_scope_p PARAMS ((dw_die_ref));
9505 static inline int
9506 class_scope_p (context_die)
9507 dw_die_ref context_die;
9509 return (context_die
9510 && (context_die->die_tag == DW_TAG_structure_type
9511 || context_die->die_tag == DW_TAG_union_type));
9514 /* Many forms of DIEs require a "type description" attribute. This
9515 routine locates the proper "type descriptor" die for the type given
9516 by 'type', and adds an DW_AT_type attribute below the given die. */
9518 static void
9519 add_type_attribute (object_die, type, decl_const, decl_volatile, context_die)
9520 register dw_die_ref object_die;
9521 register tree type;
9522 register int decl_const;
9523 register int decl_volatile;
9524 register dw_die_ref context_die;
9526 register enum tree_code code = TREE_CODE (type);
9527 register dw_die_ref type_die = NULL;
9529 /* ??? If this type is an unnamed subrange type of an integral or
9530 floating-point type, use the inner type. This is because we have no
9531 support for unnamed types in base_type_die. This can happen if this is
9532 an Ada subrange type. Correct solution is emit a subrange type die. */
9533 if ((code == INTEGER_TYPE || code == REAL_TYPE)
9534 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
9535 type = TREE_TYPE (type), code = TREE_CODE (type);
9537 if (code == ERROR_MARK)
9538 return;
9540 /* Handle a special case. For functions whose return type is void, we
9541 generate *no* type attribute. (Note that no object may have type
9542 `void', so this only applies to function return types). */
9543 if (code == VOID_TYPE)
9544 return;
9546 type_die = modified_type_die (type,
9547 decl_const || TYPE_READONLY (type),
9548 decl_volatile || TYPE_VOLATILE (type),
9549 context_die);
9550 if (type_die != NULL)
9551 add_AT_die_ref (object_die, DW_AT_type, type_die);
9554 /* Given a tree pointer to a struct, class, union, or enum type node, return
9555 a pointer to the (string) tag name for the given type, or zero if the type
9556 was declared without a tag. */
9558 static const char *
9559 type_tag (type)
9560 register tree type;
9562 register const char *name = 0;
9564 if (TYPE_NAME (type) != 0)
9566 register tree t = 0;
9568 /* Find the IDENTIFIER_NODE for the type name. */
9569 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
9570 t = TYPE_NAME (type);
9572 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
9573 a TYPE_DECL node, regardless of whether or not a `typedef' was
9574 involved. */
9575 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9576 && ! DECL_IGNORED_P (TYPE_NAME (type)))
9577 t = DECL_NAME (TYPE_NAME (type));
9579 /* Now get the name as a string, or invent one. */
9580 if (t != 0)
9581 name = IDENTIFIER_POINTER (t);
9584 return (name == 0 || *name == '\0') ? 0 : name;
9587 /* Return the type associated with a data member, make a special check
9588 for bit field types. */
9590 static inline tree
9591 member_declared_type (member)
9592 register tree member;
9594 return (DECL_BIT_FIELD_TYPE (member)
9595 ? DECL_BIT_FIELD_TYPE (member)
9596 : TREE_TYPE (member));
9599 /* Get the decl's label, as described by its RTL. This may be different
9600 from the DECL_NAME name used in the source file. */
9602 #if 0
9603 static const char *
9604 decl_start_label (decl)
9605 register tree decl;
9607 rtx x;
9608 const char *fnname;
9609 x = DECL_RTL (decl);
9610 if (GET_CODE (x) != MEM)
9611 abort ();
9613 x = XEXP (x, 0);
9614 if (GET_CODE (x) != SYMBOL_REF)
9615 abort ();
9617 fnname = XSTR (x, 0);
9618 return fnname;
9620 #endif
9622 /* These routines generate the internal representation of the DIE's for
9623 the compilation unit. Debugging information is collected by walking
9624 the declaration trees passed in from dwarf2out_decl(). */
9626 static void
9627 gen_array_type_die (type, context_die)
9628 register tree type;
9629 register dw_die_ref context_die;
9631 register dw_die_ref scope_die = scope_die_for (type, context_die);
9632 register dw_die_ref array_die;
9633 register tree element_type;
9635 /* ??? The SGI dwarf reader fails for array of array of enum types unless
9636 the inner array type comes before the outer array type. Thus we must
9637 call gen_type_die before we call new_die. See below also. */
9638 #ifdef MIPS_DEBUGGING_INFO
9639 gen_type_die (TREE_TYPE (type), context_die);
9640 #endif
9642 array_die = new_die (DW_TAG_array_type, scope_die);
9644 #if 0
9645 /* We default the array ordering. SDB will probably do
9646 the right things even if DW_AT_ordering is not present. It's not even
9647 an issue until we start to get into multidimensional arrays anyway. If
9648 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
9649 then we'll have to put the DW_AT_ordering attribute back in. (But if
9650 and when we find out that we need to put these in, we will only do so
9651 for multidimensional arrays. */
9652 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
9653 #endif
9655 #ifdef MIPS_DEBUGGING_INFO
9656 /* The SGI compilers handle arrays of unknown bound by setting
9657 AT_declaration and not emitting any subrange DIEs. */
9658 if (! TYPE_DOMAIN (type))
9659 add_AT_unsigned (array_die, DW_AT_declaration, 1);
9660 else
9661 #endif
9662 add_subscript_info (array_die, type);
9664 add_name_attribute (array_die, type_tag (type));
9665 equate_type_number_to_die (type, array_die);
9667 /* Add representation of the type of the elements of this array type. */
9668 element_type = TREE_TYPE (type);
9670 /* ??? The SGI dwarf reader fails for multidimensional arrays with a
9671 const enum type. E.g. const enum machine_mode insn_operand_mode[2][10].
9672 We work around this by disabling this feature. See also
9673 add_subscript_info. */
9674 #ifndef MIPS_DEBUGGING_INFO
9675 while (TREE_CODE (element_type) == ARRAY_TYPE)
9676 element_type = TREE_TYPE (element_type);
9678 gen_type_die (element_type, context_die);
9679 #endif
9681 add_type_attribute (array_die, element_type, 0, 0, context_die);
9684 static void
9685 gen_set_type_die (type, context_die)
9686 register tree type;
9687 register dw_die_ref context_die;
9689 register dw_die_ref type_die
9690 = new_die (DW_TAG_set_type, scope_die_for (type, context_die));
9692 equate_type_number_to_die (type, type_die);
9693 add_type_attribute (type_die, TREE_TYPE (type), 0, 0, context_die);
9696 #if 0
9697 static void
9698 gen_entry_point_die (decl, context_die)
9699 register tree decl;
9700 register dw_die_ref context_die;
9702 register tree origin = decl_ultimate_origin (decl);
9703 register dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die);
9704 if (origin != NULL)
9705 add_abstract_origin_attribute (decl_die, origin);
9706 else
9708 add_name_and_src_coords_attributes (decl_die, decl);
9709 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
9710 0, 0, context_die);
9713 if (DECL_ABSTRACT (decl))
9714 equate_decl_number_to_die (decl, decl_die);
9715 else
9716 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
9718 #endif
9720 /* Remember a type in the incomplete_types_list. */
9722 static void
9723 add_incomplete_type (type)
9724 tree type;
9726 if (incomplete_types == incomplete_types_allocated)
9728 incomplete_types_allocated += INCOMPLETE_TYPES_INCREMENT;
9729 incomplete_types_list
9730 = (tree *) xrealloc (incomplete_types_list,
9731 sizeof (tree) * incomplete_types_allocated);
9734 incomplete_types_list[incomplete_types++] = type;
9737 /* Walk through the list of incomplete types again, trying once more to
9738 emit full debugging info for them. */
9740 static void
9741 retry_incomplete_types ()
9743 register tree type;
9745 while (incomplete_types)
9747 --incomplete_types;
9748 type = incomplete_types_list[incomplete_types];
9749 gen_type_die (type, comp_unit_die);
9753 /* Generate a DIE to represent an inlined instance of an enumeration type. */
9755 static void
9756 gen_inlined_enumeration_type_die (type, context_die)
9757 register tree type;
9758 register dw_die_ref context_die;
9760 register dw_die_ref type_die = new_die (DW_TAG_enumeration_type,
9761 context_die);
9762 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
9763 be incomplete and such types are not marked. */
9764 add_abstract_origin_attribute (type_die, type);
9767 /* Generate a DIE to represent an inlined instance of a structure type. */
9769 static void
9770 gen_inlined_structure_type_die (type, context_die)
9771 register tree type;
9772 register dw_die_ref context_die;
9774 register dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die);
9776 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
9777 be incomplete and such types are not marked. */
9778 add_abstract_origin_attribute (type_die, type);
9781 /* Generate a DIE to represent an inlined instance of a union type. */
9783 static void
9784 gen_inlined_union_type_die (type, context_die)
9785 register tree type;
9786 register dw_die_ref context_die;
9788 register dw_die_ref type_die = new_die (DW_TAG_union_type, context_die);
9790 /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
9791 be incomplete and such types are not marked. */
9792 add_abstract_origin_attribute (type_die, type);
9795 /* Generate a DIE to represent an enumeration type. Note that these DIEs
9796 include all of the information about the enumeration values also. Each
9797 enumerated type name/value is listed as a child of the enumerated type
9798 DIE. */
9800 static void
9801 gen_enumeration_type_die (type, context_die)
9802 register tree type;
9803 register dw_die_ref context_die;
9805 register dw_die_ref type_die = lookup_type_die (type);
9807 if (type_die == NULL)
9809 type_die = new_die (DW_TAG_enumeration_type,
9810 scope_die_for (type, context_die));
9811 equate_type_number_to_die (type, type_die);
9812 add_name_attribute (type_die, type_tag (type));
9814 else if (! TYPE_SIZE (type))
9815 return;
9816 else
9817 remove_AT (type_die, DW_AT_declaration);
9819 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
9820 given enum type is incomplete, do not generate the DW_AT_byte_size
9821 attribute or the DW_AT_element_list attribute. */
9822 if (TYPE_SIZE (type))
9824 register tree link;
9826 TREE_ASM_WRITTEN (type) = 1;
9827 add_byte_size_attribute (type_die, type);
9828 if (TYPE_STUB_DECL (type) != NULL_TREE)
9829 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
9831 /* If the first reference to this type was as the return type of an
9832 inline function, then it may not have a parent. Fix this now. */
9833 if (type_die->die_parent == NULL)
9834 add_child_die (scope_die_for (type, context_die), type_die);
9836 for (link = TYPE_FIELDS (type);
9837 link != NULL; link = TREE_CHAIN (link))
9839 register dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die);
9841 add_name_attribute (enum_die,
9842 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
9844 if (host_integerp (TREE_VALUE (link), 0))
9846 if (tree_int_cst_sgn (TREE_VALUE (link)) < 0)
9847 add_AT_int (enum_die, DW_AT_const_value,
9848 tree_low_cst (TREE_VALUE (link), 0));
9849 else
9850 add_AT_unsigned (enum_die, DW_AT_const_value,
9851 tree_low_cst (TREE_VALUE (link), 0));
9855 else
9856 add_AT_flag (type_die, DW_AT_declaration, 1);
9859 /* Generate a DIE to represent either a real live formal parameter decl or to
9860 represent just the type of some formal parameter position in some function
9861 type.
9863 Note that this routine is a bit unusual because its argument may be a
9864 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
9865 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
9866 node. If it's the former then this function is being called to output a
9867 DIE to represent a formal parameter object (or some inlining thereof). If
9868 it's the latter, then this function is only being called to output a
9869 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
9870 argument type of some subprogram type. */
9872 static dw_die_ref
9873 gen_formal_parameter_die (node, context_die)
9874 register tree node;
9875 register dw_die_ref context_die;
9877 register dw_die_ref parm_die
9878 = new_die (DW_TAG_formal_parameter, context_die);
9879 register tree origin;
9881 switch (TREE_CODE_CLASS (TREE_CODE (node)))
9883 case 'd':
9884 origin = decl_ultimate_origin (node);
9885 if (origin != NULL)
9886 add_abstract_origin_attribute (parm_die, origin);
9887 else
9889 add_name_and_src_coords_attributes (parm_die, node);
9890 add_type_attribute (parm_die, TREE_TYPE (node),
9891 TREE_READONLY (node),
9892 TREE_THIS_VOLATILE (node),
9893 context_die);
9894 if (DECL_ARTIFICIAL (node))
9895 add_AT_flag (parm_die, DW_AT_artificial, 1);
9898 equate_decl_number_to_die (node, parm_die);
9899 if (! DECL_ABSTRACT (node))
9900 add_location_or_const_value_attribute (parm_die, node);
9902 break;
9904 case 't':
9905 /* We were called with some kind of a ..._TYPE node. */
9906 add_type_attribute (parm_die, node, 0, 0, context_die);
9907 break;
9909 default:
9910 abort ();
9913 return parm_die;
9916 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
9917 at the end of an (ANSI prototyped) formal parameters list. */
9919 static void
9920 gen_unspecified_parameters_die (decl_or_type, context_die)
9921 register tree decl_or_type ATTRIBUTE_UNUSED;
9922 register dw_die_ref context_die;
9924 new_die (DW_TAG_unspecified_parameters, context_die);
9927 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
9928 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
9929 parameters as specified in some function type specification (except for
9930 those which appear as part of a function *definition*). */
9932 static void
9933 gen_formal_types_die (function_or_method_type, context_die)
9934 register tree function_or_method_type;
9935 register dw_die_ref context_die;
9937 register tree link;
9938 register tree formal_type = NULL;
9939 register tree first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
9941 #if 0
9942 /* In the case where we are generating a formal types list for a C++
9943 non-static member function type, skip over the first thing on the
9944 TYPE_ARG_TYPES list because it only represents the type of the hidden
9945 `this pointer'. The debugger should be able to figure out (without
9946 being explicitly told) that this non-static member function type takes a
9947 `this pointer' and should be able to figure what the type of that hidden
9948 parameter is from the DW_AT_member attribute of the parent
9949 DW_TAG_subroutine_type DIE. */
9950 if (TREE_CODE (function_or_method_type) == METHOD_TYPE)
9951 first_parm_type = TREE_CHAIN (first_parm_type);
9952 #endif
9954 /* Make our first pass over the list of formal parameter types and output a
9955 DW_TAG_formal_parameter DIE for each one. */
9956 for (link = first_parm_type; link; link = TREE_CHAIN (link))
9958 register dw_die_ref parm_die;
9960 formal_type = TREE_VALUE (link);
9961 if (formal_type == void_type_node)
9962 break;
9964 /* Output a (nameless) DIE to represent the formal parameter itself. */
9965 parm_die = gen_formal_parameter_die (formal_type, context_die);
9966 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
9967 && link == first_parm_type)
9968 add_AT_flag (parm_die, DW_AT_artificial, 1);
9971 /* If this function type has an ellipsis, add a
9972 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
9973 if (formal_type != void_type_node)
9974 gen_unspecified_parameters_die (function_or_method_type, context_die);
9976 /* Make our second (and final) pass over the list of formal parameter types
9977 and output DIEs to represent those types (as necessary). */
9978 for (link = TYPE_ARG_TYPES (function_or_method_type);
9979 link;
9980 link = TREE_CHAIN (link))
9982 formal_type = TREE_VALUE (link);
9983 if (formal_type == void_type_node)
9984 break;
9986 gen_type_die (formal_type, context_die);
9990 /* We want to generate the DIE for TYPE so that we can generate the
9991 die for MEMBER, which has been defined; we will need to refer back
9992 to the member declaration nested within TYPE. If we're trying to
9993 generate minimal debug info for TYPE, processing TYPE won't do the
9994 trick; we need to attach the member declaration by hand. */
9996 static void
9997 gen_type_die_for_member (type, member, context_die)
9998 tree type, member;
9999 dw_die_ref context_die;
10001 gen_type_die (type, context_die);
10003 /* If we're trying to avoid duplicate debug info, we may not have
10004 emitted the member decl for this function. Emit it now. */
10005 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
10006 && ! lookup_decl_die (member))
10008 if (decl_ultimate_origin (member))
10009 abort ();
10011 push_decl_scope (type);
10012 if (TREE_CODE (member) == FUNCTION_DECL)
10013 gen_subprogram_die (member, lookup_type_die (type));
10014 else
10015 gen_variable_die (member, lookup_type_die (type));
10016 pop_decl_scope ();
10020 /* Generate the DWARF2 info for the "abstract" instance
10021 of a function which we may later generate inlined and/or
10022 out-of-line instances of. */
10024 void
10025 dwarf2out_abstract_function (decl)
10026 tree decl;
10028 register dw_die_ref old_die = lookup_decl_die (decl);
10029 tree save_fn;
10031 if (old_die && get_AT_unsigned (old_die, DW_AT_inline))
10032 /* We've already generated the abstract instance. */
10033 return;
10035 save_fn = current_function_decl;
10036 current_function_decl = decl;
10038 set_decl_abstract_flags (decl, 1);
10039 dwarf2out_decl (decl);
10040 set_decl_abstract_flags (decl, 0);
10042 current_function_decl = save_fn;
10045 /* Generate a DIE to represent a declared function (either file-scope or
10046 block-local). */
10048 static void
10049 gen_subprogram_die (decl, context_die)
10050 register tree decl;
10051 register dw_die_ref context_die;
10053 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10054 register tree origin = decl_ultimate_origin (decl);
10055 register dw_die_ref subr_die;
10056 register rtx fp_reg;
10057 register tree fn_arg_types;
10058 register tree outer_scope;
10059 register dw_die_ref old_die = lookup_decl_die (decl);
10060 register int declaration = (current_function_decl != decl
10061 || class_scope_p (context_die));
10063 /* Note that it is possible to have both DECL_ABSTRACT and `declaration'
10064 be true, if we started to generate the abstract instance of an inline,
10065 decided to output its containing class, and proceeded to emit the
10066 declaration of the inline from the member list for the class. In that
10067 case, `declaration' takes priority; we'll get back to the abstract
10068 instance when we're done with the class. */
10070 /* The class-scope declaration DIE must be the primary DIE. */
10071 if (origin && declaration && class_scope_p (context_die))
10073 origin = NULL;
10074 if (old_die)
10075 abort ();
10078 if (origin != NULL)
10080 if (declaration && ! local_scope_p (context_die))
10081 abort ();
10083 /* Fixup die_parent for the abstract instance of a nested
10084 inline function. */
10085 if (old_die && old_die->die_parent == NULL)
10086 add_child_die (context_die, old_die);
10088 subr_die = new_die (DW_TAG_subprogram, context_die);
10089 add_abstract_origin_attribute (subr_die, origin);
10091 else if (old_die && DECL_ABSTRACT (decl)
10092 && get_AT_unsigned (old_die, DW_AT_inline))
10094 /* This must be a redefinition of an extern inline function.
10095 We can just reuse the old die here. */
10096 subr_die = old_die;
10098 /* Clear out the inlined attribute and parm types. */
10099 remove_AT (subr_die, DW_AT_inline);
10100 remove_children (subr_die);
10102 else if (old_die)
10104 register unsigned file_index
10105 = lookup_filename (&decl_file_table, DECL_SOURCE_FILE (decl));
10107 if (!get_AT_flag (old_die, DW_AT_declaration)
10108 /* We can have a normal definition following an inline one in the
10109 case of redefinition of GNU C extern inlines.
10110 It seems reasonable to use AT_specification in this case. */
10111 && !get_AT_unsigned (old_die, DW_AT_inline))
10113 /* ??? This can happen if there is a bug in the program, for
10114 instance, if it has duplicate function definitions. Ideally,
10115 we should detect this case and ignore it. For now, if we have
10116 already reported an error, any error at all, then assume that
10117 we got here because of a input error, not a dwarf2 bug. */
10118 if (errorcount)
10119 return;
10120 abort ();
10123 /* If the definition comes from the same place as the declaration,
10124 maybe use the old DIE. We always want the DIE for this function
10125 that has the *_pc attributes to be under comp_unit_die so the
10126 debugger can find it. We also need to do this for abstract
10127 instances of inlines, since the spec requires the out-of-line copy
10128 to have the same parent. For local class methods, this doesn't
10129 apply; we just use the old DIE. */
10130 if ((old_die->die_parent == comp_unit_die || context_die == NULL)
10131 && (DECL_ARTIFICIAL (decl)
10132 || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
10133 && (get_AT_unsigned (old_die, DW_AT_decl_line)
10134 == (unsigned) DECL_SOURCE_LINE (decl)))))
10136 subr_die = old_die;
10138 /* Clear out the declaration attribute and the parm types. */
10139 remove_AT (subr_die, DW_AT_declaration);
10140 remove_children (subr_die);
10142 else
10144 subr_die = new_die (DW_TAG_subprogram, context_die);
10145 add_AT_die_ref (subr_die, DW_AT_specification, old_die);
10146 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
10147 add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
10148 if (get_AT_unsigned (old_die, DW_AT_decl_line)
10149 != (unsigned) DECL_SOURCE_LINE (decl))
10150 add_AT_unsigned
10151 (subr_die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
10154 else
10156 subr_die = new_die (DW_TAG_subprogram, context_die);
10158 if (TREE_PUBLIC (decl))
10159 add_AT_flag (subr_die, DW_AT_external, 1);
10161 add_name_and_src_coords_attributes (subr_die, decl);
10162 if (debug_info_level > DINFO_LEVEL_TERSE)
10164 register tree type = TREE_TYPE (decl);
10166 add_prototyped_attribute (subr_die, type);
10167 add_type_attribute (subr_die, TREE_TYPE (type), 0, 0, context_die);
10170 add_pure_or_virtual_attribute (subr_die, decl);
10171 if (DECL_ARTIFICIAL (decl))
10172 add_AT_flag (subr_die, DW_AT_artificial, 1);
10173 if (TREE_PROTECTED (decl))
10174 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
10175 else if (TREE_PRIVATE (decl))
10176 add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
10179 if (declaration)
10181 if (!(old_die && get_AT_unsigned (old_die, DW_AT_inline)))
10183 add_AT_flag (subr_die, DW_AT_declaration, 1);
10185 /* The first time we see a member function, it is in the context of
10186 the class to which it belongs. We make sure of this by emitting
10187 the class first. The next time is the definition, which is
10188 handled above. The two may come from the same source text. */
10189 if (DECL_CONTEXT (decl) || DECL_ABSTRACT (decl))
10190 equate_decl_number_to_die (decl, subr_die);
10193 else if (DECL_ABSTRACT (decl))
10195 if (DECL_INLINE (decl) && !flag_no_inline)
10197 /* ??? Checking DECL_DEFER_OUTPUT is correct for static
10198 inline functions, but not for extern inline functions.
10199 We can't get this completely correct because information
10200 about whether the function was declared inline is not
10201 saved anywhere. */
10202 if (DECL_DEFER_OUTPUT (decl))
10203 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
10204 else
10205 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
10207 else
10208 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
10210 equate_decl_number_to_die (decl, subr_die);
10212 else if (!DECL_EXTERNAL (decl))
10214 if (!(old_die && get_AT_unsigned (old_die, DW_AT_inline)))
10215 equate_decl_number_to_die (decl, subr_die);
10217 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
10218 current_funcdef_number);
10219 add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
10220 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10221 current_funcdef_number);
10222 add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
10224 add_pubname (decl, subr_die);
10225 add_arange (decl, subr_die);
10227 #ifdef MIPS_DEBUGGING_INFO
10228 /* Add a reference to the FDE for this routine. */
10229 add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
10230 #endif
10232 /* Define the "frame base" location for this routine. We use the
10233 frame pointer or stack pointer registers, since the RTL for local
10234 variables is relative to one of them. */
10235 fp_reg
10236 = frame_pointer_needed ? hard_frame_pointer_rtx : stack_pointer_rtx;
10237 add_AT_loc (subr_die, DW_AT_frame_base, reg_loc_descriptor (fp_reg));
10239 #if 0
10240 /* ??? This fails for nested inline functions, because context_display
10241 is not part of the state saved/restored for inline functions. */
10242 if (current_function_needs_context)
10243 add_AT_location_description (subr_die, DW_AT_static_link,
10244 lookup_static_chain (decl));
10245 #endif
10248 /* Now output descriptions of the arguments for this function. This gets
10249 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
10250 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
10251 `...' at the end of the formal parameter list. In order to find out if
10252 there was a trailing ellipsis or not, we must instead look at the type
10253 associated with the FUNCTION_DECL. This will be a node of type
10254 FUNCTION_TYPE. If the chain of type nodes hanging off of this
10255 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
10256 an ellipsis at the end. */
10258 /* In the case where we are describing a mere function declaration, all we
10259 need to do here (and all we *can* do here) is to describe the *types* of
10260 its formal parameters. */
10261 if (debug_info_level <= DINFO_LEVEL_TERSE)
10263 else if (declaration)
10264 gen_formal_types_die (TREE_TYPE (decl), subr_die);
10265 else
10267 /* Generate DIEs to represent all known formal parameters */
10268 register tree arg_decls = DECL_ARGUMENTS (decl);
10269 register tree parm;
10271 /* When generating DIEs, generate the unspecified_parameters DIE
10272 instead if we come across the arg "__builtin_va_alist" */
10273 for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
10274 if (TREE_CODE (parm) == PARM_DECL)
10276 if (DECL_NAME (parm)
10277 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
10278 "__builtin_va_alist"))
10279 gen_unspecified_parameters_die (parm, subr_die);
10280 else
10281 gen_decl_die (parm, subr_die);
10284 /* Decide whether we need a unspecified_parameters DIE at the end.
10285 There are 2 more cases to do this for: 1) the ansi ... declaration -
10286 this is detectable when the end of the arg list is not a
10287 void_type_node 2) an unprototyped function declaration (not a
10288 definition). This just means that we have no info about the
10289 parameters at all. */
10290 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
10291 if (fn_arg_types != NULL)
10293 /* this is the prototyped case, check for ... */
10294 if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
10295 gen_unspecified_parameters_die (decl, subr_die);
10297 else if (DECL_INITIAL (decl) == NULL_TREE)
10298 gen_unspecified_parameters_die (decl, subr_die);
10301 /* Output Dwarf info for all of the stuff within the body of the function
10302 (if it has one - it may be just a declaration). */
10303 outer_scope = DECL_INITIAL (decl);
10305 /* Note that here, `outer_scope' is a pointer to the outermost BLOCK
10306 node created to represent a function. This outermost BLOCK actually
10307 represents the outermost binding contour for the function, i.e. the
10308 contour in which the function's formal parameters and labels get
10309 declared. Curiously, it appears that the front end doesn't actually
10310 put the PARM_DECL nodes for the current function onto the BLOCK_VARS
10311 list for this outer scope. (They are strung off of the DECL_ARGUMENTS
10312 list for the function instead.) The BLOCK_VARS list for the
10313 `outer_scope' does provide us with a list of the LABEL_DECL nodes for
10314 the function however, and we output DWARF info for those in
10315 decls_for_scope. Just within the `outer_scope' there will be a BLOCK
10316 node representing the function's outermost pair of curly braces, and
10317 any blocks used for the base and member initializers of a C++
10318 constructor function. */
10319 if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
10321 current_function_has_inlines = 0;
10322 decls_for_scope (outer_scope, subr_die, 0);
10324 #if 0 && defined (MIPS_DEBUGGING_INFO)
10325 if (current_function_has_inlines)
10327 add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
10328 if (! comp_unit_has_inlines)
10330 add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
10331 comp_unit_has_inlines = 1;
10334 #endif
10338 /* Generate a DIE to represent a declared data object. */
10340 static void
10341 gen_variable_die (decl, context_die)
10342 register tree decl;
10343 register dw_die_ref context_die;
10345 register tree origin = decl_ultimate_origin (decl);
10346 register dw_die_ref var_die = new_die (DW_TAG_variable, context_die);
10348 dw_die_ref old_die = lookup_decl_die (decl);
10349 int declaration = (DECL_EXTERNAL (decl)
10350 || class_scope_p (context_die));
10352 if (origin != NULL)
10353 add_abstract_origin_attribute (var_die, origin);
10354 /* Loop unrolling can create multiple blocks that refer to the same
10355 static variable, so we must test for the DW_AT_declaration flag. */
10356 /* ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
10357 copy decls and set the DECL_ABSTRACT flag on them instead of
10358 sharing them. */
10359 else if (old_die && TREE_STATIC (decl)
10360 && get_AT_flag (old_die, DW_AT_declaration) == 1)
10362 /* This is a definition of a C++ class level static. */
10363 add_AT_die_ref (var_die, DW_AT_specification, old_die);
10364 if (DECL_NAME (decl))
10366 register unsigned file_index
10367 = lookup_filename (&decl_file_table, DECL_SOURCE_FILE (decl));
10369 if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
10370 add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
10372 if (get_AT_unsigned (old_die, DW_AT_decl_line)
10373 != (unsigned) DECL_SOURCE_LINE (decl))
10375 add_AT_unsigned (var_die, DW_AT_decl_line,
10376 DECL_SOURCE_LINE (decl));
10379 else
10381 add_name_and_src_coords_attributes (var_die, decl);
10382 add_type_attribute (var_die, TREE_TYPE (decl),
10383 TREE_READONLY (decl),
10384 TREE_THIS_VOLATILE (decl), context_die);
10386 if (TREE_PUBLIC (decl))
10387 add_AT_flag (var_die, DW_AT_external, 1);
10389 if (DECL_ARTIFICIAL (decl))
10390 add_AT_flag (var_die, DW_AT_artificial, 1);
10392 if (TREE_PROTECTED (decl))
10393 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
10395 else if (TREE_PRIVATE (decl))
10396 add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
10399 if (declaration)
10400 add_AT_flag (var_die, DW_AT_declaration, 1);
10402 if (class_scope_p (context_die) || DECL_ABSTRACT (decl))
10403 equate_decl_number_to_die (decl, var_die);
10405 if (! declaration && ! DECL_ABSTRACT (decl))
10407 add_location_or_const_value_attribute (var_die, decl);
10408 add_pubname (decl, var_die);
10410 else
10411 tree_add_const_value_attribute (var_die, decl);
10414 /* Generate a DIE to represent a label identifier. */
10416 static void
10417 gen_label_die (decl, context_die)
10418 register tree decl;
10419 register dw_die_ref context_die;
10421 register tree origin = decl_ultimate_origin (decl);
10422 register dw_die_ref lbl_die = new_die (DW_TAG_label, context_die);
10423 register rtx insn;
10424 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10426 if (origin != NULL)
10427 add_abstract_origin_attribute (lbl_die, origin);
10428 else
10429 add_name_and_src_coords_attributes (lbl_die, decl);
10431 if (DECL_ABSTRACT (decl))
10432 equate_decl_number_to_die (decl, lbl_die);
10433 else
10435 insn = DECL_RTL (decl);
10437 /* Deleted labels are programmer specified labels which have been
10438 eliminated because of various optimisations. We still emit them
10439 here so that it is possible to put breakpoints on them. */
10440 if (GET_CODE (insn) == CODE_LABEL
10441 || ((GET_CODE (insn) == NOTE
10442 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL)))
10444 /* When optimization is enabled (via -O) some parts of the compiler
10445 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
10446 represent source-level labels which were explicitly declared by
10447 the user. This really shouldn't be happening though, so catch
10448 it if it ever does happen. */
10449 if (INSN_DELETED_P (insn))
10450 abort ();
10452 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
10453 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
10458 /* Generate a DIE for a lexical block. */
10460 static void
10461 gen_lexical_block_die (stmt, context_die, depth)
10462 register tree stmt;
10463 register dw_die_ref context_die;
10464 int depth;
10466 register dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die);
10467 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10469 if (! BLOCK_ABSTRACT (stmt))
10471 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
10472 BLOCK_NUMBER (stmt));
10473 add_AT_lbl_id (stmt_die, DW_AT_low_pc, label);
10474 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
10475 BLOCK_NUMBER (stmt));
10476 add_AT_lbl_id (stmt_die, DW_AT_high_pc, label);
10479 decls_for_scope (stmt, stmt_die, depth);
10482 /* Generate a DIE for an inlined subprogram. */
10484 static void
10485 gen_inlined_subroutine_die (stmt, context_die, depth)
10486 register tree stmt;
10487 register dw_die_ref context_die;
10488 int depth;
10490 if (! BLOCK_ABSTRACT (stmt))
10492 register dw_die_ref subr_die
10493 = new_die (DW_TAG_inlined_subroutine, context_die);
10494 register tree decl = block_ultimate_origin (stmt);
10495 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10497 /* Emit info for the abstract instance first, if we haven't yet. */
10498 dwarf2out_abstract_function (decl);
10500 add_abstract_origin_attribute (subr_die, decl);
10501 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
10502 BLOCK_NUMBER (stmt));
10503 add_AT_lbl_id (subr_die, DW_AT_low_pc, label);
10504 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
10505 BLOCK_NUMBER (stmt));
10506 add_AT_lbl_id (subr_die, DW_AT_high_pc, label);
10507 decls_for_scope (stmt, subr_die, depth);
10508 current_function_has_inlines = 1;
10512 /* Generate a DIE for a field in a record, or structure. */
10514 static void
10515 gen_field_die (decl, context_die)
10516 register tree decl;
10517 register dw_die_ref context_die;
10519 register dw_die_ref decl_die = new_die (DW_TAG_member, context_die);
10521 add_name_and_src_coords_attributes (decl_die, decl);
10522 add_type_attribute (decl_die, member_declared_type (decl),
10523 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
10524 context_die);
10526 /* If this is a bit field... */
10527 if (DECL_BIT_FIELD_TYPE (decl))
10529 add_byte_size_attribute (decl_die, decl);
10530 add_bit_size_attribute (decl_die, decl);
10531 add_bit_offset_attribute (decl_die, decl);
10534 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
10535 add_data_member_location_attribute (decl_die, decl);
10537 if (DECL_ARTIFICIAL (decl))
10538 add_AT_flag (decl_die, DW_AT_artificial, 1);
10540 if (TREE_PROTECTED (decl))
10541 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
10543 else if (TREE_PRIVATE (decl))
10544 add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
10547 #if 0
10548 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
10549 Use modified_type_die instead.
10550 We keep this code here just in case these types of DIEs may be needed to
10551 represent certain things in other languages (e.g. Pascal) someday. */
10552 static void
10553 gen_pointer_type_die (type, context_die)
10554 register tree type;
10555 register dw_die_ref context_die;
10557 register dw_die_ref ptr_die
10558 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die));
10560 equate_type_number_to_die (type, ptr_die);
10561 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
10562 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
10565 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
10566 Use modified_type_die instead.
10567 We keep this code here just in case these types of DIEs may be needed to
10568 represent certain things in other languages (e.g. Pascal) someday. */
10569 static void
10570 gen_reference_type_die (type, context_die)
10571 register tree type;
10572 register dw_die_ref context_die;
10574 register dw_die_ref ref_die
10575 = new_die (DW_TAG_reference_type, scope_die_for (type, context_die));
10577 equate_type_number_to_die (type, ref_die);
10578 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
10579 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
10581 #endif
10583 /* Generate a DIE for a pointer to a member type. */
10584 static void
10585 gen_ptr_to_mbr_type_die (type, context_die)
10586 register tree type;
10587 register dw_die_ref context_die;
10589 register dw_die_ref ptr_die
10590 = new_die (DW_TAG_ptr_to_member_type, scope_die_for (type, context_die));
10592 equate_type_number_to_die (type, ptr_die);
10593 add_AT_die_ref (ptr_die, DW_AT_containing_type,
10594 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
10595 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
10598 /* Generate the DIE for the compilation unit. */
10600 static dw_die_ref
10601 gen_compile_unit_die (filename)
10602 register const char *filename;
10604 register dw_die_ref die;
10605 char producer[250];
10606 const char *wd = getpwd ();
10607 int language;
10609 die = new_die (DW_TAG_compile_unit, NULL);
10610 add_name_attribute (die, filename);
10612 if (wd != NULL && filename[0] != DIR_SEPARATOR)
10613 add_AT_string (die, DW_AT_comp_dir, wd);
10615 sprintf (producer, "%s %s", language_string, version_string);
10617 #ifdef MIPS_DEBUGGING_INFO
10618 /* The MIPS/SGI compilers place the 'cc' command line options in the producer
10619 string. The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
10620 not appear in the producer string, the debugger reaches the conclusion
10621 that the object file is stripped and has no debugging information.
10622 To get the MIPS/SGI debugger to believe that there is debugging
10623 information in the object file, we add a -g to the producer string. */
10624 if (debug_info_level > DINFO_LEVEL_TERSE)
10625 strcat (producer, " -g");
10626 #endif
10628 add_AT_string (die, DW_AT_producer, producer);
10630 if (strcmp (language_string, "GNU C++") == 0)
10631 language = DW_LANG_C_plus_plus;
10632 else if (strcmp (language_string, "GNU Ada") == 0)
10633 language = DW_LANG_Ada83;
10634 else if (strcmp (language_string, "GNU F77") == 0)
10635 language = DW_LANG_Fortran77;
10636 else if (strcmp (language_string, "GNU Pascal") == 0)
10637 language = DW_LANG_Pascal83;
10638 else if (strcmp (language_string, "GNU Java") == 0)
10639 language = DW_LANG_Java;
10640 else if (flag_traditional)
10641 language = DW_LANG_C;
10642 else
10643 language = DW_LANG_C89;
10645 add_AT_unsigned (die, DW_AT_language, language);
10647 return die;
10650 /* Generate a DIE for a string type. */
10652 static void
10653 gen_string_type_die (type, context_die)
10654 register tree type;
10655 register dw_die_ref context_die;
10657 register dw_die_ref type_die
10658 = new_die (DW_TAG_string_type, scope_die_for (type, context_die));
10660 equate_type_number_to_die (type, type_die);
10662 /* Fudge the string length attribute for now. */
10664 /* TODO: add string length info.
10665 string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
10666 bound_representation (upper_bound, 0, 'u'); */
10669 /* Generate the DIE for a base class. */
10671 static void
10672 gen_inheritance_die (binfo, context_die)
10673 register tree binfo;
10674 register dw_die_ref context_die;
10676 dw_die_ref die = new_die (DW_TAG_inheritance, context_die);
10678 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
10679 add_data_member_location_attribute (die, binfo);
10681 if (TREE_VIA_VIRTUAL (binfo))
10682 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10683 if (TREE_VIA_PUBLIC (binfo))
10684 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
10685 else if (TREE_VIA_PROTECTED (binfo))
10686 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
10689 /* Generate a DIE for a class member. */
10691 static void
10692 gen_member_die (type, context_die)
10693 register tree type;
10694 register dw_die_ref context_die;
10696 register tree member;
10697 dw_die_ref child;
10699 /* If this is not an incomplete type, output descriptions of each of its
10700 members. Note that as we output the DIEs necessary to represent the
10701 members of this record or union type, we will also be trying to output
10702 DIEs to represent the *types* of those members. However the `type'
10703 function (above) will specifically avoid generating type DIEs for member
10704 types *within* the list of member DIEs for this (containing) type execpt
10705 for those types (of members) which are explicitly marked as also being
10706 members of this (containing) type themselves. The g++ front- end can
10707 force any given type to be treated as a member of some other
10708 (containing) type by setting the TYPE_CONTEXT of the given (member) type
10709 to point to the TREE node representing the appropriate (containing)
10710 type. */
10712 /* First output info about the base classes. */
10713 if (TYPE_BINFO (type) && TYPE_BINFO_BASETYPES (type))
10715 register tree bases = TYPE_BINFO_BASETYPES (type);
10716 register int n_bases = TREE_VEC_LENGTH (bases);
10717 register int i;
10719 for (i = 0; i < n_bases; i++)
10720 gen_inheritance_die (TREE_VEC_ELT (bases, i), context_die);
10723 /* Now output info about the data members and type members. */
10724 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
10726 /* If we thought we were generating minimal debug info for TYPE
10727 and then changed our minds, some of the member declarations
10728 may have already been defined. Don't define them again, but
10729 do put them in the right order. */
10731 child = lookup_decl_die (member);
10732 if (child)
10733 splice_child_die (context_die, child);
10734 else
10735 gen_decl_die (member, context_die);
10738 /* Now output info about the function members (if any). */
10739 for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
10741 child = lookup_decl_die (member);
10742 if (child)
10743 splice_child_die (context_die, child);
10744 else
10745 gen_decl_die (member, context_die);
10749 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
10750 is set, we pretend that the type was never defined, so we only get the
10751 member DIEs needed by later specification DIEs. */
10753 static void
10754 gen_struct_or_union_type_die (type, context_die)
10755 register tree type;
10756 register dw_die_ref context_die;
10758 register dw_die_ref type_die = lookup_type_die (type);
10759 register dw_die_ref scope_die = 0;
10760 register int nested = 0;
10761 int complete = (TYPE_SIZE (type)
10762 && (! TYPE_STUB_DECL (type)
10763 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
10765 if (type_die && ! complete)
10766 return;
10768 if (TYPE_CONTEXT (type) != NULL_TREE
10769 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type)))
10770 nested = 1;
10772 scope_die = scope_die_for (type, context_die);
10774 if (! type_die || (nested && scope_die == comp_unit_die))
10775 /* First occurrence of type or toplevel definition of nested class. */
10777 register dw_die_ref old_die = type_die;
10779 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
10780 ? DW_TAG_structure_type : DW_TAG_union_type,
10781 scope_die);
10782 equate_type_number_to_die (type, type_die);
10783 if (old_die)
10784 add_AT_die_ref (type_die, DW_AT_specification, old_die);
10785 else
10786 add_name_attribute (type_die, type_tag (type));
10788 else
10789 remove_AT (type_die, DW_AT_declaration);
10791 /* If this type has been completed, then give it a byte_size attribute and
10792 then give a list of members. */
10793 if (complete)
10795 /* Prevent infinite recursion in cases where the type of some member of
10796 this type is expressed in terms of this type itself. */
10797 TREE_ASM_WRITTEN (type) = 1;
10798 add_byte_size_attribute (type_die, type);
10799 if (TYPE_STUB_DECL (type) != NULL_TREE)
10800 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
10802 /* If the first reference to this type was as the return type of an
10803 inline function, then it may not have a parent. Fix this now. */
10804 if (type_die->die_parent == NULL)
10805 add_child_die (scope_die, type_die);
10807 push_decl_scope (type);
10808 gen_member_die (type, type_die);
10809 pop_decl_scope ();
10811 /* GNU extension: Record what type our vtable lives in. */
10812 if (TYPE_VFIELD (type))
10814 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
10816 gen_type_die (vtype, context_die);
10817 add_AT_die_ref (type_die, DW_AT_containing_type,
10818 lookup_type_die (vtype));
10821 else
10823 add_AT_flag (type_die, DW_AT_declaration, 1);
10825 /* We don't need to do this for function-local types. */
10826 if (! decl_function_context (TYPE_STUB_DECL (type)))
10827 add_incomplete_type (type);
10831 /* Generate a DIE for a subroutine _type_. */
10833 static void
10834 gen_subroutine_type_die (type, context_die)
10835 register tree type;
10836 register dw_die_ref context_die;
10838 register tree return_type = TREE_TYPE (type);
10839 register dw_die_ref subr_die
10840 = new_die (DW_TAG_subroutine_type, scope_die_for (type, context_die));
10842 equate_type_number_to_die (type, subr_die);
10843 add_prototyped_attribute (subr_die, type);
10844 add_type_attribute (subr_die, return_type, 0, 0, context_die);
10845 gen_formal_types_die (type, subr_die);
10848 /* Generate a DIE for a type definition */
10850 static void
10851 gen_typedef_die (decl, context_die)
10852 register tree decl;
10853 register dw_die_ref context_die;
10855 register dw_die_ref type_die;
10856 register tree origin;
10858 if (TREE_ASM_WRITTEN (decl))
10859 return;
10860 TREE_ASM_WRITTEN (decl) = 1;
10862 type_die = new_die (DW_TAG_typedef, context_die);
10863 origin = decl_ultimate_origin (decl);
10864 if (origin != NULL)
10865 add_abstract_origin_attribute (type_die, origin);
10866 else
10868 register tree type;
10869 add_name_and_src_coords_attributes (type_die, decl);
10870 if (DECL_ORIGINAL_TYPE (decl))
10872 type = DECL_ORIGINAL_TYPE (decl);
10874 if (type == TREE_TYPE (decl))
10875 abort ();
10876 else
10877 equate_type_number_to_die (TREE_TYPE (decl), type_die);
10879 else
10880 type = TREE_TYPE (decl);
10881 add_type_attribute (type_die, type, TREE_READONLY (decl),
10882 TREE_THIS_VOLATILE (decl), context_die);
10885 if (DECL_ABSTRACT (decl))
10886 equate_decl_number_to_die (decl, type_die);
10889 /* Generate a type description DIE. */
10891 static void
10892 gen_type_die (type, context_die)
10893 register tree type;
10894 register dw_die_ref context_die;
10896 int need_pop;
10898 if (type == NULL_TREE || type == error_mark_node)
10899 return;
10901 /* We are going to output a DIE to represent the unqualified version of
10902 this type (i.e. without any const or volatile qualifiers) so get the
10903 main variant (i.e. the unqualified version) of this type now. */
10904 type = type_main_variant (type);
10906 if (TREE_ASM_WRITTEN (type))
10907 return;
10909 if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10910 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
10912 TREE_ASM_WRITTEN (type) = 1;
10913 gen_decl_die (TYPE_NAME (type), context_die);
10914 return;
10917 switch (TREE_CODE (type))
10919 case ERROR_MARK:
10920 break;
10922 case POINTER_TYPE:
10923 case REFERENCE_TYPE:
10924 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
10925 ensures that the gen_type_die recursion will terminate even if the
10926 type is recursive. Recursive types are possible in Ada. */
10927 /* ??? We could perhaps do this for all types before the switch
10928 statement. */
10929 TREE_ASM_WRITTEN (type) = 1;
10931 /* For these types, all that is required is that we output a DIE (or a
10932 set of DIEs) to represent the "basis" type. */
10933 gen_type_die (TREE_TYPE (type), context_die);
10934 break;
10936 case OFFSET_TYPE:
10937 /* This code is used for C++ pointer-to-data-member types.
10938 Output a description of the relevant class type. */
10939 gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
10941 /* Output a description of the type of the object pointed to. */
10942 gen_type_die (TREE_TYPE (type), context_die);
10944 /* Now output a DIE to represent this pointer-to-data-member type
10945 itself. */
10946 gen_ptr_to_mbr_type_die (type, context_die);
10947 break;
10949 case SET_TYPE:
10950 gen_type_die (TYPE_DOMAIN (type), context_die);
10951 gen_set_type_die (type, context_die);
10952 break;
10954 case FILE_TYPE:
10955 gen_type_die (TREE_TYPE (type), context_die);
10956 abort (); /* No way to represent these in Dwarf yet! */
10957 break;
10959 case FUNCTION_TYPE:
10960 /* Force out return type (in case it wasn't forced out already). */
10961 gen_type_die (TREE_TYPE (type), context_die);
10962 gen_subroutine_type_die (type, context_die);
10963 break;
10965 case METHOD_TYPE:
10966 /* Force out return type (in case it wasn't forced out already). */
10967 gen_type_die (TREE_TYPE (type), context_die);
10968 gen_subroutine_type_die (type, context_die);
10969 break;
10971 case ARRAY_TYPE:
10972 if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
10974 gen_type_die (TREE_TYPE (type), context_die);
10975 gen_string_type_die (type, context_die);
10977 else
10978 gen_array_type_die (type, context_die);
10979 break;
10981 case VECTOR_TYPE:
10982 gen_type_die (TYPE_DEBUG_REPRESENTATION_TYPE (type), context_die);
10983 break;
10985 case ENUMERAL_TYPE:
10986 case RECORD_TYPE:
10987 case UNION_TYPE:
10988 case QUAL_UNION_TYPE:
10989 /* If this is a nested type whose containing class hasn't been
10990 written out yet, writing it out will cover this one, too.
10991 This does not apply to instantiations of member class templates;
10992 they need to be added to the containing class as they are
10993 generated. FIXME: This hurts the idea of combining type decls
10994 from multiple TUs, since we can't predict what set of template
10995 instantiations we'll get. */
10996 if (TYPE_CONTEXT (type)
10997 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
10998 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
11000 gen_type_die (TYPE_CONTEXT (type), context_die);
11002 if (TREE_ASM_WRITTEN (type))
11003 return;
11005 /* If that failed, attach ourselves to the stub. */
11006 push_decl_scope (TYPE_CONTEXT (type));
11007 context_die = lookup_type_die (TYPE_CONTEXT (type));
11008 need_pop = 1;
11010 else
11011 need_pop = 0;
11013 if (TREE_CODE (type) == ENUMERAL_TYPE)
11014 gen_enumeration_type_die (type, context_die);
11015 else
11016 gen_struct_or_union_type_die (type, context_die);
11018 if (need_pop)
11019 pop_decl_scope ();
11021 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
11022 it up if it is ever completed. gen_*_type_die will set it for us
11023 when appropriate. */
11024 return;
11026 case VOID_TYPE:
11027 case INTEGER_TYPE:
11028 case REAL_TYPE:
11029 case COMPLEX_TYPE:
11030 case BOOLEAN_TYPE:
11031 case CHAR_TYPE:
11032 /* No DIEs needed for fundamental types. */
11033 break;
11035 case LANG_TYPE:
11036 /* No Dwarf representation currently defined. */
11037 break;
11039 default:
11040 abort ();
11043 TREE_ASM_WRITTEN (type) = 1;
11046 /* Generate a DIE for a tagged type instantiation. */
11048 static void
11049 gen_tagged_type_instantiation_die (type, context_die)
11050 register tree type;
11051 register dw_die_ref context_die;
11053 if (type == NULL_TREE || type == error_mark_node)
11054 return;
11056 /* We are going to output a DIE to represent the unqualified version of
11057 this type (i.e. without any const or volatile qualifiers) so make sure
11058 that we have the main variant (i.e. the unqualified version) of this
11059 type now. */
11060 if (type != type_main_variant (type))
11061 abort ();
11063 /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
11064 an instance of an unresolved type. */
11066 switch (TREE_CODE (type))
11068 case ERROR_MARK:
11069 break;
11071 case ENUMERAL_TYPE:
11072 gen_inlined_enumeration_type_die (type, context_die);
11073 break;
11075 case RECORD_TYPE:
11076 gen_inlined_structure_type_die (type, context_die);
11077 break;
11079 case UNION_TYPE:
11080 case QUAL_UNION_TYPE:
11081 gen_inlined_union_type_die (type, context_die);
11082 break;
11084 default:
11085 abort ();
11089 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
11090 things which are local to the given block. */
11092 static void
11093 gen_block_die (stmt, context_die, depth)
11094 register tree stmt;
11095 register dw_die_ref context_die;
11096 int depth;
11098 register int must_output_die = 0;
11099 register tree origin;
11100 register tree decl;
11101 register enum tree_code origin_code;
11103 /* Ignore blocks never really used to make RTL. */
11105 if (stmt == NULL_TREE || !TREE_USED (stmt)
11106 || (!TREE_ASM_WRITTEN (stmt) && !BLOCK_ABSTRACT (stmt)))
11107 return;
11109 /* Determine the "ultimate origin" of this block. This block may be an
11110 inlined instance of an inlined instance of inline function, so we have
11111 to trace all of the way back through the origin chain to find out what
11112 sort of node actually served as the original seed for the creation of
11113 the current block. */
11114 origin = block_ultimate_origin (stmt);
11115 origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
11117 /* Determine if we need to output any Dwarf DIEs at all to represent this
11118 block. */
11119 if (origin_code == FUNCTION_DECL)
11120 /* The outer scopes for inlinings *must* always be represented. We
11121 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
11122 must_output_die = 1;
11123 else
11125 /* In the case where the current block represents an inlining of the
11126 "body block" of an inline function, we must *NOT* output any DIE for
11127 this block because we have already output a DIE to represent the
11128 whole inlined function scope and the "body block" of any function
11129 doesn't really represent a different scope according to ANSI C
11130 rules. So we check here to make sure that this block does not
11131 represent a "body block inlining" before trying to set the
11132 `must_output_die' flag. */
11133 if (! is_body_block (origin ? origin : stmt))
11135 /* Determine if this block directly contains any "significant"
11136 local declarations which we will need to output DIEs for. */
11137 if (debug_info_level > DINFO_LEVEL_TERSE)
11138 /* We are not in terse mode so *any* local declaration counts
11139 as being a "significant" one. */
11140 must_output_die = (BLOCK_VARS (stmt) != NULL);
11141 else
11142 /* We are in terse mode, so only local (nested) function
11143 definitions count as "significant" local declarations. */
11144 for (decl = BLOCK_VARS (stmt);
11145 decl != NULL; decl = TREE_CHAIN (decl))
11146 if (TREE_CODE (decl) == FUNCTION_DECL
11147 && DECL_INITIAL (decl))
11149 must_output_die = 1;
11150 break;
11155 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
11156 DIE for any block which contains no significant local declarations at
11157 all. Rather, in such cases we just call `decls_for_scope' so that any
11158 needed Dwarf info for any sub-blocks will get properly generated. Note
11159 that in terse mode, our definition of what constitutes a "significant"
11160 local declaration gets restricted to include only inlined function
11161 instances and local (nested) function definitions. */
11162 if (must_output_die)
11164 if (origin_code == FUNCTION_DECL)
11165 gen_inlined_subroutine_die (stmt, context_die, depth);
11166 else
11167 gen_lexical_block_die (stmt, context_die, depth);
11169 else
11170 decls_for_scope (stmt, context_die, depth);
11173 /* Generate all of the decls declared within a given scope and (recursively)
11174 all of its sub-blocks. */
11176 static void
11177 decls_for_scope (stmt, context_die, depth)
11178 register tree stmt;
11179 register dw_die_ref context_die;
11180 int depth;
11182 register tree decl;
11183 register tree subblocks;
11185 /* Ignore blocks never really used to make RTL. */
11186 if (stmt == NULL_TREE || ! TREE_USED (stmt))
11187 return;
11189 /* Output the DIEs to represent all of the data objects and typedefs
11190 declared directly within this block but not within any nested
11191 sub-blocks. Also, nested function and tag DIEs have been
11192 generated with a parent of NULL; fix that up now. */
11193 for (decl = BLOCK_VARS (stmt);
11194 decl != NULL; decl = TREE_CHAIN (decl))
11196 register dw_die_ref die;
11198 if (TREE_CODE (decl) == FUNCTION_DECL)
11199 die = lookup_decl_die (decl);
11200 else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
11201 die = lookup_type_die (TREE_TYPE (decl));
11202 else
11203 die = NULL;
11205 if (die != NULL && die->die_parent == NULL)
11206 add_child_die (context_die, die);
11207 else
11208 gen_decl_die (decl, context_die);
11211 /* Output the DIEs to represent all sub-blocks (and the items declared
11212 therein) of this block. */
11213 for (subblocks = BLOCK_SUBBLOCKS (stmt);
11214 subblocks != NULL;
11215 subblocks = BLOCK_CHAIN (subblocks))
11216 gen_block_die (subblocks, context_die, depth + 1);
11219 /* Is this a typedef we can avoid emitting? */
11221 static inline int
11222 is_redundant_typedef (decl)
11223 register tree decl;
11225 if (TYPE_DECL_IS_STUB (decl))
11226 return 1;
11228 if (DECL_ARTIFICIAL (decl)
11229 && DECL_CONTEXT (decl)
11230 && is_tagged_type (DECL_CONTEXT (decl))
11231 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
11232 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
11233 /* Also ignore the artificial member typedef for the class name. */
11234 return 1;
11236 return 0;
11239 /* Generate Dwarf debug information for a decl described by DECL. */
11241 static void
11242 gen_decl_die (decl, context_die)
11243 register tree decl;
11244 register dw_die_ref context_die;
11246 register tree origin;
11248 if (TREE_CODE (decl) == ERROR_MARK)
11249 return;
11251 /* If this ..._DECL node is marked to be ignored, then ignore it. */
11252 if (DECL_IGNORED_P (decl))
11253 return;
11255 switch (TREE_CODE (decl))
11257 case CONST_DECL:
11258 /* The individual enumerators of an enum type get output when we output
11259 the Dwarf representation of the relevant enum type itself. */
11260 break;
11262 case FUNCTION_DECL:
11263 /* Don't output any DIEs to represent mere function declarations,
11264 unless they are class members or explicit block externs. */
11265 if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
11266 && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
11267 break;
11269 /* If we're emitting an out-of-line copy of an inline function,
11270 emit info for the abstract instance and set up to refer to it. */
11271 if (DECL_INLINE (decl) && ! DECL_ABSTRACT (decl)
11272 && ! class_scope_p (context_die)
11273 /* dwarf2out_abstract_function won't emit a die if this is just a
11274 declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
11275 that case, because that works only if we have a die. */
11276 && DECL_INITIAL (decl) != NULL_TREE)
11278 dwarf2out_abstract_function (decl);
11279 set_decl_origin_self (decl);
11282 if (debug_info_level > DINFO_LEVEL_TERSE)
11284 /* Before we describe the FUNCTION_DECL itself, make sure that we
11285 have described its return type. */
11286 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
11288 /* And its virtual context. */
11289 if (DECL_VINDEX (decl) != NULL_TREE)
11290 gen_type_die (DECL_CONTEXT (decl), context_die);
11292 /* And its containing type. */
11293 origin = decl_class_context (decl);
11294 if (origin != NULL_TREE)
11295 gen_type_die_for_member (origin, decl, context_die);
11298 /* Now output a DIE to represent the function itself. */
11299 gen_subprogram_die (decl, context_die);
11300 break;
11302 case TYPE_DECL:
11303 /* If we are in terse mode, don't generate any DIEs to represent any
11304 actual typedefs. */
11305 if (debug_info_level <= DINFO_LEVEL_TERSE)
11306 break;
11308 /* In the special case of a TYPE_DECL node representing the
11309 declaration of some type tag, if the given TYPE_DECL is marked as
11310 having been instantiated from some other (original) TYPE_DECL node
11311 (e.g. one which was generated within the original definition of an
11312 inline function) we have to generate a special (abbreviated)
11313 DW_TAG_structure_type, DW_TAG_union_type, or DW_TAG_enumeration_type
11314 DIE here. */
11315 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
11317 gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
11318 break;
11321 if (is_redundant_typedef (decl))
11322 gen_type_die (TREE_TYPE (decl), context_die);
11323 else
11324 /* Output a DIE to represent the typedef itself. */
11325 gen_typedef_die (decl, context_die);
11326 break;
11328 case LABEL_DECL:
11329 if (debug_info_level >= DINFO_LEVEL_NORMAL)
11330 gen_label_die (decl, context_die);
11331 break;
11333 case VAR_DECL:
11334 /* If we are in terse mode, don't generate any DIEs to represent any
11335 variable declarations or definitions. */
11336 if (debug_info_level <= DINFO_LEVEL_TERSE)
11337 break;
11339 /* Output any DIEs that are needed to specify the type of this data
11340 object. */
11341 gen_type_die (TREE_TYPE (decl), context_die);
11343 /* And its containing type. */
11344 origin = decl_class_context (decl);
11345 if (origin != NULL_TREE)
11346 gen_type_die_for_member (origin, decl, context_die);
11348 /* Now output the DIE to represent the data object itself. This gets
11349 complicated because of the possibility that the VAR_DECL really
11350 represents an inlined instance of a formal parameter for an inline
11351 function. */
11352 origin = decl_ultimate_origin (decl);
11353 if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
11354 gen_formal_parameter_die (decl, context_die);
11355 else
11356 gen_variable_die (decl, context_die);
11357 break;
11359 case FIELD_DECL:
11360 /* Ignore the nameless fields that are used to skip bits, but
11361 handle C++ anonymous unions. */
11362 if (DECL_NAME (decl) != NULL_TREE
11363 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE)
11365 gen_type_die (member_declared_type (decl), context_die);
11366 gen_field_die (decl, context_die);
11368 break;
11370 case PARM_DECL:
11371 gen_type_die (TREE_TYPE (decl), context_die);
11372 gen_formal_parameter_die (decl, context_die);
11373 break;
11375 case NAMESPACE_DECL:
11376 /* Ignore for now. */
11377 break;
11379 default:
11380 abort ();
11384 /* Add Ada "use" clause information for SGI Workshop debugger. */
11386 void
11387 dwarf2out_add_library_unit_info (filename, context_list)
11388 const char *filename;
11389 const char *context_list;
11391 unsigned int file_index;
11393 if (filename != NULL)
11395 dw_die_ref unit_die = new_die (DW_TAG_module, comp_unit_die);
11396 tree context_list_decl
11397 = build_decl (LABEL_DECL, get_identifier (context_list),
11398 void_type_node);
11400 TREE_PUBLIC (context_list_decl) = TRUE;
11401 add_name_attribute (unit_die, context_list);
11402 file_index = lookup_filename (&decl_file_table, filename);
11403 add_AT_unsigned (unit_die, DW_AT_decl_file, file_index);
11404 add_pubname (context_list_decl, unit_die);
11408 /* Write the debugging output for DECL. */
11410 void
11411 dwarf2out_decl (decl)
11412 register tree decl;
11414 register dw_die_ref context_die = comp_unit_die;
11416 if (TREE_CODE (decl) == ERROR_MARK)
11417 return;
11419 /* If this ..._DECL node is marked to be ignored, then ignore it. */
11420 if (DECL_IGNORED_P (decl))
11421 return;
11423 switch (TREE_CODE (decl))
11425 case FUNCTION_DECL:
11426 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration of a
11427 builtin function. Explicit programmer-supplied declarations of
11428 these same functions should NOT be ignored however. */
11429 if (DECL_EXTERNAL (decl) && DECL_BUILT_IN (decl))
11430 return;
11432 /* What we would really like to do here is to filter out all mere
11433 file-scope declarations of file-scope functions which are never
11434 referenced later within this translation unit (and keep all of ones
11435 that *are* referenced later on) but we aren't clairvoyant, so we have
11436 no idea which functions will be referenced in the future (i.e. later
11437 on within the current translation unit). So here we just ignore all
11438 file-scope function declarations which are not also definitions. If
11439 and when the debugger needs to know something about these functions,
11440 it will have to hunt around and find the DWARF information associated
11441 with the definition of the function. Note that we can't just check
11442 `DECL_EXTERNAL' to find out which FUNCTION_DECL nodes represent
11443 definitions and which ones represent mere declarations. We have to
11444 check `DECL_INITIAL' instead. That's because the C front-end
11445 supports some weird semantics for "extern inline" function
11446 definitions. These can get inlined within the current translation
11447 unit (an thus, we need to generate DWARF info for their abstract
11448 instances so that the DWARF info for the concrete inlined instances
11449 can have something to refer to) but the compiler never generates any
11450 out-of-lines instances of such things (despite the fact that they
11451 *are* definitions). The important point is that the C front-end
11452 marks these "extern inline" functions as DECL_EXTERNAL, but we need
11453 to generate DWARF for them anyway. Note that the C++ front-end also
11454 plays some similar games for inline function definitions appearing
11455 within include files which also contain
11456 `#pragma interface' pragmas. */
11457 if (DECL_INITIAL (decl) == NULL_TREE)
11458 return;
11460 /* If we're a nested function, initially use a parent of NULL; if we're
11461 a plain function, this will be fixed up in decls_for_scope. If
11462 we're a method, it will be ignored, since we already have a DIE. */
11463 if (decl_function_context (decl))
11464 context_die = NULL;
11466 break;
11468 case VAR_DECL:
11469 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
11470 declaration and if the declaration was never even referenced from
11471 within this entire compilation unit. We suppress these DIEs in
11472 order to save space in the .debug section (by eliminating entries
11473 which are probably useless). Note that we must not suppress
11474 block-local extern declarations (whether used or not) because that
11475 would screw-up the debugger's name lookup mechanism and cause it to
11476 miss things which really ought to be in scope at a given point. */
11477 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
11478 return;
11480 /* If we are in terse mode, don't generate any DIEs to represent any
11481 variable declarations or definitions. */
11482 if (debug_info_level <= DINFO_LEVEL_TERSE)
11483 return;
11484 break;
11486 case TYPE_DECL:
11487 /* Don't emit stubs for types unless they are needed by other DIEs. */
11488 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
11489 return;
11491 /* Don't bother trying to generate any DIEs to represent any of the
11492 normal built-in types for the language we are compiling. */
11493 if (DECL_SOURCE_LINE (decl) == 0)
11495 /* OK, we need to generate one for `bool' so GDB knows what type
11496 comparisons have. */
11497 if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
11498 == DW_LANG_C_plus_plus)
11499 && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE)
11500 modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
11502 return;
11505 /* If we are in terse mode, don't generate any DIEs for types. */
11506 if (debug_info_level <= DINFO_LEVEL_TERSE)
11507 return;
11509 /* If we're a function-scope tag, initially use a parent of NULL;
11510 this will be fixed up in decls_for_scope. */
11511 if (decl_function_context (decl))
11512 context_die = NULL;
11514 break;
11516 default:
11517 return;
11520 gen_decl_die (decl, context_die);
11523 /* Output a marker (i.e. a label) for the beginning of the generated code for
11524 a lexical block. */
11526 void
11527 dwarf2out_begin_block (blocknum)
11528 register unsigned blocknum;
11530 function_section (current_function_decl);
11531 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
11534 /* Output a marker (i.e. a label) for the end of the generated code for a
11535 lexical block. */
11537 void
11538 dwarf2out_end_block (blocknum)
11539 register unsigned blocknum;
11541 function_section (current_function_decl);
11542 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
11545 /* Returns nonzero if it is appropriate not to emit any debugging
11546 information for BLOCK, because it doesn't contain any instructions.
11548 Don't allow this for blocks with nested functions or local classes
11549 as we would end up with orphans, and in the presence of scheduling
11550 we may end up calling them anyway. */
11553 dwarf2out_ignore_block (block)
11554 tree block;
11556 tree decl;
11557 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
11558 if (TREE_CODE (decl) == FUNCTION_DECL
11559 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
11560 return 0;
11561 return 1;
11564 /* Lookup a filename (in the list of filenames that we know about here in
11565 dwarf2out.c) and return its "index". The index of each (known) filename is
11566 just a unique number which is associated with only that one filename.
11567 We need such numbers for the sake of generating labels
11568 (in the .debug_sfnames section) and references to those
11569 files numbers (in the .debug_srcinfo and.debug_macinfo sections).
11570 If the filename given as an argument is not found in our current list,
11571 add it to the list and assign it the next available unique index number.
11572 In order to speed up searches, we remember the index of the filename
11573 was looked up last. This handles the majority of all searches. */
11575 static unsigned
11576 lookup_filename (t, file_name)
11577 struct file_table *t;
11578 const char *file_name;
11580 register unsigned i;
11582 /* Check to see if the file name that was searched on the previous
11583 call matches this file name. If so, return the index. */
11584 if (t->last_lookup_index != 0)
11585 if (strcmp (file_name, t->table[t->last_lookup_index]) == 0)
11586 return t->last_lookup_index;
11588 /* Didn't match the previous lookup, search the table */
11589 for (i = 1; i < t->in_use; ++i)
11590 if (strcmp (file_name, t->table[i]) == 0)
11592 t->last_lookup_index = i;
11593 return i;
11596 /* Prepare to add a new table entry by making sure there is enough space in
11597 the table to do so. If not, expand the current table. */
11598 if (i == t->allocated)
11600 t->allocated = i + FILE_TABLE_INCREMENT;
11601 t->table = (char **)
11602 xrealloc (t->table, t->allocated * sizeof (char *));
11605 /* Add the new entry to the end of the filename table. */
11606 t->table[i] = xstrdup (file_name);
11607 t->in_use = i + 1;
11608 t->last_lookup_index = i;
11610 return i;
11613 static void
11614 init_file_table (t)
11615 struct file_table *t;
11617 /* Allocate the initial hunk of the file_table. */
11618 t->table = (char **) xcalloc (FILE_TABLE_INCREMENT, sizeof (char *));
11619 t->allocated = FILE_TABLE_INCREMENT;
11621 /* Skip the first entry - file numbers begin at 1. */
11622 t->in_use = 1;
11623 t->last_lookup_index = 0;
11626 /* Output a label to mark the beginning of a source code line entry
11627 and record information relating to this source line, in
11628 'line_info_table' for later output of the .debug_line section. */
11630 void
11631 dwarf2out_line (filename, line)
11632 register const char *filename;
11633 register unsigned line;
11635 if (debug_info_level >= DINFO_LEVEL_NORMAL)
11637 function_section (current_function_decl);
11639 if (DWARF2_ASM_LINE_DEBUG_INFO)
11641 #if 0
11642 unsigned old_in_use = line_file_table.in_use;
11643 #endif
11644 unsigned file_num = lookup_filename (&line_file_table, filename);
11646 /* Emit the .file and .loc directives understood by GNU as. */
11647 #if 0
11648 /* ??? As of 2000-11-25, gas has a bug in which it doesn't
11649 actually use the file number argument. It merely remembers
11650 the last .file directive emitted. */
11651 if (file_num >= old_in_use)
11652 fprintf (asm_out_file, "\t.file %d \"%s\"\n", file_num, filename);
11653 fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
11654 #else
11655 static unsigned int last_file_num;
11656 if (file_num != last_file_num)
11658 last_file_num = file_num;
11659 fprintf (asm_out_file, "\t.file 0 \"%s\"\n", filename);
11661 fprintf (asm_out_file, "\t.loc 0 %d 0\n", line);
11662 #endif
11664 /* Indicate that line number info exists. */
11665 ++line_info_table_in_use;
11667 /* Indicate that multiple line number tables exist. */
11668 if (DECL_SECTION_NAME (current_function_decl))
11669 ++separate_line_info_table_in_use;
11671 else if (DECL_SECTION_NAME (current_function_decl))
11673 register dw_separate_line_info_ref line_info;
11674 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, SEPARATE_LINE_CODE_LABEL,
11675 separate_line_info_table_in_use);
11676 if (flag_debug_asm)
11677 fprintf (asm_out_file, "\t%s %s:%d", ASM_COMMENT_START,
11678 filename, line);
11679 fputc ('\n', asm_out_file);
11681 /* expand the line info table if necessary */
11682 if (separate_line_info_table_in_use
11683 == separate_line_info_table_allocated)
11685 separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
11686 separate_line_info_table
11687 = (dw_separate_line_info_ref)
11688 xrealloc (separate_line_info_table,
11689 separate_line_info_table_allocated
11690 * sizeof (dw_separate_line_info_entry));
11693 /* Add the new entry at the end of the line_info_table. */
11694 line_info
11695 = &separate_line_info_table[separate_line_info_table_in_use++];
11696 line_info->dw_file_num = lookup_filename (&line_file_table, filename);
11697 line_info->dw_line_num = line;
11698 line_info->function = current_funcdef_number;
11700 else
11702 register dw_line_info_ref line_info;
11704 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, LINE_CODE_LABEL,
11705 line_info_table_in_use);
11706 if (flag_debug_asm)
11707 fprintf (asm_out_file, "\t%s %s:%d", ASM_COMMENT_START,
11708 filename, line);
11709 fputc ('\n', asm_out_file);
11711 /* Expand the line info table if necessary. */
11712 if (line_info_table_in_use == line_info_table_allocated)
11714 line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
11715 line_info_table
11716 = (dw_line_info_ref)
11717 xrealloc (line_info_table,
11718 (line_info_table_allocated
11719 * sizeof (dw_line_info_entry)));
11722 /* Add the new entry at the end of the line_info_table. */
11723 line_info = &line_info_table[line_info_table_in_use++];
11724 line_info->dw_file_num = lookup_filename (&line_file_table, filename);
11725 line_info->dw_line_num = line;
11730 /* Record the beginning of a new source file, for later output
11731 of the .debug_macinfo section. At present, unimplemented. */
11733 void
11734 dwarf2out_start_source_file (filename)
11735 register const char *filename ATTRIBUTE_UNUSED;
11737 if (flag_eliminate_dwarf2_dups)
11739 /* Record the beginning of the file for break_out_includes. */
11740 dw_die_ref bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die);
11741 add_AT_string (bincl_die, DW_AT_name, filename);
11745 /* Record the end of a source file, for later output
11746 of the .debug_macinfo section. At present, unimplemented. */
11748 void
11749 dwarf2out_end_source_file ()
11751 if (flag_eliminate_dwarf2_dups)
11753 /* Record the end of the file for break_out_includes. */
11754 new_die (DW_TAG_GNU_EINCL, comp_unit_die);
11758 /* Called from check_newline in c-parse.y. The `buffer' parameter contains
11759 the tail part of the directive line, i.e. the part which is past the
11760 initial whitespace, #, whitespace, directive-name, whitespace part. */
11762 void
11763 dwarf2out_define (lineno, buffer)
11764 register unsigned lineno ATTRIBUTE_UNUSED;
11765 register const char *buffer ATTRIBUTE_UNUSED;
11767 static int initialized = 0;
11768 if (!initialized)
11770 dwarf2out_start_source_file (primary_filename);
11771 initialized = 1;
11775 /* Called from check_newline in c-parse.y. The `buffer' parameter contains
11776 the tail part of the directive line, i.e. the part which is past the
11777 initial whitespace, #, whitespace, directive-name, whitespace part. */
11779 void
11780 dwarf2out_undef (lineno, buffer)
11781 register unsigned lineno ATTRIBUTE_UNUSED;
11782 register const char *buffer ATTRIBUTE_UNUSED;
11786 /* Set up for Dwarf output at the start of compilation. */
11788 void
11789 dwarf2out_init (asm_out_file, main_input_filename)
11790 register FILE *asm_out_file;
11791 register const char *main_input_filename;
11793 /* Remember the name of the primary input file. */
11794 primary_filename = main_input_filename;
11796 init_file_table (&decl_file_table);
11797 init_file_table (&line_file_table);
11799 /* Allocate the initial hunk of the decl_die_table. */
11800 decl_die_table
11801 = (dw_die_ref *) xcalloc (DECL_DIE_TABLE_INCREMENT, sizeof (dw_die_ref));
11802 decl_die_table_allocated = DECL_DIE_TABLE_INCREMENT;
11803 decl_die_table_in_use = 0;
11805 /* Allocate the initial hunk of the decl_scope_table. */
11806 decl_scope_table
11807 = (tree *) xcalloc (DECL_SCOPE_TABLE_INCREMENT, sizeof (tree));
11808 decl_scope_table_allocated = DECL_SCOPE_TABLE_INCREMENT;
11809 decl_scope_depth = 0;
11811 /* Allocate the initial hunk of the abbrev_die_table. */
11812 abbrev_die_table
11813 = (dw_die_ref *) xcalloc (ABBREV_DIE_TABLE_INCREMENT,
11814 sizeof (dw_die_ref));
11815 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
11816 /* Zero-th entry is allocated, but unused */
11817 abbrev_die_table_in_use = 1;
11819 /* Allocate the initial hunk of the line_info_table. */
11820 line_info_table
11821 = (dw_line_info_ref) xcalloc (LINE_INFO_TABLE_INCREMENT,
11822 sizeof (dw_line_info_entry));
11823 line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
11824 /* Zero-th entry is allocated, but unused */
11825 line_info_table_in_use = 1;
11827 /* Generate the initial DIE for the .debug section. Note that the (string)
11828 value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
11829 will (typically) be a relative pathname and that this pathname should be
11830 taken as being relative to the directory from which the compiler was
11831 invoked when the given (base) source file was compiled. */
11832 comp_unit_die = gen_compile_unit_die (main_input_filename);
11834 VARRAY_RTX_INIT (used_rtx_varray, 32, "used_rtx_varray");
11835 ggc_add_rtx_varray_root (&used_rtx_varray, 1);
11837 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
11838 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label, ABBREV_SECTION_LABEL, 0);
11839 if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
11840 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
11841 else
11842 strcpy (text_section_label, stripattributes (TEXT_SECTION));
11843 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
11844 DEBUG_INFO_SECTION_LABEL, 0);
11845 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
11846 DEBUG_LINE_SECTION_LABEL, 0);
11848 ASM_OUTPUT_SECTION (asm_out_file, ABBREV_SECTION);
11849 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
11850 if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
11852 ASM_OUTPUT_SECTION (asm_out_file, TEXT_SECTION);
11853 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
11855 ASM_OUTPUT_SECTION (asm_out_file, DEBUG_INFO_SECTION);
11856 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11857 ASM_OUTPUT_SECTION (asm_out_file, DEBUG_LINE_SECTION);
11858 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
11861 /* Output stuff that dwarf requires at the end of every file,
11862 and generate the DWARF-2 debugging info. */
11864 void
11865 dwarf2out_finish ()
11867 limbo_die_node *node, *next_node;
11868 dw_die_ref die;
11870 /* Traverse the limbo die list, and add parent/child links. The only
11871 dies without parents that should be here are concrete instances of
11872 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
11873 For concrete instances, we can get the parent die from the abstract
11874 instance. */
11875 for (node = limbo_die_list; node; node = next_node)
11877 next_node = node->next;
11878 die = node->die;
11880 if (die->die_parent == NULL)
11882 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
11883 if (origin)
11884 add_child_die (origin->die_parent, die);
11885 else if (die == comp_unit_die)
11887 else
11888 abort ();
11890 free (node);
11892 limbo_die_list = NULL;
11894 /* Walk through the list of incomplete types again, trying once more to
11895 emit full debugging info for them. */
11896 retry_incomplete_types ();
11898 /* We need to reverse all the dies before break_out_includes, or
11899 we'll see the end of an include file before the beginning. */
11900 reverse_all_dies (comp_unit_die);
11902 /* Generate separate CUs for each of the include files we've seen.
11903 They will go into limbo_die_list. */
11904 if (flag_eliminate_dwarf2_dups)
11905 break_out_includes (comp_unit_die);
11907 /* Traverse the DIE's and add add sibling attributes to those DIE's
11908 that have children. */
11909 add_sibling_attributes (comp_unit_die);
11910 for (node = limbo_die_list; node; node = node->next)
11911 add_sibling_attributes (node->die);
11913 /* Output a terminator label for the .text section. */
11914 fputc ('\n', asm_out_file);
11915 ASM_OUTPUT_SECTION (asm_out_file, TEXT_SECTION);
11916 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, TEXT_END_LABEL, 0);
11918 #if 0
11919 /* Output a terminator label for the .data section. */
11920 fputc ('\n', asm_out_file);
11921 ASM_OUTPUT_SECTION (asm_out_file, DATA_SECTION);
11922 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, DATA_END_LABEL, 0);
11924 /* Output a terminator label for the .bss section. */
11925 fputc ('\n', asm_out_file);
11926 ASM_OUTPUT_SECTION (asm_out_file, BSS_SECTION);
11927 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, BSS_END_LABEL, 0);
11928 #endif
11930 /* Output the source line correspondence table. */
11931 if (line_info_table_in_use > 1 || separate_line_info_table_in_use)
11933 if (! DWARF2_ASM_LINE_DEBUG_INFO)
11935 fputc ('\n', asm_out_file);
11936 ASM_OUTPUT_SECTION (asm_out_file, DEBUG_LINE_SECTION);
11937 output_line_info ();
11940 /* We can only use the low/high_pc attributes if all of the code
11941 was in .text. */
11942 if (separate_line_info_table_in_use == 0)
11944 add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
11945 add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
11948 add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
11949 debug_line_section_label);
11952 #if 0 /* unimplemented */
11953 if (debug_info_level >= DINFO_LEVEL_VERBOSE && primary)
11954 add_AT_unsigned (die, DW_AT_macro_info, 0);
11955 #endif
11957 /* Output all of the compilation units. We put the main one last so that
11958 the offsets are available to output_pubnames. */
11959 for (node = limbo_die_list; node; node = node->next)
11960 output_comp_unit (node->die);
11961 output_comp_unit (comp_unit_die);
11963 /* Output the abbreviation table. */
11964 fputc ('\n', asm_out_file);
11965 ASM_OUTPUT_SECTION (asm_out_file, ABBREV_SECTION);
11966 output_abbrev_section ();
11968 if (pubname_table_in_use)
11970 /* Output public names table. */
11971 fputc ('\n', asm_out_file);
11972 ASM_OUTPUT_SECTION (asm_out_file, PUBNAMES_SECTION);
11973 output_pubnames ();
11976 /* We only put functions in the arange table, so don't write it out if
11977 we don't have any. */
11978 if (fde_table_in_use)
11980 /* Output the address range information. */
11981 fputc ('\n', asm_out_file);
11982 ASM_OUTPUT_SECTION (asm_out_file, ARANGES_SECTION);
11983 output_aranges ();
11986 #endif /* DWARF2_DEBUGGING_INFO */