OSDN Git Service

Remove previous dwarf2out.c change. It was incorrect according to Jim.
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from the GNU C compiler.
2    Copyright (C) 1992, 93, 95, 96, 97, 1998 Free Software Foundation, Inc.
3    Contributed by Gary Funck (gary@intrepid.com).
4    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5    Extensively modified by Jason Merrill (jason@cygnus.com).
6
7 This file is part of GNU CC.
8
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING.  If not, write to
21 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
22
23 /* The first part of this file deals with the DWARF 2 frame unwind
24    information, which is also used by the GCC efficient exception handling
25    mechanism.  The second part, controlled only by an #ifdef
26    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
27    information.  */
28
29 #include "config.h"
30 #include "system.h"
31 #include "defaults.h"
32 #include "tree.h"
33 #include "flags.h"
34 #include "rtl.h"
35 #include "hard-reg-set.h"
36 #include "regs.h"
37 #include "insn-config.h"
38 #include "reload.h"
39 #include "output.h"
40 #include "expr.h"
41 #include "except.h"
42 #include "dwarf2.h"
43 #include "toplev.h"
44
45 /* We cannot use <assert.h> in GCC source, since that would include
46    GCC's assert.h, which may not be compatible with the host compiler.  */
47 #undef assert
48 #ifdef NDEBUG
49 # define assert(e)
50 #else
51 # define assert(e) do { if (! (e)) abort (); } while (0)
52 #endif
53
54 /* Decide whether we want to emit frame unwind information for the current
55    translation unit.  */
56
57 int
58 dwarf2out_do_frame ()
59 {
60   return (write_symbols == DWARF2_DEBUG
61 #ifdef DWARF2_UNWIND_INFO
62           || (flag_exceptions && ! exceptions_via_longjmp)
63 #endif
64           );
65 }
66
67 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
68
69 #ifndef __GNUC__
70 #define inline
71 #endif
72
73 /* How to start an assembler comment.  */
74 #ifndef ASM_COMMENT_START
75 #define ASM_COMMENT_START ";#"
76 #endif
77
78 typedef struct dw_cfi_struct *dw_cfi_ref;
79 typedef struct dw_fde_struct *dw_fde_ref;
80 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
81
82 /* Call frames are described using a sequence of Call Frame
83    Information instructions.  The register number, offset
84    and address fields are provided as possible operands;
85    their use is selected by the opcode field.  */
86
87 typedef union dw_cfi_oprnd_struct
88 {
89   unsigned long dw_cfi_reg_num;
90   long int dw_cfi_offset;
91   char *dw_cfi_addr;
92 }
93 dw_cfi_oprnd;
94
95 typedef struct dw_cfi_struct
96 {
97   dw_cfi_ref dw_cfi_next;
98   enum dwarf_call_frame_info dw_cfi_opc;
99   dw_cfi_oprnd dw_cfi_oprnd1;
100   dw_cfi_oprnd dw_cfi_oprnd2;
101 }
102 dw_cfi_node;
103
104 /* All call frame descriptions (FDE's) in the GCC generated DWARF
105    refer to a single Common Information Entry (CIE), defined at
106    the beginning of the .debug_frame section.  This used of a single
107    CIE obviates the need to keep track of multiple CIE's
108    in the DWARF generation routines below.  */
109
110 typedef struct dw_fde_struct
111 {
112   char *dw_fde_begin;
113   char *dw_fde_current_label;
114   char *dw_fde_end;
115   dw_cfi_ref dw_fde_cfi;
116 }
117 dw_fde_node;
118
119 /* Maximum size (in bytes) of an artificially generated label.   */
120 #define MAX_ARTIFICIAL_LABEL_BYTES      30
121
122 /* Make sure we know the sizes of the various types dwarf can describe. These
123    are only defaults.  If the sizes are different for your target, you should
124    override these values by defining the appropriate symbols in your tm.h
125    file.  */
126
127 #ifndef CHAR_TYPE_SIZE
128 #define CHAR_TYPE_SIZE BITS_PER_UNIT
129 #endif
130 #ifndef PTR_SIZE
131 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
132 #endif
133
134 /* The size in bytes of a DWARF field indicating an offset or length
135    relative to a debug info section, specified to be 4 bytes in the DWARF-2
136    specification.  The SGI/MIPS ABI defines it to be the same as PTR_SIZE.  */
137
138 #ifndef DWARF_OFFSET_SIZE
139 #define DWARF_OFFSET_SIZE 4
140 #endif
141
142 #define DWARF_VERSION 2
143
144 /* Round SIZE up to the nearest BOUNDARY.  */
145 #define DWARF_ROUND(SIZE,BOUNDARY) \
146   (((SIZE) + (BOUNDARY) - 1) & ~((BOUNDARY) - 1))
147
148 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
149 #ifdef STACK_GROWS_DOWNWARD
150 #define DWARF_CIE_DATA_ALIGNMENT (-UNITS_PER_WORD)
151 #else
152 #define DWARF_CIE_DATA_ALIGNMENT UNITS_PER_WORD
153 #endif
154
155 /* A pointer to the base of a table that contains frame description
156    information for each routine.  */
157 static dw_fde_ref fde_table;
158
159 /* Number of elements currently allocated for fde_table.  */
160 static unsigned fde_table_allocated;
161
162 /* Number of elements in fde_table currently in use.  */
163 static unsigned fde_table_in_use;
164
165 /* Size (in elements) of increments by which we may expand the
166    fde_table.  */
167 #define FDE_TABLE_INCREMENT 256
168
169 /* A list of call frame insns for the CIE.  */
170 static dw_cfi_ref cie_cfi_head;
171
172 /* The number of the current function definition for which debugging
173    information is being generated.  These numbers range from 1 up to the
174    maximum number of function definitions contained within the current
175    compilation unit.  These numbers are used to create unique label id's
176    unique to each function definition.  */
177 static unsigned current_funcdef_number = 0;
178
179 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
180    attribute that accelerates the lookup of the FDE associated
181    with the subprogram.  This variable holds the table index of the FDE 
182    associated with the current function (body) definition.  */
183 static unsigned current_funcdef_fde;
184
185 /* Forward declarations for functions defined in this file.  */
186
187 static char *stripattributes            PROTO((char *));
188 static char *dwarf_cfi_name             PROTO((unsigned));
189 static dw_cfi_ref new_cfi               PROTO((void));
190 static void add_cfi                     PROTO((dw_cfi_ref *, dw_cfi_ref));
191 static unsigned long size_of_uleb128    PROTO((unsigned long));
192 static unsigned long size_of_sleb128    PROTO((long));
193 static void output_uleb128              PROTO((unsigned long));
194 static void output_sleb128              PROTO((long));
195 static void add_fde_cfi                 PROTO((char *, dw_cfi_ref));
196 static void lookup_cfa_1                PROTO((dw_cfi_ref, unsigned long *,
197                                                long *));
198 static void lookup_cfa                  PROTO((unsigned long *, long *));
199 static void reg_save                    PROTO((char *, unsigned, unsigned,
200                                                long));
201 static void initial_return_save         PROTO((rtx));
202 static void output_cfi                  PROTO((dw_cfi_ref, dw_fde_ref));
203 static void output_call_frame_info      PROTO((int));
204 static unsigned reg_number              PROTO((rtx));
205 static void dwarf2out_stack_adjust      PROTO((rtx));
206
207 /* Definitions of defaults for assembler-dependent names of various
208    pseudo-ops and section names.
209    Theses may be overridden in the tm.h file (if necessary) for a particular
210    assembler.  */
211
212 #ifdef OBJECT_FORMAT_ELF
213 #ifndef UNALIGNED_SHORT_ASM_OP
214 #define UNALIGNED_SHORT_ASM_OP  ".2byte"
215 #endif
216 #ifndef UNALIGNED_INT_ASM_OP
217 #define UNALIGNED_INT_ASM_OP    ".4byte"
218 #endif
219 #ifndef UNALIGNED_DOUBLE_INT_ASM_OP
220 #define UNALIGNED_DOUBLE_INT_ASM_OP     ".8byte"
221 #endif
222 #endif /* OBJECT_FORMAT_ELF */
223
224 #ifndef ASM_BYTE_OP
225 #define ASM_BYTE_OP             ".byte"
226 #endif
227
228 /* Data and reference forms for relocatable data.  */
229 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
230 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
231
232 /* Pseudo-op for defining a new section.  */
233 #ifndef SECTION_ASM_OP
234 #define SECTION_ASM_OP  ".section"
235 #endif
236
237 /* The default format used by the ASM_OUTPUT_SECTION macro (see below) to
238    print the SECTION_ASM_OP and the section name.  The default here works for
239    almost all svr4 assemblers, except for the sparc, where the section name
240    must be enclosed in double quotes.  (See sparcv4.h).  */
241 #ifndef SECTION_FORMAT
242 #ifdef PUSHSECTION_FORMAT
243 #define SECTION_FORMAT PUSHSECTION_FORMAT
244 #else
245 #define SECTION_FORMAT          "\t%s\t%s\n"
246 #endif
247 #endif
248
249 #ifndef FRAME_SECTION
250 #define FRAME_SECTION           ".debug_frame"
251 #endif
252
253 #ifndef FUNC_BEGIN_LABEL
254 #define FUNC_BEGIN_LABEL        "LFB"
255 #endif
256 #ifndef FUNC_END_LABEL
257 #define FUNC_END_LABEL          "LFE"
258 #endif
259 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
260 #define CIE_END_LABEL           "LECIE"
261 #define CIE_LENGTH_LABEL        "LLCIE"
262 #define FDE_AFTER_SIZE_LABEL    "LSFDE"
263 #define FDE_END_LABEL           "LEFDE"
264 #define FDE_LENGTH_LABEL        "LLFDE"
265
266 /* Definitions of defaults for various types of primitive assembly language
267    output operations.  These may be overridden from within the tm.h file,
268    but typically, that is unnecessary.  */
269
270 #ifndef ASM_OUTPUT_SECTION
271 #define ASM_OUTPUT_SECTION(FILE, SECTION) \
272   fprintf ((FILE), SECTION_FORMAT, SECTION_ASM_OP, SECTION)
273 #endif
274
275 #ifndef ASM_OUTPUT_DWARF_DATA1
276 #define ASM_OUTPUT_DWARF_DATA1(FILE,VALUE) \
277   fprintf ((FILE), "\t%s\t0x%x", ASM_BYTE_OP, VALUE)
278 #endif
279
280 #ifndef ASM_OUTPUT_DWARF_DELTA1
281 #define ASM_OUTPUT_DWARF_DELTA1(FILE,LABEL1,LABEL2)                     \
282  do {   fprintf ((FILE), "\t%s\t", ASM_BYTE_OP);                        \
283         assemble_name (FILE, LABEL1);                                   \
284         fprintf (FILE, "-");                                            \
285         assemble_name (FILE, LABEL2);                                   \
286   } while (0)
287 #endif
288
289 #ifdef UNALIGNED_INT_ASM_OP
290
291 #ifndef UNALIGNED_OFFSET_ASM_OP
292 #define UNALIGNED_OFFSET_ASM_OP \
293   (DWARF_OFFSET_SIZE == 8 ? UNALIGNED_DOUBLE_INT_ASM_OP : UNALIGNED_INT_ASM_OP)
294 #endif
295
296 #ifndef UNALIGNED_WORD_ASM_OP
297 #define UNALIGNED_WORD_ASM_OP \
298   (PTR_SIZE == 8 ? UNALIGNED_DOUBLE_INT_ASM_OP : UNALIGNED_INT_ASM_OP)
299 #endif
300
301 #ifndef ASM_OUTPUT_DWARF_DELTA2
302 #define ASM_OUTPUT_DWARF_DELTA2(FILE,LABEL1,LABEL2)                     \
303  do {   fprintf ((FILE), "\t%s\t", UNALIGNED_SHORT_ASM_OP);             \
304         assemble_name (FILE, LABEL1);                                   \
305         fprintf (FILE, "-");                                            \
306         assemble_name (FILE, LABEL2);                                   \
307   } while (0)
308 #endif
309
310 #ifndef ASM_OUTPUT_DWARF_DELTA4
311 #define ASM_OUTPUT_DWARF_DELTA4(FILE,LABEL1,LABEL2)                     \
312  do {   fprintf ((FILE), "\t%s\t", UNALIGNED_INT_ASM_OP);               \
313         assemble_name (FILE, LABEL1);                                   \
314         fprintf (FILE, "-");                                            \
315         assemble_name (FILE, LABEL2);                                   \
316   } while (0)
317 #endif
318
319 #ifndef ASM_OUTPUT_DWARF_DELTA
320 #define ASM_OUTPUT_DWARF_DELTA(FILE,LABEL1,LABEL2)                      \
321  do {   fprintf ((FILE), "\t%s\t", UNALIGNED_OFFSET_ASM_OP);            \
322         assemble_name (FILE, LABEL1);                                   \
323         fprintf (FILE, "-");                                            \
324         assemble_name (FILE, LABEL2);                                   \
325   } while (0)
326 #endif
327
328 #ifndef ASM_OUTPUT_DWARF_ADDR_DELTA
329 #define ASM_OUTPUT_DWARF_ADDR_DELTA(FILE,LABEL1,LABEL2)                 \
330  do {   fprintf ((FILE), "\t%s\t", UNALIGNED_WORD_ASM_OP);              \
331         assemble_name (FILE, LABEL1);                                   \
332         fprintf (FILE, "-");                                            \
333         assemble_name (FILE, LABEL2);                                   \
334   } while (0)
335 #endif
336
337 #ifndef ASM_OUTPUT_DWARF_ADDR
338 #define ASM_OUTPUT_DWARF_ADDR(FILE,LABEL)                               \
339  do {   fprintf ((FILE), "\t%s\t", UNALIGNED_WORD_ASM_OP);              \
340         assemble_name (FILE, LABEL);                                    \
341   } while (0)
342 #endif
343
344 #ifndef ASM_OUTPUT_DWARF_ADDR_CONST
345 #define ASM_OUTPUT_DWARF_ADDR_CONST(FILE,ADDR)                          \
346   fprintf ((FILE), "\t%s\t%s", UNALIGNED_WORD_ASM_OP, (ADDR))
347 #endif
348
349 #ifndef ASM_OUTPUT_DWARF_OFFSET4
350 #define ASM_OUTPUT_DWARF_OFFSET4(FILE,LABEL) \
351  do {   fprintf ((FILE), "\t%s\t", UNALIGNED_INT_ASM_OP);               \
352         assemble_name (FILE, LABEL);                                    \
353   } while (0)
354 #endif
355
356 #ifndef ASM_OUTPUT_DWARF_OFFSET
357 #define ASM_OUTPUT_DWARF_OFFSET(FILE,LABEL)                             \
358  do {   fprintf ((FILE), "\t%s\t", UNALIGNED_OFFSET_ASM_OP);            \
359         assemble_name (FILE, LABEL);                                    \
360   } while (0)
361 #endif
362
363 #ifndef ASM_OUTPUT_DWARF_DATA2
364 #define ASM_OUTPUT_DWARF_DATA2(FILE,VALUE) \
365   fprintf ((FILE), "\t%s\t0x%x", UNALIGNED_SHORT_ASM_OP, (unsigned) VALUE)
366 #endif
367
368 #ifndef ASM_OUTPUT_DWARF_DATA4
369 #define ASM_OUTPUT_DWARF_DATA4(FILE,VALUE) \
370   fprintf ((FILE), "\t%s\t0x%x", UNALIGNED_INT_ASM_OP, (unsigned) VALUE)
371 #endif
372
373 #ifndef ASM_OUTPUT_DWARF_DATA
374 #define ASM_OUTPUT_DWARF_DATA(FILE,VALUE) \
375   fprintf ((FILE), "\t%s\t0x%lx", UNALIGNED_OFFSET_ASM_OP, \
376            (unsigned long) VALUE)
377 #endif
378
379 #ifndef ASM_OUTPUT_DWARF_ADDR_DATA
380 #define ASM_OUTPUT_DWARF_ADDR_DATA(FILE,VALUE) \
381   fprintf ((FILE), "\t%s\t0x%lx", UNALIGNED_WORD_ASM_OP, \
382            (unsigned long) VALUE)
383 #endif
384
385 #ifndef ASM_OUTPUT_DWARF_DATA8
386 #define ASM_OUTPUT_DWARF_DATA8(FILE,HIGH_VALUE,LOW_VALUE)               \
387   do {                                                                  \
388     if (WORDS_BIG_ENDIAN)                                               \
389       {                                                                 \
390         fprintf ((FILE), "\t%s\t0x%lx\n", UNALIGNED_INT_ASM_OP, HIGH_VALUE); \
391         fprintf ((FILE), "\t%s\t0x%lx", UNALIGNED_INT_ASM_OP, LOW_VALUE);\
392       }                                                                 \
393     else                                                                \
394       {                                                                 \
395         fprintf ((FILE), "\t%s\t0x%lx\n", UNALIGNED_INT_ASM_OP, LOW_VALUE);\
396         fprintf ((FILE), "\t%s\t0x%lx", UNALIGNED_INT_ASM_OP, HIGH_VALUE); \
397       }                                                                 \
398   } while (0)
399 #endif
400
401 #else /* UNALIGNED_INT_ASM_OP */
402
403 /* We don't have unaligned support, let's hope the normal output works for
404    .debug_frame.  */
405
406 #define ASM_OUTPUT_DWARF_ADDR(FILE,LABEL) \
407   assemble_integer (gen_rtx_SYMBOL_REF (Pmode, LABEL), PTR_SIZE, 1)
408
409 #define ASM_OUTPUT_DWARF_OFFSET4(FILE,LABEL) \
410   assemble_integer (gen_rtx_SYMBOL_REF (SImode, LABEL), 4, 1)
411
412 #define ASM_OUTPUT_DWARF_OFFSET(FILE,LABEL) \
413   assemble_integer (gen_rtx_SYMBOL_REF (SImode, LABEL), 4, 1)
414
415 #define ASM_OUTPUT_DWARF_DELTA2(FILE,LABEL1,LABEL2)                     \
416   assemble_integer (gen_rtx_MINUS (HImode,                              \
417                              gen_rtx_SYMBOL_REF (Pmode, LABEL1),        \
418                              gen_rtx_SYMBOL_REF (Pmode, LABEL2)),       \
419                     2, 1)
420   
421 #define ASM_OUTPUT_DWARF_DELTA4(FILE,LABEL1,LABEL2)                     \
422   assemble_integer (gen_rtx_MINUS (SImode,                              \
423                              gen_rtx_SYMBOL_REF (Pmode, LABEL1),        \
424                              gen_rtx_SYMBOL_REF (Pmode, LABEL2)),       \
425                     4, 1)
426
427 #define ASM_OUTPUT_DWARF_ADDR_DELTA(FILE,LABEL1,LABEL2)                 \
428   assemble_integer (gen_rtx_MINUS (Pmode,                               \
429                              gen_rtx_SYMBOL_REF (Pmode, LABEL1),        \
430                              gen_rtx_SYMBOL_REF (Pmode, LABEL2)),       \
431                     PTR_SIZE, 1)
432
433 #define ASM_OUTPUT_DWARF_DELTA(FILE,LABEL1,LABEL2) \
434   ASM_OUTPUT_DWARF_DELTA4 (FILE,LABEL1,LABEL2)
435
436 #define ASM_OUTPUT_DWARF_DATA4(FILE,VALUE) \
437   assemble_integer (GEN_INT (VALUE), 4, 1)
438
439 #endif /* UNALIGNED_INT_ASM_OP */
440
441 #ifdef SET_ASM_OP
442 #ifndef ASM_OUTPUT_DEFINE_LABEL_DIFFERENCE_SYMBOL
443 #define ASM_OUTPUT_DEFINE_LABEL_DIFFERENCE_SYMBOL(FILE, SY, HI, LO)     \
444  do {                                                                   \
445   fprintf (FILE, "\t%s\t", SET_ASM_OP);                                 \
446   assemble_name (FILE, SY);                                             \
447   fputc (',', FILE);                                                    \
448   assemble_name (FILE, HI);                                             \
449   fputc ('-', FILE);                                                    \
450   assemble_name (FILE, LO);                                             \
451  } while (0)
452 #endif
453 #endif /* SET_ASM_OP */
454
455 /* This is similar to the default ASM_OUTPUT_ASCII, except that no trailing
456    newline is produced.  When flag_debug_asm is asserted, we add commentary
457    at the end of the line, so we must avoid output of a newline here.  */
458 #ifndef ASM_OUTPUT_DWARF_STRING
459 #define ASM_OUTPUT_DWARF_STRING(FILE,P) \
460   do {                                                                        \
461     register int slen = strlen(P);                                            \
462     register char *p = (P);                                                   \
463     register int i;                                                           \
464     fprintf (FILE, "\t.ascii \"");                                            \
465     for (i = 0; i < slen; i++)                                                \
466       {                                                                       \
467           register int c = p[i];                                              \
468           if (c == '\"' || c == '\\')                                         \
469             putc ('\\', FILE);                                                \
470           if (c >= ' ' && c < 0177)                                           \
471             putc (c, FILE);                                                   \
472           else                                                                \
473             {                                                                 \
474               fprintf (FILE, "\\%o", c);                                      \
475             }                                                                 \
476       }                                                                       \
477     fprintf (FILE, "\\0\"");                                                  \
478   }                                                                           \
479   while (0)
480 #endif
481
482 /* The DWARF 2 CFA column which tracks the return address.  Normally this
483    is the column for PC, or the first column after all of the hard
484    registers.  */
485 #ifndef DWARF_FRAME_RETURN_COLUMN
486 #ifdef PC_REGNUM
487 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
488 #else
489 #define DWARF_FRAME_RETURN_COLUMN       FIRST_PSEUDO_REGISTER
490 #endif
491 #endif
492
493 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
494    default, we just provide columns for all registers.  */
495 #ifndef DWARF_FRAME_REGNUM
496 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
497 #endif
498
499 /* Hook used by __throw.  */
500
501 rtx
502 expand_builtin_dwarf_fp_regnum ()
503 {
504   return GEN_INT (DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM));
505 }
506
507 /* The offset from the incoming value of %sp to the top of the stack frame
508    for the current function.  */
509 #ifndef INCOMING_FRAME_SP_OFFSET
510 #define INCOMING_FRAME_SP_OFFSET 0
511 #endif
512
513 /* Return a pointer to a copy of the section string name S with all
514    attributes stripped off.  */
515
516 static inline char *
517 stripattributes (s)
518      char *s;
519 {
520   char *stripped = xstrdup (s);
521   char *p = stripped;
522
523   while (*p && *p != ',')
524     p++;
525
526   *p = '\0';
527   return stripped;
528 }
529
530 /* Return the register number described by a given RTL node.  */
531
532 static unsigned
533 reg_number (rtl)
534      register rtx rtl;
535 {
536   register unsigned regno = REGNO (rtl);
537
538   if (regno >= FIRST_PSEUDO_REGISTER)
539     {
540       warning ("internal regno botch: regno = %d\n", regno);
541       regno = 0;
542     }
543
544   regno = DBX_REGISTER_NUMBER (regno);
545   return regno;
546 }
547
548 struct reg_size_range
549 {
550   int beg;
551   int end;
552   int size;
553 };
554
555 /* Given a register number in REG_TREE, return an rtx for its size in bytes.
556    We do this in kind of a roundabout way, by building up a list of
557    register size ranges and seeing where our register falls in one of those
558    ranges.  We need to do it this way because REG_TREE is not a constant,
559    and the target macros were not designed to make this task easy.  */
560
561 rtx
562 expand_builtin_dwarf_reg_size (reg_tree, target)
563      tree reg_tree;
564      rtx target;
565 {
566   enum machine_mode mode;
567   int size;
568   struct reg_size_range ranges[5];
569   tree t, t2;
570
571   int i = 0;
572   int n_ranges = 0;
573   int last_size = -1;
574
575   for (; i < FIRST_PSEUDO_REGISTER; ++i)
576     {
577       /* The return address is out of order on the MIPS, and we don't use
578          copy_reg for it anyway, so we don't care here how large it is.  */
579       if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
580         continue;
581
582       mode = reg_raw_mode[i];
583
584       /* CCmode is arbitrarily given a size of 4 bytes.  It is more useful
585          to use the same size as word_mode, since that reduces the number
586          of ranges we need.  It should not matter, since the result should
587          never be used for a condition code register anyways.  */
588       if (GET_MODE_CLASS (mode) == MODE_CC)
589         mode = word_mode;
590
591       size = GET_MODE_SIZE (mode);
592
593       /* If this register is not valid in the specified mode and
594          we have a previous size, use that for the size of this
595          register to avoid making junk tiny ranges.  */
596       if (! HARD_REGNO_MODE_OK (i, mode) && last_size != -1)
597         size = last_size;
598
599       if (size != last_size)
600         {
601           ranges[n_ranges].beg = i;
602           ranges[n_ranges].size = last_size = size;
603           ++n_ranges;
604           if (n_ranges >= 5)
605             abort ();
606         }
607       ranges[n_ranges-1].end = i;
608     }
609
610   /* The usual case: fp regs surrounded by general regs.  */
611   if (n_ranges == 3 && ranges[0].size == ranges[2].size)
612     {
613       if ((DWARF_FRAME_REGNUM (ranges[1].end)
614            - DWARF_FRAME_REGNUM (ranges[1].beg))
615           != ranges[1].end - ranges[1].beg)
616         abort ();
617       t  = fold (build (GE_EXPR, integer_type_node, reg_tree,
618                         build_int_2 (DWARF_FRAME_REGNUM (ranges[1].beg), 0)));
619       t2 = fold (build (LE_EXPR, integer_type_node, reg_tree,
620                         build_int_2 (DWARF_FRAME_REGNUM (ranges[1].end), 0)));
621       t = fold (build (TRUTH_ANDIF_EXPR, integer_type_node, t, t2));
622       t = fold (build (COND_EXPR, integer_type_node, t,
623                        build_int_2 (ranges[1].size, 0),
624                        build_int_2 (ranges[0].size, 0)));
625     }
626   else
627     {
628       --n_ranges;
629       t = build_int_2 (ranges[n_ranges].size, 0);
630       size = DWARF_FRAME_REGNUM (ranges[n_ranges].beg);
631       for (; n_ranges--; )
632         {
633           if ((DWARF_FRAME_REGNUM (ranges[n_ranges].end)
634                - DWARF_FRAME_REGNUM (ranges[n_ranges].beg))
635               != ranges[n_ranges].end - ranges[n_ranges].beg)
636             abort ();
637           if (DWARF_FRAME_REGNUM (ranges[n_ranges].beg) >= size)
638             abort ();
639           size = DWARF_FRAME_REGNUM (ranges[n_ranges].beg);
640           t2 = fold (build (LE_EXPR, integer_type_node, reg_tree,
641                             build_int_2 (DWARF_FRAME_REGNUM
642                                          (ranges[n_ranges].end), 0)));
643           t = fold (build (COND_EXPR, integer_type_node, t2,
644                            build_int_2 (ranges[n_ranges].size, 0), t));
645         }
646     }
647   return expand_expr (t, target, Pmode, 0);
648 }
649
650 /* Convert a DWARF call frame info. operation to its string name */
651
652 static char *
653 dwarf_cfi_name (cfi_opc)
654      register unsigned cfi_opc;
655 {
656   switch (cfi_opc)
657     {
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
695     /* SGI/MIPS specific */
696     case DW_CFA_MIPS_advance_loc8:
697       return "DW_CFA_MIPS_advance_loc8";
698
699     /* GNU extensions */
700     case DW_CFA_GNU_window_save:
701       return "DW_CFA_GNU_window_save";
702     case DW_CFA_GNU_args_size:
703       return "DW_CFA_GNU_args_size";
704
705     default:
706       return "DW_CFA_<unknown>";
707     }
708 }
709
710 /* Return a pointer to a newly allocated Call Frame Instruction.  */
711
712 static inline dw_cfi_ref
713 new_cfi ()
714 {
715   register dw_cfi_ref cfi = (dw_cfi_ref) xmalloc (sizeof (dw_cfi_node));
716
717   cfi->dw_cfi_next = NULL;
718   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
719   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
720
721   return cfi;
722 }
723
724 /* Add a Call Frame Instruction to list of instructions.  */
725
726 static inline void
727 add_cfi (list_head, cfi)
728      register dw_cfi_ref *list_head;
729      register dw_cfi_ref cfi;
730 {
731   register dw_cfi_ref *p;
732
733   /* Find the end of the chain.  */
734   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
735     ;
736
737   *p = cfi;
738 }
739
740 /* Generate a new label for the CFI info to refer to.  */
741
742 char *
743 dwarf2out_cfi_label ()
744 {
745   static char label[20];
746   static unsigned long label_num = 0;
747   
748   ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", label_num++);
749   ASM_OUTPUT_LABEL (asm_out_file, label);
750
751   return label;
752 }
753
754 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
755    or to the CIE if LABEL is NULL.  */
756
757 static void
758 add_fde_cfi (label, cfi)
759      register char *label;
760      register dw_cfi_ref cfi;
761 {
762   if (label)
763     {
764       register dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
765
766       if (*label == 0)
767         label = dwarf2out_cfi_label ();
768
769       if (fde->dw_fde_current_label == NULL
770           || strcmp (label, fde->dw_fde_current_label) != 0)
771         {
772           register dw_cfi_ref xcfi;
773
774           fde->dw_fde_current_label = label = xstrdup (label);
775
776           /* Set the location counter to the new label.  */
777           xcfi = new_cfi ();
778           xcfi->dw_cfi_opc = DW_CFA_advance_loc4;
779           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
780           add_cfi (&fde->dw_fde_cfi, xcfi);
781         }
782
783       add_cfi (&fde->dw_fde_cfi, cfi);
784     }
785
786   else
787     add_cfi (&cie_cfi_head, cfi);
788 }
789
790 /* Subroutine of lookup_cfa.  */
791
792 static inline void
793 lookup_cfa_1 (cfi, regp, offsetp)
794      register dw_cfi_ref cfi;
795      register unsigned long *regp;
796      register long *offsetp;
797 {
798   switch (cfi->dw_cfi_opc)
799     {
800     case DW_CFA_def_cfa_offset:
801       *offsetp = cfi->dw_cfi_oprnd1.dw_cfi_offset;
802       break;
803     case DW_CFA_def_cfa_register:
804       *regp = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
805       break;
806     case DW_CFA_def_cfa:
807       *regp = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
808       *offsetp = cfi->dw_cfi_oprnd2.dw_cfi_offset;
809       break;
810     default:
811       break;
812     }
813 }
814
815 /* Find the previous value for the CFA.  */
816
817 static void
818 lookup_cfa (regp, offsetp)
819      register unsigned long *regp;
820      register long *offsetp;
821 {
822   register dw_cfi_ref cfi;
823
824   *regp = (unsigned long) -1;
825   *offsetp = 0;
826
827   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
828     lookup_cfa_1 (cfi, regp, offsetp);
829
830   if (fde_table_in_use)
831     {
832       register dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
833       for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
834         lookup_cfa_1 (cfi, regp, offsetp);
835     }
836 }
837
838 /* The current rule for calculating the DWARF2 canonical frame address.  */
839 static unsigned long cfa_reg;
840 static long cfa_offset;
841
842 /* The register used for saving registers to the stack, and its offset
843    from the CFA.  */
844 static unsigned cfa_store_reg;
845 static long cfa_store_offset;
846
847 /* The running total of the size of arguments pushed onto the stack.  */
848 static long args_size;
849
850 /* The last args_size we actually output.  */
851 static long old_args_size;
852
853 /* Entry point to update the canonical frame address (CFA).
854    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
855    calculated from REG+OFFSET.  */
856
857 void
858 dwarf2out_def_cfa (label, reg, offset)
859      register char *label;
860      register unsigned reg;
861      register long offset;
862 {
863   register dw_cfi_ref cfi;
864   unsigned long old_reg;
865   long old_offset;
866
867   cfa_reg = reg;
868   cfa_offset = offset;
869   if (cfa_store_reg == reg)
870     cfa_store_offset = offset;
871
872   reg = DWARF_FRAME_REGNUM (reg);
873   lookup_cfa (&old_reg, &old_offset);
874
875   if (reg == old_reg && offset == old_offset)
876     return;
877
878   cfi = new_cfi ();
879
880   if (reg == old_reg)
881     {
882       cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
883       cfi->dw_cfi_oprnd1.dw_cfi_offset = offset;
884     }
885
886 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
887   else if (offset == old_offset && old_reg != (unsigned long) -1)
888     {
889       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
890       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
891     }
892 #endif
893
894   else
895     {
896       cfi->dw_cfi_opc = DW_CFA_def_cfa;
897       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
898       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
899     }
900
901   add_fde_cfi (label, cfi);
902 }
903
904 /* Add the CFI for saving a register.  REG is the CFA column number.
905    LABEL is passed to add_fde_cfi.
906    If SREG is -1, the register is saved at OFFSET from the CFA;
907    otherwise it is saved in SREG.  */
908
909 static void
910 reg_save (label, reg, sreg, offset)
911      register char * label;
912      register unsigned reg;
913      register unsigned sreg;
914      register long offset;
915 {
916   register dw_cfi_ref cfi = new_cfi ();
917
918   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
919
920   /* The following comparison is correct. -1 is used to indicate that
921      the value isn't a register number.  */
922   if (sreg == (unsigned int) -1)
923     {
924       if (reg & ~0x3f)
925         /* The register number won't fit in 6 bits, so we have to use
926            the long form.  */
927         cfi->dw_cfi_opc = DW_CFA_offset_extended;
928       else
929         cfi->dw_cfi_opc = DW_CFA_offset;
930
931       offset /= DWARF_CIE_DATA_ALIGNMENT;
932       if (offset < 0)
933         abort ();
934       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
935     }
936   else
937     {
938       cfi->dw_cfi_opc = DW_CFA_register;
939       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
940     }
941
942   add_fde_cfi (label, cfi);
943 }
944
945 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
946    This CFI tells the unwinder that it needs to restore the window registers
947    from the previous frame's window save area.
948    
949    ??? Perhaps we should note in the CIE where windows are saved (instead of
950    assuming 0(cfa)) and what registers are in the window.  */
951
952 void
953 dwarf2out_window_save (label)
954      register char * label;
955 {
956   register dw_cfi_ref cfi = new_cfi ();
957   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
958   add_fde_cfi (label, cfi);
959 }
960
961 /* Add a CFI to update the running total of the size of arguments
962    pushed onto the stack.  */
963
964 void
965 dwarf2out_args_size (label, size)
966      char *label;
967      long size;
968 {
969   register dw_cfi_ref cfi;
970
971   if (size == old_args_size)
972     return;
973   old_args_size = size;
974
975   cfi = new_cfi ();
976   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
977   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
978   add_fde_cfi (label, cfi);
979 }
980
981 /* Entry point for saving a register to the stack.  REG is the GCC register
982    number.  LABEL and OFFSET are passed to reg_save.  */
983
984 void
985 dwarf2out_reg_save (label, reg, offset)
986      register char * label;
987      register unsigned reg;
988      register long offset;
989 {
990   reg_save (label, DWARF_FRAME_REGNUM (reg), -1, offset);
991 }
992
993 /* Entry point for saving the return address in the stack.
994    LABEL and OFFSET are passed to reg_save.  */
995
996 void
997 dwarf2out_return_save (label, offset)
998      register char * label;
999      register long offset;
1000 {
1001   reg_save (label, DWARF_FRAME_RETURN_COLUMN, -1, offset);
1002 }
1003
1004 /* Entry point for saving the return address in a register.
1005    LABEL and SREG are passed to reg_save.  */
1006
1007 void
1008 dwarf2out_return_reg (label, sreg)
1009      register char * label;
1010      register unsigned sreg;
1011 {
1012   reg_save (label, DWARF_FRAME_RETURN_COLUMN, sreg, 0);
1013 }
1014
1015 /* Record the initial position of the return address.  RTL is
1016    INCOMING_RETURN_ADDR_RTX.  */
1017
1018 static void
1019 initial_return_save (rtl)
1020      register rtx rtl;
1021 {
1022   unsigned reg = -1;
1023   long offset = 0;
1024
1025   switch (GET_CODE (rtl))
1026     {
1027     case REG:
1028       /* RA is in a register.  */
1029       reg = reg_number (rtl);
1030       break;
1031     case MEM:
1032       /* RA is on the stack.  */
1033       rtl = XEXP (rtl, 0);
1034       switch (GET_CODE (rtl))
1035         {
1036         case REG:
1037           if (REGNO (rtl) != STACK_POINTER_REGNUM)
1038             abort ();
1039           offset = 0;
1040           break;
1041         case PLUS:
1042           if (REGNO (XEXP (rtl, 0)) != STACK_POINTER_REGNUM)
1043             abort ();
1044           offset = INTVAL (XEXP (rtl, 1));
1045           break;
1046         case MINUS:
1047           if (REGNO (XEXP (rtl, 0)) != STACK_POINTER_REGNUM)
1048             abort ();
1049           offset = -INTVAL (XEXP (rtl, 1));
1050           break;
1051         default:
1052           abort ();
1053         }
1054       break;
1055     case PLUS:
1056       /* The return address is at some offset from any value we can
1057          actually load.  For instance, on the SPARC it is in %i7+8. Just
1058          ignore the offset for now; it doesn't matter for unwinding frames.  */
1059       if (GET_CODE (XEXP (rtl, 1)) != CONST_INT)
1060         abort ();
1061       initial_return_save (XEXP (rtl, 0));
1062       return;
1063     default:
1064       abort ();
1065     }
1066
1067   reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa_offset);
1068 }
1069
1070 /* Check INSN to see if it looks like a push or a stack adjustment, and
1071    make a note of it if it does.  EH uses this information to find out how
1072    much extra space it needs to pop off the stack.  */
1073
1074 static void
1075 dwarf2out_stack_adjust (insn)
1076      rtx insn;
1077 {
1078   long offset;
1079   char *label;
1080
1081   if (! asynchronous_exceptions && GET_CODE (insn) == CALL_INSN)
1082     {
1083       /* Extract the size of the args from the CALL rtx itself.  */
1084
1085       insn = PATTERN (insn);
1086       if (GET_CODE (insn) == PARALLEL)
1087         insn = XVECEXP (insn, 0, 0);
1088       if (GET_CODE (insn) == SET)
1089         insn = SET_SRC (insn);
1090       assert (GET_CODE (insn) == CALL);
1091       dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1092       return;
1093     }
1094
1095   /* If only calls can throw, and we have a frame pointer,
1096      save up adjustments until we see the CALL_INSN.  */
1097   else if (! asynchronous_exceptions
1098            && cfa_reg != STACK_POINTER_REGNUM)
1099     return;
1100
1101   if (GET_CODE (insn) == BARRIER)
1102     {
1103       /* When we see a BARRIER, we know to reset args_size to 0.  Usually
1104          the compiler will have already emitted a stack adjustment, but
1105          doesn't bother for calls to noreturn functions.  */
1106 #ifdef STACK_GROWS_DOWNWARD
1107       offset = -args_size;
1108 #else
1109       offset = args_size;
1110 #endif
1111     }
1112   else if (GET_CODE (PATTERN (insn)) == SET)
1113     {
1114       rtx src, dest;
1115       enum rtx_code code;
1116
1117       insn = PATTERN (insn);
1118       src = SET_SRC (insn);
1119       dest = SET_DEST (insn);
1120
1121       if (dest == stack_pointer_rtx)
1122         {
1123           /* (set (reg sp) (plus (reg sp) (const_int))) */
1124           code = GET_CODE (src);
1125           if (! (code == PLUS || code == MINUS)
1126               || XEXP (src, 0) != stack_pointer_rtx
1127               || GET_CODE (XEXP (src, 1)) != CONST_INT)
1128             return;
1129
1130           offset = INTVAL (XEXP (src, 1));
1131         }
1132       else if (GET_CODE (dest) == MEM)
1133         {
1134           /* (set (mem (pre_dec (reg sp))) (foo)) */
1135           src = XEXP (dest, 0);
1136           code = GET_CODE (src);
1137
1138           if (! (code == PRE_DEC || code == PRE_INC)
1139               || XEXP (src, 0) != stack_pointer_rtx)
1140             return;
1141
1142           offset = GET_MODE_SIZE (GET_MODE (dest));
1143         }
1144       else
1145         return;
1146
1147       if (code == PLUS || code == PRE_INC)
1148         offset = -offset;
1149     }
1150   else
1151     return;
1152
1153   if (offset == 0)
1154     return;
1155
1156   if (cfa_reg == STACK_POINTER_REGNUM)
1157     cfa_offset += offset;
1158
1159 #ifndef STACK_GROWS_DOWNWARD
1160   offset = -offset;
1161 #endif
1162   args_size += offset;
1163   if (args_size < 0)
1164     args_size = 0;
1165
1166   label = dwarf2out_cfi_label ();
1167   dwarf2out_def_cfa (label, cfa_reg, cfa_offset);
1168   dwarf2out_args_size (label, args_size);
1169 }
1170
1171 /* Record call frame debugging information for INSN, which either
1172    sets SP or FP (adjusting how we calculate the frame address) or saves a
1173    register to the stack.  If INSN is NULL_RTX, initialize our state.  */
1174
1175 void
1176 dwarf2out_frame_debug (insn)
1177      rtx insn;
1178 {
1179   char *label;
1180   rtx src, dest;
1181   long offset;
1182
1183   /* A temporary register used in adjusting SP or setting up the store_reg.  */
1184   static unsigned cfa_temp_reg;
1185   static long cfa_temp_value;
1186
1187   if (insn == NULL_RTX)
1188     {
1189       /* Set up state for generating call frame debug info.  */
1190       lookup_cfa (&cfa_reg, &cfa_offset);
1191       if (cfa_reg != DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM))
1192         abort ();
1193       cfa_reg = STACK_POINTER_REGNUM;
1194       cfa_store_reg = cfa_reg;
1195       cfa_store_offset = cfa_offset;
1196       cfa_temp_reg = -1;
1197       cfa_temp_value = 0;
1198       return;
1199     }
1200
1201   if (! RTX_FRAME_RELATED_P (insn))
1202     {
1203       dwarf2out_stack_adjust (insn);
1204       return;
1205     }
1206
1207   label = dwarf2out_cfi_label ();
1208     
1209   insn = PATTERN (insn);
1210   /* Assume that in a PARALLEL prologue insn, only the first elt is
1211      significant.  Currently this is true.  */
1212   if (GET_CODE (insn) == PARALLEL)
1213     insn = XVECEXP (insn, 0, 0);
1214   if (GET_CODE (insn) != SET)
1215     abort ();
1216
1217   src = SET_SRC (insn);
1218   dest = SET_DEST (insn);
1219
1220   switch (GET_CODE (dest))
1221     {
1222     case REG:
1223       /* Update the CFA rule wrt SP or FP.  Make sure src is
1224          relative to the current CFA register.  */
1225       switch (GET_CODE (src))
1226         {
1227           /* Setting FP from SP.  */
1228         case REG:
1229           if (cfa_reg != REGNO (src))
1230             abort ();
1231           if (REGNO (dest) != STACK_POINTER_REGNUM
1232               && !(frame_pointer_needed
1233                    && REGNO (dest) == HARD_FRAME_POINTER_REGNUM))
1234             abort ();
1235           cfa_reg = REGNO (dest);
1236           break;
1237
1238         case PLUS:
1239         case MINUS:
1240           if (dest == stack_pointer_rtx)
1241             {
1242               /* Adjusting SP.  */
1243               switch (GET_CODE (XEXP (src, 1)))
1244                 {
1245                 case CONST_INT:
1246                   offset = INTVAL (XEXP (src, 1));
1247                   break;
1248                 case REG:
1249                   if (REGNO (XEXP (src, 1)) != cfa_temp_reg)
1250                     abort ();
1251                   offset = cfa_temp_value;
1252                   break;
1253                 default:
1254                   abort ();
1255                 }
1256
1257               if (XEXP (src, 0) == hard_frame_pointer_rtx)
1258                 {
1259                   /* Restoring SP from FP in the epilogue.  */
1260                   if (cfa_reg != HARD_FRAME_POINTER_REGNUM)
1261                     abort ();
1262                   cfa_reg = STACK_POINTER_REGNUM;
1263                 }
1264               else if (XEXP (src, 0) != stack_pointer_rtx)
1265                 abort ();
1266
1267               if (GET_CODE (src) == PLUS)
1268                 offset = -offset;
1269               if (cfa_reg == STACK_POINTER_REGNUM)
1270                 cfa_offset += offset;
1271               if (cfa_store_reg == STACK_POINTER_REGNUM)
1272                 cfa_store_offset += offset;
1273             }
1274           else if (dest == hard_frame_pointer_rtx)
1275             {
1276               /* Either setting the FP from an offset of the SP,
1277                  or adjusting the FP */
1278               if (! frame_pointer_needed
1279                   || REGNO (dest) != HARD_FRAME_POINTER_REGNUM)
1280                 abort ();
1281
1282               if (XEXP (src, 0) == stack_pointer_rtx
1283                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
1284                 {
1285                   if (cfa_reg != STACK_POINTER_REGNUM)
1286                     abort ();
1287                   offset = INTVAL (XEXP (src, 1));
1288                   if (GET_CODE (src) == PLUS)
1289                     offset = -offset;
1290                   cfa_offset += offset;
1291                   cfa_reg = HARD_FRAME_POINTER_REGNUM;
1292                 }
1293               else if (XEXP (src, 0) == hard_frame_pointer_rtx
1294                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
1295                 {
1296                   if (cfa_reg != HARD_FRAME_POINTER_REGNUM)
1297                     abort ();
1298                   offset = INTVAL (XEXP (src, 1));
1299                   if (GET_CODE (src) == PLUS)
1300                     offset = -offset;
1301                   cfa_offset += offset;
1302                 }
1303
1304               else 
1305                 abort();
1306             }
1307           else
1308             {
1309               if (GET_CODE (src) != PLUS
1310                   || XEXP (src, 1) != stack_pointer_rtx)
1311                 abort ();
1312               if (GET_CODE (XEXP (src, 0)) != REG
1313                   || REGNO (XEXP (src, 0)) != cfa_temp_reg)
1314                 abort ();
1315               if (cfa_reg != STACK_POINTER_REGNUM)
1316                 abort ();
1317               cfa_store_reg = REGNO (dest);
1318               cfa_store_offset = cfa_offset - cfa_temp_value;
1319             }
1320           break;
1321
1322         case CONST_INT:
1323           cfa_temp_reg = REGNO (dest);
1324           cfa_temp_value = INTVAL (src);
1325           break;
1326
1327         case IOR:
1328           if (GET_CODE (XEXP (src, 0)) != REG
1329               || REGNO (XEXP (src, 0)) != cfa_temp_reg
1330               || REGNO (dest) != cfa_temp_reg
1331               || GET_CODE (XEXP (src, 1)) != CONST_INT)
1332             abort ();
1333           cfa_temp_value |= INTVAL (XEXP (src, 1));
1334           break;
1335
1336         default:
1337           abort ();
1338         }
1339       dwarf2out_def_cfa (label, cfa_reg, cfa_offset);
1340       break;
1341
1342     case MEM:
1343       /* Saving a register to the stack.  Make sure dest is relative to the
1344          CFA register.  */
1345       if (GET_CODE (src) != REG)
1346         abort ();
1347       switch (GET_CODE (XEXP (dest, 0)))
1348         {
1349           /* With a push.  */
1350         case PRE_INC:
1351         case PRE_DEC:
1352           offset = GET_MODE_SIZE (GET_MODE (dest));
1353           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1354             offset = -offset;
1355
1356           if (REGNO (XEXP (XEXP (dest, 0), 0)) != STACK_POINTER_REGNUM
1357               || cfa_store_reg != STACK_POINTER_REGNUM)
1358             abort ();
1359           cfa_store_offset += offset;
1360           if (cfa_reg == STACK_POINTER_REGNUM)
1361             cfa_offset = cfa_store_offset;
1362
1363           offset = -cfa_store_offset;
1364           break;
1365
1366           /* With an offset.  */
1367         case PLUS:
1368         case MINUS:
1369           offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1370           if (GET_CODE (src) == MINUS)
1371             offset = -offset;
1372
1373           if (cfa_store_reg != REGNO (XEXP (XEXP (dest, 0), 0)))
1374             abort ();
1375           offset -= cfa_store_offset;
1376           break;
1377
1378         default:
1379           abort ();
1380         }
1381       dwarf2out_def_cfa (label, cfa_reg, cfa_offset);
1382       dwarf2out_reg_save (label, REGNO (src), offset);
1383       break;
1384
1385     default:
1386       abort ();
1387     }
1388 }
1389
1390 /* Return the size of an unsigned LEB128 quantity.  */
1391
1392 static inline unsigned long
1393 size_of_uleb128 (value)
1394      register unsigned long value;
1395 {
1396   register unsigned long size = 0;
1397   register unsigned byte;
1398
1399   do
1400     {
1401       byte = (value & 0x7f);
1402       value >>= 7;
1403       size += 1;
1404     }
1405   while (value != 0);
1406
1407   return size;
1408 }
1409
1410 /* Return the size of a signed LEB128 quantity.  */
1411
1412 static inline unsigned long
1413 size_of_sleb128 (value)
1414      register long value;
1415 {
1416   register unsigned long size = 0;
1417   register unsigned byte;
1418
1419   do
1420     {
1421       byte = (value & 0x7f);
1422       value >>= 7;
1423       size += 1;
1424     }
1425   while (!(((value == 0) && ((byte & 0x40) == 0))
1426            || ((value == -1) && ((byte & 0x40) != 0))));
1427
1428   return size;
1429 }
1430
1431 /* Output an unsigned LEB128 quantity.  */
1432
1433 static void
1434 output_uleb128 (value)
1435      register unsigned long value;
1436 {
1437   unsigned long save_value = value;
1438
1439   fprintf (asm_out_file, "\t%s\t", ASM_BYTE_OP);
1440   do
1441     {
1442       register unsigned byte = (value & 0x7f);
1443       value >>= 7;
1444       if (value != 0)
1445         /* More bytes to follow.  */
1446         byte |= 0x80;
1447
1448       fprintf (asm_out_file, "0x%x", byte);
1449       if (value != 0)
1450         fprintf (asm_out_file, ",");
1451     }
1452   while (value != 0);
1453
1454   if (flag_debug_asm)
1455     fprintf (asm_out_file, "\t%s ULEB128 0x%lx", ASM_COMMENT_START, save_value);
1456 }
1457
1458 /* Output an signed LEB128 quantity.  */
1459
1460 static void
1461 output_sleb128 (value)
1462      register long value;
1463 {
1464   register int more;
1465   register unsigned byte;
1466   long save_value = value;
1467
1468   fprintf (asm_out_file, "\t%s\t", ASM_BYTE_OP);
1469   do
1470     {
1471       byte = (value & 0x7f);
1472       /* arithmetic shift */
1473       value >>= 7;
1474       more = !((((value == 0) && ((byte & 0x40) == 0))
1475                 || ((value == -1) && ((byte & 0x40) != 0))));
1476       if (more)
1477         byte |= 0x80;
1478
1479       fprintf (asm_out_file, "0x%x", byte);
1480       if (more)
1481         fprintf (asm_out_file, ",");
1482     }
1483
1484   while (more);
1485   if (flag_debug_asm)
1486     fprintf (asm_out_file, "\t%s SLEB128 %ld", ASM_COMMENT_START, save_value);
1487 }
1488
1489 /* Output a Call Frame Information opcode and its operand(s).  */
1490
1491 static void
1492 output_cfi (cfi, fde)
1493      register dw_cfi_ref cfi;
1494      register dw_fde_ref fde;
1495 {
1496   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
1497     {
1498       ASM_OUTPUT_DWARF_DATA1 (asm_out_file,
1499                               cfi->dw_cfi_opc
1500                               | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f));
1501       if (flag_debug_asm)
1502         fprintf (asm_out_file, "\t%s DW_CFA_advance_loc 0x%lx",
1503                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
1504       fputc ('\n', asm_out_file);
1505     }
1506
1507   else if (cfi->dw_cfi_opc == DW_CFA_offset)
1508     {
1509       ASM_OUTPUT_DWARF_DATA1 (asm_out_file,
1510                               cfi->dw_cfi_opc
1511                               | (cfi->dw_cfi_oprnd1.dw_cfi_reg_num & 0x3f));
1512       if (flag_debug_asm)
1513         fprintf (asm_out_file, "\t%s DW_CFA_offset, column 0x%lx",
1514                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
1515
1516       fputc ('\n', asm_out_file);
1517       output_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset);
1518       fputc ('\n', asm_out_file);
1519     }
1520   else if (cfi->dw_cfi_opc == DW_CFA_restore)
1521     {
1522       ASM_OUTPUT_DWARF_DATA1 (asm_out_file,
1523                               cfi->dw_cfi_opc
1524                               | (cfi->dw_cfi_oprnd1.dw_cfi_reg_num & 0x3f));
1525       if (flag_debug_asm)
1526         fprintf (asm_out_file, "\t%s DW_CFA_restore, column 0x%lx",
1527                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
1528
1529       fputc ('\n', asm_out_file);
1530     }
1531   else
1532     {
1533       ASM_OUTPUT_DWARF_DATA1 (asm_out_file, cfi->dw_cfi_opc);
1534       if (flag_debug_asm)
1535         fprintf (asm_out_file, "\t%s %s", ASM_COMMENT_START,
1536                  dwarf_cfi_name (cfi->dw_cfi_opc));
1537
1538       fputc ('\n', asm_out_file);
1539       switch (cfi->dw_cfi_opc)
1540         {
1541         case DW_CFA_set_loc:
1542           ASM_OUTPUT_DWARF_ADDR (asm_out_file, cfi->dw_cfi_oprnd1.dw_cfi_addr);
1543           fputc ('\n', asm_out_file);
1544           break;
1545         case DW_CFA_advance_loc1:
1546           ASM_OUTPUT_DWARF_DELTA1 (asm_out_file,
1547                                    cfi->dw_cfi_oprnd1.dw_cfi_addr,
1548                                    fde->dw_fde_current_label);
1549           fputc ('\n', asm_out_file);
1550           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1551           break;
1552         case DW_CFA_advance_loc2:
1553           ASM_OUTPUT_DWARF_DELTA2 (asm_out_file,
1554                                    cfi->dw_cfi_oprnd1.dw_cfi_addr,
1555                                    fde->dw_fde_current_label);
1556           fputc ('\n', asm_out_file);
1557           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1558           break;
1559         case DW_CFA_advance_loc4:
1560           ASM_OUTPUT_DWARF_DELTA4 (asm_out_file,
1561                                    cfi->dw_cfi_oprnd1.dw_cfi_addr,
1562                                    fde->dw_fde_current_label);
1563           fputc ('\n', asm_out_file);
1564           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1565           break;
1566 #ifdef MIPS_DEBUGGING_INFO
1567         case DW_CFA_MIPS_advance_loc8:
1568           /* TODO: not currently implemented.  */
1569           abort ();
1570           break;
1571 #endif
1572         case DW_CFA_offset_extended:
1573         case DW_CFA_def_cfa:
1574           output_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
1575           fputc ('\n', asm_out_file);
1576           output_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset);
1577           fputc ('\n', asm_out_file);
1578           break;
1579         case DW_CFA_restore_extended:
1580         case DW_CFA_undefined:
1581           output_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
1582           fputc ('\n', asm_out_file);
1583           break;
1584         case DW_CFA_same_value:
1585         case DW_CFA_def_cfa_register:
1586           output_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
1587           fputc ('\n', asm_out_file);
1588           break;
1589         case DW_CFA_register:
1590           output_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
1591           fputc ('\n', asm_out_file);
1592           output_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
1593           fputc ('\n', asm_out_file);
1594           break;
1595         case DW_CFA_def_cfa_offset:
1596           output_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset);
1597           fputc ('\n', asm_out_file);
1598           break;
1599         case DW_CFA_GNU_window_save:
1600           break;
1601         case DW_CFA_GNU_args_size:
1602           output_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset);
1603           fputc ('\n', asm_out_file);
1604           break;
1605         default:
1606           break;
1607         }
1608      }
1609 }
1610
1611 #if !defined (EH_FRAME_SECTION)
1612 #if defined (EH_FRAME_SECTION_ASM_OP)
1613 #define EH_FRAME_SECTION() eh_frame_section();
1614 #else
1615 #if defined (ASM_OUTPUT_SECTION_NAME)
1616 #define EH_FRAME_SECTION()                              \
1617   do {                                                  \
1618       named_section (NULL_TREE, ".eh_frame", 0);        \
1619   } while (0)
1620 #endif
1621 #endif
1622 #endif
1623
1624 /* Output the call frame information used to used to record information
1625    that relates to calculating the frame pointer, and records the
1626    location of saved registers.  */
1627
1628 static void
1629 output_call_frame_info (for_eh)
1630      int for_eh;
1631 {
1632   register unsigned long i;
1633   register dw_fde_ref fde;
1634   register dw_cfi_ref cfi;
1635   char l1[20], l2[20];
1636 #ifdef ASM_OUTPUT_DEFINE_LABEL_DIFFERENCE_SYMBOL
1637   char ld[20];
1638 #endif
1639
1640   /* Do we want to include a pointer to the exception table?  */
1641   int eh_ptr = for_eh && exception_table_p ();
1642
1643   fputc ('\n', asm_out_file);
1644
1645   /* We're going to be generating comments, so turn on app.  */
1646   if (flag_debug_asm)
1647     app_enable ();
1648
1649   if (for_eh)
1650     {
1651 #ifdef EH_FRAME_SECTION
1652       EH_FRAME_SECTION ();
1653 #else
1654       tree label = get_file_function_name ('F');
1655
1656       data_section ();
1657       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
1658       ASM_GLOBALIZE_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
1659       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
1660 #endif
1661       assemble_label ("__FRAME_BEGIN__");
1662     }
1663   else
1664     ASM_OUTPUT_SECTION (asm_out_file, FRAME_SECTION);
1665
1666   /* Output the CIE. */
1667   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
1668   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
1669 #ifdef ASM_OUTPUT_DEFINE_LABEL_DIFFERENCE_SYMBOL
1670   ASM_GENERATE_INTERNAL_LABEL (ld, CIE_LENGTH_LABEL, for_eh);
1671   if (for_eh)
1672     ASM_OUTPUT_DWARF_OFFSET4 (asm_out_file, ld);
1673   else
1674     ASM_OUTPUT_DWARF_OFFSET (asm_out_file, ld);
1675 #else
1676   if (for_eh)
1677     ASM_OUTPUT_DWARF_DELTA4 (asm_out_file, l2, l1);
1678   else
1679     ASM_OUTPUT_DWARF_DELTA (asm_out_file, l2, l1);
1680 #endif
1681   if (flag_debug_asm)
1682     fprintf (asm_out_file, "\t%s Length of Common Information Entry",
1683              ASM_COMMENT_START);
1684
1685   fputc ('\n', asm_out_file);
1686   ASM_OUTPUT_LABEL (asm_out_file, l1);
1687
1688   if (for_eh)
1689     /* Now that the CIE pointer is PC-relative for EH,
1690        use 0 to identify the CIE.  */
1691     ASM_OUTPUT_DWARF_DATA4 (asm_out_file, 0);
1692   else
1693     ASM_OUTPUT_DWARF_DATA4 (asm_out_file, DW_CIE_ID);
1694
1695   if (flag_debug_asm)
1696     fprintf (asm_out_file, "\t%s CIE Identifier Tag", ASM_COMMENT_START);
1697
1698   fputc ('\n', asm_out_file);
1699   if (! for_eh && DWARF_OFFSET_SIZE == 8)
1700     {
1701       ASM_OUTPUT_DWARF_DATA4 (asm_out_file, DW_CIE_ID);
1702       fputc ('\n', asm_out_file);
1703     }
1704
1705   ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_CIE_VERSION);
1706   if (flag_debug_asm)
1707     fprintf (asm_out_file, "\t%s CIE Version", ASM_COMMENT_START);
1708
1709   fputc ('\n', asm_out_file);
1710   if (eh_ptr)
1711     {
1712       /* The CIE contains a pointer to the exception region info for the
1713          frame.  Make the augmentation string three bytes (including the
1714          trailing null) so the pointer is 4-byte aligned.  The Solaris ld
1715          can't handle unaligned relocs.  */
1716       if (flag_debug_asm)
1717         {
1718           ASM_OUTPUT_DWARF_STRING (asm_out_file, "eh");
1719           fprintf (asm_out_file, "\t%s CIE Augmentation", ASM_COMMENT_START);
1720         }
1721       else
1722         {
1723           ASM_OUTPUT_ASCII (asm_out_file, "eh", 3);
1724         }
1725       fputc ('\n', asm_out_file);
1726
1727       ASM_OUTPUT_DWARF_ADDR (asm_out_file, "__EXCEPTION_TABLE__");
1728       if (flag_debug_asm)
1729         fprintf (asm_out_file, "\t%s pointer to exception region info",
1730                  ASM_COMMENT_START);
1731     }
1732   else
1733     {
1734       ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
1735       if (flag_debug_asm)
1736         fprintf (asm_out_file, "\t%s CIE Augmentation (none)",
1737                  ASM_COMMENT_START);
1738     }
1739
1740   fputc ('\n', asm_out_file);
1741   output_uleb128 (1);
1742   if (flag_debug_asm)
1743     fprintf (asm_out_file, " (CIE Code Alignment Factor)");
1744
1745   fputc ('\n', asm_out_file);
1746   output_sleb128 (DWARF_CIE_DATA_ALIGNMENT);
1747   if (flag_debug_asm)
1748     fprintf (asm_out_file, " (CIE Data Alignment Factor)");
1749
1750   fputc ('\n', asm_out_file);
1751   ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DWARF_FRAME_RETURN_COLUMN);
1752   if (flag_debug_asm)
1753     fprintf (asm_out_file, "\t%s CIE RA Column", ASM_COMMENT_START);
1754
1755   fputc ('\n', asm_out_file);
1756
1757   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
1758     output_cfi (cfi, NULL);
1759
1760   /* Pad the CIE out to an address sized boundary.  */
1761   ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
1762   ASM_OUTPUT_LABEL (asm_out_file, l2);
1763 #ifdef ASM_OUTPUT_DEFINE_LABEL_DIFFERENCE_SYMBOL
1764   ASM_OUTPUT_DEFINE_LABEL_DIFFERENCE_SYMBOL (asm_out_file, ld, l2, l1);
1765   if (flag_debug_asm)
1766     fprintf (asm_out_file, "\t%s CIE Length Symbol", ASM_COMMENT_START);
1767   fputc ('\n', asm_out_file);
1768 #endif
1769
1770   /* Loop through all of the FDE's.  */
1771   for (i = 0; i < fde_table_in_use; ++i)
1772     {
1773       fde = &fde_table[i];
1774
1775       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i*2);
1776       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i*2);
1777 #ifdef ASM_OUTPUT_DEFINE_LABEL_DIFFERENCE_SYMBOL
1778       ASM_GENERATE_INTERNAL_LABEL (ld, FDE_LENGTH_LABEL, for_eh + i*2);
1779       if (for_eh)
1780         ASM_OUTPUT_DWARF_OFFSET4 (asm_out_file, ld);
1781       else
1782         ASM_OUTPUT_DWARF_OFFSET (asm_out_file, ld);
1783 #else
1784       if (for_eh)
1785         ASM_OUTPUT_DWARF_DELTA4 (asm_out_file, l2, l1);
1786       else
1787         ASM_OUTPUT_DWARF_DELTA (asm_out_file, l2, l1);
1788 #endif
1789       if (flag_debug_asm)
1790         fprintf (asm_out_file, "\t%s FDE Length", ASM_COMMENT_START);
1791       fputc ('\n', asm_out_file);
1792       ASM_OUTPUT_LABEL (asm_out_file, l1);
1793
1794       if (for_eh)
1795         ASM_OUTPUT_DWARF_DELTA (asm_out_file, l1, "__FRAME_BEGIN__");
1796       else
1797         ASM_OUTPUT_DWARF_OFFSET (asm_out_file, stripattributes (FRAME_SECTION));
1798       if (flag_debug_asm)
1799         fprintf (asm_out_file, "\t%s FDE CIE offset", ASM_COMMENT_START);
1800
1801       fputc ('\n', asm_out_file);
1802       ASM_OUTPUT_DWARF_ADDR (asm_out_file, fde->dw_fde_begin);
1803       if (flag_debug_asm)
1804         fprintf (asm_out_file, "\t%s FDE initial location", ASM_COMMENT_START);
1805
1806       fputc ('\n', asm_out_file);
1807       ASM_OUTPUT_DWARF_ADDR_DELTA (asm_out_file,
1808                                    fde->dw_fde_end, fde->dw_fde_begin);
1809       if (flag_debug_asm)
1810         fprintf (asm_out_file, "\t%s FDE address range", ASM_COMMENT_START);
1811
1812       fputc ('\n', asm_out_file);
1813
1814       /* Loop through the Call Frame Instructions associated with
1815          this FDE.  */
1816       fde->dw_fde_current_label = fde->dw_fde_begin;
1817       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
1818         output_cfi (cfi, fde);
1819
1820       /* Pad the FDE out to an address sized boundary.  */
1821       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
1822       ASM_OUTPUT_LABEL (asm_out_file, l2);
1823 #ifdef ASM_OUTPUT_DEFINE_LABEL_DIFFERENCE_SYMBOL
1824       ASM_OUTPUT_DEFINE_LABEL_DIFFERENCE_SYMBOL (asm_out_file, ld, l2, l1);
1825       if (flag_debug_asm)
1826         fprintf (asm_out_file, "\t%s FDE Length Symbol", ASM_COMMENT_START);
1827       fputc ('\n', asm_out_file);
1828 #endif
1829     }
1830 #ifndef EH_FRAME_SECTION
1831   if (for_eh)
1832     {
1833       /* Emit terminating zero for table.  */
1834       ASM_OUTPUT_DWARF_DATA4 (asm_out_file, 0);
1835       fputc ('\n', asm_out_file);
1836     }
1837 #endif
1838 #ifdef MIPS_DEBUGGING_INFO
1839   /* Work around Irix 6 assembler bug whereby labels at the end of a section
1840      get a value of 0.  Putting .align 0 after the label fixes it.  */
1841   ASM_OUTPUT_ALIGN (asm_out_file, 0);
1842 #endif
1843
1844   /* Turn off app to make assembly quicker.  */
1845   if (flag_debug_asm)
1846     app_disable ();
1847 }
1848
1849 /* Output a marker (i.e. a label) for the beginning of a function, before
1850    the prologue.  */
1851
1852 void
1853 dwarf2out_begin_prologue ()
1854 {
1855   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1856   register dw_fde_ref fde;
1857
1858   ++current_funcdef_number;
1859
1860   function_section (current_function_decl);
1861   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1862                                current_funcdef_number);
1863   ASM_OUTPUT_LABEL (asm_out_file, label);
1864
1865   /* Expand the fde table if necessary.  */
1866   if (fde_table_in_use == fde_table_allocated)
1867     {
1868       fde_table_allocated += FDE_TABLE_INCREMENT;
1869       fde_table
1870         = (dw_fde_ref) xrealloc (fde_table,
1871                                  fde_table_allocated * sizeof (dw_fde_node));
1872     }
1873
1874   /* Record the FDE associated with this function.  */
1875   current_funcdef_fde = fde_table_in_use;
1876
1877   /* Add the new FDE at the end of the fde_table.  */
1878   fde = &fde_table[fde_table_in_use++];
1879   fde->dw_fde_begin = xstrdup (label);
1880   fde->dw_fde_current_label = NULL;
1881   fde->dw_fde_end = NULL;
1882   fde->dw_fde_cfi = NULL;
1883
1884   args_size = old_args_size = 0;
1885 }
1886
1887 /* Output a marker (i.e. a label) for the absolute end of the generated code
1888    for a function definition.  This gets called *after* the epilogue code has
1889    been generated.  */
1890
1891 void
1892 dwarf2out_end_epilogue ()
1893 {
1894   dw_fde_ref fde;
1895   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1896
1897   /* Output a label to mark the endpoint of the code generated for this
1898      function.        */
1899   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL, current_funcdef_number);
1900   ASM_OUTPUT_LABEL (asm_out_file, label);
1901   fde = &fde_table[fde_table_in_use - 1];
1902   fde->dw_fde_end = xstrdup (label);
1903 }
1904
1905 void
1906 dwarf2out_frame_init ()
1907 {
1908   /* Allocate the initial hunk of the fde_table.  */
1909   fde_table
1910     = (dw_fde_ref) xmalloc (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
1911   bzero ((char *) fde_table, FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
1912   fde_table_allocated = FDE_TABLE_INCREMENT;
1913   fde_table_in_use = 0;
1914
1915   /* Generate the CFA instructions common to all FDE's.  Do it now for the
1916      sake of lookup_cfa.  */
1917
1918 #ifdef DWARF2_UNWIND_INFO
1919   /* On entry, the Canonical Frame Address is at SP.  */
1920   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
1921   initial_return_save (INCOMING_RETURN_ADDR_RTX);
1922 #endif
1923 }
1924
1925 void
1926 dwarf2out_frame_finish ()
1927 {
1928   /* Output call frame information.  */
1929 #ifdef MIPS_DEBUGGING_INFO
1930   if (write_symbols == DWARF2_DEBUG)
1931     output_call_frame_info (0);
1932   if (flag_exceptions && ! exceptions_via_longjmp)
1933     output_call_frame_info (1);
1934 #else
1935   if (write_symbols == DWARF2_DEBUG
1936       || (flag_exceptions && ! exceptions_via_longjmp))
1937     output_call_frame_info (1);  
1938 #endif
1939 }  
1940
1941 #endif /* .debug_frame support */
1942
1943 /* And now, the support for symbolic debugging information.  */
1944 #ifdef DWARF2_DEBUGGING_INFO
1945
1946 extern char *getpwd ();
1947
1948 /* NOTE: In the comments in this file, many references are made to
1949    "Debugging Information Entries".  This term is abbreviated as `DIE'
1950    throughout the remainder of this file.  */
1951
1952 /* An internal representation of the DWARF output is built, and then
1953    walked to generate the DWARF debugging info.  The walk of the internal
1954    representation is done after the entire program has been compiled.
1955    The types below are used to describe the internal representation.  */
1956
1957 /* Each DIE may have a series of attribute/value pairs.  Values
1958    can take on several forms.  The forms that are used in this
1959    implementation are listed below.  */
1960
1961 typedef enum
1962 {
1963   dw_val_class_addr,
1964   dw_val_class_loc,
1965   dw_val_class_const,
1966   dw_val_class_unsigned_const,
1967   dw_val_class_long_long,
1968   dw_val_class_float,
1969   dw_val_class_flag,
1970   dw_val_class_die_ref,
1971   dw_val_class_fde_ref,
1972   dw_val_class_lbl_id,
1973   dw_val_class_section_offset,
1974   dw_val_class_str
1975 }
1976 dw_val_class;
1977
1978 /* Various DIE's use offsets relative to the beginning of the
1979    .debug_info section to refer to each other.  */
1980
1981 typedef long int dw_offset;
1982
1983 /* Define typedefs here to avoid circular dependencies.  */
1984
1985 typedef struct die_struct *dw_die_ref;
1986 typedef struct dw_attr_struct *dw_attr_ref;
1987 typedef struct dw_val_struct *dw_val_ref;
1988 typedef struct dw_line_info_struct *dw_line_info_ref;
1989 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
1990 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
1991 typedef struct pubname_struct *pubname_ref;
1992 typedef dw_die_ref *arange_ref;
1993
1994 /* Describe a double word constant value.  */
1995
1996 typedef struct dw_long_long_struct
1997 {
1998   unsigned long hi;
1999   unsigned long low;
2000 }
2001 dw_long_long_const;
2002
2003 /* Describe a floating point constant value.  */
2004
2005 typedef struct dw_fp_struct
2006 {
2007   long *array;
2008   unsigned length;
2009 }
2010 dw_float_const;
2011
2012 /* Each entry in the line_info_table maintains the file and
2013    line number associated with the label generated for that
2014    entry.  The label gives the PC value associated with
2015    the line number entry.  */
2016
2017 typedef struct dw_line_info_struct
2018 {
2019   unsigned long dw_file_num;
2020   unsigned long dw_line_num;
2021 }
2022 dw_line_info_entry;
2023
2024 /* Line information for functions in separate sections; each one gets its
2025    own sequence.  */
2026 typedef struct dw_separate_line_info_struct
2027 {
2028   unsigned long dw_file_num;
2029   unsigned long dw_line_num;
2030   unsigned long function;
2031 }
2032 dw_separate_line_info_entry;
2033
2034 /* The dw_val_node describes an attribute's value, as it is
2035    represented internally.  */
2036
2037 typedef struct dw_val_struct
2038 {
2039   dw_val_class val_class;
2040   union
2041     {
2042       char *val_addr;
2043       dw_loc_descr_ref val_loc;
2044       long int val_int;
2045       long unsigned val_unsigned;
2046       dw_long_long_const val_long_long;
2047       dw_float_const val_float;
2048       dw_die_ref val_die_ref;
2049       unsigned val_fde_index;
2050       char *val_str;
2051       char *val_lbl_id;
2052       char *val_section;
2053       unsigned char val_flag;
2054     }
2055   v;
2056 }
2057 dw_val_node;
2058
2059 /* Locations in memory are described using a sequence of stack machine
2060    operations.  */
2061
2062 typedef struct dw_loc_descr_struct
2063 {
2064   dw_loc_descr_ref dw_loc_next;
2065   enum dwarf_location_atom dw_loc_opc;
2066   dw_val_node dw_loc_oprnd1;
2067   dw_val_node dw_loc_oprnd2;
2068 }
2069 dw_loc_descr_node;
2070
2071 /* Each DIE attribute has a field specifying the attribute kind,
2072    a link to the next attribute in the chain, and an attribute value.
2073    Attributes are typically linked below the DIE they modify.  */
2074
2075 typedef struct dw_attr_struct
2076 {
2077   enum dwarf_attribute dw_attr;
2078   dw_attr_ref dw_attr_next;
2079   dw_val_node dw_attr_val;
2080 }
2081 dw_attr_node;
2082
2083 /* The Debugging Information Entry (DIE) structure */
2084
2085 typedef struct die_struct
2086 {
2087   enum dwarf_tag die_tag;
2088   dw_attr_ref die_attr;
2089   dw_attr_ref die_attr_last;
2090   dw_die_ref die_parent;
2091   dw_die_ref die_child;
2092   dw_die_ref die_child_last;
2093   dw_die_ref die_sib;
2094   dw_offset die_offset;
2095   unsigned long die_abbrev;
2096 }
2097 die_node;
2098
2099 /* The pubname structure */
2100
2101 typedef struct pubname_struct
2102 {
2103   dw_die_ref die;
2104   char * name;
2105 }
2106 pubname_entry;
2107
2108 /* The limbo die list structure.  */
2109 typedef struct limbo_die_struct
2110 {
2111   dw_die_ref die;
2112   struct limbo_die_struct *next;
2113 }
2114 limbo_die_node;
2115
2116 /* How to start an assembler comment.  */
2117 #ifndef ASM_COMMENT_START
2118 #define ASM_COMMENT_START ";#"
2119 #endif
2120
2121 /* Define a macro which returns non-zero for a TYPE_DECL which was
2122    implicitly generated for a tagged type.
2123
2124    Note that unlike the gcc front end (which generates a NULL named
2125    TYPE_DECL node for each complete tagged type, each array type, and
2126    each function type node created) the g++ front end generates a
2127    _named_ TYPE_DECL node for each tagged type node created.
2128    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2129    generate a DW_TAG_typedef DIE for them.  */
2130
2131 #define TYPE_DECL_IS_STUB(decl)                         \
2132   (DECL_NAME (decl) == NULL_TREE                        \
2133    || (DECL_ARTIFICIAL (decl)                           \
2134        && is_tagged_type (TREE_TYPE (decl))             \
2135        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
2136            /* This is necessary for stub decls that     \
2137               appear in nested inline functions.  */    \
2138            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2139                && (decl_ultimate_origin (decl)          \
2140                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2141
2142 /* Information concerning the compilation unit's programming
2143    language, and compiler version.  */
2144
2145 extern int flag_traditional;
2146 extern char *version_string;
2147 extern char *language_string;
2148
2149 /* Fixed size portion of the DWARF compilation unit header.  */
2150 #define DWARF_COMPILE_UNIT_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 3)
2151
2152 /* Fixed size portion of debugging line information prolog.  */
2153 #define DWARF_LINE_PROLOG_HEADER_SIZE 5
2154
2155 /* Fixed size portion of public names info.  */
2156 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2157
2158 /* Fixed size portion of the address range info.  */
2159 #define DWARF_ARANGES_HEADER_SIZE \
2160   (DWARF_ROUND (2 * DWARF_OFFSET_SIZE + 4, PTR_SIZE * 2) - DWARF_OFFSET_SIZE)
2161
2162 /* Define the architecture-dependent minimum instruction length (in bytes).
2163    In this implementation of DWARF, this field is used for information
2164    purposes only.  Since GCC generates assembly language, we have
2165    no a priori knowledge of how many instruction bytes are generated
2166    for each source line, and therefore can use only the  DW_LNE_set_address
2167    and DW_LNS_fixed_advance_pc line information commands.  */
2168
2169 #ifndef DWARF_LINE_MIN_INSTR_LENGTH
2170 #define DWARF_LINE_MIN_INSTR_LENGTH 4
2171 #endif
2172
2173 /* Minimum line offset in a special line info. opcode.
2174    This value was chosen to give a reasonable range of values.  */
2175 #define DWARF_LINE_BASE  -10
2176
2177 /* First special line opcde - leave room for the standard opcodes.  */
2178 #define DWARF_LINE_OPCODE_BASE  10
2179
2180 /* Range of line offsets in a special line info. opcode.  */
2181 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
2182
2183 /* Flag that indicates the initial value of the is_stmt_start flag.
2184    In the present implementation, we do not mark any lines as
2185    the beginning of a source statement, because that information
2186    is not made available by the GCC front-end.  */
2187 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2188
2189 /* This location is used by calc_die_sizes() to keep track
2190    the offset of each DIE within the .debug_info section.  */
2191 static unsigned long next_die_offset;
2192
2193 /* Record the root of the DIE's built for the current compilation unit.  */
2194 static dw_die_ref comp_unit_die;
2195
2196 /* A list of DIEs with a NULL parent waiting to be relocated.  */
2197 static limbo_die_node *limbo_die_list = 0;
2198
2199 /* Pointer to an array of filenames referenced by this compilation unit.  */
2200 static char **file_table;
2201
2202 /* Total number of entries in the table (i.e. array) pointed to by
2203    `file_table'.  This is the *total* and includes both used and unused
2204    slots.  */
2205 static unsigned file_table_allocated;
2206
2207 /* Number of entries in the file_table which are actually in use.  */
2208 static unsigned file_table_in_use;
2209
2210 /* Size (in elements) of increments by which we may expand the filename
2211    table.  */
2212 #define FILE_TABLE_INCREMENT 64
2213
2214 /* Local pointer to the name of the main input file.  Initialized in
2215    dwarf2out_init.  */
2216 static char *primary_filename;
2217
2218 /* For Dwarf output, we must assign lexical-blocks id numbers in the order in
2219    which their beginnings are encountered. We output Dwarf debugging info
2220    that refers to the beginnings and ends of the ranges of code for each
2221    lexical block.  The labels themselves are generated in final.c, which
2222    assigns numbers to the blocks in the same way.  */
2223 static unsigned next_block_number = 2;
2224
2225 /* A pointer to the base of a table of references to DIE's that describe
2226    declarations.  The table is indexed by DECL_UID() which is a unique
2227    number identifying each decl.  */
2228 static dw_die_ref *decl_die_table;
2229
2230 /* Number of elements currently allocated for the decl_die_table.  */
2231 static unsigned decl_die_table_allocated;
2232
2233 /* Number of elements in decl_die_table currently in use.  */
2234 static unsigned decl_die_table_in_use;
2235
2236 /* Size (in elements) of increments by which we may expand the
2237    decl_die_table.  */
2238 #define DECL_DIE_TABLE_INCREMENT 256
2239
2240 /* Structure used for the decl_scope table.  scope is the current declaration
2241    scope, and previous is the entry that is the parent of this scope.  This
2242    is usually but not always the immediately preceeding entry.  */
2243
2244 typedef struct decl_scope_struct
2245 {
2246   tree scope;
2247   int previous;
2248 }
2249 decl_scope_node;
2250
2251 /* A pointer to the base of a table of references to declaration
2252    scopes.  This table is a display which tracks the nesting
2253    of declaration scopes at the current scope and containing
2254    scopes.  This table is used to find the proper place to
2255    define type declaration DIE's.  */
2256 static decl_scope_node *decl_scope_table;
2257
2258 /* Number of elements currently allocated for the decl_scope_table.  */
2259 static int decl_scope_table_allocated;
2260
2261 /* Current level of nesting of declaration scopes.  */
2262 static int decl_scope_depth;
2263
2264 /* Size (in elements) of increments by which we may expand the
2265    decl_scope_table.  */
2266 #define DECL_SCOPE_TABLE_INCREMENT 64
2267
2268 /* A pointer to the base of a list of references to DIE's that
2269    are uniquely identified by their tag, presence/absence of
2270    children DIE's, and list of attribute/value pairs.  */
2271 static dw_die_ref *abbrev_die_table;
2272
2273 /* Number of elements currently allocated for abbrev_die_table.  */
2274 static unsigned abbrev_die_table_allocated;
2275
2276 /* Number of elements in type_die_table currently in use.  */
2277 static unsigned abbrev_die_table_in_use;
2278
2279 /* Size (in elements) of increments by which we may expand the
2280    abbrev_die_table.  */
2281 #define ABBREV_DIE_TABLE_INCREMENT 256
2282
2283 /* A pointer to the base of a table that contains line information
2284    for each source code line in .text in the compilation unit.  */
2285 static dw_line_info_ref line_info_table;
2286
2287 /* Number of elements currently allocated for line_info_table.  */
2288 static unsigned line_info_table_allocated;
2289
2290 /* Number of elements in separate_line_info_table currently in use.  */
2291 static unsigned separate_line_info_table_in_use;
2292
2293 /* A pointer to the base of a table that contains line information
2294    for each source code line outside of .text in the compilation unit.  */
2295 static dw_separate_line_info_ref separate_line_info_table;
2296
2297 /* Number of elements currently allocated for separate_line_info_table.  */
2298 static unsigned separate_line_info_table_allocated;
2299
2300 /* Number of elements in line_info_table currently in use.  */
2301 static unsigned line_info_table_in_use;
2302
2303 /* Size (in elements) of increments by which we may expand the
2304    line_info_table.  */
2305 #define LINE_INFO_TABLE_INCREMENT 1024
2306
2307 /* A pointer to the base of a table that contains a list of publicly
2308    accessible names.  */
2309 static pubname_ref pubname_table;
2310
2311 /* Number of elements currently allocated for pubname_table.  */
2312 static unsigned pubname_table_allocated;
2313
2314 /* Number of elements in pubname_table currently in use.  */
2315 static unsigned pubname_table_in_use;
2316
2317 /* Size (in elements) of increments by which we may expand the
2318    pubname_table.  */
2319 #define PUBNAME_TABLE_INCREMENT 64
2320
2321 /* A pointer to the base of a table that contains a list of publicly
2322    accessible names.  */
2323 static arange_ref arange_table;
2324
2325 /* Number of elements currently allocated for arange_table.  */
2326 static unsigned arange_table_allocated;
2327
2328 /* Number of elements in arange_table currently in use.  */
2329 static unsigned arange_table_in_use;
2330
2331 /* Size (in elements) of increments by which we may expand the
2332    arange_table.  */
2333 #define ARANGE_TABLE_INCREMENT 64
2334
2335 /* A pointer to the base of a list of pending types which we haven't
2336    generated DIEs for yet, but which we will have to come back to
2337    later on.  */
2338
2339 static tree *pending_types_list;
2340
2341 /* Number of elements currently allocated for the pending_types_list.  */
2342 static unsigned pending_types_allocated;
2343
2344 /* Number of elements of pending_types_list currently in use.  */
2345 static unsigned pending_types;
2346
2347 /* Size (in elements) of increments by which we may expand the pending
2348    types list.  Actually, a single hunk of space of this size should
2349    be enough for most typical programs.  */
2350 #define PENDING_TYPES_INCREMENT 64
2351
2352 /* Record whether the function being analyzed contains inlined functions.  */
2353 static int current_function_has_inlines;
2354 #if 0 && defined (MIPS_DEBUGGING_INFO)
2355 static int comp_unit_has_inlines;
2356 #endif
2357
2358 /* A pointer to the ..._DECL node which we have most recently been working
2359    on.  We keep this around just in case something about it looks screwy and
2360    we want to tell the user what the source coordinates for the actual
2361    declaration are.  */
2362 static tree dwarf_last_decl;
2363
2364 /* Forward declarations for functions defined in this file.  */
2365
2366 static void addr_const_to_string        PROTO((char *, rtx));
2367 static char *addr_to_string             PROTO((rtx));
2368 static int is_pseudo_reg                PROTO((rtx));
2369 static tree type_main_variant           PROTO((tree));
2370 static int is_tagged_type               PROTO((tree));
2371 static char *dwarf_tag_name             PROTO((unsigned));
2372 static char *dwarf_attr_name            PROTO((unsigned));
2373 static char *dwarf_form_name            PROTO((unsigned));
2374 static char *dwarf_stack_op_name        PROTO((unsigned));
2375 static char *dwarf_type_encoding_name   PROTO((unsigned));
2376 static tree decl_ultimate_origin        PROTO((tree));
2377 static tree block_ultimate_origin       PROTO((tree));
2378 static tree decl_class_context          PROTO((tree));
2379 static void add_dwarf_attr              PROTO((dw_die_ref, dw_attr_ref));
2380 static void add_AT_flag                 PROTO((dw_die_ref,
2381                                                enum dwarf_attribute,
2382                                                unsigned));
2383 static void add_AT_int                  PROTO((dw_die_ref,
2384                                                enum dwarf_attribute, long));
2385 static void add_AT_unsigned             PROTO((dw_die_ref,
2386                                                enum dwarf_attribute,
2387                                                unsigned long));
2388 static void add_AT_long_long            PROTO((dw_die_ref,
2389                                                enum dwarf_attribute,
2390                                                unsigned long, unsigned long));
2391 static void add_AT_float                PROTO((dw_die_ref,
2392                                                enum dwarf_attribute,
2393                                                unsigned, long *));
2394 static void add_AT_string               PROTO((dw_die_ref,
2395                                                enum dwarf_attribute, char *));
2396 static void add_AT_die_ref              PROTO((dw_die_ref,
2397                                                enum dwarf_attribute,
2398                                                dw_die_ref));
2399 static void add_AT_fde_ref              PROTO((dw_die_ref,
2400                                                enum dwarf_attribute,
2401                                                unsigned));
2402 static void add_AT_loc                  PROTO((dw_die_ref,
2403                                                enum dwarf_attribute,
2404                                                dw_loc_descr_ref));
2405 static void add_AT_addr                 PROTO((dw_die_ref,
2406                                                enum dwarf_attribute, char *));
2407 static void add_AT_lbl_id               PROTO((dw_die_ref,
2408                                                enum dwarf_attribute, char *));
2409 static void add_AT_section_offset       PROTO((dw_die_ref,
2410                                                enum dwarf_attribute, char *));
2411 static int is_extern_subr_die           PROTO((dw_die_ref));
2412 static dw_attr_ref get_AT               PROTO((dw_die_ref,
2413                                                enum dwarf_attribute));
2414 static char *get_AT_low_pc              PROTO((dw_die_ref));
2415 static char *get_AT_hi_pc               PROTO((dw_die_ref));
2416 static char *get_AT_string              PROTO((dw_die_ref,
2417                                                enum dwarf_attribute));
2418 static int get_AT_flag                  PROTO((dw_die_ref,
2419                                                enum dwarf_attribute));
2420 static unsigned get_AT_unsigned         PROTO((dw_die_ref,
2421                                                enum dwarf_attribute));
2422 static int is_c_family                  PROTO((void));
2423 static int is_fortran                   PROTO((void));
2424 static void remove_AT                   PROTO((dw_die_ref,
2425                                                enum dwarf_attribute));
2426 static void remove_children             PROTO((dw_die_ref));
2427 static void add_child_die               PROTO((dw_die_ref, dw_die_ref));
2428 static dw_die_ref new_die               PROTO((enum dwarf_tag, dw_die_ref));
2429 static dw_die_ref lookup_type_die       PROTO((tree));
2430 static void equate_type_number_to_die   PROTO((tree, dw_die_ref));
2431 static dw_die_ref lookup_decl_die       PROTO((tree));
2432 static void equate_decl_number_to_die   PROTO((tree, dw_die_ref));
2433 static dw_loc_descr_ref new_loc_descr   PROTO((enum dwarf_location_atom,
2434                                                unsigned long, unsigned long));
2435 static void add_loc_descr               PROTO((dw_loc_descr_ref *,
2436                                                dw_loc_descr_ref));
2437 static void print_spaces                PROTO((FILE *));
2438 static void print_die                   PROTO((dw_die_ref, FILE *));
2439 static void print_dwarf_line_table      PROTO((FILE *));
2440 static void add_sibling_attributes      PROTO((dw_die_ref));
2441 static void build_abbrev_table          PROTO((dw_die_ref));
2442 static unsigned long size_of_string     PROTO((char *));
2443 static unsigned long size_of_loc_descr  PROTO((dw_loc_descr_ref));
2444 static unsigned long size_of_locs       PROTO((dw_loc_descr_ref));
2445 static int constant_size                PROTO((long unsigned));
2446 static unsigned long size_of_die        PROTO((dw_die_ref));
2447 static void calc_die_sizes              PROTO((dw_die_ref));
2448 static unsigned long size_of_line_prolog        PROTO((void));
2449 static unsigned long size_of_line_info  PROTO((void));
2450 static unsigned long size_of_pubnames   PROTO((void));
2451 static unsigned long size_of_aranges    PROTO((void));
2452 static enum dwarf_form value_format     PROTO((dw_val_ref));
2453 static void output_value_format         PROTO((dw_val_ref));
2454 static void output_abbrev_section       PROTO((void));
2455 static void output_loc_operands         PROTO((dw_loc_descr_ref));
2456 static unsigned long sibling_offset     PROTO((dw_die_ref));
2457 static void output_die                  PROTO((dw_die_ref));
2458 static void output_compilation_unit_header PROTO((void));
2459 static char *dwarf2_name                PROTO((tree, int));
2460 static void add_pubname                 PROTO((tree, dw_die_ref));
2461 static void output_pubnames             PROTO((void));
2462 static void add_arange                  PROTO((tree, dw_die_ref));
2463 static void output_aranges              PROTO((void));
2464 static void output_line_info            PROTO((void));
2465 static int is_body_block                PROTO((tree));
2466 static dw_die_ref base_type_die         PROTO((tree));
2467 static tree root_type                   PROTO((tree));
2468 static int is_base_type                 PROTO((tree));
2469 static dw_die_ref modified_type_die     PROTO((tree, int, int, dw_die_ref));
2470 static int type_is_enum                 PROTO((tree));
2471 static dw_loc_descr_ref reg_loc_descriptor PROTO((rtx));
2472 static dw_loc_descr_ref based_loc_descr PROTO((unsigned, long));
2473 static int is_based_loc                 PROTO((rtx));
2474 static dw_loc_descr_ref mem_loc_descriptor PROTO((rtx));
2475 static dw_loc_descr_ref concat_loc_descriptor PROTO((rtx, rtx));
2476 static dw_loc_descr_ref loc_descriptor  PROTO((rtx));
2477 static unsigned ceiling                 PROTO((unsigned, unsigned));
2478 static tree field_type                  PROTO((tree));
2479 static unsigned simple_type_align_in_bits PROTO((tree));
2480 static unsigned simple_type_size_in_bits PROTO((tree));
2481 static unsigned field_byte_offset               PROTO((tree));
2482 static void add_AT_location_description PROTO((dw_die_ref,
2483                                                enum dwarf_attribute, rtx));
2484 static void add_data_member_location_attribute PROTO((dw_die_ref, tree));
2485 static void add_const_value_attribute   PROTO((dw_die_ref, rtx));
2486 static void add_location_or_const_value_attribute PROTO((dw_die_ref, tree));
2487 static void add_name_attribute          PROTO((dw_die_ref, char *));
2488 static void add_bound_info              PROTO((dw_die_ref,
2489                                                enum dwarf_attribute, tree));
2490 static void add_subscript_info          PROTO((dw_die_ref, tree));
2491 static void add_byte_size_attribute     PROTO((dw_die_ref, tree));
2492 static void add_bit_offset_attribute    PROTO((dw_die_ref, tree));
2493 static void add_bit_size_attribute      PROTO((dw_die_ref, tree));
2494 static void add_prototyped_attribute    PROTO((dw_die_ref, tree));
2495 static void add_abstract_origin_attribute PROTO((dw_die_ref, tree));
2496 static void add_pure_or_virtual_attribute PROTO((dw_die_ref, tree));
2497 static void add_src_coords_attributes   PROTO((dw_die_ref, tree));
2498 static void add_name_and_src_coords_attributes PROTO((dw_die_ref, tree));
2499 static void push_decl_scope             PROTO((tree));
2500 static dw_die_ref scope_die_for         PROTO((tree, dw_die_ref));
2501 static void pop_decl_scope              PROTO((void));
2502 static void add_type_attribute          PROTO((dw_die_ref, tree, int, int,
2503                                                dw_die_ref));
2504 static char *type_tag                   PROTO((tree));
2505 static tree member_declared_type        PROTO((tree));
2506 static char *decl_start_label           PROTO((tree));
2507 static void gen_array_type_die          PROTO((tree, dw_die_ref));
2508 static void gen_set_type_die            PROTO((tree, dw_die_ref));
2509 static void gen_entry_point_die         PROTO((tree, dw_die_ref));
2510 static void pend_type                   PROTO((tree));
2511 static void output_pending_types_for_scope PROTO((dw_die_ref));
2512 static void gen_inlined_enumeration_type_die PROTO((tree, dw_die_ref));
2513 static void gen_inlined_structure_type_die PROTO((tree, dw_die_ref));
2514 static void gen_inlined_union_type_die  PROTO((tree, dw_die_ref));
2515 static void gen_enumeration_type_die    PROTO((tree, dw_die_ref));
2516 static dw_die_ref gen_formal_parameter_die PROTO((tree, dw_die_ref));
2517 static void gen_unspecified_parameters_die PROTO((tree, dw_die_ref));
2518 static void gen_formal_types_die        PROTO((tree, dw_die_ref));
2519 static void gen_subprogram_die          PROTO((tree, dw_die_ref));
2520 static void gen_variable_die            PROTO((tree, dw_die_ref));
2521 static void gen_label_die               PROTO((tree, dw_die_ref));
2522 static void gen_lexical_block_die       PROTO((tree, dw_die_ref, int));
2523 static void gen_inlined_subroutine_die  PROTO((tree, dw_die_ref, int));
2524 static void gen_field_die               PROTO((tree, dw_die_ref));
2525 static void gen_ptr_to_mbr_type_die     PROTO((tree, dw_die_ref));
2526 static void gen_compile_unit_die        PROTO((char *));
2527 static void gen_string_type_die         PROTO((tree, dw_die_ref));
2528 static void gen_inheritance_die         PROTO((tree, dw_die_ref));
2529 static void gen_member_die              PROTO((tree, dw_die_ref));
2530 static void gen_struct_or_union_type_die PROTO((tree, dw_die_ref));
2531 static void gen_subroutine_type_die     PROTO((tree, dw_die_ref));
2532 static void gen_typedef_die             PROTO((tree, dw_die_ref));
2533 static void gen_type_die                PROTO((tree, dw_die_ref));
2534 static void gen_tagged_type_instantiation_die PROTO((tree, dw_die_ref));
2535 static void gen_block_die               PROTO((tree, dw_die_ref, int));
2536 static void decls_for_scope             PROTO((tree, dw_die_ref, int));
2537 static int is_redundant_typedef         PROTO((tree));
2538 static void gen_decl_die                PROTO((tree, dw_die_ref));
2539 static unsigned lookup_filename         PROTO((char *));
2540
2541 /* Section names used to hold DWARF debugging information.  */
2542 #ifndef DEBUG_INFO_SECTION
2543 #define DEBUG_INFO_SECTION      ".debug_info"
2544 #endif
2545 #ifndef ABBREV_SECTION
2546 #define ABBREV_SECTION          ".debug_abbrev"
2547 #endif
2548 #ifndef ARANGES_SECTION
2549 #define ARANGES_SECTION         ".debug_aranges"
2550 #endif
2551 #ifndef DW_MACINFO_SECTION
2552 #define DW_MACINFO_SECTION      ".debug_macinfo"
2553 #endif
2554 #ifndef DEBUG_LINE_SECTION
2555 #define DEBUG_LINE_SECTION      ".debug_line"
2556 #endif
2557 #ifndef LOC_SECTION
2558 #define LOC_SECTION             ".debug_loc"
2559 #endif
2560 #ifndef PUBNAMES_SECTION
2561 #define PUBNAMES_SECTION        ".debug_pubnames"
2562 #endif
2563 #ifndef STR_SECTION
2564 #define STR_SECTION             ".debug_str"
2565 #endif
2566
2567 /* Standard ELF section names for compiled code and data.  */
2568 #ifndef TEXT_SECTION
2569 #define TEXT_SECTION            ".text"
2570 #endif
2571 #ifndef DATA_SECTION
2572 #define DATA_SECTION            ".data"
2573 #endif
2574 #ifndef BSS_SECTION
2575 #define BSS_SECTION             ".bss"
2576 #endif
2577
2578
2579 /* Definitions of defaults for formats and names of various special
2580    (artificial) labels which may be generated within this file (when the -g
2581    options is used and DWARF_DEBUGGING_INFO is in effect.
2582    If necessary, these may be overridden from within the tm.h file, but
2583    typically, overriding these defaults is unnecessary.  */
2584
2585 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
2586
2587 #ifndef TEXT_END_LABEL
2588 #define TEXT_END_LABEL          "Letext"
2589 #endif
2590 #ifndef DATA_END_LABEL
2591 #define DATA_END_LABEL          "Ledata"
2592 #endif
2593 #ifndef BSS_END_LABEL
2594 #define BSS_END_LABEL           "Lebss"
2595 #endif
2596 #ifndef INSN_LABEL_FMT
2597 #define INSN_LABEL_FMT          "LI%u_"
2598 #endif
2599 #ifndef BLOCK_BEGIN_LABEL
2600 #define BLOCK_BEGIN_LABEL       "LBB"
2601 #endif
2602 #ifndef BLOCK_END_LABEL
2603 #define BLOCK_END_LABEL         "LBE"
2604 #endif
2605 #ifndef BODY_BEGIN_LABEL
2606 #define BODY_BEGIN_LABEL        "Lbb"
2607 #endif
2608 #ifndef BODY_END_LABEL
2609 #define BODY_END_LABEL          "Lbe"
2610 #endif
2611 #ifndef LINE_CODE_LABEL
2612 #define LINE_CODE_LABEL         "LM"
2613 #endif
2614 #ifndef SEPARATE_LINE_CODE_LABEL
2615 #define SEPARATE_LINE_CODE_LABEL        "LSM"
2616 #endif
2617
2618 /* Convert a reference to the assembler name of a C-level name.  This
2619    macro has the same effect as ASM_OUTPUT_LABELREF, but copies to
2620    a string rather than writing to a file.  */
2621 #ifndef ASM_NAME_TO_STRING
2622 #define ASM_NAME_TO_STRING(STR, NAME) \
2623   do {                                                                        \
2624       if ((NAME)[0] == '*')                                                   \
2625         strcpy (STR, NAME+1);                                                 \
2626       else                                                                    \
2627         strcpy (STR, NAME);                                                   \
2628   }                                                                           \
2629   while (0)
2630 #endif
2631 \f
2632 /* Convert an integer constant expression into assembler syntax.  Addition
2633    and subtraction are the only arithmetic that may appear in these
2634    expressions.   This is an adaptation of output_addr_const in final.c.
2635    Here, the target of the conversion is a string buffer.  We can't use
2636    output_addr_const directly, because it writes to a file.  */
2637
2638 static void
2639 addr_const_to_string (str, x)
2640      char *str;
2641      rtx x;
2642 {
2643   char buf1[256];
2644   char buf2[256];
2645
2646 restart:
2647   str[0] = '\0';
2648   switch (GET_CODE (x))
2649     {
2650     case PC:
2651       if (flag_pic)
2652         strcat (str, ",");
2653       else
2654         abort ();
2655       break;
2656
2657     case SYMBOL_REF:
2658       ASM_NAME_TO_STRING (buf1, XSTR (x, 0));
2659       strcat (str, buf1);
2660       break;
2661
2662     case LABEL_REF:
2663       ASM_GENERATE_INTERNAL_LABEL (buf1, "L", CODE_LABEL_NUMBER (XEXP (x, 0)));
2664       ASM_NAME_TO_STRING (buf2, buf1);
2665       strcat (str, buf2);
2666       break;
2667
2668     case CODE_LABEL:
2669       ASM_GENERATE_INTERNAL_LABEL (buf1, "L", CODE_LABEL_NUMBER (x));
2670       ASM_NAME_TO_STRING (buf2, buf1);
2671       strcat (str, buf2);
2672       break;
2673
2674     case CONST_INT:
2675       sprintf (buf1, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
2676       strcat (str, buf1);
2677       break;
2678
2679     case CONST:
2680       /* This used to output parentheses around the expression, but that does 
2681          not work on the 386 (either ATT or BSD assembler).  */
2682       addr_const_to_string (buf1, XEXP (x, 0));
2683       strcat (str, buf1);
2684       break;
2685
2686     case CONST_DOUBLE:
2687       if (GET_MODE (x) == VOIDmode)
2688         {
2689           /* We can use %d if the number is one word and positive.  */
2690           if (CONST_DOUBLE_HIGH (x))
2691             sprintf (buf1, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
2692                      CONST_DOUBLE_HIGH (x), CONST_DOUBLE_LOW (x));
2693           else if (CONST_DOUBLE_LOW (x) < 0)
2694             sprintf (buf1, HOST_WIDE_INT_PRINT_HEX, CONST_DOUBLE_LOW (x));
2695           else
2696             sprintf (buf1, HOST_WIDE_INT_PRINT_DEC,
2697                      CONST_DOUBLE_LOW (x));
2698           strcat (str, buf1);
2699         }
2700       else
2701         /* We can't handle floating point constants; PRINT_OPERAND must
2702            handle them.  */
2703         output_operand_lossage ("floating constant misused");
2704       break;
2705
2706     case PLUS:
2707       /* Some assemblers need integer constants to appear last (eg masm).  */
2708       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
2709         {
2710           addr_const_to_string (buf1, XEXP (x, 1));
2711           strcat (str, buf1);
2712           if (INTVAL (XEXP (x, 0)) >= 0)
2713             strcat (str, "+");
2714
2715           addr_const_to_string (buf1, XEXP (x, 0));
2716           strcat (str, buf1);
2717         }
2718       else
2719         {
2720           addr_const_to_string (buf1, XEXP (x, 0));
2721           strcat (str, buf1);
2722           if (INTVAL (XEXP (x, 1)) >= 0)
2723             strcat (str, "+");
2724
2725           addr_const_to_string (buf1, XEXP (x, 1));
2726           strcat (str, buf1);
2727         }
2728       break;
2729
2730     case MINUS:
2731       /* Avoid outputting things like x-x or x+5-x, since some assemblers
2732          can't handle that.  */
2733       x = simplify_subtraction (x);
2734       if (GET_CODE (x) != MINUS)
2735         goto restart;
2736
2737       addr_const_to_string (buf1, XEXP (x, 0));
2738       strcat (str, buf1);
2739       strcat (str, "-");
2740       if (GET_CODE (XEXP (x, 1)) == CONST_INT
2741           && INTVAL (XEXP (x, 1)) < 0)
2742         {
2743           strcat (str, ASM_OPEN_PAREN);
2744           addr_const_to_string (buf1, XEXP (x, 1));
2745           strcat (str, buf1);
2746           strcat (str, ASM_CLOSE_PAREN);
2747         }
2748       else
2749         {
2750           addr_const_to_string (buf1, XEXP (x, 1));
2751           strcat (str, buf1);
2752         }
2753       break;
2754
2755     case ZERO_EXTEND:
2756     case SIGN_EXTEND:
2757       addr_const_to_string (buf1, XEXP (x, 0));
2758       strcat (str, buf1);
2759       break;
2760
2761     default:
2762       output_operand_lossage ("invalid expression as operand");
2763     }
2764 }
2765
2766 /* Convert an address constant to a string, and return a pointer to
2767    a copy of the result, located on the heap.  */
2768
2769 static char *
2770 addr_to_string (x)
2771      rtx x;
2772 {
2773   char buf[1024];
2774   addr_const_to_string (buf, x);
2775   return xstrdup (buf);
2776 }
2777
2778 /* Test if rtl node points to a pseudo register.  */
2779
2780 static inline int
2781 is_pseudo_reg (rtl)
2782      register rtx rtl;
2783 {
2784   return (((GET_CODE (rtl) == REG) && (REGNO (rtl) >= FIRST_PSEUDO_REGISTER))
2785           || ((GET_CODE (rtl) == SUBREG)
2786               && (REGNO (XEXP (rtl, 0)) >= FIRST_PSEUDO_REGISTER)));
2787 }
2788
2789 /* Return a reference to a type, with its const and volatile qualifiers
2790    removed.  */
2791
2792 static inline tree
2793 type_main_variant (type)
2794      register tree type;
2795 {
2796   type = TYPE_MAIN_VARIANT (type);
2797
2798   /* There really should be only one main variant among any group of variants 
2799      of a given type (and all of the MAIN_VARIANT values for all members of
2800      the group should point to that one type) but sometimes the C front-end
2801      messes this up for array types, so we work around that bug here.  */
2802
2803   if (TREE_CODE (type) == ARRAY_TYPE)
2804     while (type != TYPE_MAIN_VARIANT (type))
2805       type = TYPE_MAIN_VARIANT (type);
2806
2807   return type;
2808 }
2809
2810 /* Return non-zero if the given type node represents a tagged type.  */
2811
2812 static inline int
2813 is_tagged_type (type)
2814      register tree type;
2815 {
2816   register enum tree_code code = TREE_CODE (type);
2817
2818   return (code == RECORD_TYPE || code == UNION_TYPE
2819           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
2820 }
2821
2822 /* Convert a DIE tag into its string name.  */
2823
2824 static char *
2825 dwarf_tag_name (tag)
2826      register unsigned tag;
2827 {
2828   switch (tag)
2829     {
2830     case DW_TAG_padding:
2831       return "DW_TAG_padding";
2832     case DW_TAG_array_type:
2833       return "DW_TAG_array_type";
2834     case DW_TAG_class_type:
2835       return "DW_TAG_class_type";
2836     case DW_TAG_entry_point:
2837       return "DW_TAG_entry_point";
2838     case DW_TAG_enumeration_type:
2839       return "DW_TAG_enumeration_type";
2840     case DW_TAG_formal_parameter:
2841       return "DW_TAG_formal_parameter";
2842     case DW_TAG_imported_declaration:
2843       return "DW_TAG_imported_declaration";
2844     case DW_TAG_label:
2845       return "DW_TAG_label";
2846     case DW_TAG_lexical_block:
2847       return "DW_TAG_lexical_block";
2848     case DW_TAG_member:
2849       return "DW_TAG_member";
2850     case DW_TAG_pointer_type:
2851       return "DW_TAG_pointer_type";
2852     case DW_TAG_reference_type:
2853       return "DW_TAG_reference_type";
2854     case DW_TAG_compile_unit:
2855       return "DW_TAG_compile_unit";
2856     case DW_TAG_string_type:
2857       return "DW_TAG_string_type";
2858     case DW_TAG_structure_type:
2859       return "DW_TAG_structure_type";
2860     case DW_TAG_subroutine_type:
2861       return "DW_TAG_subroutine_type";
2862     case DW_TAG_typedef:
2863       return "DW_TAG_typedef";
2864     case DW_TAG_union_type:
2865       return "DW_TAG_union_type";
2866     case DW_TAG_unspecified_parameters:
2867       return "DW_TAG_unspecified_parameters";
2868     case DW_TAG_variant:
2869       return "DW_TAG_variant";
2870     case DW_TAG_common_block:
2871       return "DW_TAG_common_block";
2872     case DW_TAG_common_inclusion:
2873       return "DW_TAG_common_inclusion";
2874     case DW_TAG_inheritance:
2875       return "DW_TAG_inheritance";
2876     case DW_TAG_inlined_subroutine:
2877       return "DW_TAG_inlined_subroutine";
2878     case DW_TAG_module:
2879       return "DW_TAG_module";
2880     case DW_TAG_ptr_to_member_type:
2881       return "DW_TAG_ptr_to_member_type";
2882     case DW_TAG_set_type:
2883       return "DW_TAG_set_type";
2884     case DW_TAG_subrange_type:
2885       return "DW_TAG_subrange_type";
2886     case DW_TAG_with_stmt:
2887       return "DW_TAG_with_stmt";
2888     case DW_TAG_access_declaration:
2889       return "DW_TAG_access_declaration";
2890     case DW_TAG_base_type:
2891       return "DW_TAG_base_type";
2892     case DW_TAG_catch_block:
2893       return "DW_TAG_catch_block";
2894     case DW_TAG_const_type:
2895       return "DW_TAG_const_type";
2896     case DW_TAG_constant:
2897       return "DW_TAG_constant";
2898     case DW_TAG_enumerator:
2899       return "DW_TAG_enumerator";
2900     case DW_TAG_file_type:
2901       return "DW_TAG_file_type";
2902     case DW_TAG_friend:
2903       return "DW_TAG_friend";
2904     case DW_TAG_namelist:
2905       return "DW_TAG_namelist";
2906     case DW_TAG_namelist_item:
2907       return "DW_TAG_namelist_item";
2908     case DW_TAG_packed_type:
2909       return "DW_TAG_packed_type";
2910     case DW_TAG_subprogram:
2911       return "DW_TAG_subprogram";
2912     case DW_TAG_template_type_param:
2913       return "DW_TAG_template_type_param";
2914     case DW_TAG_template_value_param:
2915       return "DW_TAG_template_value_param";
2916     case DW_TAG_thrown_type:
2917       return "DW_TAG_thrown_type";
2918     case DW_TAG_try_block:
2919       return "DW_TAG_try_block";
2920     case DW_TAG_variant_part:
2921       return "DW_TAG_variant_part";
2922     case DW_TAG_variable:
2923       return "DW_TAG_variable";
2924     case DW_TAG_volatile_type:
2925       return "DW_TAG_volatile_type";
2926     case DW_TAG_MIPS_loop:
2927       return "DW_TAG_MIPS_loop";
2928     case DW_TAG_format_label:
2929       return "DW_TAG_format_label";
2930     case DW_TAG_function_template:
2931       return "DW_TAG_function_template";
2932     case DW_TAG_class_template:
2933       return "DW_TAG_class_template";
2934     default:
2935       return "DW_TAG_<unknown>";
2936     }
2937 }
2938
2939 /* Convert a DWARF attribute code into its string name.  */
2940
2941 static char *
2942 dwarf_attr_name (attr)
2943      register unsigned attr;
2944 {
2945   switch (attr)
2946     {
2947     case DW_AT_sibling:
2948       return "DW_AT_sibling";
2949     case DW_AT_location:
2950       return "DW_AT_location";
2951     case DW_AT_name:
2952       return "DW_AT_name";
2953     case DW_AT_ordering:
2954       return "DW_AT_ordering";
2955     case DW_AT_subscr_data:
2956       return "DW_AT_subscr_data";
2957     case DW_AT_byte_size:
2958       return "DW_AT_byte_size";
2959     case DW_AT_bit_offset:
2960       return "DW_AT_bit_offset";
2961     case DW_AT_bit_size:
2962       return "DW_AT_bit_size";
2963     case DW_AT_element_list:
2964       return "DW_AT_element_list";
2965     case DW_AT_stmt_list:
2966       return "DW_AT_stmt_list";
2967     case DW_AT_low_pc:
2968       return "DW_AT_low_pc";
2969     case DW_AT_high_pc:
2970       return "DW_AT_high_pc";
2971     case DW_AT_language:
2972       return "DW_AT_language";
2973     case DW_AT_member:
2974       return "DW_AT_member";
2975     case DW_AT_discr:
2976       return "DW_AT_discr";
2977     case DW_AT_discr_value:
2978       return "DW_AT_discr_value";
2979     case DW_AT_visibility:
2980       return "DW_AT_visibility";
2981     case DW_AT_import:
2982       return "DW_AT_import";
2983     case DW_AT_string_length:
2984       return "DW_AT_string_length";
2985     case DW_AT_common_reference:
2986       return "DW_AT_common_reference";
2987     case DW_AT_comp_dir:
2988       return "DW_AT_comp_dir";
2989     case DW_AT_const_value:
2990       return "DW_AT_const_value";
2991     case DW_AT_containing_type:
2992       return "DW_AT_containing_type";
2993     case DW_AT_default_value:
2994       return "DW_AT_default_value";
2995     case DW_AT_inline:
2996       return "DW_AT_inline";
2997     case DW_AT_is_optional:
2998       return "DW_AT_is_optional";
2999     case DW_AT_lower_bound:
3000       return "DW_AT_lower_bound";
3001     case DW_AT_producer:
3002       return "DW_AT_producer";
3003     case DW_AT_prototyped:
3004       return "DW_AT_prototyped";
3005     case DW_AT_return_addr:
3006       return "DW_AT_return_addr";
3007     case DW_AT_start_scope:
3008       return "DW_AT_start_scope";
3009     case DW_AT_stride_size:
3010       return "DW_AT_stride_size";
3011     case DW_AT_upper_bound:
3012       return "DW_AT_upper_bound";
3013     case DW_AT_abstract_origin:
3014       return "DW_AT_abstract_origin";
3015     case DW_AT_accessibility:
3016       return "DW_AT_accessibility";
3017     case DW_AT_address_class:
3018       return "DW_AT_address_class";
3019     case DW_AT_artificial:
3020       return "DW_AT_artificial";
3021     case DW_AT_base_types:
3022       return "DW_AT_base_types";
3023     case DW_AT_calling_convention:
3024       return "DW_AT_calling_convention";
3025     case DW_AT_count:
3026       return "DW_AT_count";
3027     case DW_AT_data_member_location:
3028       return "DW_AT_data_member_location";
3029     case DW_AT_decl_column:
3030       return "DW_AT_decl_column";
3031     case DW_AT_decl_file:
3032       return "DW_AT_decl_file";
3033     case DW_AT_decl_line:
3034       return "DW_AT_decl_line";
3035     case DW_AT_declaration:
3036       return "DW_AT_declaration";
3037     case DW_AT_discr_list:
3038       return "DW_AT_discr_list";
3039     case DW_AT_encoding:
3040       return "DW_AT_encoding";
3041     case DW_AT_external:
3042       return "DW_AT_external";
3043     case DW_AT_frame_base:
3044       return "DW_AT_frame_base";
3045     case DW_AT_friend:
3046       return "DW_AT_friend";
3047     case DW_AT_identifier_case:
3048       return "DW_AT_identifier_case";
3049     case DW_AT_macro_info:
3050       return "DW_AT_macro_info";
3051     case DW_AT_namelist_items:
3052       return "DW_AT_namelist_items";
3053     case DW_AT_priority:
3054       return "DW_AT_priority";
3055     case DW_AT_segment:
3056       return "DW_AT_segment";
3057     case DW_AT_specification:
3058       return "DW_AT_specification";
3059     case DW_AT_static_link:
3060       return "DW_AT_static_link";
3061     case DW_AT_type:
3062       return "DW_AT_type";
3063     case DW_AT_use_location:
3064       return "DW_AT_use_location";
3065     case DW_AT_variable_parameter:
3066       return "DW_AT_variable_parameter";
3067     case DW_AT_virtuality:
3068       return "DW_AT_virtuality";
3069     case DW_AT_vtable_elem_location:
3070       return "DW_AT_vtable_elem_location";
3071
3072     case DW_AT_MIPS_fde:
3073       return "DW_AT_MIPS_fde";
3074     case DW_AT_MIPS_loop_begin:
3075       return "DW_AT_MIPS_loop_begin";
3076     case DW_AT_MIPS_tail_loop_begin:
3077       return "DW_AT_MIPS_tail_loop_begin";
3078     case DW_AT_MIPS_epilog_begin:
3079       return "DW_AT_MIPS_epilog_begin";
3080     case DW_AT_MIPS_loop_unroll_factor:
3081       return "DW_AT_MIPS_loop_unroll_factor";
3082     case DW_AT_MIPS_software_pipeline_depth:
3083       return "DW_AT_MIPS_software_pipeline_depth";
3084     case DW_AT_MIPS_linkage_name:
3085       return "DW_AT_MIPS_linkage_name";
3086     case DW_AT_MIPS_stride:
3087       return "DW_AT_MIPS_stride";
3088     case DW_AT_MIPS_abstract_name:
3089       return "DW_AT_MIPS_abstract_name";
3090     case DW_AT_MIPS_clone_origin:
3091       return "DW_AT_MIPS_clone_origin";
3092     case DW_AT_MIPS_has_inlines:
3093       return "DW_AT_MIPS_has_inlines";
3094
3095     case DW_AT_sf_names:
3096       return "DW_AT_sf_names";
3097     case DW_AT_src_info:
3098       return "DW_AT_src_info";
3099     case DW_AT_mac_info:
3100       return "DW_AT_mac_info";
3101     case DW_AT_src_coords:
3102       return "DW_AT_src_coords";
3103     case DW_AT_body_begin:
3104       return "DW_AT_body_begin";
3105     case DW_AT_body_end:
3106       return "DW_AT_body_end";
3107     default:
3108       return "DW_AT_<unknown>";
3109     }
3110 }
3111
3112 /* Convert a DWARF value form code into its string name.  */
3113
3114 static char *
3115 dwarf_form_name (form)
3116      register unsigned form;
3117 {
3118   switch (form)
3119     {
3120     case DW_FORM_addr:
3121       return "DW_FORM_addr";
3122     case DW_FORM_block2:
3123       return "DW_FORM_block2";
3124     case DW_FORM_block4:
3125       return "DW_FORM_block4";
3126     case DW_FORM_data2:
3127       return "DW_FORM_data2";
3128     case DW_FORM_data4:
3129       return "DW_FORM_data4";
3130     case DW_FORM_data8:
3131       return "DW_FORM_data8";
3132     case DW_FORM_string:
3133       return "DW_FORM_string";
3134     case DW_FORM_block:
3135       return "DW_FORM_block";
3136     case DW_FORM_block1:
3137       return "DW_FORM_block1";
3138     case DW_FORM_data1:
3139       return "DW_FORM_data1";
3140     case DW_FORM_flag:
3141       return "DW_FORM_flag";
3142     case DW_FORM_sdata:
3143       return "DW_FORM_sdata";
3144     case DW_FORM_strp:
3145       return "DW_FORM_strp";
3146     case DW_FORM_udata:
3147       return "DW_FORM_udata";
3148     case DW_FORM_ref_addr:
3149       return "DW_FORM_ref_addr";
3150     case DW_FORM_ref1:
3151       return "DW_FORM_ref1";
3152     case DW_FORM_ref2:
3153       return "DW_FORM_ref2";
3154     case DW_FORM_ref4:
3155       return "DW_FORM_ref4";
3156     case DW_FORM_ref8:
3157       return "DW_FORM_ref8";
3158     case DW_FORM_ref_udata:
3159       return "DW_FORM_ref_udata";
3160     case DW_FORM_indirect:
3161       return "DW_FORM_indirect";
3162     default:
3163       return "DW_FORM_<unknown>";
3164     }
3165 }
3166
3167 /* Convert a DWARF stack opcode into its string name.  */
3168
3169 static char *
3170 dwarf_stack_op_name (op)
3171      register unsigned op;
3172 {
3173   switch (op)
3174     {
3175     case DW_OP_addr:
3176       return "DW_OP_addr";
3177     case DW_OP_deref:
3178       return "DW_OP_deref";
3179     case DW_OP_const1u:
3180       return "DW_OP_const1u";
3181     case DW_OP_const1s:
3182       return "DW_OP_const1s";
3183     case DW_OP_const2u:
3184       return "DW_OP_const2u";
3185     case DW_OP_const2s:
3186       return "DW_OP_const2s";
3187     case DW_OP_const4u:
3188       return "DW_OP_const4u";
3189     case DW_OP_const4s:
3190       return "DW_OP_const4s";
3191     case DW_OP_const8u:
3192       return "DW_OP_const8u";
3193     case DW_OP_const8s:
3194       return "DW_OP_const8s";
3195     case DW_OP_constu:
3196       return "DW_OP_constu";
3197     case DW_OP_consts:
3198       return "DW_OP_consts";
3199     case DW_OP_dup:
3200       return "DW_OP_dup";
3201     case DW_OP_drop:
3202       return "DW_OP_drop";
3203     case DW_OP_over:
3204       return "DW_OP_over";
3205     case DW_OP_pick:
3206       return "DW_OP_pick";
3207     case DW_OP_swap:
3208       return "DW_OP_swap";
3209     case DW_OP_rot:
3210       return "DW_OP_rot";
3211     case DW_OP_xderef:
3212       return "DW_OP_xderef";
3213     case DW_OP_abs:
3214       return "DW_OP_abs";
3215     case DW_OP_and:
3216       return "DW_OP_and";
3217     case DW_OP_div:
3218       return "DW_OP_div";
3219     case DW_OP_minus:
3220       return "DW_OP_minus";
3221     case DW_OP_mod:
3222       return "DW_OP_mod";
3223     case DW_OP_mul:
3224       return "DW_OP_mul";
3225     case DW_OP_neg:
3226       return "DW_OP_neg";
3227     case DW_OP_not:
3228       return "DW_OP_not";
3229     case DW_OP_or:
3230       return "DW_OP_or";
3231     case DW_OP_plus:
3232       return "DW_OP_plus";
3233     case DW_OP_plus_uconst:
3234       return "DW_OP_plus_uconst";
3235     case DW_OP_shl:
3236       return "DW_OP_shl";
3237     case DW_OP_shr:
3238       return "DW_OP_shr";
3239     case DW_OP_shra:
3240       return "DW_OP_shra";
3241     case DW_OP_xor:
3242       return "DW_OP_xor";
3243     case DW_OP_bra:
3244       return "DW_OP_bra";
3245     case DW_OP_eq:
3246       return "DW_OP_eq";
3247     case DW_OP_ge:
3248       return "DW_OP_ge";
3249     case DW_OP_gt:
3250       return "DW_OP_gt";
3251     case DW_OP_le:
3252       return "DW_OP_le";
3253     case DW_OP_lt:
3254       return "DW_OP_lt";
3255     case DW_OP_ne:
3256       return "DW_OP_ne";
3257     case DW_OP_skip:
3258       return "DW_OP_skip";
3259     case DW_OP_lit0:
3260       return "DW_OP_lit0";
3261     case DW_OP_lit1:
3262       return "DW_OP_lit1";
3263     case DW_OP_lit2:
3264       return "DW_OP_lit2";
3265     case DW_OP_lit3:
3266       return "DW_OP_lit3";
3267     case DW_OP_lit4:
3268       return "DW_OP_lit4";
3269     case DW_OP_lit5:
3270       return "DW_OP_lit5";
3271     case DW_OP_lit6:
3272       return "DW_OP_lit6";
3273     case DW_OP_lit7:
3274       return "DW_OP_lit7";
3275     case DW_OP_lit8:
3276       return "DW_OP_lit8";
3277     case DW_OP_lit9:
3278       return "DW_OP_lit9";
3279     case DW_OP_lit10:
3280       return "DW_OP_lit10";
3281     case DW_OP_lit11:
3282       return "DW_OP_lit11";
3283     case DW_OP_lit12:
3284       return "DW_OP_lit12";
3285     case DW_OP_lit13:
3286       return "DW_OP_lit13";
3287     case DW_OP_lit14:
3288       return "DW_OP_lit14";
3289     case DW_OP_lit15:
3290       return "DW_OP_lit15";
3291     case DW_OP_lit16:
3292       return "DW_OP_lit16";
3293     case DW_OP_lit17:
3294       return "DW_OP_lit17";
3295     case DW_OP_lit18:
3296       return "DW_OP_lit18";
3297     case DW_OP_lit19:
3298       return "DW_OP_lit19";
3299     case DW_OP_lit20:
3300       return "DW_OP_lit20";
3301     case DW_OP_lit21:
3302       return "DW_OP_lit21";
3303     case DW_OP_lit22:
3304       return "DW_OP_lit22";
3305     case DW_OP_lit23:
3306       return "DW_OP_lit23";
3307     case DW_OP_lit24:
3308       return "DW_OP_lit24";
3309     case DW_OP_lit25:
3310       return "DW_OP_lit25";
3311     case DW_OP_lit26:
3312       return "DW_OP_lit26";
3313     case DW_OP_lit27:
3314       return "DW_OP_lit27";
3315     case DW_OP_lit28:
3316       return "DW_OP_lit28";
3317     case DW_OP_lit29:
3318       return "DW_OP_lit29";
3319     case DW_OP_lit30:
3320       return "DW_OP_lit30";
3321     case DW_OP_lit31:
3322       return "DW_OP_lit31";
3323     case DW_OP_reg0:
3324       return "DW_OP_reg0";
3325     case DW_OP_reg1:
3326       return "DW_OP_reg1";
3327     case DW_OP_reg2:
3328       return "DW_OP_reg2";
3329     case DW_OP_reg3:
3330       return "DW_OP_reg3";
3331     case DW_OP_reg4:
3332       return "DW_OP_reg4";
3333     case DW_OP_reg5:
3334       return "DW_OP_reg5";
3335     case DW_OP_reg6:
3336       return "DW_OP_reg6";
3337     case DW_OP_reg7:
3338       return "DW_OP_reg7";
3339     case DW_OP_reg8:
3340       return "DW_OP_reg8";
3341     case DW_OP_reg9:
3342       return "DW_OP_reg9";
3343     case DW_OP_reg10:
3344       return "DW_OP_reg10";
3345     case DW_OP_reg11:
3346       return "DW_OP_reg11";
3347     case DW_OP_reg12:
3348       return "DW_OP_reg12";
3349     case DW_OP_reg13:
3350       return "DW_OP_reg13";
3351     case DW_OP_reg14:
3352       return "DW_OP_reg14";
3353     case DW_OP_reg15:
3354       return "DW_OP_reg15";
3355     case DW_OP_reg16:
3356       return "DW_OP_reg16";
3357     case DW_OP_reg17:
3358       return "DW_OP_reg17";
3359     case DW_OP_reg18:
3360       return "DW_OP_reg18";
3361     case DW_OP_reg19:
3362       return "DW_OP_reg19";
3363     case DW_OP_reg20:
3364       return "DW_OP_reg20";
3365     case DW_OP_reg21:
3366       return "DW_OP_reg21";
3367     case DW_OP_reg22:
3368       return "DW_OP_reg22";
3369     case DW_OP_reg23:
3370       return "DW_OP_reg23";
3371     case DW_OP_reg24:
3372       return "DW_OP_reg24";
3373     case DW_OP_reg25:
3374       return "DW_OP_reg25";
3375     case DW_OP_reg26:
3376       return "DW_OP_reg26";
3377     case DW_OP_reg27:
3378       return "DW_OP_reg27";
3379     case DW_OP_reg28:
3380       return "DW_OP_reg28";
3381     case DW_OP_reg29:
3382       return "DW_OP_reg29";
3383     case DW_OP_reg30:
3384       return "DW_OP_reg30";
3385     case DW_OP_reg31:
3386       return "DW_OP_reg31";
3387     case DW_OP_breg0:
3388       return "DW_OP_breg0";
3389     case DW_OP_breg1:
3390       return "DW_OP_breg1";
3391     case DW_OP_breg2:
3392       return "DW_OP_breg2";
3393     case DW_OP_breg3:
3394       return "DW_OP_breg3";
3395     case DW_OP_breg4:
3396       return "DW_OP_breg4";
3397     case DW_OP_breg5:
3398       return "DW_OP_breg5";
3399     case DW_OP_breg6:
3400       return "DW_OP_breg6";
3401     case DW_OP_breg7:
3402       return "DW_OP_breg7";
3403     case DW_OP_breg8:
3404       return "DW_OP_breg8";
3405     case DW_OP_breg9:
3406       return "DW_OP_breg9";
3407     case DW_OP_breg10:
3408       return "DW_OP_breg10";
3409     case DW_OP_breg11:
3410       return "DW_OP_breg11";
3411     case DW_OP_breg12:
3412       return "DW_OP_breg12";
3413     case DW_OP_breg13:
3414       return "DW_OP_breg13";
3415     case DW_OP_breg14:
3416       return "DW_OP_breg14";
3417     case DW_OP_breg15:
3418       return "DW_OP_breg15";
3419     case DW_OP_breg16:
3420       return "DW_OP_breg16";
3421     case DW_OP_breg17:
3422       return "DW_OP_breg17";
3423     case DW_OP_breg18:
3424       return "DW_OP_breg18";
3425     case DW_OP_breg19:
3426       return "DW_OP_breg19";
3427     case DW_OP_breg20:
3428       return "DW_OP_breg20";
3429     case DW_OP_breg21:
3430       return "DW_OP_breg21";
3431     case DW_OP_breg22:
3432       return "DW_OP_breg22";
3433     case DW_OP_breg23:
3434       return "DW_OP_breg23";
3435     case DW_OP_breg24:
3436       return "DW_OP_breg24";
3437     case DW_OP_breg25:
3438       return "DW_OP_breg25";
3439     case DW_OP_breg26:
3440       return "DW_OP_breg26";
3441     case DW_OP_breg27:
3442       return "DW_OP_breg27";
3443     case DW_OP_breg28:
3444       return "DW_OP_breg28";
3445     case DW_OP_breg29:
3446       return "DW_OP_breg29";
3447     case DW_OP_breg30:
3448       return "DW_OP_breg30";
3449     case DW_OP_breg31:
3450       return "DW_OP_breg31";
3451     case DW_OP_regx:
3452       return "DW_OP_regx";
3453     case DW_OP_fbreg:
3454       return "DW_OP_fbreg";
3455     case DW_OP_bregx:
3456       return "DW_OP_bregx";
3457     case DW_OP_piece:
3458       return "DW_OP_piece";
3459     case DW_OP_deref_size:
3460       return "DW_OP_deref_size";
3461     case DW_OP_xderef_size:
3462       return "DW_OP_xderef_size";
3463     case DW_OP_nop:
3464       return "DW_OP_nop";
3465     default:
3466       return "OP_<unknown>";
3467     }
3468 }
3469
3470 /* Convert a DWARF type code into its string name.  */
3471
3472 static char *
3473 dwarf_type_encoding_name (enc)
3474      register unsigned enc;
3475 {
3476   switch (enc)
3477     {
3478     case DW_ATE_address:
3479       return "DW_ATE_address";
3480     case DW_ATE_boolean:
3481       return "DW_ATE_boolean";
3482     case DW_ATE_complex_float:
3483       return "DW_ATE_complex_float";
3484     case DW_ATE_float:
3485       return "DW_ATE_float";
3486     case DW_ATE_signed:
3487       return "DW_ATE_signed";
3488     case DW_ATE_signed_char:
3489       return "DW_ATE_signed_char";
3490     case DW_ATE_unsigned:
3491       return "DW_ATE_unsigned";
3492     case DW_ATE_unsigned_char:
3493       return "DW_ATE_unsigned_char";
3494     default:
3495       return "DW_ATE_<unknown>";
3496     }
3497 }
3498 \f
3499 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
3500    instance of an inlined instance of a decl which is local to an inline
3501    function, so we have to trace all of the way back through the origin chain
3502    to find out what sort of node actually served as the original seed for the
3503    given block.  */
3504
3505 static tree
3506 decl_ultimate_origin (decl)
3507      register tree decl;
3508 {
3509   register tree immediate_origin = DECL_ABSTRACT_ORIGIN (decl);
3510
3511   if (immediate_origin == NULL_TREE)
3512     return NULL_TREE;
3513   else
3514     {
3515       register tree ret_val;
3516       register tree lookahead = immediate_origin;
3517
3518       do
3519         {
3520           ret_val = lookahead;
3521           lookahead = DECL_ABSTRACT_ORIGIN (ret_val);
3522         }
3523       while (lookahead != NULL && lookahead != ret_val);
3524
3525       return ret_val;
3526     }
3527 }
3528
3529 /* Determine the "ultimate origin" of a block.  The block may be an inlined
3530    instance of an inlined instance of a block which is local to an inline
3531    function, so we have to trace all of the way back through the origin chain
3532    to find out what sort of node actually served as the original seed for the
3533    given block.  */
3534
3535 static tree
3536 block_ultimate_origin (block)
3537      register tree block;
3538 {
3539   register tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
3540
3541   if (immediate_origin == NULL_TREE)
3542     return NULL_TREE;
3543   else
3544     {
3545       register tree ret_val;
3546       register tree lookahead = immediate_origin;
3547
3548       do
3549         {
3550           ret_val = lookahead;
3551           lookahead = (TREE_CODE (ret_val) == BLOCK)
3552             ? BLOCK_ABSTRACT_ORIGIN (ret_val)
3553             : NULL;
3554         }
3555       while (lookahead != NULL && lookahead != ret_val);
3556
3557       return ret_val;
3558     }
3559 }
3560
3561 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
3562    of a virtual function may refer to a base class, so we check the 'this'
3563    parameter.  */
3564
3565 static tree
3566 decl_class_context (decl)
3567      tree decl;
3568 {
3569   tree context = NULL_TREE;
3570
3571   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3572     context = DECL_CONTEXT (decl);
3573   else
3574     context = TYPE_MAIN_VARIANT
3575       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3576
3577   if (context && TREE_CODE_CLASS (TREE_CODE (context)) != 't')
3578     context = NULL_TREE;
3579
3580   return context;
3581 }
3582 \f
3583 /* Add an attribute/value pair to a DIE */
3584
3585 static inline void
3586 add_dwarf_attr (die, attr)
3587      register dw_die_ref die;
3588      register dw_attr_ref attr;
3589 {
3590   if (die != NULL && attr != NULL)
3591     {
3592       if (die->die_attr == NULL)
3593         {
3594           die->die_attr = attr;
3595           die->die_attr_last = attr;
3596         }
3597       else
3598         {
3599           die->die_attr_last->dw_attr_next = attr;
3600           die->die_attr_last = attr;
3601         }
3602     }
3603 }
3604
3605 /* Add a flag value attribute to a DIE.  */
3606
3607 static inline void
3608 add_AT_flag (die, attr_kind, flag)
3609      register dw_die_ref die;
3610      register enum dwarf_attribute attr_kind;
3611      register unsigned flag;
3612 {
3613   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
3614
3615   attr->dw_attr_next = NULL;
3616   attr->dw_attr = attr_kind;
3617   attr->dw_attr_val.val_class = dw_val_class_flag;
3618   attr->dw_attr_val.v.val_flag = flag;
3619   add_dwarf_attr (die, attr);
3620 }
3621
3622 /* Add a signed integer attribute value to a DIE.  */
3623
3624 static inline void
3625 add_AT_int (die, attr_kind, int_val)
3626      register dw_die_ref die;
3627      register enum dwarf_attribute attr_kind;
3628      register long int int_val;
3629 {
3630   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
3631
3632   attr->dw_attr_next = NULL;
3633   attr->dw_attr = attr_kind;
3634   attr->dw_attr_val.val_class = dw_val_class_const;
3635   attr->dw_attr_val.v.val_int = int_val;
3636   add_dwarf_attr (die, attr);
3637 }
3638
3639 /* Add an unsigned integer attribute value to a DIE.  */
3640
3641 static inline void
3642 add_AT_unsigned (die, attr_kind, unsigned_val)
3643      register dw_die_ref die;
3644      register enum dwarf_attribute attr_kind;
3645      register unsigned long unsigned_val;
3646 {
3647   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
3648
3649   attr->dw_attr_next = NULL;
3650   attr->dw_attr = attr_kind;
3651   attr->dw_attr_val.val_class = dw_val_class_unsigned_const;
3652   attr->dw_attr_val.v.val_unsigned = unsigned_val;
3653   add_dwarf_attr (die, attr);
3654 }
3655
3656 /* Add an unsigned double integer attribute value to a DIE.  */
3657
3658 static inline void
3659 add_AT_long_long (die, attr_kind, val_hi, val_low)
3660      register dw_die_ref die;
3661      register enum dwarf_attribute attr_kind;
3662      register unsigned long val_hi;
3663      register unsigned long val_low;
3664 {
3665   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
3666
3667   attr->dw_attr_next = NULL;
3668   attr->dw_attr = attr_kind;
3669   attr->dw_attr_val.val_class = dw_val_class_long_long;
3670   attr->dw_attr_val.v.val_long_long.hi = val_hi;
3671   attr->dw_attr_val.v.val_long_long.low = val_low;
3672   add_dwarf_attr (die, attr);
3673 }
3674
3675 /* Add a floating point attribute value to a DIE and return it.  */
3676
3677 static inline void
3678 add_AT_float (die, attr_kind, length, array)
3679      register dw_die_ref die;
3680      register enum dwarf_attribute attr_kind;
3681      register unsigned length;
3682      register long *array;
3683 {
3684   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
3685
3686   attr->dw_attr_next = NULL;
3687   attr->dw_attr = attr_kind;
3688   attr->dw_attr_val.val_class = dw_val_class_float;
3689   attr->dw_attr_val.v.val_float.length = length;
3690   attr->dw_attr_val.v.val_float.array = array;
3691   add_dwarf_attr (die, attr);
3692 }
3693
3694 /* Add a string attribute value to a DIE.  */
3695
3696 static inline void
3697 add_AT_string (die, attr_kind, str)
3698      register dw_die_ref die;
3699      register enum dwarf_attribute attr_kind;
3700      register char *str;
3701 {
3702   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
3703
3704   attr->dw_attr_next = NULL;
3705   attr->dw_attr = attr_kind;
3706   attr->dw_attr_val.val_class = dw_val_class_str;
3707   attr->dw_attr_val.v.val_str = xstrdup (str);
3708   add_dwarf_attr (die, attr);
3709 }
3710
3711 /* Add a DIE reference attribute value to a DIE.  */
3712
3713 static inline void
3714 add_AT_die_ref (die, attr_kind, targ_die)
3715      register dw_die_ref die;
3716      register enum dwarf_attribute attr_kind;
3717      register dw_die_ref targ_die;
3718 {
3719   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
3720
3721   attr->dw_attr_next = NULL;
3722   attr->dw_attr = attr_kind;
3723   attr->dw_attr_val.val_class = dw_val_class_die_ref;
3724   attr->dw_attr_val.v.val_die_ref = targ_die;
3725   add_dwarf_attr (die, attr);
3726 }
3727
3728 /* Add an FDE reference attribute value to a DIE.  */
3729
3730 static inline void
3731 add_AT_fde_ref (die, attr_kind, targ_fde)
3732      register dw_die_ref die;
3733      register enum dwarf_attribute attr_kind;
3734      register unsigned targ_fde;
3735 {
3736   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
3737
3738   attr->dw_attr_next = NULL;
3739   attr->dw_attr = attr_kind;
3740   attr->dw_attr_val.val_class = dw_val_class_fde_ref;
3741   attr->dw_attr_val.v.val_fde_index = targ_fde;
3742   add_dwarf_attr (die, attr);
3743 }
3744
3745 /* Add a location description attribute value to a DIE.  */
3746
3747 static inline void
3748 add_AT_loc (die, attr_kind, loc)
3749      register dw_die_ref die;
3750      register enum dwarf_attribute attr_kind;
3751      register dw_loc_descr_ref loc;
3752 {
3753   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
3754
3755   attr->dw_attr_next = NULL;
3756   attr->dw_attr = attr_kind;
3757   attr->dw_attr_val.val_class = dw_val_class_loc;
3758   attr->dw_attr_val.v.val_loc = loc;
3759   add_dwarf_attr (die, attr);
3760 }
3761
3762 /* Add an address constant attribute value to a DIE.  */
3763
3764 static inline void
3765 add_AT_addr (die, attr_kind, addr)
3766      register dw_die_ref die;
3767      register enum dwarf_attribute attr_kind;
3768      char *addr;
3769 {
3770   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
3771
3772   attr->dw_attr_next = NULL;
3773   attr->dw_attr = attr_kind;
3774   attr->dw_attr_val.val_class = dw_val_class_addr;
3775   attr->dw_attr_val.v.val_addr = addr;
3776   add_dwarf_attr (die, attr);
3777 }
3778
3779 /* Add a label identifier attribute value to a DIE.  */
3780
3781 static inline void
3782 add_AT_lbl_id (die, attr_kind, lbl_id)
3783      register dw_die_ref die;
3784      register enum dwarf_attribute attr_kind;
3785      register char *lbl_id;
3786 {
3787   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
3788
3789   attr->dw_attr_next = NULL;
3790   attr->dw_attr = attr_kind;
3791   attr->dw_attr_val.val_class = dw_val_class_lbl_id;
3792   attr->dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
3793   add_dwarf_attr (die, attr);
3794 }
3795
3796 /* Add a section offset attribute value to a DIE.  */
3797
3798 static inline void
3799 add_AT_section_offset (die, attr_kind, section)
3800      register dw_die_ref die;
3801      register enum dwarf_attribute attr_kind;
3802      register char *section;
3803 {
3804   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
3805
3806   attr->dw_attr_next = NULL;
3807   attr->dw_attr = attr_kind;
3808   attr->dw_attr_val.val_class = dw_val_class_section_offset;
3809   attr->dw_attr_val.v.val_section = section;
3810   add_dwarf_attr (die, attr);
3811   
3812 }
3813
3814 /* Test if die refers to an external subroutine.  */
3815
3816 static inline int
3817 is_extern_subr_die (die)
3818      register dw_die_ref die;
3819 {
3820   register dw_attr_ref a;
3821   register int is_subr = FALSE;
3822   register int is_extern = FALSE;
3823
3824   if (die != NULL && die->die_tag == DW_TAG_subprogram)
3825     {
3826       is_subr = TRUE;
3827       for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
3828         {
3829           if (a->dw_attr == DW_AT_external
3830               && a->dw_attr_val.val_class == dw_val_class_flag
3831               && a->dw_attr_val.v.val_flag != 0)
3832             {
3833               is_extern = TRUE;
3834               break;
3835             }
3836         }
3837     }
3838
3839   return is_subr && is_extern;
3840 }
3841
3842 /* Get the attribute of type attr_kind.  */
3843
3844 static inline dw_attr_ref
3845 get_AT (die, attr_kind)
3846      register dw_die_ref die;
3847      register enum dwarf_attribute attr_kind;
3848 {
3849   register dw_attr_ref a;
3850   register dw_die_ref spec = NULL;
3851   
3852   if (die != NULL)
3853     {
3854       for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
3855         {
3856           if (a->dw_attr == attr_kind)
3857             return a;
3858
3859           if (a->dw_attr == DW_AT_specification
3860               || a->dw_attr == DW_AT_abstract_origin)
3861             spec = a->dw_attr_val.v.val_die_ref;
3862         }
3863
3864       if (spec)
3865         return get_AT (spec, attr_kind);
3866     }
3867
3868   return NULL;
3869 }
3870
3871 /* Return the "low pc" attribute value, typically associated with
3872    a subprogram DIE.  Return null if the "low pc" attribute is
3873    either not prsent, or if it cannot be represented as an
3874    assembler label identifier.  */
3875
3876 static inline char *
3877 get_AT_low_pc (die)
3878      register dw_die_ref die;
3879 {
3880   register dw_attr_ref a = get_AT (die, DW_AT_low_pc);
3881
3882   if (a && a->dw_attr_val.val_class == dw_val_class_lbl_id)
3883     return a->dw_attr_val.v.val_lbl_id;
3884
3885   return NULL;
3886 }
3887
3888 /* Return the "high pc" attribute value, typically associated with
3889    a subprogram DIE.  Return null if the "high pc" attribute is
3890    either not prsent, or if it cannot be represented as an
3891    assembler label identifier.  */
3892
3893 static inline char *
3894 get_AT_hi_pc (die)
3895      register dw_die_ref die;
3896 {
3897   register dw_attr_ref a = get_AT (die, DW_AT_high_pc);
3898
3899   if (a && a->dw_attr_val.val_class == dw_val_class_lbl_id)
3900     return a->dw_attr_val.v.val_lbl_id;
3901
3902   return NULL;
3903 }
3904
3905 /* Return the value of the string attribute designated by ATTR_KIND, or
3906    NULL if it is not present.  */
3907
3908 static inline char *
3909 get_AT_string (die, attr_kind)
3910      register dw_die_ref die;
3911      register enum dwarf_attribute attr_kind;
3912 {
3913   register dw_attr_ref a = get_AT (die, attr_kind);
3914
3915   if (a && a->dw_attr_val.val_class == dw_val_class_str)
3916     return a->dw_attr_val.v.val_str;
3917
3918   return NULL;
3919 }
3920
3921 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
3922    if it is not present.  */
3923
3924 static inline int
3925 get_AT_flag (die, attr_kind)
3926      register dw_die_ref die;
3927      register enum dwarf_attribute attr_kind;
3928 {
3929   register dw_attr_ref a = get_AT (die, attr_kind);
3930
3931   if (a && a->dw_attr_val.val_class == dw_val_class_flag)
3932     return a->dw_attr_val.v.val_flag;
3933
3934   return -1;
3935 }
3936
3937 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
3938    if it is not present.  */
3939
3940 static inline unsigned
3941 get_AT_unsigned (die, attr_kind)
3942      register dw_die_ref die;
3943      register enum dwarf_attribute attr_kind;
3944 {
3945   register dw_attr_ref a = get_AT (die, attr_kind);
3946
3947   if (a && a->dw_attr_val.val_class == dw_val_class_unsigned_const)
3948     return a->dw_attr_val.v.val_unsigned;
3949
3950   return 0;
3951 }
3952
3953 static inline int
3954 is_c_family ()
3955 {
3956   register unsigned lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
3957
3958   return (lang == DW_LANG_C || lang == DW_LANG_C89
3959           || lang == DW_LANG_C_plus_plus);
3960
3961
3962 static inline int
3963 is_fortran ()
3964 {
3965   register unsigned lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
3966
3967   return (lang == DW_LANG_Fortran77 || lang == DW_LANG_Fortran90);
3968
3969
3970 /* Remove the specified attribute if present.  */
3971
3972 static inline void
3973 remove_AT (die, attr_kind)
3974      register dw_die_ref die;
3975      register enum dwarf_attribute attr_kind;
3976 {
3977   register dw_attr_ref a;
3978   register dw_attr_ref removed = NULL;;
3979
3980   if (die != NULL)
3981     {
3982       if (die->die_attr->dw_attr == attr_kind)
3983         {
3984           removed = die->die_attr;
3985           if (die->die_attr_last == die->die_attr)
3986             die->die_attr_last = NULL;
3987
3988           die->die_attr = die->die_attr->dw_attr_next;
3989         }
3990
3991       else
3992         for (a = die->die_attr; a->dw_attr_next != NULL;
3993              a = a->dw_attr_next)
3994           if (a->dw_attr_next->dw_attr == attr_kind)
3995             {
3996               removed = a->dw_attr_next;
3997               if (die->die_attr_last == a->dw_attr_next)
3998                 die->die_attr_last = a;
3999
4000               a->dw_attr_next = a->dw_attr_next->dw_attr_next;
4001               break;
4002             }
4003
4004       if (removed != 0)
4005         free (removed);
4006     }
4007 }
4008
4009 /* Discard the children of this DIE.  */
4010
4011 static inline void
4012 remove_children (die)
4013      register dw_die_ref die;
4014 {
4015   register dw_die_ref child_die = die->die_child;
4016
4017   die->die_child = NULL;
4018   die->die_child_last = NULL;
4019
4020   while (child_die != NULL)
4021     {
4022       register dw_die_ref tmp_die = child_die;
4023       register dw_attr_ref a;
4024
4025       child_die = child_die->die_sib;
4026       
4027       for (a = tmp_die->die_attr; a != NULL; )
4028         {
4029           register dw_attr_ref tmp_a = a;
4030
4031           a = a->dw_attr_next;
4032           free (tmp_a);
4033         }
4034
4035       free (tmp_die);
4036     }
4037 }
4038
4039 /* Add a child DIE below its parent.  */
4040
4041 static inline void
4042 add_child_die (die, child_die)
4043      register dw_die_ref die;
4044      register dw_die_ref child_die;
4045 {
4046   if (die != NULL && child_die != NULL)
4047     {
4048       if (die == child_die)
4049         abort ();
4050       child_die->die_parent = die;
4051       child_die->die_sib = NULL;
4052
4053       if (die->die_child == NULL)
4054         {
4055           die->die_child = child_die;
4056           die->die_child_last = child_die;
4057         }
4058       else
4059         {
4060           die->die_child_last->die_sib = child_die;
4061           die->die_child_last = child_die;
4062         }
4063     }
4064 }
4065
4066 /* Return a pointer to a newly created DIE node.  */
4067
4068 static inline dw_die_ref
4069 new_die (tag_value, parent_die)
4070      register enum dwarf_tag tag_value;
4071      register dw_die_ref parent_die;
4072 {
4073   register dw_die_ref die = (dw_die_ref) xmalloc (sizeof (die_node));
4074
4075   die->die_tag = tag_value;
4076   die->die_abbrev = 0;
4077   die->die_offset = 0;
4078   die->die_child = NULL;
4079   die->die_parent = NULL;
4080   die->die_sib = NULL;
4081   die->die_child_last = NULL;
4082   die->die_attr = NULL;
4083   die->die_attr_last = NULL;
4084
4085   if (parent_die != NULL)
4086     add_child_die (parent_die, die);
4087   else
4088     {
4089       limbo_die_node *limbo_node;
4090
4091       limbo_node = (limbo_die_node *) xmalloc (sizeof (limbo_die_node));
4092       limbo_node->die = die;
4093       limbo_node->next = limbo_die_list;
4094       limbo_die_list = limbo_node;
4095     }
4096
4097   return die;
4098 }
4099
4100 /* Return the DIE associated with the given type specifier.  */
4101
4102 static inline dw_die_ref
4103 lookup_type_die (type)
4104      register tree type;
4105 {
4106   return (dw_die_ref) TYPE_SYMTAB_POINTER (type);
4107 }
4108
4109 /* Equate a DIE to a given type specifier.  */
4110
4111 static void
4112 equate_type_number_to_die (type, type_die)
4113      register tree type;
4114      register dw_die_ref type_die;
4115 {
4116   TYPE_SYMTAB_POINTER (type) = (char *) type_die;
4117 }
4118
4119 /* Return the DIE associated with a given declaration.  */
4120
4121 static inline dw_die_ref
4122 lookup_decl_die (decl)
4123      register tree decl;
4124 {
4125   register unsigned decl_id = DECL_UID (decl);
4126
4127   return (decl_id < decl_die_table_in_use
4128           ? decl_die_table[decl_id] : NULL);
4129 }
4130
4131 /* Equate a DIE to a particular declaration.  */
4132
4133 static void
4134 equate_decl_number_to_die (decl, decl_die)
4135      register tree decl;
4136      register dw_die_ref decl_die;
4137 {
4138   register unsigned decl_id = DECL_UID (decl);
4139   register unsigned num_allocated;
4140
4141   if (decl_id >= decl_die_table_allocated)
4142     {
4143       num_allocated
4144         = ((decl_id + 1 + DECL_DIE_TABLE_INCREMENT - 1)
4145            / DECL_DIE_TABLE_INCREMENT)
4146           * DECL_DIE_TABLE_INCREMENT;
4147
4148       decl_die_table
4149         = (dw_die_ref *) xrealloc (decl_die_table,
4150                                    sizeof (dw_die_ref) * num_allocated);
4151
4152       bzero ((char *) &decl_die_table[decl_die_table_allocated],
4153              (num_allocated - decl_die_table_allocated) * sizeof (dw_die_ref));
4154       decl_die_table_allocated = num_allocated;
4155     }
4156
4157   if (decl_id >= decl_die_table_in_use)
4158     decl_die_table_in_use = (decl_id + 1);
4159
4160   decl_die_table[decl_id] = decl_die;
4161 }
4162
4163 /* Return a pointer to a newly allocated location description.  Location
4164    descriptions are simple expression terms that can be strung
4165    together to form more complicated location (address) descriptions.  */
4166
4167 static inline dw_loc_descr_ref
4168 new_loc_descr (op, oprnd1, oprnd2)
4169      register enum dwarf_location_atom op;
4170      register unsigned long oprnd1;
4171      register unsigned long oprnd2;
4172 {
4173   register dw_loc_descr_ref descr
4174     = (dw_loc_descr_ref) xmalloc (sizeof (dw_loc_descr_node));
4175
4176   descr->dw_loc_next = NULL;
4177   descr->dw_loc_opc = op;
4178   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4179   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4180   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4181   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4182
4183   return descr;
4184 }
4185
4186 /* Add a location description term to a location description expression.  */
4187
4188 static inline void
4189 add_loc_descr (list_head, descr)
4190      register dw_loc_descr_ref *list_head;
4191      register dw_loc_descr_ref descr;
4192 {
4193   register dw_loc_descr_ref *d;
4194
4195   /* Find the end of the chain.  */
4196   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4197     ;
4198
4199   *d = descr;
4200 }
4201 \f
4202 /* Keep track of the number of spaces used to indent the
4203    output of the debugging routines that print the structure of
4204    the DIE internal representation.  */
4205 static int print_indent;
4206
4207 /* Indent the line the number of spaces given by print_indent.  */
4208
4209 static inline void
4210 print_spaces (outfile)
4211      FILE *outfile;
4212 {
4213   fprintf (outfile, "%*s", print_indent, "");
4214 }
4215
4216 /* Print the information associated with a given DIE, and its children.
4217    This routine is a debugging aid only.  */
4218
4219 static void
4220 print_die (die, outfile)
4221      dw_die_ref die;
4222      FILE *outfile;
4223 {
4224   register dw_attr_ref a;
4225   register dw_die_ref c;
4226
4227   print_spaces (outfile);
4228   fprintf (outfile, "DIE %4lu: %s\n",
4229            die->die_offset, dwarf_tag_name (die->die_tag));
4230   print_spaces (outfile);
4231   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
4232   fprintf (outfile, " offset: %lu\n", die->die_offset);
4233
4234   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
4235     {
4236       print_spaces (outfile);
4237       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
4238
4239       switch (a->dw_attr_val.val_class)
4240         {
4241         case dw_val_class_addr:
4242           fprintf (outfile, "address");
4243           break;
4244         case dw_val_class_loc:
4245           fprintf (outfile, "location descriptor");
4246           break;
4247         case dw_val_class_const:
4248           fprintf (outfile, "%ld", a->dw_attr_val.v.val_int);
4249           break;
4250         case dw_val_class_unsigned_const:
4251           fprintf (outfile, "%lu", a->dw_attr_val.v.val_unsigned);
4252           break;
4253         case dw_val_class_long_long:
4254           fprintf (outfile, "constant (%lu,%lu)",
4255                   a->dw_attr_val.v.val_long_long.hi,
4256                   a->dw_attr_val.v.val_long_long.low);
4257           break;
4258         case dw_val_class_float:
4259           fprintf (outfile, "floating-point constant");
4260           break;
4261         case dw_val_class_flag:
4262           fprintf (outfile, "%u", a->dw_attr_val.v.val_flag);
4263           break;
4264         case dw_val_class_die_ref:
4265           if (a->dw_attr_val.v.val_die_ref != NULL)
4266             fprintf (outfile, "die -> %lu",
4267                      a->dw_attr_val.v.val_die_ref->die_offset);
4268           else
4269             fprintf (outfile, "die -> <null>");
4270           break;
4271         case dw_val_class_lbl_id:
4272           fprintf (outfile, "label: %s", a->dw_attr_val.v.val_lbl_id);
4273           break;
4274         case dw_val_class_section_offset:
4275           fprintf (outfile, "section: %s", a->dw_attr_val.v.val_section);
4276           break;
4277         case dw_val_class_str:
4278           if (a->dw_attr_val.v.val_str != NULL)
4279             fprintf (outfile, "\"%s\"", a->dw_attr_val.v.val_str);
4280           else
4281             fprintf (outfile, "<null>");
4282           break;
4283         default:
4284           break;
4285         }
4286
4287       fprintf (outfile, "\n");
4288     }
4289
4290   if (die->die_child != NULL)
4291     {
4292       print_indent += 4;
4293       for (c = die->die_child; c != NULL; c = c->die_sib)
4294         print_die (c, outfile);
4295
4296       print_indent -= 4;
4297     }
4298 }
4299
4300 /* Print the contents of the source code line number correspondence table.
4301    This routine is a debugging aid only.  */
4302
4303 static void
4304 print_dwarf_line_table (outfile)
4305      FILE *outfile;
4306 {
4307   register unsigned i;
4308   register dw_line_info_ref line_info;
4309
4310   fprintf (outfile, "\n\nDWARF source line information\n");
4311   for (i = 1; i < line_info_table_in_use; ++i)
4312     {
4313       line_info = &line_info_table[i];
4314       fprintf (outfile, "%5d: ", i);
4315       fprintf (outfile, "%-20s", file_table[line_info->dw_file_num]);
4316       fprintf (outfile, "%6ld", line_info->dw_line_num);
4317       fprintf (outfile, "\n");
4318     }
4319
4320   fprintf (outfile, "\n\n");
4321 }
4322
4323 /* Print the information collected for a given DIE.  */
4324
4325 void
4326 debug_dwarf_die (die)
4327      dw_die_ref die;
4328 {
4329   print_die (die, stderr);
4330 }
4331
4332 /* Print all DWARF information collected for the compilation unit.
4333    This routine is a debugging aid only.  */
4334
4335 void
4336 debug_dwarf ()
4337 {
4338   print_indent = 0;
4339   print_die (comp_unit_die, stderr);
4340   print_dwarf_line_table (stderr);
4341 }
4342 \f
4343 /* Traverse the DIE, and add a sibling attribute if it may have the
4344    effect of speeding up access to siblings.  To save some space,
4345    avoid generating sibling attributes for DIE's without children.  */
4346
4347 static void
4348 add_sibling_attributes(die)
4349      register dw_die_ref die;
4350 {
4351   register dw_die_ref c;
4352   register dw_attr_ref attr;
4353   if (die != comp_unit_die && die->die_child != NULL)
4354     {
4355       attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4356       attr->dw_attr_next = NULL;
4357       attr->dw_attr = DW_AT_sibling;
4358       attr->dw_attr_val.val_class = dw_val_class_die_ref;
4359       attr->dw_attr_val.v.val_die_ref = die->die_sib;
4360
4361       /* Add the sibling link to the front of the attribute list.  */
4362       attr->dw_attr_next = die->die_attr;
4363       if (die->die_attr == NULL)
4364         die->die_attr_last = attr;
4365
4366       die->die_attr = attr;
4367     }
4368
4369   for (c = die->die_child; c != NULL; c = c->die_sib)
4370     add_sibling_attributes (c);
4371 }
4372
4373 /* The format of each DIE (and its attribute value pairs)
4374    is encoded in an abbreviation table.  This routine builds the
4375    abbreviation table and assigns a unique abbreviation id for
4376    each abbreviation entry.  The children of each die are visited
4377    recursively.  */
4378
4379 static void
4380 build_abbrev_table (die)
4381      register dw_die_ref die;
4382 {
4383   register unsigned long abbrev_id;
4384   register unsigned long n_alloc;
4385   register dw_die_ref c;
4386   register dw_attr_ref d_attr, a_attr;
4387   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
4388     {
4389       register dw_die_ref abbrev = abbrev_die_table[abbrev_id];
4390
4391       if (abbrev->die_tag == die->die_tag)
4392         {
4393           if ((abbrev->die_child != NULL) == (die->die_child != NULL))
4394             {
4395               a_attr = abbrev->die_attr;
4396               d_attr = die->die_attr;
4397
4398               while (a_attr != NULL && d_attr != NULL)
4399                 {
4400                   if ((a_attr->dw_attr != d_attr->dw_attr)
4401                       || (value_format (&a_attr->dw_attr_val)
4402                           != value_format (&d_attr->dw_attr_val)))
4403                     break;
4404
4405                   a_attr = a_attr->dw_attr_next;
4406                   d_attr = d_attr->dw_attr_next;
4407                 }
4408
4409               if (a_attr == NULL && d_attr == NULL)
4410                 break;
4411             }
4412         }
4413     }
4414
4415   if (abbrev_id >= abbrev_die_table_in_use)
4416     {
4417       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
4418         {
4419           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
4420           abbrev_die_table 
4421             = (dw_die_ref *) xrealloc (abbrev_die_table,
4422                                        sizeof (dw_die_ref) * n_alloc);
4423
4424           bzero ((char *) &abbrev_die_table[abbrev_die_table_allocated],
4425                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
4426           abbrev_die_table_allocated = n_alloc;
4427         }
4428
4429       ++abbrev_die_table_in_use;
4430       abbrev_die_table[abbrev_id] = die;
4431     }
4432
4433   die->die_abbrev = abbrev_id;
4434   for (c = die->die_child; c != NULL; c = c->die_sib)
4435     build_abbrev_table (c);
4436 }
4437 \f
4438 /* Return the size of a string, including the null byte.  */
4439
4440 static unsigned long
4441 size_of_string (str)
4442      register char *str;
4443 {
4444   register unsigned long size = 0;
4445   register unsigned long slen = strlen (str);
4446   register unsigned long i;
4447   register unsigned c;
4448
4449   for (i = 0; i < slen; ++i)
4450     {
4451       c = str[i];
4452       if (c == '\\')
4453         ++i;
4454
4455       size += 1;
4456     }
4457
4458   /* Null terminator.  */
4459   size += 1;
4460   return size;
4461 }
4462
4463 /* Return the size of a location descriptor.  */
4464
4465 static unsigned long
4466 size_of_loc_descr (loc)
4467      register dw_loc_descr_ref loc;
4468 {
4469   register unsigned long size = 1;
4470
4471   switch (loc->dw_loc_opc)
4472     {
4473     case DW_OP_addr:
4474       size += PTR_SIZE;
4475       break;
4476     case DW_OP_const1u:
4477     case DW_OP_const1s:
4478       size += 1;
4479       break;
4480     case DW_OP_const2u:
4481     case DW_OP_const2s:
4482       size += 2;
4483       break;
4484     case DW_OP_const4u:
4485     case DW_OP_const4s:
4486       size += 4;
4487       break;
4488     case DW_OP_const8u:
4489     case DW_OP_const8s:
4490       size += 8;
4491       break;
4492     case DW_OP_constu:
4493       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4494       break;
4495     case DW_OP_consts:
4496       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4497       break;
4498     case DW_OP_pick:
4499       size += 1;
4500       break;
4501     case DW_OP_plus_uconst:
4502       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4503       break;
4504     case DW_OP_skip:
4505     case DW_OP_bra:
4506       size += 2;
4507       break;
4508     case DW_OP_breg0:
4509     case DW_OP_breg1:
4510     case DW_OP_breg2:
4511     case DW_OP_breg3:
4512     case DW_OP_breg4:
4513     case DW_OP_breg5:
4514     case DW_OP_breg6:
4515     case DW_OP_breg7:
4516     case DW_OP_breg8:
4517     case DW_OP_breg9:
4518     case DW_OP_breg10:
4519     case DW_OP_breg11:
4520     case DW_OP_breg12:
4521     case DW_OP_breg13:
4522     case DW_OP_breg14:
4523     case DW_OP_breg15:
4524     case DW_OP_breg16:
4525     case DW_OP_breg17:
4526     case DW_OP_breg18:
4527     case DW_OP_breg19:
4528     case DW_OP_breg20:
4529     case DW_OP_breg21:
4530     case DW_OP_breg22:
4531     case DW_OP_breg23:
4532     case DW_OP_breg24:
4533     case DW_OP_breg25:
4534     case DW_OP_breg26:
4535     case DW_OP_breg27:
4536     case DW_OP_breg28:
4537     case DW_OP_breg29:
4538     case DW_OP_breg30:
4539     case DW_OP_breg31:
4540       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4541       break;
4542     case DW_OP_regx:
4543       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4544       break;
4545     case DW_OP_fbreg:
4546       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4547       break;
4548     case DW_OP_bregx:
4549       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4550       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4551       break;
4552     case DW_OP_piece:
4553       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4554       break;
4555     case DW_OP_deref_size:
4556     case DW_OP_xderef_size:
4557       size += 1;
4558       break;
4559     default:
4560       break;
4561     }
4562
4563   return size;
4564 }
4565
4566 /* Return the size of a series of location descriptors.  */
4567
4568 static unsigned long
4569 size_of_locs (loc)
4570      register dw_loc_descr_ref loc;
4571 {
4572   register unsigned long size = 0;
4573
4574   for (; loc != NULL; loc = loc->dw_loc_next)
4575     size += size_of_loc_descr (loc);
4576
4577   return size;
4578 }
4579
4580 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
4581
4582 static int
4583 constant_size (value)
4584      long unsigned value;
4585 {
4586   int log;
4587
4588   if (value == 0)
4589     log = 0;
4590   else
4591     log = floor_log2 (value);
4592
4593   log = log / 8;
4594   log = 1 << (floor_log2 (log) + 1);
4595
4596   return log;
4597 }
4598
4599 /* Return the size of a DIE, as it is represented in the
4600    .debug_info section.  */
4601
4602 static unsigned long
4603 size_of_die (die)
4604      register dw_die_ref die;
4605 {
4606   register unsigned long size = 0;
4607   register dw_attr_ref a;
4608
4609   size += size_of_uleb128 (die->die_abbrev);
4610   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
4611     {
4612       switch (a->dw_attr_val.val_class)
4613         {
4614         case dw_val_class_addr:
4615           size += PTR_SIZE;
4616           break;
4617         case dw_val_class_loc:
4618           {
4619             register unsigned long lsize
4620               = size_of_locs (a->dw_attr_val.v.val_loc);
4621
4622             /* Block length.  */
4623             size += constant_size (lsize);
4624             size += lsize;
4625           }
4626           break;
4627         case dw_val_class_const:
4628           size += 4;
4629           break;
4630         case dw_val_class_unsigned_const:
4631           size += constant_size (a->dw_attr_val.v.val_unsigned);
4632           break;
4633         case dw_val_class_long_long:
4634           size += 1 + 8; /* block */
4635           break;
4636         case dw_val_class_float:
4637           size += 1 + a->dw_attr_val.v.val_float.length * 4; /* block */
4638           break;
4639         case dw_val_class_flag:
4640           size += 1;
4641           break;
4642         case dw_val_class_die_ref:
4643           size += DWARF_OFFSET_SIZE;
4644           break;
4645         case dw_val_class_fde_ref:
4646           size += DWARF_OFFSET_SIZE;
4647           break;
4648         case dw_val_class_lbl_id:
4649           size += PTR_SIZE;
4650           break;
4651         case dw_val_class_section_offset:
4652           size += DWARF_OFFSET_SIZE;
4653           break;
4654         case dw_val_class_str:
4655           size += size_of_string (a->dw_attr_val.v.val_str);
4656           break;
4657         default:
4658           abort ();
4659         }
4660     }
4661
4662   return size;
4663 }
4664
4665 /* Size the debugging information associated with a given DIE.
4666    Visits the DIE's children recursively.  Updates the global
4667    variable next_die_offset, on each time through.  Uses the
4668    current value of next_die_offset to update the die_offset
4669    field in each DIE.  */
4670
4671 static void
4672 calc_die_sizes (die)
4673      dw_die_ref die;
4674 {
4675   register dw_die_ref c;
4676   die->die_offset = next_die_offset;
4677   next_die_offset += size_of_die (die);
4678
4679   for (c = die->die_child; c != NULL; c = c->die_sib)
4680     calc_die_sizes (c);
4681
4682   if (die->die_child != NULL)
4683     /* Count the null byte used to terminate sibling lists.  */
4684     next_die_offset += 1;
4685 }
4686
4687 /* Return the size of the line information prolog generated for the
4688    compilation unit.  */
4689
4690 static unsigned long
4691 size_of_line_prolog ()
4692 {
4693   register unsigned long size;
4694   register unsigned long ft_index;
4695
4696   size = DWARF_LINE_PROLOG_HEADER_SIZE;
4697
4698   /* Count the size of the table giving number of args for each
4699      standard opcode.  */
4700   size += DWARF_LINE_OPCODE_BASE - 1;
4701
4702   /* Include directory table is empty (at present).  Count only the
4703      null byte used to terminate the table.  */
4704   size += 1;
4705
4706   for (ft_index = 1; ft_index < file_table_in_use; ++ft_index)
4707     {
4708       /* File name entry.  */
4709       size += size_of_string (file_table[ft_index]);
4710
4711       /* Include directory index.  */
4712       size += size_of_uleb128 (0);
4713
4714       /* Modification time.  */
4715       size += size_of_uleb128 (0);
4716
4717       /* File length in bytes.  */
4718       size += size_of_uleb128 (0);
4719     }
4720
4721   /* Count the file table terminator.  */
4722   size += 1;
4723   return size;
4724 }
4725
4726 /* Return the size of the line information generated for this
4727    compilation unit.  */
4728
4729 static unsigned long
4730 size_of_line_info ()
4731 {
4732   register unsigned long size;
4733   register unsigned long lt_index;
4734   register unsigned long current_line;
4735   register long line_offset;
4736   register long line_delta;
4737   register unsigned long current_file;
4738   register unsigned long function;
4739   unsigned long size_of_set_address;
4740
4741   /* Size of a DW_LNE_set_address instruction.  */
4742   size_of_set_address = 1 + size_of_uleb128 (1 + PTR_SIZE) + 1 + PTR_SIZE;
4743
4744   /* Version number.  */
4745   size = 2;
4746
4747   /* Prolog length specifier.  */
4748   size += DWARF_OFFSET_SIZE;
4749
4750   /* Prolog.  */
4751   size += size_of_line_prolog ();
4752
4753   /* Set address register instruction.  */
4754   size += size_of_set_address;
4755
4756   current_file = 1;
4757   current_line = 1;
4758   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
4759     {
4760       register dw_line_info_ref line_info;
4761
4762       /* Advance pc instruction.  */
4763       /* ??? See the DW_LNS_advance_pc comment in output_line_info.  */
4764       if (0)
4765         size += 1 + 2;
4766       else
4767         size += size_of_set_address;
4768
4769       line_info = &line_info_table[lt_index];
4770       if (line_info->dw_file_num != current_file)
4771         {
4772           /* Set file number instruction.  */
4773           size += 1;
4774           current_file = line_info->dw_file_num;
4775           size += size_of_uleb128 (current_file);
4776         }
4777
4778       if (line_info->dw_line_num != current_line)
4779         {
4780           line_offset = line_info->dw_line_num - current_line;
4781           line_delta = line_offset - DWARF_LINE_BASE;
4782           current_line = line_info->dw_line_num;
4783           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
4784             /* 1-byte special line number instruction.  */
4785             size += 1;
4786           else
4787             {
4788               /* Advance line instruction.  */
4789               size += 1;
4790               size += size_of_sleb128 (line_offset);
4791               /* Generate line entry instruction.  */
4792               size += 1;
4793             }
4794         }
4795     }
4796
4797   /* Advance pc instruction.  */
4798   if (0)
4799     size += 1 + 2;
4800   else
4801     size += size_of_set_address;
4802
4803   /* End of line number info. marker.  */
4804   size += 1 + size_of_uleb128 (1) + 1;
4805
4806   function = 0;
4807   current_file = 1;
4808   current_line = 1;
4809   for (lt_index = 0; lt_index < separate_line_info_table_in_use; )
4810     {
4811       register dw_separate_line_info_ref line_info
4812         = &separate_line_info_table[lt_index];
4813       if (function != line_info->function)
4814         {
4815           function = line_info->function;
4816           /* Set address register instruction.  */
4817           size += size_of_set_address;
4818         }
4819       else
4820         {
4821           /* Advance pc instruction.  */
4822           if (0)
4823             size += 1 + 2;
4824           else
4825             size += size_of_set_address;
4826         }
4827
4828       if (line_info->dw_file_num != current_file)
4829         {
4830           /* Set file number instruction.  */
4831           size += 1;
4832           current_file = line_info->dw_file_num;
4833           size += size_of_uleb128 (current_file);
4834         }
4835
4836       if (line_info->dw_line_num != current_line)
4837         {
4838           line_offset = line_info->dw_line_num - current_line;
4839           line_delta = line_offset - DWARF_LINE_BASE;
4840           current_line = line_info->dw_line_num;
4841           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
4842             /* 1-byte special line number instruction.  */
4843             size += 1;
4844           else
4845             {
4846               /* Advance line instruction.  */
4847               size += 1;
4848               size += size_of_sleb128 (line_offset);
4849
4850               /* Generate line entry instruction.  */
4851               size += 1;
4852             }
4853         }
4854
4855       ++lt_index;
4856
4857       /* If we're done with a function, end its sequence.  */
4858       if (lt_index == separate_line_info_table_in_use
4859           || separate_line_info_table[lt_index].function != function)
4860         {
4861           current_file = 1;
4862           current_line = 1;
4863
4864           /* Advance pc instruction.  */
4865           if (0)
4866             size += 1 + 2;
4867           else
4868             size += size_of_set_address;
4869
4870           /* End of line number info. marker.  */
4871           size += 1 + size_of_uleb128 (1) + 1;
4872         }
4873     }
4874
4875   return size;
4876 }
4877
4878 /* Return the size of the .debug_pubnames table  generated for the
4879    compilation unit.  */
4880
4881 static unsigned long
4882 size_of_pubnames ()
4883 {
4884   register unsigned long size;
4885   register unsigned i;
4886
4887   size = DWARF_PUBNAMES_HEADER_SIZE;
4888   for (i = 0; i < pubname_table_in_use; ++i)
4889     {
4890       register pubname_ref p = &pubname_table[i];
4891       size += DWARF_OFFSET_SIZE + size_of_string (p->name);
4892     }
4893
4894   size += DWARF_OFFSET_SIZE;
4895   return size;
4896 }
4897
4898 /* Return the size of the information in the .debug_aranges section.  */
4899
4900 static unsigned long
4901 size_of_aranges ()
4902 {
4903   register unsigned long size;
4904
4905   size = DWARF_ARANGES_HEADER_SIZE;
4906
4907   /* Count the address/length pair for this compilation unit.  */
4908   size += 2 * PTR_SIZE;
4909   size += 2 * PTR_SIZE * arange_table_in_use;
4910
4911   /* Count the two zero words used to terminated the address range table.  */
4912   size += 2 * PTR_SIZE;
4913   return size;
4914 }
4915 \f
4916 /* Select the encoding of an attribute value.  */
4917
4918 static enum dwarf_form
4919 value_format (v)
4920      dw_val_ref v;
4921 {
4922   switch (v->val_class)
4923     {
4924     case dw_val_class_addr:
4925       return DW_FORM_addr;
4926     case dw_val_class_loc:
4927       switch (constant_size (size_of_locs (v->v.val_loc)))
4928         {
4929         case 1:
4930           return DW_FORM_block1;
4931         case 2:
4932           return DW_FORM_block2;
4933         default:
4934           abort ();
4935         }
4936     case dw_val_class_const:
4937       return DW_FORM_data4;
4938     case dw_val_class_unsigned_const:
4939       switch (constant_size (v->v.val_unsigned))
4940         {
4941         case 1:
4942           return DW_FORM_data1;
4943         case 2:
4944           return DW_FORM_data2;
4945         case 4:
4946           return DW_FORM_data4;
4947         case 8:
4948           return DW_FORM_data8;
4949         default:
4950           abort ();
4951         }
4952     case dw_val_class_long_long:
4953       return DW_FORM_block1;
4954     case dw_val_class_float:
4955       return DW_FORM_block1;
4956     case dw_val_class_flag:
4957       return DW_FORM_flag;
4958     case dw_val_class_die_ref:
4959       return DW_FORM_ref;
4960     case dw_val_class_fde_ref:
4961       return DW_FORM_data;
4962     case dw_val_class_lbl_id:
4963       return DW_FORM_addr;
4964     case dw_val_class_section_offset:
4965       return DW_FORM_data;
4966     case dw_val_class_str:
4967       return DW_FORM_string;
4968     default:
4969       abort ();
4970     }
4971 }
4972
4973 /* Output the encoding of an attribute value.  */
4974
4975 static void
4976 output_value_format (v)
4977      dw_val_ref v;
4978 {
4979   enum dwarf_form form = value_format (v);
4980
4981   output_uleb128 (form);
4982   if (flag_debug_asm)
4983     fprintf (asm_out_file, " (%s)", dwarf_form_name (form));
4984
4985   fputc ('\n', asm_out_file);
4986 }
4987
4988 /* Output the .debug_abbrev section which defines the DIE abbreviation
4989    table.  */
4990
4991 static void
4992 output_abbrev_section ()
4993 {
4994   unsigned long abbrev_id;
4995
4996   dw_attr_ref a_attr;
4997   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
4998     {
4999       register dw_die_ref abbrev = abbrev_die_table[abbrev_id];
5000
5001       output_uleb128 (abbrev_id);
5002       if (flag_debug_asm)
5003         fprintf (asm_out_file, " (abbrev code)");
5004
5005       fputc ('\n', asm_out_file);
5006       output_uleb128 (abbrev->die_tag);
5007       if (flag_debug_asm)
5008         fprintf (asm_out_file, " (TAG: %s)",
5009                  dwarf_tag_name (abbrev->die_tag));
5010
5011       fputc ('\n', asm_out_file);
5012       fprintf (asm_out_file, "\t%s\t0x%x", ASM_BYTE_OP,
5013                abbrev->die_child != NULL ? DW_children_yes : DW_children_no);
5014
5015       if (flag_debug_asm)
5016         fprintf (asm_out_file, "\t%s %s",
5017                  ASM_COMMENT_START,
5018                  (abbrev->die_child != NULL
5019                   ? "DW_children_yes" : "DW_children_no"));
5020
5021       fputc ('\n', asm_out_file);
5022
5023       for (a_attr = abbrev->die_attr; a_attr != NULL;
5024            a_attr = a_attr->dw_attr_next)
5025         {
5026           output_uleb128 (a_attr->dw_attr);
5027           if (flag_debug_asm)
5028             fprintf (asm_out_file, " (%s)",
5029                      dwarf_attr_name (a_attr->dw_attr));
5030
5031           fputc ('\n', asm_out_file);
5032           output_value_format (&a_attr->dw_attr_val);
5033         }
5034
5035       fprintf (asm_out_file, "\t%s\t0,0\n", ASM_BYTE_OP);
5036     }
5037 }
5038
5039 /* Output location description stack opcode's operands (if any).  */
5040
5041 static void
5042 output_loc_operands (loc)
5043      register dw_loc_descr_ref loc;
5044 {
5045   register dw_val_ref val1 = &loc->dw_loc_oprnd1;
5046   register dw_val_ref val2 = &loc->dw_loc_oprnd2;
5047
5048   switch (loc->dw_loc_opc)
5049     {
5050     case DW_OP_addr:
5051       ASM_OUTPUT_DWARF_ADDR_CONST (asm_out_file, val1->v.val_addr);
5052       fputc ('\n', asm_out_file);
5053       break;
5054     case DW_OP_const1u:
5055     case DW_OP_const1s:
5056       ASM_OUTPUT_DWARF_DATA1 (asm_out_file, val1->v.val_flag);
5057       fputc ('\n', asm_out_file);
5058       break;
5059     case DW_OP_const2u:
5060     case DW_OP_const2s:
5061       ASM_OUTPUT_DWARF_DATA2 (asm_out_file, val1->v.val_int);
5062       fputc ('\n', asm_out_file);
5063       break;
5064     case DW_OP_const4u:
5065     case DW_OP_const4s:
5066       ASM_OUTPUT_DWARF_DATA4 (asm_out_file, val1->v.val_int);
5067       fputc ('\n', asm_out_file);
5068       break;
5069     case DW_OP_const8u:
5070     case DW_OP_const8s:
5071       abort ();
5072       fputc ('\n', asm_out_file);
5073       break;
5074     case DW_OP_constu:
5075       output_uleb128 (val1->v.val_unsigned);
5076       fputc ('\n', asm_out_file);
5077       break;
5078     case DW_OP_consts:
5079       output_sleb128 (val1->v.val_int);
5080       fputc ('\n', asm_out_file);
5081       break;
5082     case DW_OP_pick:
5083       ASM_OUTPUT_DWARF_DATA1 (asm_out_file, val1->v.val_int);
5084       fputc ('\n', asm_out_file);
5085       break;
5086     case DW_OP_plus_uconst:
5087       output_uleb128 (val1->v.val_unsigned);
5088       fputc ('\n', asm_out_file);
5089       break;
5090     case DW_OP_skip:
5091     case DW_OP_bra:
5092       ASM_OUTPUT_DWARF_DATA2 (asm_out_file, val1->v.val_int);
5093       fputc ('\n', asm_out_file);
5094       break;
5095     case DW_OP_breg0:
5096     case DW_OP_breg1:
5097     case DW_OP_breg2:
5098     case DW_OP_breg3:
5099     case DW_OP_breg4:
5100     case DW_OP_breg5:
5101     case DW_OP_breg6:
5102     case DW_OP_breg7:
5103     case DW_OP_breg8:
5104     case DW_OP_breg9:
5105     case DW_OP_breg10:
5106     case DW_OP_breg11:
5107     case DW_OP_breg12:
5108     case DW_OP_breg13:
5109     case DW_OP_breg14:
5110     case DW_OP_breg15:
5111     case DW_OP_breg16:
5112     case DW_OP_breg17:
5113     case DW_OP_breg18:
5114     case DW_OP_breg19:
5115     case DW_OP_breg20:
5116     case DW_OP_breg21:
5117     case DW_OP_breg22:
5118     case DW_OP_breg23:
5119     case DW_OP_breg24:
5120     case DW_OP_breg25:
5121     case DW_OP_breg26:
5122     case DW_OP_breg27:
5123     case DW_OP_breg28:
5124     case DW_OP_breg29:
5125     case DW_OP_breg30:
5126     case DW_OP_breg31:
5127       output_sleb128 (val1->v.val_int);
5128       fputc ('\n', asm_out_file);
5129       break;
5130     case DW_OP_regx:
5131       output_uleb128 (val1->v.val_unsigned);
5132       fputc ('\n', asm_out_file);
5133       break;
5134     case DW_OP_fbreg:
5135       output_sleb128 (val1->v.val_int);
5136       fputc ('\n', asm_out_file);
5137       break;
5138     case DW_OP_bregx:
5139       output_uleb128 (val1->v.val_unsigned);
5140       fputc ('\n', asm_out_file);
5141       output_sleb128 (val2->v.val_int);
5142       fputc ('\n', asm_out_file);
5143       break;
5144     case DW_OP_piece:
5145       output_uleb128 (val1->v.val_unsigned);
5146       fputc ('\n', asm_out_file);
5147       break;
5148     case DW_OP_deref_size:
5149     case DW_OP_xderef_size:
5150       ASM_OUTPUT_DWARF_DATA1 (asm_out_file, val1->v.val_flag);
5151       fputc ('\n', asm_out_file);
5152       break;
5153     default:
5154       break;
5155     }
5156 }
5157
5158 /* Compute the offset of a sibling.  */
5159
5160 static unsigned long
5161 sibling_offset (die)
5162      dw_die_ref die;
5163 {
5164   unsigned long offset;
5165
5166   if (die->die_child_last == NULL)
5167     offset = die->die_offset + size_of_die (die);
5168   else
5169     offset = sibling_offset (die->die_child_last) + 1;
5170
5171   return offset;
5172 }
5173
5174 /* Output the DIE and its attributes.  Called recursively to generate
5175    the definitions of each child DIE.  */
5176
5177 static void
5178 output_die (die)
5179      register dw_die_ref die;
5180 {
5181   register dw_attr_ref a;
5182   register dw_die_ref c;
5183   register unsigned long ref_offset;
5184   register unsigned long size;
5185   register dw_loc_descr_ref loc;
5186   register int i;
5187
5188   output_uleb128 (die->die_abbrev);
5189   if (flag_debug_asm)
5190     fprintf (asm_out_file, " (DIE (0x%lx) %s)",
5191              die->die_offset, dwarf_tag_name (die->die_tag));
5192
5193   fputc ('\n', asm_out_file);
5194
5195   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5196     {
5197       switch (a->dw_attr_val.val_class)
5198         {
5199         case dw_val_class_addr:
5200           ASM_OUTPUT_DWARF_ADDR_CONST (asm_out_file,
5201                                        a->dw_attr_val.v.val_addr);
5202           break;
5203
5204         case dw_val_class_loc:
5205           size = size_of_locs (a->dw_attr_val.v.val_loc);
5206
5207           /* Output the block length for this list of location operations.  */
5208           switch (constant_size (size))
5209             {
5210             case 1:
5211               ASM_OUTPUT_DWARF_DATA1 (asm_out_file, size);
5212               break;
5213             case 2:
5214               ASM_OUTPUT_DWARF_DATA2 (asm_out_file, size);
5215               break;
5216             default:
5217               abort ();
5218             }
5219
5220           if (flag_debug_asm)
5221             fprintf (asm_out_file, "\t%s %s",
5222                      ASM_COMMENT_START, dwarf_attr_name (a->dw_attr));
5223
5224           fputc ('\n', asm_out_file);
5225           for (loc = a->dw_attr_val.v.val_loc; loc != NULL;
5226                loc = loc->dw_loc_next)
5227             {
5228               /* Output the opcode.  */
5229               ASM_OUTPUT_DWARF_DATA1 (asm_out_file, loc->dw_loc_opc);
5230               if (flag_debug_asm)
5231                 fprintf (asm_out_file, "\t%s %s", ASM_COMMENT_START,
5232                          dwarf_stack_op_name (loc->dw_loc_opc));
5233
5234               fputc ('\n', asm_out_file);
5235
5236               /* Output the operand(s) (if any).  */
5237               output_loc_operands (loc);
5238             }
5239           break;
5240
5241         case dw_val_class_const:
5242           ASM_OUTPUT_DWARF_DATA4 (asm_out_file, a->dw_attr_val.v.val_int);
5243           break;
5244
5245         case dw_val_class_unsigned_const:
5246           switch (constant_size (a->dw_attr_val.v.val_unsigned))
5247             {
5248             case 1:
5249               ASM_OUTPUT_DWARF_DATA1 (asm_out_file,
5250                                       a->dw_attr_val.v.val_unsigned);
5251               break;
5252             case 2:
5253               ASM_OUTPUT_DWARF_DATA2 (asm_out_file,
5254                                       a->dw_attr_val.v.val_unsigned);
5255               break;
5256             case 4:
5257               ASM_OUTPUT_DWARF_DATA4 (asm_out_file,
5258                                       a->dw_attr_val.v.val_unsigned);
5259               break;
5260             case 8:
5261               ASM_OUTPUT_DWARF_DATA8 (asm_out_file,
5262                                       a->dw_attr_val.v.val_long_long.hi,
5263                                       a->dw_attr_val.v.val_long_long.low);
5264               break;
5265             default:
5266               abort ();
5267             }
5268           break;
5269
5270         case dw_val_class_long_long:
5271           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 8);
5272           if (flag_debug_asm)
5273             fprintf (asm_out_file, "\t%s %s",
5274                    ASM_COMMENT_START, dwarf_attr_name (a->dw_attr));
5275
5276           fputc ('\n', asm_out_file);
5277           ASM_OUTPUT_DWARF_DATA8 (asm_out_file,
5278                                   a->dw_attr_val.v.val_long_long.hi,
5279                                   a->dw_attr_val.v.val_long_long.low);
5280
5281           if (flag_debug_asm)
5282             fprintf (asm_out_file,
5283                      "\t%s long long constant", ASM_COMMENT_START);
5284           
5285           fputc ('\n', asm_out_file);
5286           break;
5287
5288         case dw_val_class_float:
5289           ASM_OUTPUT_DWARF_DATA1 (asm_out_file,
5290                                   a->dw_attr_val.v.val_float.length * 4);
5291           if (flag_debug_asm)
5292             fprintf (asm_out_file, "\t%s %s",
5293                      ASM_COMMENT_START, dwarf_attr_name (a->dw_attr));
5294
5295           fputc ('\n', asm_out_file);
5296           for (i = 0; i < a->dw_attr_val.v.val_float.length; ++i)
5297             {
5298               ASM_OUTPUT_DWARF_DATA4 (asm_out_file,
5299                                       a->dw_attr_val.v.val_float.array[i]);
5300               if (flag_debug_asm)
5301                 fprintf (asm_out_file, "\t%s fp constant word %d",
5302                          ASM_COMMENT_START, i);
5303
5304               fputc ('\n', asm_out_file);
5305             }
5306           break;
5307
5308         case dw_val_class_flag:
5309           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, a->dw_attr_val.v.val_flag);
5310           break;
5311
5312         case dw_val_class_die_ref:
5313           if (a->dw_attr_val.v.val_die_ref != NULL)
5314             ref_offset = a->dw_attr_val.v.val_die_ref->die_offset;
5315           else if (a->dw_attr == DW_AT_sibling)
5316             ref_offset = sibling_offset(die);
5317           else
5318             abort ();
5319
5320           ASM_OUTPUT_DWARF_DATA (asm_out_file, ref_offset);
5321           break;
5322
5323         case dw_val_class_fde_ref:
5324           {
5325             char l1[20];
5326             ASM_GENERATE_INTERNAL_LABEL
5327               (l1, FDE_AFTER_SIZE_LABEL, a->dw_attr_val.v.val_fde_index * 2);
5328             ASM_OUTPUT_DWARF_OFFSET (asm_out_file, l1);
5329             fprintf (asm_out_file, " - %d", DWARF_OFFSET_SIZE);
5330           }
5331           break;
5332
5333         case dw_val_class_lbl_id:
5334           ASM_OUTPUT_DWARF_ADDR (asm_out_file, a->dw_attr_val.v.val_lbl_id);
5335           break;
5336
5337         case dw_val_class_section_offset:
5338           ASM_OUTPUT_DWARF_OFFSET (asm_out_file,
5339                                    stripattributes
5340                                    (a->dw_attr_val.v.val_section));
5341           break;
5342
5343         case dw_val_class_str:
5344           if (flag_debug_asm)
5345             ASM_OUTPUT_DWARF_STRING (asm_out_file, a->dw_attr_val.v.val_str);
5346           else
5347             ASM_OUTPUT_ASCII (asm_out_file,
5348                               a->dw_attr_val.v.val_str,
5349                               strlen (a->dw_attr_val.v.val_str) + 1);
5350           break;
5351
5352         default:
5353           abort ();
5354         }
5355
5356       if (a->dw_attr_val.val_class != dw_val_class_loc
5357           && a->dw_attr_val.val_class != dw_val_class_long_long
5358           && a->dw_attr_val.val_class != dw_val_class_float)
5359         {
5360           if (flag_debug_asm)
5361             fprintf (asm_out_file, "\t%s %s",
5362                      ASM_COMMENT_START, dwarf_attr_name (a->dw_attr));
5363
5364           fputc ('\n', asm_out_file);
5365         }
5366     }
5367
5368   for (c = die->die_child; c != NULL; c = c->die_sib)
5369     output_die (c);
5370
5371   if (die->die_child != NULL)
5372     {
5373       /* Add null byte to terminate sibling list. */
5374       ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
5375       if (flag_debug_asm)
5376         fprintf (asm_out_file, "\t%s end of children of DIE 0x%lx",
5377                  ASM_COMMENT_START, die->die_offset);
5378
5379       fputc ('\n', asm_out_file);
5380     }
5381 }
5382
5383 /* Output the compilation unit that appears at the beginning of the
5384    .debug_info section, and precedes the DIE descriptions.  */
5385
5386 static void
5387 output_compilation_unit_header ()
5388 {
5389   ASM_OUTPUT_DWARF_DATA (asm_out_file, next_die_offset - DWARF_OFFSET_SIZE);
5390   if (flag_debug_asm)
5391     fprintf (asm_out_file, "\t%s Length of Compilation Unit Info.",
5392              ASM_COMMENT_START);
5393
5394   fputc ('\n', asm_out_file);
5395   ASM_OUTPUT_DWARF_DATA2 (asm_out_file, DWARF_VERSION);
5396   if (flag_debug_asm)
5397     fprintf (asm_out_file, "\t%s DWARF version number", ASM_COMMENT_START);
5398
5399   fputc ('\n', asm_out_file);
5400   ASM_OUTPUT_DWARF_OFFSET (asm_out_file, stripattributes (ABBREV_SECTION));
5401   if (flag_debug_asm)
5402     fprintf (asm_out_file, "\t%s Offset Into Abbrev. Section",
5403              ASM_COMMENT_START);
5404
5405   fputc ('\n', asm_out_file);
5406   ASM_OUTPUT_DWARF_DATA1 (asm_out_file, PTR_SIZE);
5407   if (flag_debug_asm)
5408     fprintf (asm_out_file, "\t%s Pointer Size (in bytes)", ASM_COMMENT_START);
5409
5410   fputc ('\n', asm_out_file);
5411 }
5412
5413 /* The DWARF2 pubname for a nested thingy looks like "A::f".  The output
5414    of decl_printable_name for C++ looks like "A::f(int)".  Let's drop the
5415    argument list, and maybe the scope.  */
5416
5417 static char *
5418 dwarf2_name (decl, scope)
5419      tree decl;
5420      int scope;
5421 {
5422   return (*decl_printable_name) (decl, scope ? 1 : 0);
5423 }
5424
5425 /* Add a new entry to .debug_pubnames if appropriate.  */
5426
5427 static void
5428 add_pubname (decl, die)
5429      tree decl;
5430      dw_die_ref die;
5431 {
5432   pubname_ref p;
5433
5434   if (! TREE_PUBLIC (decl))
5435     return;
5436
5437   if (pubname_table_in_use == pubname_table_allocated)
5438     {
5439       pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
5440       pubname_table = (pubname_ref) xrealloc
5441         (pubname_table, pubname_table_allocated * sizeof (pubname_entry));
5442     }
5443
5444   p = &pubname_table[pubname_table_in_use++];
5445   p->die = die;
5446
5447   p->name = xstrdup (dwarf2_name (decl, 1));
5448 }
5449
5450 /* Output the public names table used to speed up access to externally
5451    visible names.  For now, only generate entries for externally
5452    visible procedures.  */
5453
5454 static void
5455 output_pubnames ()
5456 {
5457   register unsigned i;
5458   register unsigned long pubnames_length = size_of_pubnames ();
5459
5460   ASM_OUTPUT_DWARF_DATA (asm_out_file, pubnames_length);
5461
5462   if (flag_debug_asm)
5463     fprintf (asm_out_file, "\t%s Length of Public Names Info.",
5464              ASM_COMMENT_START);
5465
5466   fputc ('\n', asm_out_file);
5467   ASM_OUTPUT_DWARF_DATA2 (asm_out_file, DWARF_VERSION);
5468
5469   if (flag_debug_asm)
5470     fprintf (asm_out_file, "\t%s DWARF Version", ASM_COMMENT_START);
5471
5472   fputc ('\n', asm_out_file);
5473   ASM_OUTPUT_DWARF_OFFSET (asm_out_file, stripattributes (DEBUG_INFO_SECTION));
5474   if (flag_debug_asm)
5475     fprintf (asm_out_file, "\t%s Offset of Compilation Unit Info.",
5476              ASM_COMMENT_START);
5477
5478   fputc ('\n', asm_out_file);
5479   ASM_OUTPUT_DWARF_DATA (asm_out_file, next_die_offset);
5480   if (flag_debug_asm)
5481     fprintf (asm_out_file, "\t%s Compilation Unit Length", ASM_COMMENT_START);
5482
5483   fputc ('\n', asm_out_file);
5484   for (i = 0; i < pubname_table_in_use; ++i)
5485     {
5486       register pubname_ref pub = &pubname_table[i];
5487
5488       ASM_OUTPUT_DWARF_DATA (asm_out_file, pub->die->die_offset);
5489       if (flag_debug_asm)
5490         fprintf (asm_out_file, "\t%s DIE offset", ASM_COMMENT_START);
5491
5492       fputc ('\n', asm_out_file);
5493
5494       if (flag_debug_asm)
5495         {
5496           ASM_OUTPUT_DWARF_STRING (asm_out_file, pub->name);
5497           fprintf (asm_out_file, "%s external name", ASM_COMMENT_START);
5498         }
5499       else
5500         {
5501           ASM_OUTPUT_ASCII (asm_out_file, pub->name, strlen (pub->name) + 1);
5502         }
5503
5504       fputc ('\n', asm_out_file);
5505     }
5506
5507   ASM_OUTPUT_DWARF_DATA (asm_out_file, 0);
5508   fputc ('\n', asm_out_file);
5509 }
5510
5511 /* Add a new entry to .debug_aranges if appropriate.  */
5512
5513 static void
5514 add_arange (decl, die)
5515      tree decl;
5516      dw_die_ref die;
5517 {
5518   if (! DECL_SECTION_NAME (decl))
5519     return;
5520
5521   if (arange_table_in_use == arange_table_allocated)
5522     {
5523       arange_table_allocated += ARANGE_TABLE_INCREMENT;
5524       arange_table
5525         = (arange_ref) xrealloc (arange_table,
5526                                  arange_table_allocated * sizeof (dw_die_ref));
5527     }
5528
5529   arange_table[arange_table_in_use++] = die;
5530 }
5531
5532 /* Output the information that goes into the .debug_aranges table.
5533    Namely, define the beginning and ending address range of the
5534    text section generated for this compilation unit.  */
5535
5536 static void
5537 output_aranges ()
5538 {
5539   register unsigned i;
5540   register unsigned long aranges_length = size_of_aranges ();
5541
5542   ASM_OUTPUT_DWARF_DATA (asm_out_file, aranges_length);
5543   if (flag_debug_asm)
5544     fprintf (asm_out_file, "\t%s Length of Address Ranges Info.",
5545              ASM_COMMENT_START);
5546
5547   fputc ('\n', asm_out_file);
5548   ASM_OUTPUT_DWARF_DATA2 (asm_out_file, DWARF_VERSION);
5549   if (flag_debug_asm)
5550     fprintf (asm_out_file, "\t%s DWARF Version", ASM_COMMENT_START);
5551
5552   fputc ('\n', asm_out_file);
5553   ASM_OUTPUT_DWARF_OFFSET (asm_out_file, stripattributes (DEBUG_INFO_SECTION));
5554   if (flag_debug_asm)
5555     fprintf (asm_out_file, "\t%s Offset of Compilation Unit Info.",
5556              ASM_COMMENT_START);
5557
5558   fputc ('\n', asm_out_file);
5559   ASM_OUTPUT_DWARF_DATA1 (asm_out_file, PTR_SIZE);
5560   if (flag_debug_asm)
5561     fprintf (asm_out_file, "\t%s Size of Address", ASM_COMMENT_START);
5562
5563   fputc ('\n', asm_out_file);
5564   ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
5565   if (flag_debug_asm)
5566     fprintf (asm_out_file, "\t%s Size of Segment Descriptor",
5567              ASM_COMMENT_START);
5568
5569   fputc ('\n', asm_out_file);
5570   ASM_OUTPUT_DWARF_DATA4 (asm_out_file, 4);
5571   if (PTR_SIZE == 8)
5572     fprintf (asm_out_file, ",0,0");
5573
5574   if (flag_debug_asm)
5575     fprintf (asm_out_file, "\t%s Pad to %d byte boundary",
5576              ASM_COMMENT_START, 2 * PTR_SIZE);
5577
5578   fputc ('\n', asm_out_file);
5579   ASM_OUTPUT_DWARF_ADDR (asm_out_file, TEXT_SECTION);
5580   if (flag_debug_asm)
5581     fprintf (asm_out_file, "\t%s Address", ASM_COMMENT_START);
5582
5583   fputc ('\n', asm_out_file);
5584   ASM_OUTPUT_DWARF_ADDR_DELTA (asm_out_file, text_end_label, TEXT_SECTION);
5585   if (flag_debug_asm)
5586     fprintf (asm_out_file, "%s Length", ASM_COMMENT_START);
5587
5588   fputc ('\n', asm_out_file);
5589   for (i = 0; i < arange_table_in_use; ++i)
5590     {
5591       dw_die_ref a = arange_table[i];
5592
5593       if (a->die_tag == DW_TAG_subprogram)
5594         ASM_OUTPUT_DWARF_ADDR (asm_out_file, get_AT_low_pc (a));
5595       else
5596         {
5597           char *name = get_AT_string (a, DW_AT_MIPS_linkage_name);
5598           if (! name)
5599             name = get_AT_string (a, DW_AT_name);
5600
5601           ASM_OUTPUT_DWARF_ADDR (asm_out_file, name);
5602         }
5603
5604       if (flag_debug_asm)
5605         fprintf (asm_out_file, "\t%s Address", ASM_COMMENT_START);
5606
5607       fputc ('\n', asm_out_file);
5608       if (a->die_tag == DW_TAG_subprogram)
5609         ASM_OUTPUT_DWARF_ADDR_DELTA (asm_out_file, get_AT_hi_pc (a),
5610                                      get_AT_low_pc (a));
5611       else
5612         ASM_OUTPUT_DWARF_ADDR_DATA (asm_out_file,
5613                                     get_AT_unsigned (a, DW_AT_byte_size));
5614
5615       if (flag_debug_asm)
5616         fprintf (asm_out_file, "%s Length", ASM_COMMENT_START);
5617
5618       fputc ('\n', asm_out_file);
5619     }
5620
5621   /* Output the terminator words.  */
5622   ASM_OUTPUT_DWARF_ADDR_DATA (asm_out_file, 0);
5623   fputc ('\n', asm_out_file);
5624   ASM_OUTPUT_DWARF_ADDR_DATA (asm_out_file, 0);
5625   fputc ('\n', asm_out_file);
5626 }
5627
5628 /* Output the source line number correspondence information.  This
5629    information goes into the .debug_line section.
5630
5631    If the format of this data changes, then the function size_of_line_info
5632    must also be adjusted the same way.  */
5633
5634 static void
5635 output_line_info ()
5636 {
5637   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
5638   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
5639   register unsigned opc;
5640   register unsigned n_op_args;
5641   register unsigned long ft_index;
5642   register unsigned long lt_index;
5643   register unsigned long current_line;
5644   register long line_offset;
5645   register long line_delta;
5646   register unsigned long current_file;
5647   register unsigned long function;
5648
5649   ASM_OUTPUT_DWARF_DATA (asm_out_file, size_of_line_info ());
5650   if (flag_debug_asm)
5651     fprintf (asm_out_file, "\t%s Length of Source Line Info.",
5652              ASM_COMMENT_START);
5653
5654   fputc ('\n', asm_out_file);
5655   ASM_OUTPUT_DWARF_DATA2 (asm_out_file, DWARF_VERSION);
5656   if (flag_debug_asm)
5657     fprintf (asm_out_file, "\t%s DWARF Version", ASM_COMMENT_START);
5658
5659   fputc ('\n', asm_out_file);
5660   ASM_OUTPUT_DWARF_DATA (asm_out_file, size_of_line_prolog ());
5661   if (flag_debug_asm)
5662     fprintf (asm_out_file, "\t%s Prolog Length", ASM_COMMENT_START);
5663
5664   fputc ('\n', asm_out_file);
5665   ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DWARF_LINE_MIN_INSTR_LENGTH);
5666   if (flag_debug_asm)
5667     fprintf (asm_out_file, "\t%s Minimum Instruction Length",
5668              ASM_COMMENT_START);
5669
5670   fputc ('\n', asm_out_file);
5671   ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DWARF_LINE_DEFAULT_IS_STMT_START);
5672   if (flag_debug_asm)
5673     fprintf (asm_out_file, "\t%s Default is_stmt_start flag",
5674              ASM_COMMENT_START);
5675
5676   fputc ('\n', asm_out_file);
5677   fprintf (asm_out_file, "\t%s\t%d", ASM_BYTE_OP, DWARF_LINE_BASE);
5678   if (flag_debug_asm)
5679     fprintf (asm_out_file, "\t%s Line Base Value (Special Opcodes)",
5680              ASM_COMMENT_START);
5681
5682   fputc ('\n', asm_out_file);
5683   fprintf (asm_out_file, "\t%s\t%u", ASM_BYTE_OP, DWARF_LINE_RANGE);
5684   if (flag_debug_asm)
5685     fprintf (asm_out_file, "\t%s Line Range Value (Special Opcodes)",
5686              ASM_COMMENT_START);
5687
5688   fputc ('\n', asm_out_file);
5689   fprintf (asm_out_file, "\t%s\t%u", ASM_BYTE_OP, DWARF_LINE_OPCODE_BASE);
5690   if (flag_debug_asm)
5691     fprintf (asm_out_file, "\t%s Special Opcode Base", ASM_COMMENT_START);
5692
5693   fputc ('\n', asm_out_file);
5694   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; ++opc)
5695     {
5696       switch (opc)
5697         {
5698         case DW_LNS_advance_pc:
5699         case DW_LNS_advance_line:
5700         case DW_LNS_set_file:
5701         case DW_LNS_set_column:
5702         case DW_LNS_fixed_advance_pc:
5703           n_op_args = 1;
5704           break;
5705         default:
5706           n_op_args = 0;
5707           break;
5708         }
5709       ASM_OUTPUT_DWARF_DATA1 (asm_out_file, n_op_args);
5710       if (flag_debug_asm)
5711         fprintf (asm_out_file, "\t%s opcode: 0x%x has %d args",
5712                  ASM_COMMENT_START, opc, n_op_args);
5713       fputc ('\n', asm_out_file);
5714     }
5715
5716   if (flag_debug_asm)
5717     fprintf (asm_out_file, "%s Include Directory Table\n", ASM_COMMENT_START);
5718
5719   /* Include directory table is empty, at present */
5720   ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
5721   fputc ('\n', asm_out_file);
5722   if (flag_debug_asm)
5723     fprintf (asm_out_file, "%s File Name Table\n", ASM_COMMENT_START);
5724
5725   for (ft_index = 1; ft_index < file_table_in_use; ++ft_index)
5726     {
5727       if (flag_debug_asm)
5728         {
5729           ASM_OUTPUT_DWARF_STRING (asm_out_file, file_table[ft_index]);
5730           fprintf (asm_out_file, "%s File Entry: 0x%lx",
5731                    ASM_COMMENT_START, ft_index);
5732         }
5733       else
5734         {
5735           ASM_OUTPUT_ASCII (asm_out_file,
5736                             file_table[ft_index],
5737                             strlen (file_table[ft_index]) + 1);
5738         }
5739
5740       fputc ('\n', asm_out_file);
5741
5742       /* Include directory index */
5743       output_uleb128 (0);
5744       fputc ('\n', asm_out_file);
5745
5746       /* Modification time */
5747       output_uleb128 (0);
5748       fputc ('\n', asm_out_file);
5749
5750       /* File length in bytes */
5751       output_uleb128 (0);
5752       fputc ('\n', asm_out_file);
5753     }
5754
5755   /* Terminate the file name table */
5756   ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
5757   fputc ('\n', asm_out_file);
5758
5759   /* Set the address register to the first location in the text section */
5760   ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
5761   if (flag_debug_asm)
5762     fprintf (asm_out_file, "\t%s DW_LNE_set_address", ASM_COMMENT_START);
5763
5764   fputc ('\n', asm_out_file);
5765   output_uleb128 (1 + PTR_SIZE);
5766   fputc ('\n', asm_out_file);
5767   ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNE_set_address);
5768   fputc ('\n', asm_out_file);
5769   ASM_OUTPUT_DWARF_ADDR (asm_out_file, TEXT_SECTION);
5770   fputc ('\n', asm_out_file);
5771
5772   /* Generate the line number to PC correspondence table, encoded as
5773      a series of state machine operations.  */
5774   current_file = 1;
5775   current_line = 1;
5776   strcpy (prev_line_label, TEXT_SECTION);
5777   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
5778     {
5779       register dw_line_info_ref line_info;
5780
5781       /* Emit debug info for the address of the current line, choosing
5782          the encoding that uses the least amount of space.  */
5783       /* ??? Unfortunately, we have little choice here currently, and must
5784          always use the most general form.  Gcc does not know the address
5785          delta itself, so we can't use DW_LNS_advance_pc.  There are no known
5786          dwarf2 aware assemblers at this time, so we can't use any special
5787          pseudo ops that would allow the assembler to optimally encode this for
5788          us.  Many ports do have length attributes which will give an upper
5789          bound on the address range.  We could perhaps use length attributes
5790          to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
5791       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
5792       if (0)
5793         {
5794           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
5795           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_fixed_advance_pc);
5796           if (flag_debug_asm)
5797             fprintf (asm_out_file, "\t%s DW_LNS_fixed_advance_pc",
5798                      ASM_COMMENT_START);
5799
5800           fputc ('\n', asm_out_file);
5801           ASM_OUTPUT_DWARF_DELTA2 (asm_out_file, line_label, prev_line_label);
5802           fputc ('\n', asm_out_file);
5803         }
5804       else
5805         {
5806           /* This can handle any delta.  This takes 4+PTR_SIZE bytes.  */
5807           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
5808           if (flag_debug_asm)
5809             fprintf (asm_out_file, "\t%s DW_LNE_set_address",
5810                      ASM_COMMENT_START);
5811           fputc ('\n', asm_out_file);
5812           output_uleb128 (1 + PTR_SIZE);
5813           fputc ('\n', asm_out_file);
5814           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNE_set_address);
5815           fputc ('\n', asm_out_file);
5816           ASM_OUTPUT_DWARF_ADDR (asm_out_file, line_label);
5817           fputc ('\n', asm_out_file);
5818         }
5819       strcpy (prev_line_label, line_label);
5820
5821       /* Emit debug info for the source file of the current line, if
5822          different from the previous line.  */
5823       line_info = &line_info_table[lt_index];
5824       if (line_info->dw_file_num != current_file)
5825         {
5826           current_file = line_info->dw_file_num;
5827           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_set_file);
5828           if (flag_debug_asm)
5829             fprintf (asm_out_file, "\t%s DW_LNS_set_file", ASM_COMMENT_START);
5830
5831           fputc ('\n', asm_out_file);
5832           output_uleb128 (current_file);
5833           if (flag_debug_asm)
5834             fprintf (asm_out_file, " (\"%s\")", file_table[current_file]);
5835
5836           fputc ('\n', asm_out_file);
5837         }
5838
5839       /* Emit debug info for the current line number, choosing the encoding
5840          that uses the least amount of space.  */
5841       line_offset = line_info->dw_line_num - current_line;
5842       line_delta = line_offset - DWARF_LINE_BASE;
5843       current_line = line_info->dw_line_num;
5844       if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
5845         {
5846           /* This can handle deltas from -10 to 234, using the current
5847              definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
5848              takes 1 byte.  */
5849           ASM_OUTPUT_DWARF_DATA1 (asm_out_file,
5850                                   DWARF_LINE_OPCODE_BASE + line_delta);
5851           if (flag_debug_asm)
5852               fprintf (asm_out_file,
5853                        "\t%s line %ld", ASM_COMMENT_START, current_line);
5854
5855           fputc ('\n', asm_out_file);
5856         }
5857       else
5858         {
5859           /* This can handle any delta.  This takes at least 4 bytes, depending
5860              on the value being encoded.  */
5861           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_advance_line);
5862           if (flag_debug_asm)
5863             fprintf (asm_out_file, "\t%s advance to line %ld",
5864                      ASM_COMMENT_START, current_line);
5865
5866           fputc ('\n', asm_out_file);
5867           output_sleb128 (line_offset);
5868           fputc ('\n', asm_out_file);
5869           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_copy);
5870           fputc ('\n', asm_out_file);
5871         }
5872     }
5873
5874   /* Emit debug info for the address of the end of the function.  */
5875   if (0)
5876     {
5877       ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_fixed_advance_pc);
5878       if (flag_debug_asm)
5879         fprintf (asm_out_file, "\t%s DW_LNS_fixed_advance_pc",
5880                  ASM_COMMENT_START);
5881
5882       fputc ('\n', asm_out_file);
5883       ASM_OUTPUT_DWARF_DELTA2 (asm_out_file, text_end_label, prev_line_label);
5884       fputc ('\n', asm_out_file);
5885     }
5886   else
5887     {
5888       ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
5889       if (flag_debug_asm)
5890         fprintf (asm_out_file, "\t%s DW_LNE_set_address", ASM_COMMENT_START);
5891       fputc ('\n', asm_out_file);
5892       output_uleb128 (1 + PTR_SIZE);
5893       fputc ('\n', asm_out_file);
5894       ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNE_set_address);
5895       fputc ('\n', asm_out_file);
5896       ASM_OUTPUT_DWARF_ADDR (asm_out_file, text_end_label);
5897       fputc ('\n', asm_out_file);
5898     }
5899
5900   /* Output the marker for the end of the line number info.  */
5901   ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
5902   if (flag_debug_asm)
5903     fprintf (asm_out_file, "\t%s DW_LNE_end_sequence", ASM_COMMENT_START);
5904
5905   fputc ('\n', asm_out_file);
5906   output_uleb128 (1);
5907   fputc ('\n', asm_out_file);
5908   ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNE_end_sequence);
5909   fputc ('\n', asm_out_file);
5910
5911   function = 0;
5912   current_file = 1;
5913   current_line = 1;
5914   for (lt_index = 0; lt_index < separate_line_info_table_in_use; )
5915     {
5916       register dw_separate_line_info_ref line_info
5917         = &separate_line_info_table[lt_index];
5918
5919       /* Emit debug info for the address of the current line.  If this is
5920          a new function, or the first line of a function, then we need
5921          to handle it differently.  */
5922       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
5923                                    lt_index);
5924       if (function != line_info->function)
5925         {
5926           function = line_info->function;
5927
5928           /* Set the address register to the first line in the function */
5929           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
5930           if (flag_debug_asm)
5931             fprintf (asm_out_file, "\t%s DW_LNE_set_address",
5932                      ASM_COMMENT_START);
5933
5934           fputc ('\n', asm_out_file);
5935           output_uleb128 (1 + PTR_SIZE);
5936           fputc ('\n', asm_out_file);
5937           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNE_set_address);
5938           fputc ('\n', asm_out_file);
5939           ASM_OUTPUT_DWARF_ADDR (asm_out_file, line_label);
5940           fputc ('\n', asm_out_file);
5941         }
5942       else
5943         {
5944           /* ??? See the DW_LNS_advance_pc comment above.  */
5945           if (0)
5946             {
5947               ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_fixed_advance_pc);
5948               if (flag_debug_asm)
5949                 fprintf (asm_out_file, "\t%s DW_LNS_fixed_advance_pc",
5950                          ASM_COMMENT_START);
5951
5952               fputc ('\n', asm_out_file);
5953               ASM_OUTPUT_DWARF_DELTA2 (asm_out_file, line_label,
5954                                        prev_line_label);
5955               fputc ('\n', asm_out_file);
5956             }
5957           else
5958             {
5959               ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
5960               if (flag_debug_asm)
5961                 fprintf (asm_out_file, "\t%s DW_LNE_set_address",
5962                          ASM_COMMENT_START);
5963               fputc ('\n', asm_out_file);
5964               output_uleb128 (1 + PTR_SIZE);
5965               fputc ('\n', asm_out_file);
5966               ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNE_set_address);
5967               fputc ('\n', asm_out_file);
5968               ASM_OUTPUT_DWARF_ADDR (asm_out_file, line_label);
5969               fputc ('\n', asm_out_file);
5970             }
5971         }
5972       strcpy (prev_line_label, line_label);
5973
5974       /* Emit debug info for the source file of the current line, if
5975          different from the previous line.  */
5976       if (line_info->dw_file_num != current_file)
5977         {
5978           current_file = line_info->dw_file_num;
5979           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_set_file);
5980           if (flag_debug_asm)
5981             fprintf (asm_out_file, "\t%s DW_LNS_set_file", ASM_COMMENT_START);
5982
5983           fputc ('\n', asm_out_file);
5984           output_uleb128 (current_file);
5985           if (flag_debug_asm)
5986             fprintf (asm_out_file, " (\"%s\")", file_table[current_file]);
5987
5988           fputc ('\n', asm_out_file);
5989         }
5990
5991       /* Emit debug info for the current line number, choosing the encoding
5992          that uses the least amount of space.  */
5993       if (line_info->dw_line_num != current_line)
5994         {
5995           line_offset = line_info->dw_line_num - current_line;
5996           line_delta = line_offset - DWARF_LINE_BASE;
5997           current_line = line_info->dw_line_num;
5998           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
5999             {
6000               ASM_OUTPUT_DWARF_DATA1 (asm_out_file,
6001                                       DWARF_LINE_OPCODE_BASE + line_delta);
6002               if (flag_debug_asm)
6003                 fprintf (asm_out_file,
6004                          "\t%s line %ld", ASM_COMMENT_START, current_line);
6005
6006               fputc ('\n', asm_out_file);
6007             }
6008           else
6009             {
6010               ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_advance_line);
6011               if (flag_debug_asm)
6012                 fprintf (asm_out_file, "\t%s advance to line %ld",
6013                          ASM_COMMENT_START, current_line);
6014
6015               fputc ('\n', asm_out_file);
6016               output_sleb128 (line_offset);
6017               fputc ('\n', asm_out_file);
6018               ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_copy);
6019               fputc ('\n', asm_out_file);
6020             }
6021         }
6022
6023       ++lt_index;
6024
6025       /* If we're done with a function, end its sequence.  */
6026       if (lt_index == separate_line_info_table_in_use
6027           || separate_line_info_table[lt_index].function != function)
6028         {
6029           current_file = 1;
6030           current_line = 1;
6031
6032           /* Emit debug info for the address of the end of the function.  */
6033           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
6034           if (0)
6035             {
6036               ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_fixed_advance_pc);
6037               if (flag_debug_asm)
6038                 fprintf (asm_out_file, "\t%s DW_LNS_fixed_advance_pc",
6039                          ASM_COMMENT_START);
6040
6041               fputc ('\n', asm_out_file);
6042               ASM_OUTPUT_DWARF_DELTA2 (asm_out_file, line_label,
6043                                        prev_line_label);
6044               fputc ('\n', asm_out_file);
6045             }
6046           else
6047             {
6048               ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
6049               if (flag_debug_asm)
6050                 fprintf (asm_out_file, "\t%s DW_LNE_set_address",
6051                          ASM_COMMENT_START);
6052               fputc ('\n', asm_out_file);
6053               output_uleb128 (1 + PTR_SIZE);
6054               fputc ('\n', asm_out_file);
6055               ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNE_set_address);
6056               fputc ('\n', asm_out_file);
6057               ASM_OUTPUT_DWARF_ADDR (asm_out_file, line_label);
6058               fputc ('\n', asm_out_file);
6059             }
6060
6061           /* Output the marker for the end of this sequence.  */
6062           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
6063           if (flag_debug_asm)
6064             fprintf (asm_out_file, "\t%s DW_LNE_end_sequence",
6065                      ASM_COMMENT_START);
6066
6067           fputc ('\n', asm_out_file);
6068           output_uleb128 (1);
6069           fputc ('\n', asm_out_file);
6070           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNE_end_sequence);
6071           fputc ('\n', asm_out_file);
6072         }
6073     }
6074 }
6075 \f
6076 /* Given a pointer to a BLOCK node return non-zero if (and only if) the node
6077    in question represents the outermost pair of curly braces (i.e. the "body
6078    block") of a function or method.
6079
6080    For any BLOCK node representing a "body block" of a function or method, the
6081    BLOCK_SUPERCONTEXT of the node will point to another BLOCK node which
6082    represents the outermost (function) scope for the function or method (i.e.
6083    the one which includes the formal parameters).  The BLOCK_SUPERCONTEXT of
6084    *that* node in turn will point to the relevant FUNCTION_DECL node. */
6085
6086 static inline int
6087 is_body_block (stmt)
6088      register tree stmt;
6089 {
6090   if (TREE_CODE (stmt) == BLOCK)
6091     {
6092       register tree parent = BLOCK_SUPERCONTEXT (stmt);
6093
6094       if (TREE_CODE (parent) == BLOCK)
6095         {
6096           register tree grandparent = BLOCK_SUPERCONTEXT (parent);
6097
6098           if (TREE_CODE (grandparent) == FUNCTION_DECL)
6099             return 1;
6100         }
6101     }
6102
6103   return 0;
6104 }
6105
6106 /* Given a pointer to a tree node for some base type, return a pointer to
6107    a DIE that describes the given type.
6108
6109    This routine must only be called for GCC type nodes that correspond to
6110    Dwarf base (fundamental) types.  */
6111
6112 static dw_die_ref
6113 base_type_die (type)
6114      register tree type;
6115 {
6116   register dw_die_ref base_type_result;
6117   register char *type_name;
6118   register enum dwarf_type encoding;
6119   register tree name = TYPE_NAME (type);
6120
6121   if (TREE_CODE (type) == ERROR_MARK
6122       || TREE_CODE (type) == VOID_TYPE)
6123     return 0;
6124
6125   if (TREE_CODE (name) == TYPE_DECL)
6126     name = DECL_NAME (name);
6127   type_name = IDENTIFIER_POINTER (name);
6128
6129   switch (TREE_CODE (type))
6130     {
6131     case INTEGER_TYPE:
6132       /* Carefully distinguish the C character types, without messing
6133          up if the language is not C. Note that we check only for the names
6134          that contain spaces; other names might occur by coincidence in other 
6135          languages.  */
6136       if (! (TYPE_PRECISION (type) == CHAR_TYPE_SIZE
6137              && (type == char_type_node
6138                  || ! strcmp (type_name, "signed char")
6139                  || ! strcmp (type_name, "unsigned char"))))
6140         {
6141           if (TREE_UNSIGNED (type))
6142             encoding = DW_ATE_unsigned;
6143           else
6144             encoding = DW_ATE_signed;
6145           break;
6146         }
6147       /* else fall through */
6148
6149     case CHAR_TYPE:
6150       /* GNU Pascal/Ada CHAR type.  Not used in C.  */
6151       if (TREE_UNSIGNED (type))
6152         encoding = DW_ATE_unsigned_char;
6153       else
6154         encoding = DW_ATE_signed_char;
6155       break;
6156
6157     case REAL_TYPE:
6158       encoding = DW_ATE_float;
6159       break;
6160
6161     case COMPLEX_TYPE:
6162       encoding = DW_ATE_complex_float;
6163       break;
6164
6165     case BOOLEAN_TYPE:
6166       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
6167       encoding = DW_ATE_boolean;
6168       break;
6169
6170     default:
6171       abort (); /* No other TREE_CODEs are Dwarf fundamental types.  */
6172     }
6173
6174   base_type_result = new_die (DW_TAG_base_type, comp_unit_die);
6175   add_AT_string (base_type_result, DW_AT_name, type_name);
6176   add_AT_unsigned (base_type_result, DW_AT_byte_size,
6177                    TYPE_PRECISION (type) / BITS_PER_UNIT);
6178   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
6179
6180   return base_type_result;
6181 }
6182
6183 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
6184    the Dwarf "root" type for the given input type.  The Dwarf "root" type of
6185    a given type is generally the same as the given type, except that if the
6186    given type is a pointer or reference type, then the root type of the given
6187    type is the root type of the "basis" type for the pointer or reference
6188    type.  (This definition of the "root" type is recursive.) Also, the root
6189    type of a `const' qualified type or a `volatile' qualified type is the
6190    root type of the given type without the qualifiers.  */
6191
6192 static tree
6193 root_type (type)
6194      register tree type;
6195 {
6196   if (TREE_CODE (type) == ERROR_MARK)
6197     return error_mark_node;
6198
6199   switch (TREE_CODE (type))
6200     {
6201     case ERROR_MARK:
6202       return error_mark_node;
6203
6204     case POINTER_TYPE:
6205     case REFERENCE_TYPE:
6206       return type_main_variant (root_type (TREE_TYPE (type)));
6207
6208     default:
6209       return type_main_variant (type);
6210     }
6211 }
6212
6213 /* Given a pointer to an arbitrary ..._TYPE tree node, return non-zero if the
6214    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
6215
6216 static inline int
6217 is_base_type (type)
6218      register tree type;
6219 {
6220   switch (TREE_CODE (type))
6221     {
6222     case ERROR_MARK:
6223     case VOID_TYPE:
6224     case INTEGER_TYPE:
6225     case REAL_TYPE:
6226     case COMPLEX_TYPE:
6227     case BOOLEAN_TYPE:
6228     case CHAR_TYPE:
6229       return 1;
6230
6231     case SET_TYPE:
6232     case ARRAY_TYPE:
6233     case RECORD_TYPE:
6234     case UNION_TYPE:
6235     case QUAL_UNION_TYPE:
6236     case ENUMERAL_TYPE:
6237     case FUNCTION_TYPE:
6238     case METHOD_TYPE:
6239     case POINTER_TYPE:
6240     case REFERENCE_TYPE:
6241     case FILE_TYPE:
6242     case OFFSET_TYPE:
6243     case LANG_TYPE:
6244       return 0;
6245
6246     default:
6247       abort ();
6248     }
6249
6250   return 0;
6251 }
6252
6253 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
6254    entry that chains various modifiers in front of the given type.  */
6255
6256 static dw_die_ref
6257 modified_type_die (type, is_const_type, is_volatile_type, context_die)
6258      register tree type;
6259      register int is_const_type;
6260      register int is_volatile_type;
6261      register dw_die_ref context_die;
6262 {
6263   register enum tree_code code = TREE_CODE (type);
6264   register dw_die_ref mod_type_die = NULL;
6265   register dw_die_ref sub_die = NULL;
6266   register tree item_type = NULL;
6267
6268   if (code != ERROR_MARK)
6269     {
6270       type = build_type_variant (type, is_const_type, is_volatile_type);
6271
6272       mod_type_die = lookup_type_die (type);
6273       if (mod_type_die)
6274         return mod_type_die;
6275
6276       /* Handle C typedef types. */
6277       if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
6278           && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
6279         {
6280           tree dtype = TREE_TYPE (TYPE_NAME (type));
6281           if (type == dtype)
6282             {
6283               /* For a named type, use the typedef.  */
6284               gen_type_die (type, context_die);
6285               mod_type_die = lookup_type_die (type);
6286             }
6287
6288           else if (is_const_type < TYPE_READONLY (dtype)
6289                    || is_volatile_type < TYPE_VOLATILE (dtype))
6290             /* cv-unqualified version of named type.  Just use the unnamed
6291                type to which it refers.  */
6292             mod_type_die
6293               = modified_type_die (DECL_ORIGINAL_TYPE (TYPE_NAME (type)),
6294                                    is_const_type, is_volatile_type,
6295                                    context_die);
6296           /* Else cv-qualified version of named type; fall through.  */
6297         }
6298
6299       if (mod_type_die)
6300         /* OK */;
6301       else if (is_const_type)
6302         {
6303           mod_type_die = new_die (DW_TAG_const_type, comp_unit_die);
6304           sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
6305         }
6306       else if (is_volatile_type)
6307         {
6308           mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die);
6309           sub_die = modified_type_die (type, 0, 0, context_die);
6310         }
6311       else if (code == POINTER_TYPE)
6312         {
6313           mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die);
6314           add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
6315 #if 0
6316           add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
6317 #endif
6318           item_type = TREE_TYPE (type);
6319         }
6320       else if (code == REFERENCE_TYPE)
6321         {
6322           mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die);
6323           add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
6324 #if 0
6325           add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
6326 #endif 
6327           item_type = TREE_TYPE (type);
6328         }
6329       else if (is_base_type (type))
6330         mod_type_die = base_type_die (type);
6331       else
6332         {
6333           gen_type_die (type, context_die);
6334
6335           /* We have to get the type_main_variant here (and pass that to the
6336              `lookup_type_die' routine) because the ..._TYPE node we have
6337              might simply be a *copy* of some original type node (where the
6338              copy was created to help us keep track of typedef names) and
6339              that copy might have a different TYPE_UID from the original
6340              ..._TYPE node.  */
6341           mod_type_die = lookup_type_die (type_main_variant (type));
6342           if (mod_type_die == NULL)
6343             abort ();
6344         }
6345     }
6346
6347   equate_type_number_to_die (type, mod_type_die);
6348   if (item_type)
6349     /* We must do this after the equate_type_number_to_die call, in case
6350        this is a recursive type.  This ensures that the modified_type_die
6351        recursion will terminate even if the type is recursive.  Recursive
6352        types are possible in Ada.  */
6353     sub_die = modified_type_die (item_type,
6354                                  TYPE_READONLY (item_type),
6355                                  TYPE_VOLATILE (item_type),
6356                                  context_die);
6357
6358   if (sub_die != NULL)
6359     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
6360
6361   return mod_type_die;
6362 }
6363
6364 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
6365    an enumerated type.   */
6366
6367 static inline int
6368 type_is_enum (type)
6369      register tree type;
6370 {
6371   return TREE_CODE (type) == ENUMERAL_TYPE;
6372 }
6373
6374 /* Return a location descriptor that designates a machine register.  */
6375
6376 static dw_loc_descr_ref
6377 reg_loc_descriptor (rtl)
6378      register rtx rtl;
6379 {
6380   register dw_loc_descr_ref loc_result = NULL;
6381   register unsigned reg = reg_number (rtl);
6382
6383   if (reg <= 31)
6384     loc_result = new_loc_descr (DW_OP_reg0 + reg, 0, 0);
6385   else
6386     loc_result = new_loc_descr (DW_OP_regx, reg, 0);
6387
6388   return loc_result;
6389 }
6390
6391 /* Return a location descriptor that designates a base+offset location.  */
6392
6393 static dw_loc_descr_ref
6394 based_loc_descr (reg, offset)
6395      unsigned reg;
6396      long int offset;
6397 {
6398   register dw_loc_descr_ref loc_result;
6399   /* For the "frame base", we use the frame pointer or stack pointer
6400      registers, since the RTL for local variables is relative to one of
6401      them.  */
6402   register unsigned fp_reg = DBX_REGISTER_NUMBER (frame_pointer_needed
6403                                                   ? HARD_FRAME_POINTER_REGNUM
6404                                                   : STACK_POINTER_REGNUM);
6405
6406   if (reg == fp_reg)
6407     loc_result = new_loc_descr (DW_OP_fbreg, offset, 0);
6408   else if (reg <= 31)
6409     loc_result = new_loc_descr (DW_OP_breg0 + reg, offset, 0);
6410   else
6411     loc_result = new_loc_descr (DW_OP_bregx, reg, offset);
6412
6413   return loc_result;
6414 }
6415
6416 /* Return true if this RTL expression describes a base+offset calculation.  */
6417
6418 static inline int
6419 is_based_loc (rtl)
6420      register rtx rtl;
6421 {
6422     return (GET_CODE (rtl) == PLUS
6423             && ((GET_CODE (XEXP (rtl, 0)) == REG
6424                  && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
6425 }
6426
6427 /* The following routine converts the RTL for a variable or parameter
6428    (resident in memory) into an equivalent Dwarf representation of a
6429    mechanism for getting the address of that same variable onto the top of a
6430    hypothetical "address evaluation" stack.
6431
6432    When creating memory location descriptors, we are effectively transforming
6433    the RTL for a memory-resident object into its Dwarf postfix expression
6434    equivalent.  This routine recursively descends an RTL tree, turning
6435    it into Dwarf postfix code as it goes.  */
6436
6437 static dw_loc_descr_ref
6438 mem_loc_descriptor (rtl)
6439      register rtx rtl;
6440 {
6441   dw_loc_descr_ref mem_loc_result = NULL;
6442   /* Note that for a dynamically sized array, the location we will generate a 
6443      description of here will be the lowest numbered location which is
6444      actually within the array.  That's *not* necessarily the same as the
6445      zeroth element of the array.  */
6446
6447   switch (GET_CODE (rtl))
6448     {
6449     case SUBREG:
6450       /* The case of a subreg may arise when we have a local (register)
6451          variable or a formal (register) parameter which doesn't quite fill
6452          up an entire register.  For now, just assume that it is
6453          legitimate to make the Dwarf info refer to the whole register which
6454          contains the given subreg.  */
6455       rtl = XEXP (rtl, 0);
6456
6457       /* ... fall through ... */
6458
6459     case REG:
6460       /* Whenever a register number forms a part of the description of the
6461          method for calculating the (dynamic) address of a memory resident
6462          object, DWARF rules require the register number be referred to as 
6463          a "base register".  This distinction is not based in any way upon
6464          what category of register the hardware believes the given register
6465          belongs to.  This is strictly DWARF terminology we're dealing with
6466          here. Note that in cases where the location of a memory-resident
6467          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
6468          OP_CONST (0)) the actual DWARF location descriptor that we generate
6469          may just be OP_BASEREG (basereg).  This may look deceptively like
6470          the object in question was allocated to a register (rather than in
6471          memory) so DWARF consumers need to be aware of the subtle
6472          distinction between OP_REG and OP_BASEREG.  */
6473       mem_loc_result = based_loc_descr (reg_number (rtl), 0);
6474       break;
6475
6476     case MEM:
6477       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0));
6478       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
6479       break;
6480
6481     case CONST:
6482     case SYMBOL_REF:
6483       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
6484       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
6485       mem_loc_result->dw_loc_oprnd1.v.val_addr = addr_to_string (rtl);
6486       break;
6487
6488     case PLUS:
6489       if (is_based_loc (rtl))
6490         mem_loc_result = based_loc_descr (reg_number (XEXP (rtl, 0)),
6491                                           INTVAL (XEXP (rtl, 1)));
6492       else
6493         {
6494           add_loc_descr (&mem_loc_result, mem_loc_descriptor (XEXP (rtl, 0)));
6495           add_loc_descr (&mem_loc_result, mem_loc_descriptor (XEXP (rtl, 1)));
6496           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_plus, 0, 0));
6497         }
6498       break;
6499
6500     case MULT:
6501       /* If a pseudo-reg is optimized away, it is possible for it to
6502          be replaced with a MEM containing a multiply.  */
6503       add_loc_descr (&mem_loc_result, mem_loc_descriptor (XEXP (rtl, 0)));
6504       add_loc_descr (&mem_loc_result, mem_loc_descriptor (XEXP (rtl, 1)));
6505       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
6506       break;
6507
6508     case CONST_INT:
6509       mem_loc_result = new_loc_descr (DW_OP_constu, INTVAL (rtl), 0);
6510       break;
6511
6512     default:
6513       abort ();
6514     }
6515
6516   return mem_loc_result;
6517 }
6518
6519 /* Return a descriptor that describes the concatenation of two locations.
6520    This is typically a complex variable.  */
6521
6522 static dw_loc_descr_ref
6523 concat_loc_descriptor (x0, x1)
6524      register rtx x0, x1;
6525 {
6526   dw_loc_descr_ref cc_loc_result = NULL;
6527
6528   if (!is_pseudo_reg (x0)
6529       && (GET_CODE (x0) != MEM || !is_pseudo_reg (XEXP (x0, 0))))
6530     add_loc_descr (&cc_loc_result, loc_descriptor (x0));
6531   add_loc_descr (&cc_loc_result,
6532                  new_loc_descr (DW_OP_piece, GET_MODE_SIZE (GET_MODE (x0)), 0));
6533
6534   if (!is_pseudo_reg (x1)
6535       && (GET_CODE (x1) != MEM || !is_pseudo_reg (XEXP (x1, 0))))
6536     add_loc_descr (&cc_loc_result, loc_descriptor (x1));
6537   add_loc_descr (&cc_loc_result,
6538                  new_loc_descr (DW_OP_piece, GET_MODE_SIZE (GET_MODE (x1)), 0));
6539
6540   return cc_loc_result;
6541 }
6542
6543 /* Output a proper Dwarf location descriptor for a variable or parameter
6544    which is either allocated in a register or in a memory location.  For a
6545    register, we just generate an OP_REG and the register number.  For a
6546    memory location we provide a Dwarf postfix expression describing how to
6547    generate the (dynamic) address of the object onto the address stack.  */
6548
6549 static dw_loc_descr_ref
6550 loc_descriptor (rtl)
6551      register rtx rtl;
6552 {
6553   dw_loc_descr_ref loc_result = NULL;
6554   switch (GET_CODE (rtl))
6555     {
6556     case SUBREG:
6557       /* The case of a subreg may arise when we have a local (register)
6558          variable or a formal (register) parameter which doesn't quite fill
6559          up an entire register.  For now, just assume that it is
6560          legitimate to make the Dwarf info refer to the whole register which
6561          contains the given subreg.  */
6562       rtl = XEXP (rtl, 0);
6563
6564       /* ... fall through ... */
6565
6566     case REG:
6567       loc_result = reg_loc_descriptor (rtl);
6568       break;
6569
6570     case MEM:
6571       loc_result = mem_loc_descriptor (XEXP (rtl, 0));
6572       break;
6573
6574     case CONCAT:
6575       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
6576       break;
6577
6578     default:
6579       abort ();
6580     }
6581
6582   return loc_result;
6583 }
6584
6585 /* Given an unsigned value, round it up to the lowest multiple of `boundary'
6586    which is not less than the value itself.  */
6587
6588 static inline unsigned
6589 ceiling (value, boundary)
6590      register unsigned value;
6591      register unsigned boundary;
6592 {
6593   return (((value + boundary - 1) / boundary) * boundary);
6594 }
6595
6596 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
6597    pointer to the declared type for the relevant field variable, or return
6598    `integer_type_node' if the given node turns out to be an
6599    ERROR_MARK node.  */
6600
6601 static inline tree
6602 field_type (decl)
6603      register tree decl;
6604 {
6605   register tree type;
6606
6607   if (TREE_CODE (decl) == ERROR_MARK)
6608     return integer_type_node;
6609
6610   type = DECL_BIT_FIELD_TYPE (decl);
6611   if (type == NULL_TREE)
6612     type = TREE_TYPE (decl);
6613
6614   return type;
6615 }
6616
6617 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
6618    node, return the alignment in bits for the type, or else return
6619    BITS_PER_WORD if the node actually turns out to be an
6620    ERROR_MARK node.  */
6621
6622 static inline unsigned
6623 simple_type_align_in_bits (type)
6624      register tree type;
6625 {
6626   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
6627 }
6628
6629 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
6630    node, return the size in bits for the type if it is a constant, or else
6631    return the alignment for the type if the type's size is not constant, or
6632    else return BITS_PER_WORD if the type actually turns out to be an
6633    ERROR_MARK node.  */
6634
6635 static inline unsigned
6636 simple_type_size_in_bits (type)
6637      register tree type;
6638 {
6639   if (TREE_CODE (type) == ERROR_MARK)
6640     return BITS_PER_WORD;
6641   else
6642     {
6643       register tree type_size_tree = TYPE_SIZE (type);
6644
6645       if (TREE_CODE (type_size_tree) != INTEGER_CST)
6646         return TYPE_ALIGN (type);
6647
6648       return (unsigned) TREE_INT_CST_LOW (type_size_tree);
6649     }
6650 }
6651
6652 /* Given a pointer to what is assumed to be a FIELD_DECL node, compute and
6653    return the byte offset of the lowest addressed byte of the "containing
6654    object" for the given FIELD_DECL, or return 0 if we are unable to
6655    determine what that offset is, either because the argument turns out to
6656    be a pointer to an ERROR_MARK node, or because the offset is actually
6657    variable.  (We can't handle the latter case just yet).  */
6658
6659 static unsigned
6660 field_byte_offset (decl)
6661      register tree decl;
6662 {
6663   register unsigned type_align_in_bytes;
6664   register unsigned type_align_in_bits;
6665   register unsigned type_size_in_bits;
6666   register unsigned object_offset_in_align_units;
6667   register unsigned object_offset_in_bits;
6668   register unsigned object_offset_in_bytes;
6669   register tree type;
6670   register tree bitpos_tree;
6671   register tree field_size_tree;
6672   register unsigned bitpos_int;
6673   register unsigned deepest_bitpos;
6674   register unsigned field_size_in_bits;
6675
6676   if (TREE_CODE (decl) == ERROR_MARK)
6677     return 0;
6678
6679   if (TREE_CODE (decl) != FIELD_DECL)
6680     abort ();
6681
6682   type = field_type (decl);
6683
6684   bitpos_tree = DECL_FIELD_BITPOS (decl);
6685   field_size_tree = DECL_SIZE (decl);
6686
6687   /* We cannot yet cope with fields whose positions or sizes are variable, so 
6688      for now, when we see such things, we simply return 0.  Someday, we may
6689      be able to handle such cases, but it will be damn difficult.  */
6690   if (TREE_CODE (bitpos_tree) != INTEGER_CST)
6691     return 0;
6692   bitpos_int = (unsigned) TREE_INT_CST_LOW (bitpos_tree);
6693
6694   if (TREE_CODE (field_size_tree) != INTEGER_CST)
6695     return 0;
6696
6697   field_size_in_bits = (unsigned) TREE_INT_CST_LOW (field_size_tree);
6698   type_size_in_bits = simple_type_size_in_bits (type);
6699   type_align_in_bits = simple_type_align_in_bits (type);
6700   type_align_in_bytes = type_align_in_bits / BITS_PER_UNIT;
6701
6702   /* Note that the GCC front-end doesn't make any attempt to keep track of
6703      the starting bit offset (relative to the start of the containing
6704      structure type) of the hypothetical "containing object" for a bit-
6705      field.  Thus, when computing the byte offset value for the start of the
6706      "containing object" of a bit-field, we must deduce this information on 
6707      our own. This can be rather tricky to do in some cases.  For example,
6708      handling the following structure type definition when compiling for an
6709      i386/i486 target (which only aligns long long's to 32-bit boundaries)
6710      can be very tricky:
6711
6712          struct S { int field1; long long field2:31; };
6713
6714      Fortunately, there is a simple rule-of-thumb which can be
6715      used in such cases.  When compiling for an i386/i486, GCC will allocate
6716      8 bytes for the structure shown above.  It decides to do this based upon 
6717      one simple rule for bit-field allocation.  Quite simply, GCC allocates
6718      each "containing object" for each bit-field at the first (i.e. lowest
6719      addressed) legitimate alignment boundary (based upon the required
6720      minimum alignment for the declared type of the field) which it can
6721      possibly use, subject to the condition that there is still enough
6722      available space remaining in the containing object (when allocated at
6723      the selected point) to fully accommodate all of the bits of the
6724      bit-field itself.  This simple rule makes it obvious why GCC allocates
6725      8 bytes for each object of the structure type shown above.  When looking
6726      for a place to allocate the "containing object" for `field2', the
6727      compiler simply tries to allocate a 64-bit "containing object" at each
6728      successive 32-bit boundary (starting at zero) until it finds a place to
6729      allocate that 64- bit field such that at least 31 contiguous (and
6730      previously unallocated) bits remain within that selected 64 bit field.
6731      (As it turns out, for the example above, the compiler finds that it is
6732      OK to allocate the "containing object" 64-bit field at bit-offset zero
6733      within the structure type.) Here we attempt to work backwards from the
6734      limited set of facts we're given, and we try to deduce from those facts, 
6735      where GCC must have believed that the containing object started (within
6736      the structure type). The value we deduce is then used (by the callers of 
6737      this routine) to generate DW_AT_location and DW_AT_bit_offset attributes 
6738      for fields (both bit-fields and, in the case of DW_AT_location, regular
6739      fields as well).  */
6740
6741   /* Figure out the bit-distance from the start of the structure to the
6742      "deepest" bit of the bit-field.  */
6743   deepest_bitpos = bitpos_int + field_size_in_bits;
6744
6745   /* This is the tricky part.  Use some fancy footwork to deduce where the
6746      lowest addressed bit of the containing object must be.  */
6747   object_offset_in_bits
6748     = ceiling (deepest_bitpos, type_align_in_bits) - type_size_in_bits;
6749
6750   /* Compute the offset of the containing object in "alignment units".  */
6751   object_offset_in_align_units = object_offset_in_bits / type_align_in_bits;
6752
6753   /* Compute the offset of the containing object in bytes.  */
6754   object_offset_in_bytes = object_offset_in_align_units * type_align_in_bytes;
6755
6756   return object_offset_in_bytes;
6757 }
6758 \f
6759 /* The following routines define various Dwarf attributes and any data
6760    associated with them.  */
6761
6762 /* Add a location description attribute value to a DIE.
6763
6764    This emits location attributes suitable for whole variables and
6765    whole parameters.  Note that the location attributes for struct fields are
6766    generated by the routine `data_member_location_attribute' below.  */
6767
6768 static void
6769 add_AT_location_description (die, attr_kind, rtl)
6770      dw_die_ref die;
6771      enum dwarf_attribute attr_kind;
6772      register rtx rtl;
6773 {
6774   /* Handle a special case.  If we are about to output a location descriptor
6775      for a variable or parameter which has been optimized out of existence,
6776      don't do that.  A variable which has been optimized out
6777      of existence will have a DECL_RTL value which denotes a pseudo-reg.
6778      Currently, in some rare cases, variables can have DECL_RTL values which
6779      look like (MEM (REG pseudo-reg#)).  These cases are due to bugs
6780      elsewhere in the compiler.  We treat such cases as if the variable(s) in 
6781      question had been optimized out of existence.  */
6782
6783   if (is_pseudo_reg (rtl)
6784       || (GET_CODE (rtl) == MEM
6785           && is_pseudo_reg (XEXP (rtl, 0)))
6786       || (GET_CODE (rtl) == CONCAT
6787           && is_pseudo_reg (XEXP (rtl, 0))
6788           && is_pseudo_reg (XEXP (rtl, 1))))
6789     return;
6790
6791   add_AT_loc (die, attr_kind, loc_descriptor (rtl));
6792 }
6793
6794 /* Attach the specialized form of location attribute used for data
6795    members of struct and union types.  In the special case of a
6796    FIELD_DECL node which represents a bit-field, the "offset" part
6797    of this special location descriptor must indicate the distance
6798    in bytes from the lowest-addressed byte of the containing struct
6799    or union type to the lowest-addressed byte of the "containing
6800    object" for the bit-field.  (See the `field_byte_offset' function
6801    above).. For any given bit-field, the "containing object" is a
6802    hypothetical object (of some integral or enum type) within which
6803    the given bit-field lives.  The type of this hypothetical
6804    "containing object" is always the same as the declared type of
6805    the individual bit-field itself (for GCC anyway... the DWARF
6806    spec doesn't actually mandate this).  Note that it is the size
6807    (in bytes) of the hypothetical "containing object" which will
6808    be given in the DW_AT_byte_size attribute for this bit-field.
6809    (See the `byte_size_attribute' function below.)  It is also used
6810    when calculating the value of the DW_AT_bit_offset attribute.
6811    (See the `bit_offset_attribute' function below).  */
6812
6813 static void
6814 add_data_member_location_attribute (die, decl)
6815      register dw_die_ref die;
6816      register tree decl;
6817 {
6818   register unsigned long offset;
6819   register dw_loc_descr_ref loc_descr;
6820   register enum dwarf_location_atom op;
6821
6822   if (TREE_CODE (decl) == TREE_VEC)
6823     offset = TREE_INT_CST_LOW (BINFO_OFFSET (decl));
6824   else
6825     offset = field_byte_offset (decl);
6826
6827   /* The DWARF2 standard says that we should assume that the structure address
6828      is already on the stack, so we can specify a structure field address
6829      by using DW_OP_plus_uconst.  */
6830
6831 #ifdef MIPS_DEBUGGING_INFO
6832   /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst operator
6833      correctly.  It works only if we leave the offset on the stack.  */
6834   op = DW_OP_constu;
6835 #else
6836   op = DW_OP_plus_uconst;
6837 #endif
6838
6839   loc_descr = new_loc_descr (op, offset, 0);
6840   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
6841 }
6842
6843 /* Attach an DW_AT_const_value attribute for a variable or a parameter which
6844    does not have a "location" either in memory or in a register.  These
6845    things can arise in GNU C when a constant is passed as an actual parameter
6846    to an inlined function.  They can also arise in C++ where declared
6847    constants do not necessarily get memory "homes".  */
6848
6849 static void
6850 add_const_value_attribute (die, rtl)
6851      register dw_die_ref die;
6852      register rtx rtl;
6853 {
6854   switch (GET_CODE (rtl))
6855     {
6856     case CONST_INT:
6857       /* Note that a CONST_INT rtx could represent either an integer or a
6858          floating-point constant.  A CONST_INT is used whenever the constant
6859          will fit into a single word.  In all such cases, the original mode
6860          of the constant value is wiped out, and the CONST_INT rtx is
6861          assigned VOIDmode.  */
6862       add_AT_unsigned (die, DW_AT_const_value, (unsigned) INTVAL (rtl));
6863       break;
6864
6865     case CONST_DOUBLE:
6866       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
6867          floating-point constant.  A CONST_DOUBLE is used whenever the
6868          constant requires more than one word in order to be adequately
6869          represented.  We output CONST_DOUBLEs as blocks.  */
6870       {
6871         register enum machine_mode mode = GET_MODE (rtl);
6872
6873         if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6874           {
6875             register unsigned length = GET_MODE_SIZE (mode) / sizeof (long);
6876             long array[4];
6877             REAL_VALUE_TYPE rv;
6878
6879             REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
6880             switch (mode)
6881               {
6882               case SFmode:
6883                 REAL_VALUE_TO_TARGET_SINGLE (rv, array[0]);
6884                 break;
6885
6886               case DFmode:
6887                 REAL_VALUE_TO_TARGET_DOUBLE (rv, array);
6888                 break;
6889
6890               case XFmode:
6891               case TFmode:
6892                 REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, array);
6893                 break;
6894
6895               default:
6896                 abort ();
6897               }
6898
6899             add_AT_float (die, DW_AT_const_value, length, array);
6900           }
6901         else
6902           add_AT_long_long (die, DW_AT_const_value,
6903                             CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
6904       }
6905       break;
6906
6907     case CONST_STRING:
6908       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
6909       break;
6910
6911     case SYMBOL_REF:
6912     case LABEL_REF:
6913     case CONST:
6914       add_AT_addr (die, DW_AT_const_value, addr_to_string (rtl));
6915       break;
6916
6917     case PLUS:
6918       /* In cases where an inlined instance of an inline function is passed
6919          the address of an `auto' variable (which is local to the caller) we
6920          can get a situation where the DECL_RTL of the artificial local
6921          variable (for the inlining) which acts as a stand-in for the
6922          corresponding formal parameter (of the inline function) will look
6923          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
6924          exactly a compile-time constant expression, but it isn't the address 
6925          of the (artificial) local variable either.  Rather, it represents the 
6926          *value* which the artificial local variable always has during its
6927          lifetime.  We currently have no way to represent such quasi-constant 
6928          values in Dwarf, so for now we just punt and generate nothing.  */
6929       break;
6930
6931     default:
6932       /* No other kinds of rtx should be possible here.  */
6933       abort ();
6934     }
6935
6936 }
6937
6938 /* Generate *either* an DW_AT_location attribute or else an DW_AT_const_value
6939    data attribute for a variable or a parameter.  We generate the
6940    DW_AT_const_value attribute only in those cases where the given variable
6941    or parameter does not have a true "location" either in memory or in a
6942    register.  This can happen (for example) when a constant is passed as an
6943    actual argument in a call to an inline function.  (It's possible that
6944    these things can crop up in other ways also.)  Note that one type of
6945    constant value which can be passed into an inlined function is a constant
6946    pointer.  This can happen for example if an actual argument in an inlined
6947    function call evaluates to a compile-time constant address.  */
6948
6949 static void
6950 add_location_or_const_value_attribute (die, decl)
6951      register dw_die_ref die;
6952      register tree decl;
6953 {
6954   register rtx rtl;
6955   register tree declared_type;
6956   register tree passed_type;
6957
6958   if (TREE_CODE (decl) == ERROR_MARK)
6959     return;
6960
6961   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != PARM_DECL)
6962     abort ();
6963
6964   /* Here we have to decide where we are going to say the parameter "lives"
6965      (as far as the debugger is concerned).  We only have a couple of
6966      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
6967
6968      DECL_RTL normally indicates where the parameter lives during most of the 
6969      activation of the function.  If optimization is enabled however, this
6970      could be either NULL or else a pseudo-reg.  Both of those cases indicate 
6971      that the parameter doesn't really live anywhere (as far as the code
6972      generation parts of GCC are concerned) during most of the function's
6973      activation.  That will happen (for example) if the parameter is never
6974      referenced within the function.
6975
6976      We could just generate a location descriptor here for all non-NULL
6977      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
6978      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
6979      where DECL_RTL is NULL or is a pseudo-reg.
6980
6981      Note however that we can only get away with using DECL_INCOMING_RTL as
6982      a backup substitute for DECL_RTL in certain limited cases.  In cases
6983      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
6984      we can be sure that the parameter was passed using the same type as it is
6985      declared to have within the function, and that its DECL_INCOMING_RTL
6986      points us to a place where a value of that type is passed.
6987
6988      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
6989      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
6990      because in these cases DECL_INCOMING_RTL points us to a value of some
6991      type which is *different* from the type of the parameter itself.  Thus,
6992      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
6993      such cases, the debugger would end up (for example) trying to fetch a
6994      `float' from a place which actually contains the first part of a
6995      `double'.  That would lead to really incorrect and confusing
6996      output at debug-time.
6997
6998      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
6999      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
7000      are a couple of exceptions however.  On little-endian machines we can
7001      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
7002      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
7003      an integral type that is smaller than TREE_TYPE (decl). These cases arise
7004      when (on a little-endian machine) a non-prototyped function has a
7005      parameter declared to be of type `short' or `char'.  In such cases,
7006      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
7007      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
7008      passed `int' value.  If the debugger then uses that address to fetch
7009      a `short' or a `char' (on a little-endian machine) the result will be
7010      the correct data, so we allow for such exceptional cases below.
7011
7012      Note that our goal here is to describe the place where the given formal
7013      parameter lives during most of the function's activation (i.e. between
7014      the end of the prologue and the start of the epilogue).  We'll do that
7015      as best as we can. Note however that if the given formal parameter is
7016      modified sometime during the execution of the function, then a stack
7017      backtrace (at debug-time) will show the function as having been
7018      called with the *new* value rather than the value which was
7019      originally passed in.  This happens rarely enough that it is not
7020      a major problem, but it *is* a problem, and I'd like to fix it.
7021
7022      A future version of dwarf2out.c may generate two additional
7023      attributes for any given DW_TAG_formal_parameter DIE which will
7024      describe the "passed type" and the "passed location" for the
7025      given formal parameter in addition to the attributes we now
7026      generate to indicate the "declared type" and the "active
7027      location" for each parameter.  This additional set of attributes
7028      could be used by debuggers for stack backtraces. Separately, note
7029      that sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be
7030      NULL also.  This happens (for example) for inlined-instances of
7031      inline function formal parameters which are never referenced.
7032      This really shouldn't be happening.  All PARM_DECL nodes should
7033      get valid non-NULL DECL_INCOMING_RTL values, but integrate.c
7034      doesn't currently generate these values for inlined instances of
7035      inline function parameters, so when we see such cases, we are
7036      just out-of-luck for the time being (until integrate.c
7037      gets fixed).  */
7038
7039   /* Use DECL_RTL as the "location" unless we find something better.  */
7040   rtl = DECL_RTL (decl);
7041
7042   if (TREE_CODE (decl) == PARM_DECL)
7043     {
7044       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
7045         {
7046           declared_type = type_main_variant (TREE_TYPE (decl));
7047           passed_type = type_main_variant (DECL_ARG_TYPE (decl));
7048
7049           /* This decl represents a formal parameter which was optimized out.
7050              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
7051              all* cases where (rtl == NULL_RTX) just below.  */
7052           if (declared_type == passed_type)
7053             rtl = DECL_INCOMING_RTL (decl);
7054           else if (! BYTES_BIG_ENDIAN
7055                    && TREE_CODE (declared_type) == INTEGER_TYPE
7056                    && TYPE_SIZE (declared_type) <= TYPE_SIZE (passed_type))
7057                 rtl = DECL_INCOMING_RTL (decl);
7058         }
7059     }
7060
7061   if (rtl == NULL_RTX)
7062     return;
7063
7064   rtl = eliminate_regs (rtl, 0, NULL_RTX);
7065 #ifdef LEAF_REG_REMAP
7066   if (leaf_function)
7067     leaf_renumber_regs_insn (rtl);
7068 #endif
7069
7070   switch (GET_CODE (rtl))
7071     {
7072     case ADDRESSOF:
7073       /* The address of a variable that was optimized away; don't emit
7074          anything.  */
7075       break;
7076
7077     case CONST_INT:
7078     case CONST_DOUBLE:
7079     case CONST_STRING:
7080     case SYMBOL_REF:
7081     case LABEL_REF:
7082     case CONST:
7083     case PLUS:
7084       /* DECL_RTL could be (plus (reg ...) (const_int ...)) */
7085       add_const_value_attribute (die, rtl);
7086       break;
7087
7088     case MEM:
7089     case REG:
7090     case SUBREG:
7091     case CONCAT:
7092       add_AT_location_description (die, DW_AT_location, rtl);
7093       break;
7094
7095     default:
7096       abort ();
7097     }
7098 }
7099
7100 /* Generate an DW_AT_name attribute given some string value to be included as
7101    the value of the attribute.  */
7102
7103 static inline void
7104 add_name_attribute (die, name_string)
7105      register dw_die_ref die;
7106      register char *name_string;
7107 {
7108   if (name_string != NULL && *name_string != 0)
7109     add_AT_string (die, DW_AT_name, name_string);
7110 }
7111
7112 /* Given a tree node describing an array bound (either lower or upper) output
7113    a representation for that bound.  */
7114
7115 static void
7116 add_bound_info (subrange_die, bound_attr, bound)
7117      register dw_die_ref subrange_die;
7118      register enum dwarf_attribute bound_attr;
7119      register tree bound;
7120 {
7121   register unsigned bound_value = 0;
7122
7123   /* If this is an Ada unconstrained array type, then don't emit any debug
7124      info because the array bounds are unknown.  They are parameterized when
7125      the type is instantiated.  */
7126   if (contains_placeholder_p (bound))
7127     return;
7128
7129   switch (TREE_CODE (bound))
7130     {
7131     case ERROR_MARK:
7132       return;
7133
7134     /* All fixed-bounds are represented by INTEGER_CST nodes.        */
7135     case INTEGER_CST:
7136       bound_value = TREE_INT_CST_LOW (bound);
7137       if (bound_attr == DW_AT_lower_bound
7138           && ((is_c_family () && bound_value == 0)
7139               || (is_fortran () && bound_value == 1)))
7140         /* use the default */;
7141       else
7142         add_AT_unsigned (subrange_die, bound_attr, bound_value);
7143       break;
7144
7145     case CONVERT_EXPR:
7146     case NOP_EXPR:
7147     case NON_LVALUE_EXPR:
7148       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
7149       break;
7150       
7151     case SAVE_EXPR:
7152       /* If optimization is turned on, the SAVE_EXPRs that describe how to
7153          access the upper bound values may be bogus.  If they refer to a
7154          register, they may only describe how to get at these values at the
7155          points in the generated code right after they have just been
7156          computed.  Worse yet, in the typical case, the upper bound values
7157          will not even *be* computed in the optimized code (though the
7158          number of elements will), so these SAVE_EXPRs are entirely
7159          bogus. In order to compensate for this fact, we check here to see
7160          if optimization is enabled, and if so, we don't add an attribute
7161          for the (unknown and unknowable) upper bound.  This should not
7162          cause too much trouble for existing (stupid?)  debuggers because
7163          they have to deal with empty upper bounds location descriptions
7164          anyway in order to be able to deal with incomplete array types.
7165          Of course an intelligent debugger (GDB?)  should be able to
7166          comprehend that a missing upper bound specification in a array
7167          type used for a storage class `auto' local array variable
7168          indicates that the upper bound is both unknown (at compile- time)
7169          and unknowable (at run-time) due to optimization.
7170
7171          We assume that a MEM rtx is safe because gcc wouldn't put the
7172          value there unless it was going to be used repeatedly in the
7173          function, i.e. for cleanups.  */
7174       if (! optimize || GET_CODE (SAVE_EXPR_RTL (bound)) == MEM)
7175         {
7176           register dw_die_ref ctx = lookup_decl_die (current_function_decl);
7177           register dw_die_ref decl_die = new_die (DW_TAG_variable, ctx);
7178           register rtx loc = SAVE_EXPR_RTL (bound);
7179
7180           /* If the RTL for the SAVE_EXPR is memory, handle the case where
7181              it references an outer function's frame.  */
7182
7183           if (GET_CODE (loc) == MEM)
7184             {
7185               rtx new_addr = fix_lexical_addr (XEXP (loc, 0), bound);
7186
7187               if (XEXP (loc, 0) != new_addr)
7188                 loc = gen_rtx (MEM, GET_MODE (loc), new_addr);
7189             }
7190
7191           add_AT_flag (decl_die, DW_AT_artificial, 1);
7192           add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
7193           add_AT_location_description (decl_die, DW_AT_location, loc);
7194           add_AT_die_ref (subrange_die, bound_attr, decl_die);
7195         }
7196
7197       /* Else leave out the attribute.  */
7198       break;
7199
7200     case MAX_EXPR:
7201     case VAR_DECL:
7202     case COMPONENT_REF:
7203       /* ??? These types of bounds can be created by the Ada front end,
7204          and it isn't clear how to emit debug info for them.  */
7205       break;
7206
7207     default:
7208       abort ();
7209     }
7210 }
7211
7212 /* Note that the block of subscript information for an array type also
7213    includes information about the element type of type given array type.  */
7214
7215 static void
7216 add_subscript_info (type_die, type)
7217      register dw_die_ref type_die;
7218      register tree type;
7219 {
7220 #ifndef MIPS_DEBUGGING_INFO
7221   register unsigned dimension_number;
7222 #endif
7223   register tree lower, upper;
7224   register dw_die_ref subrange_die;
7225
7226   /* The GNU compilers represent multidimensional array types as sequences of 
7227      one dimensional array types whose element types are themselves array
7228      types.  Here we squish that down, so that each multidimensional array
7229      type gets only one array_type DIE in the Dwarf debugging info. The draft 
7230      Dwarf specification say that we are allowed to do this kind of
7231      compression in C (because there is no difference between an array or
7232      arrays and a multidimensional array in C) but for other source languages 
7233      (e.g. Ada) we probably shouldn't do this.  */
7234
7235   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
7236      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
7237      We work around this by disabling this feature.  See also
7238      gen_array_type_die.  */
7239 #ifndef MIPS_DEBUGGING_INFO
7240   for (dimension_number = 0;
7241        TREE_CODE (type) == ARRAY_TYPE;
7242        type = TREE_TYPE (type), dimension_number++)
7243     {
7244 #endif
7245       register tree domain = TYPE_DOMAIN (type);
7246
7247       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
7248          and (in GNU C only) variable bounds.  Handle all three forms 
7249          here.  */
7250       subrange_die = new_die (DW_TAG_subrange_type, type_die);
7251       if (domain)
7252         {
7253           /* We have an array type with specified bounds.  */
7254           lower = TYPE_MIN_VALUE (domain);
7255           upper = TYPE_MAX_VALUE (domain);
7256
7257           /* define the index type.  */
7258           if (TREE_TYPE (domain))
7259             {
7260               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
7261                  TREE_TYPE field.  We can't emit debug info for this
7262                  because it is an unnamed integral type.  */
7263               if (TREE_CODE (domain) == INTEGER_TYPE
7264                   && TYPE_NAME (domain) == NULL_TREE
7265                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
7266                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
7267                 ;       
7268               else
7269                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
7270                                     type_die);
7271             }
7272
7273           /* ??? If upper is NULL, the array has unspecified length,
7274              but it does have a lower bound.  This happens with Fortran
7275                dimension arr(N:*)
7276              Since the debugger is definitely going to need to know N
7277              to produce useful results, go ahead and output the lower
7278              bound solo, and hope the debugger can cope.  */
7279
7280           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
7281           if (upper)
7282             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
7283         }
7284       else
7285         /* We have an array type with an unspecified length.  The DWARF-2
7286              spec does not say how to handle this; let's just leave out the
7287              bounds.  */
7288         {;}
7289       
7290
7291 #ifndef MIPS_DEBUGGING_INFO
7292     }
7293 #endif
7294 }
7295
7296 static void
7297 add_byte_size_attribute (die, tree_node)
7298      dw_die_ref die;
7299      register tree tree_node;
7300 {
7301   register unsigned size;
7302
7303   switch (TREE_CODE (tree_node))
7304     {
7305     case ERROR_MARK:
7306       size = 0;
7307       break;
7308     case ENUMERAL_TYPE:
7309     case RECORD_TYPE:
7310     case UNION_TYPE:
7311     case QUAL_UNION_TYPE:
7312       size = int_size_in_bytes (tree_node);
7313       break;
7314     case FIELD_DECL:
7315       /* For a data member of a struct or union, the DW_AT_byte_size is
7316          generally given as the number of bytes normally allocated for an
7317          object of the *declared* type of the member itself.  This is true
7318          even for bit-fields.  */
7319       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
7320       break;
7321     default:
7322       abort ();
7323     }
7324
7325   /* Note that `size' might be -1 when we get to this point.  If it is, that
7326      indicates that the byte size of the entity in question is variable.  We
7327      have no good way of expressing this fact in Dwarf at the present time,
7328      so just let the -1 pass on through.  */
7329
7330   add_AT_unsigned (die, DW_AT_byte_size, size);
7331 }
7332
7333 /* For a FIELD_DECL node which represents a bit-field, output an attribute
7334    which specifies the distance in bits from the highest order bit of the
7335    "containing object" for the bit-field to the highest order bit of the
7336    bit-field itself.
7337
7338    For any given bit-field, the "containing object" is a hypothetical
7339    object (of some integral or enum type) within which the given bit-field
7340    lives.  The type of this hypothetical "containing object" is always the
7341    same as the declared type of the individual bit-field itself.  The
7342    determination of the exact location of the "containing object" for a
7343    bit-field is rather complicated.  It's handled by the
7344    `field_byte_offset' function (above).
7345
7346    Note that it is the size (in bytes) of the hypothetical "containing object"
7347    which will be given in the DW_AT_byte_size attribute for this bit-field.
7348    (See `byte_size_attribute' above).  */
7349
7350 static inline void
7351 add_bit_offset_attribute (die, decl)
7352      register dw_die_ref die;
7353      register tree decl;
7354 {
7355   register unsigned object_offset_in_bytes = field_byte_offset (decl);
7356   register tree type = DECL_BIT_FIELD_TYPE (decl);
7357   register tree bitpos_tree = DECL_FIELD_BITPOS (decl);
7358   register unsigned bitpos_int;
7359   register unsigned highest_order_object_bit_offset;
7360   register unsigned highest_order_field_bit_offset;
7361   register unsigned bit_offset;
7362
7363   /* Must be a field and a bit field.  */
7364   if (!type
7365       || TREE_CODE (decl) != FIELD_DECL)
7366     abort ();
7367
7368   /* We can't yet handle bit-fields whose offsets are variable, so if we
7369      encounter such things, just return without generating any attribute
7370      whatsoever.  */
7371   if (TREE_CODE (bitpos_tree) != INTEGER_CST)
7372     return;
7373
7374   bitpos_int = (unsigned) TREE_INT_CST_LOW (bitpos_tree);
7375
7376   /* Note that the bit offset is always the distance (in bits) from the
7377      highest-order bit of the "containing object" to the highest-order bit of 
7378      the bit-field itself.  Since the "high-order end" of any object or field 
7379      is different on big-endian and little-endian machines, the computation
7380      below must take account of these differences.  */
7381   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
7382   highest_order_field_bit_offset = bitpos_int;
7383
7384   if (! BYTES_BIG_ENDIAN)
7385     {
7386       highest_order_field_bit_offset
7387         += (unsigned) TREE_INT_CST_LOW (DECL_SIZE (decl));
7388
7389       highest_order_object_bit_offset += simple_type_size_in_bits (type);
7390     }
7391
7392   bit_offset
7393     = (! BYTES_BIG_ENDIAN
7394        ? highest_order_object_bit_offset - highest_order_field_bit_offset
7395        : highest_order_field_bit_offset - highest_order_object_bit_offset);
7396
7397   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
7398 }
7399
7400 /* For a FIELD_DECL node which represents a bit field, output an attribute
7401    which specifies the length in bits of the given field.  */
7402
7403 static inline void
7404 add_bit_size_attribute (die, decl)
7405      register dw_die_ref die;
7406      register tree decl;
7407 {
7408   /* Must be a field and a bit field.  */
7409   if (TREE_CODE (decl) != FIELD_DECL
7410       || ! DECL_BIT_FIELD_TYPE (decl))
7411     abort ();
7412   add_AT_unsigned (die, DW_AT_bit_size,
7413                    (unsigned) TREE_INT_CST_LOW (DECL_SIZE (decl)));
7414 }
7415
7416 /* If the compiled language is ANSI C, then add a 'prototyped'
7417    attribute, if arg types are given for the parameters of a function.  */
7418
7419 static inline void
7420 add_prototyped_attribute (die, func_type)
7421      register dw_die_ref die;
7422      register tree func_type;
7423 {
7424   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
7425       && TYPE_ARG_TYPES (func_type) != NULL)
7426     add_AT_flag (die, DW_AT_prototyped, 1);
7427 }
7428
7429
7430 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
7431    by looking in either the type declaration or object declaration
7432    equate table.  */
7433
7434 static inline void
7435 add_abstract_origin_attribute (die, origin)
7436      register dw_die_ref die;
7437      register tree origin;
7438 {
7439   dw_die_ref origin_die = NULL;
7440   if (TREE_CODE_CLASS (TREE_CODE (origin)) == 'd')
7441     origin_die = lookup_decl_die (origin);
7442   else if (TREE_CODE_CLASS (TREE_CODE (origin)) == 't')
7443     origin_die = lookup_type_die (origin);
7444
7445   add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
7446 }
7447
7448 /* We do not currently support the pure_virtual attribute.  */
7449
7450 static inline void
7451 add_pure_or_virtual_attribute (die, func_decl)
7452      register dw_die_ref die;
7453      register tree func_decl;
7454 {
7455   if (DECL_VINDEX (func_decl))
7456     {
7457       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
7458       add_AT_loc (die, DW_AT_vtable_elem_location,
7459                   new_loc_descr (DW_OP_constu,
7460                                  TREE_INT_CST_LOW (DECL_VINDEX (func_decl)),
7461                                  0));
7462
7463       /* GNU extension: Record what type this method came from originally.  */
7464       if (debug_info_level > DINFO_LEVEL_TERSE)
7465         add_AT_die_ref (die, DW_AT_containing_type,
7466                         lookup_type_die (DECL_CONTEXT (func_decl)));
7467     }
7468 }
7469 \f
7470 /* Add source coordinate attributes for the given decl.  */
7471
7472 static void
7473 add_src_coords_attributes (die, decl)
7474      register dw_die_ref die;
7475      register tree decl;
7476 {
7477   register unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
7478
7479   add_AT_unsigned (die, DW_AT_decl_file, file_index);
7480   add_AT_unsigned (die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
7481 }
7482
7483 /* Add an DW_AT_name attribute and source coordinate attribute for the
7484    given decl, but only if it actually has a name.  */
7485
7486 static void
7487 add_name_and_src_coords_attributes (die, decl)
7488      register dw_die_ref die;
7489      register tree decl;
7490 {
7491   register tree decl_name;
7492
7493   decl_name = DECL_NAME (decl); 
7494   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
7495     {
7496       add_name_attribute (die, dwarf2_name (decl, 0));
7497       add_src_coords_attributes (die, decl);
7498       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
7499           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
7500         add_AT_string (die, DW_AT_MIPS_linkage_name,
7501                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
7502     }
7503 }
7504
7505 /* Push a new declaration scope. */
7506
7507 static void
7508 push_decl_scope (scope)
7509      tree scope;
7510 {
7511   tree containing_scope;
7512   int i;
7513
7514   /* Make room in the decl_scope_table, if necessary.  */
7515   if (decl_scope_table_allocated == decl_scope_depth)
7516     {
7517       decl_scope_table_allocated += DECL_SCOPE_TABLE_INCREMENT;
7518       decl_scope_table
7519         = (decl_scope_node *) xrealloc (decl_scope_table,
7520                                         (decl_scope_table_allocated
7521                                          * sizeof (decl_scope_node)));
7522     }
7523
7524   decl_scope_table[decl_scope_depth].scope = scope;
7525
7526   /* Sometimes, while recursively emitting subtypes within a class type,
7527      we end up recuring on a subtype at a higher level then the current
7528      subtype.  In such a case, we need to search the decl_scope_table to
7529      find the parent of this subtype.  */
7530
7531   if (TREE_CODE_CLASS (TREE_CODE (scope)) == 't')
7532     containing_scope = TYPE_CONTEXT (scope);
7533   else
7534     containing_scope = NULL_TREE;
7535
7536   /* The normal case.  */
7537   if (decl_scope_depth == 0
7538       || containing_scope == NULL_TREE
7539       /* Ignore namespaces for the moment.  */
7540       || TREE_CODE (containing_scope) == NAMESPACE_DECL
7541       || containing_scope == decl_scope_table[decl_scope_depth - 1].scope)
7542     decl_scope_table[decl_scope_depth].previous = decl_scope_depth - 1;
7543   else
7544     {
7545       /* We need to search for the containing_scope.  */
7546       for (i = 0; i < decl_scope_depth; i++)
7547         if (decl_scope_table[i].scope == containing_scope)
7548           break;
7549
7550       if (i == decl_scope_depth)
7551         abort ();
7552       else
7553         decl_scope_table[decl_scope_depth].previous = i;
7554     }
7555
7556   decl_scope_depth++;
7557 }
7558
7559 /* Return the DIE for the scope that immediately contains this declaration.  */
7560
7561 static dw_die_ref
7562 scope_die_for (t, context_die)
7563     register tree t; 
7564     register dw_die_ref context_die;
7565 {
7566   register dw_die_ref scope_die = NULL;
7567   register tree containing_scope;
7568   register int i;
7569
7570   /* Walk back up the declaration tree looking for a place to define
7571      this type.  */
7572   if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
7573     containing_scope = TYPE_CONTEXT (t);
7574   else if (TREE_CODE (t) == FUNCTION_DECL && DECL_VINDEX (t))
7575     containing_scope = decl_class_context (t);
7576   else
7577     containing_scope = DECL_CONTEXT (t);
7578
7579   /* Ignore namespaces for the moment.  */
7580   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
7581     containing_scope = NULL_TREE;
7582
7583   /* Function-local tags and functions get stuck in limbo until they are
7584      fixed up by decls_for_scope.  */
7585   if (context_die == NULL && containing_scope != NULL_TREE
7586       && (TREE_CODE (t) == FUNCTION_DECL || is_tagged_type (t)))
7587     return NULL;
7588
7589   if (containing_scope == NULL_TREE)
7590     scope_die = comp_unit_die;
7591   else
7592     {
7593       for (i = decl_scope_depth - 1, scope_die = context_die;
7594            i >= 0 && decl_scope_table[i].scope != containing_scope;
7595            (scope_die = scope_die->die_parent,
7596             i = decl_scope_table[i].previous))
7597         ;
7598
7599       /* ??? Integrate_decl_tree does not handle BLOCK_TYPE_TAGS, nor
7600          does it try to handle types defined by TYPE_DECLs.  Such types
7601          thus have an incorrect TYPE_CONTEXT, which points to the block
7602          they were originally defined in, instead of the current block
7603          created by function inlining.  We try to detect that here and
7604          work around it.  */
7605
7606       if (i < 0 && scope_die == comp_unit_die
7607           && TREE_CODE (containing_scope) == BLOCK
7608           && is_tagged_type (t)
7609           && (block_ultimate_origin (decl_scope_table[decl_scope_depth - 1].scope)
7610               == containing_scope))
7611         {
7612           scope_die = context_die;
7613           /* Since the checks below are no longer applicable.  */
7614           i = 0;
7615         }
7616
7617       if (i < 0)
7618         {
7619           if (scope_die != comp_unit_die
7620               || TREE_CODE_CLASS (TREE_CODE (containing_scope)) != 't')
7621             abort ();
7622           if (debug_info_level > DINFO_LEVEL_TERSE
7623               && !TREE_ASM_WRITTEN (containing_scope))
7624             abort ();
7625         }
7626     }
7627
7628   return scope_die;
7629 }
7630
7631 /* Pop a declaration scope.  */
7632 static inline void
7633 pop_decl_scope ()
7634 {
7635   if (decl_scope_depth <= 0)
7636     abort ();
7637   --decl_scope_depth;
7638 }
7639
7640 /* Many forms of DIEs require a "type description" attribute.  This
7641    routine locates the proper "type descriptor" die for the type given
7642    by 'type', and adds an DW_AT_type attribute below the given die.  */
7643
7644 static void
7645 add_type_attribute (object_die, type, decl_const, decl_volatile, context_die)
7646      register dw_die_ref object_die;
7647      register tree type;
7648      register int decl_const;
7649      register int decl_volatile;
7650      register dw_die_ref context_die;
7651 {
7652   register enum tree_code code  = TREE_CODE (type);
7653   register dw_die_ref type_die  = NULL;
7654
7655   /* ??? If this type is an unnamed subrange type of an integral or
7656      floating-point type, use the inner type.  This is because we have no
7657      support for unnamed types in base_type_die.  This can happen if this is
7658      an Ada subrange type.  Correct solution is emit a subrange type die.  */
7659   if ((code == INTEGER_TYPE || code == REAL_TYPE)
7660       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
7661     type = TREE_TYPE (type), code = TREE_CODE (type);
7662
7663   if (code == ERROR_MARK)
7664     return;
7665
7666   /* Handle a special case.  For functions whose return type is void, we
7667      generate *no* type attribute.  (Note that no object may have type
7668      `void', so this only applies to function return types).  */
7669   if (code == VOID_TYPE)
7670     return;
7671
7672   type_die = modified_type_die (type,
7673                                 decl_const || TYPE_READONLY (type),
7674                                 decl_volatile || TYPE_VOLATILE (type),
7675                                 context_die);
7676   if (type_die != NULL)
7677     add_AT_die_ref (object_die, DW_AT_type, type_die);
7678 }
7679
7680 /* Given a tree pointer to a struct, class, union, or enum type node, return
7681    a pointer to the (string) tag name for the given type, or zero if the type
7682    was declared without a tag.  */
7683
7684 static char *
7685 type_tag (type)
7686      register tree type;
7687 {
7688   register char *name = 0;
7689
7690   if (TYPE_NAME (type) != 0)
7691     {
7692       register tree t = 0;
7693
7694       /* Find the IDENTIFIER_NODE for the type name.  */
7695       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
7696         t = TYPE_NAME (type);
7697
7698       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to 
7699          a TYPE_DECL node, regardless of whether or not a `typedef' was
7700          involved.  */
7701       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
7702                && ! DECL_IGNORED_P (TYPE_NAME (type)))
7703         t = DECL_NAME (TYPE_NAME (type));
7704
7705       /* Now get the name as a string, or invent one.  */
7706       if (t != 0)
7707         name = IDENTIFIER_POINTER (t);
7708     }
7709
7710   return (name == 0 || *name == '\0') ? 0 : name;
7711 }
7712
7713 /* Return the type associated with a data member, make a special check
7714    for bit field types.  */
7715
7716 static inline tree
7717 member_declared_type (member)
7718      register tree member;
7719 {
7720   return (DECL_BIT_FIELD_TYPE (member)
7721           ? DECL_BIT_FIELD_TYPE (member)
7722           : TREE_TYPE (member));
7723 }
7724
7725 /* Get the decl's label, as described by its RTL. This may be different
7726    from the DECL_NAME name used in the source file.  */
7727
7728 static char *
7729 decl_start_label (decl)
7730      register tree decl;
7731 {
7732   rtx x;
7733   char *fnname;
7734   x = DECL_RTL (decl);
7735   if (GET_CODE (x) != MEM)
7736     abort ();
7737
7738   x = XEXP (x, 0);
7739   if (GET_CODE (x) != SYMBOL_REF)
7740     abort ();
7741
7742   fnname = XSTR (x, 0);
7743   return fnname;
7744 }
7745 \f
7746 /* These routines generate the internal representation of the DIE's for
7747    the compilation unit.  Debugging information is collected by walking
7748    the declaration trees passed in from dwarf2out_decl().  */
7749
7750 static void
7751 gen_array_type_die (type, context_die)
7752      register tree type;
7753      register dw_die_ref context_die;
7754 {
7755   register dw_die_ref scope_die = scope_die_for (type, context_die);
7756   register dw_die_ref array_die;
7757   register tree element_type;
7758
7759   /* ??? The SGI dwarf reader fails for array of array of enum types unless
7760      the inner array type comes before the outer array type.  Thus we must
7761      call gen_type_die before we call new_die.  See below also.  */
7762 #ifdef MIPS_DEBUGGING_INFO
7763   gen_type_die (TREE_TYPE (type), context_die);
7764 #endif
7765
7766   array_die = new_die (DW_TAG_array_type, scope_die);
7767
7768 #if 0
7769   /* We default the array ordering.  SDB will probably do
7770      the right things even if DW_AT_ordering is not present.  It's not even
7771      an issue until we start to get into multidimensional arrays anyway.  If
7772      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
7773      then we'll have to put the DW_AT_ordering attribute back in.  (But if
7774      and when we find out that we need to put these in, we will only do so
7775      for multidimensional arrays.  */
7776   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
7777 #endif
7778
7779 #ifdef MIPS_DEBUGGING_INFO
7780   /* The SGI compilers handle arrays of unknown bound by setting
7781      AT_declaration and not emitting any subrange DIEs.  */
7782   if (! TYPE_DOMAIN (type))
7783     add_AT_unsigned (array_die, DW_AT_declaration, 1);
7784   else
7785 #endif
7786     add_subscript_info (array_die, type);
7787
7788   equate_type_number_to_die (type, array_die);
7789
7790   /* Add representation of the type of the elements of this array type.  */
7791   element_type = TREE_TYPE (type);
7792
7793   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
7794      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
7795      We work around this by disabling this feature.  See also
7796      add_subscript_info.  */
7797 #ifndef MIPS_DEBUGGING_INFO
7798   while (TREE_CODE (element_type) == ARRAY_TYPE)
7799     element_type = TREE_TYPE (element_type);
7800
7801   gen_type_die (element_type, context_die);
7802 #endif
7803
7804   add_type_attribute (array_die, element_type, 0, 0, context_die);
7805 }
7806
7807 static void
7808 gen_set_type_die (type, context_die)
7809      register tree type;
7810      register dw_die_ref context_die;
7811 {
7812   register dw_die_ref type_die
7813     = new_die (DW_TAG_set_type, scope_die_for (type, context_die));
7814
7815   equate_type_number_to_die (type, type_die);
7816   add_type_attribute (type_die, TREE_TYPE (type), 0, 0, context_die);
7817 }
7818
7819 static void
7820 gen_entry_point_die (decl, context_die)
7821      register tree decl;
7822      register dw_die_ref context_die;
7823 {
7824   register tree origin = decl_ultimate_origin (decl);
7825   register dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die);
7826   if (origin != NULL)
7827     add_abstract_origin_attribute (decl_die, origin);
7828   else
7829     {
7830       add_name_and_src_coords_attributes (decl_die, decl);
7831       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
7832                           0, 0, context_die);
7833     }
7834
7835   if (DECL_ABSTRACT (decl))
7836     equate_decl_number_to_die (decl, decl_die);
7837   else
7838     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
7839 }
7840
7841 /* Remember a type in the pending_types_list.  */
7842
7843 static void
7844 pend_type (type)
7845      register tree type;
7846 {
7847   if (pending_types == pending_types_allocated)
7848     {
7849       pending_types_allocated += PENDING_TYPES_INCREMENT;
7850       pending_types_list
7851         = (tree *) xrealloc (pending_types_list,
7852                              sizeof (tree) * pending_types_allocated);
7853     }
7854
7855   pending_types_list[pending_types++] = type;
7856 }
7857
7858 /* Output any pending types (from the pending_types list) which we can output
7859    now (taking into account the scope that we are working on now).
7860
7861    For each type output, remove the given type from the pending_types_list
7862    *before* we try to output it.  */
7863
7864 static void
7865 output_pending_types_for_scope (context_die)
7866      register dw_die_ref context_die;
7867 {
7868   register tree type;
7869
7870   while (pending_types)
7871     {
7872       --pending_types;
7873       type = pending_types_list[pending_types];
7874       gen_type_die (type, context_die);
7875       if (!TREE_ASM_WRITTEN (type))
7876         abort ();
7877     }
7878 }
7879
7880 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
7881
7882 static void
7883 gen_inlined_enumeration_type_die (type, context_die)
7884      register tree type;
7885      register dw_die_ref context_die;
7886 {
7887   register dw_die_ref type_die = new_die (DW_TAG_enumeration_type,
7888                                           scope_die_for (type, context_die));
7889
7890   if (!TREE_ASM_WRITTEN (type))
7891     abort ();
7892   add_abstract_origin_attribute (type_die, type);
7893 }
7894
7895 /* Generate a DIE to represent an inlined instance of a structure type.  */
7896
7897 static void
7898 gen_inlined_structure_type_die (type, context_die)
7899      register tree type;
7900      register dw_die_ref context_die;
7901 {
7902   register dw_die_ref type_die = new_die (DW_TAG_structure_type,
7903                                           scope_die_for (type, context_die));
7904
7905   if (!TREE_ASM_WRITTEN (type))
7906     abort ();
7907   add_abstract_origin_attribute (type_die, type);
7908 }
7909
7910 /* Generate a DIE to represent an inlined instance of a union type.  */
7911
7912 static void
7913 gen_inlined_union_type_die (type, context_die)
7914      register tree type;
7915      register dw_die_ref context_die;
7916 {
7917   register dw_die_ref type_die = new_die (DW_TAG_union_type,
7918                                           scope_die_for (type, context_die));
7919
7920   if (!TREE_ASM_WRITTEN (type))
7921     abort ();
7922   add_abstract_origin_attribute (type_die, type);
7923 }
7924
7925 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
7926    include all of the information about the enumeration values also. Each
7927    enumerated type name/value is listed as a child of the enumerated type
7928    DIE.  */
7929
7930 static void
7931 gen_enumeration_type_die (type, context_die)
7932      register tree type;
7933      register dw_die_ref context_die;
7934 {
7935   register dw_die_ref type_die = lookup_type_die (type);
7936
7937   if (type_die == NULL)
7938     {
7939       type_die = new_die (DW_TAG_enumeration_type,
7940                           scope_die_for (type, context_die));
7941       equate_type_number_to_die (type, type_die);
7942       add_name_attribute (type_die, type_tag (type));
7943     }
7944   else if (! TYPE_SIZE (type))
7945     return;
7946   else
7947     remove_AT (type_die, DW_AT_declaration);
7948
7949   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
7950      given enum type is incomplete, do not generate the DW_AT_byte_size
7951      attribute or the DW_AT_element_list attribute.  */
7952   if (TYPE_SIZE (type))
7953     {
7954       register tree link;
7955
7956       TREE_ASM_WRITTEN (type) = 1;
7957       add_byte_size_attribute (type_die, type);
7958       if (TYPE_STUB_DECL (type) != NULL_TREE)
7959         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
7960
7961       /* If the first reference to this type was as the return type of an
7962          inline function, then it may not have a parent.  Fix this now.  */
7963       if (type_die->die_parent == NULL)
7964         add_child_die (scope_die_for (type, context_die), type_die);
7965
7966       for (link = TYPE_FIELDS (type);
7967            link != NULL; link = TREE_CHAIN (link))
7968         {
7969           register dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die);
7970
7971           add_name_attribute (enum_die,
7972                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
7973           add_AT_unsigned (enum_die, DW_AT_const_value,
7974                            (unsigned) TREE_INT_CST_LOW (TREE_VALUE (link)));
7975         }
7976     }
7977   else
7978     add_AT_flag (type_die, DW_AT_declaration, 1);
7979 }
7980
7981
7982 /* Generate a DIE to represent either a real live formal parameter decl or to
7983    represent just the type of some formal parameter position in some function
7984    type.
7985
7986    Note that this routine is a bit unusual because its argument may be a
7987    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
7988    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
7989    node.  If it's the former then this function is being called to output a
7990    DIE to represent a formal parameter object (or some inlining thereof).  If
7991    it's the latter, then this function is only being called to output a
7992    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
7993    argument type of some subprogram type.  */
7994
7995 static dw_die_ref
7996 gen_formal_parameter_die (node, context_die)
7997      register tree node;
7998      register dw_die_ref context_die;
7999 {
8000   register dw_die_ref parm_die
8001     = new_die (DW_TAG_formal_parameter, context_die);
8002   register tree origin;
8003
8004   switch (TREE_CODE_CLASS (TREE_CODE (node)))
8005     {
8006     case 'd':
8007       origin = decl_ultimate_origin (node);
8008       if (origin != NULL)
8009         add_abstract_origin_attribute (parm_die, origin);
8010       else
8011         {
8012           add_name_and_src_coords_attributes (parm_die, node);
8013           add_type_attribute (parm_die, TREE_TYPE (node),
8014                               TREE_READONLY (node),
8015                               TREE_THIS_VOLATILE (node),
8016                               context_die);
8017           if (DECL_ARTIFICIAL (node))
8018             add_AT_flag (parm_die, DW_AT_artificial, 1);
8019         }
8020
8021       equate_decl_number_to_die (node, parm_die);
8022       if (! DECL_ABSTRACT (node))
8023         add_location_or_const_value_attribute (parm_die, node);
8024
8025       break;
8026
8027     case 't':
8028       /* We were called with some kind of a ..._TYPE node.  */
8029       add_type_attribute (parm_die, node, 0, 0, context_die);
8030       break;
8031
8032     default:
8033       abort ();
8034     }
8035
8036   return parm_die;
8037 }
8038
8039 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
8040    at the end of an (ANSI prototyped) formal parameters list.  */
8041
8042 static void
8043 gen_unspecified_parameters_die (decl_or_type, context_die)
8044      register tree decl_or_type;
8045      register dw_die_ref context_die;
8046 {
8047   register dw_die_ref parm_die = new_die (DW_TAG_unspecified_parameters,
8048                                           context_die);
8049 }
8050
8051 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
8052    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
8053    parameters as specified in some function type specification (except for
8054    those which appear as part of a function *definition*).
8055
8056    Note we must be careful here to output all of the parameter DIEs before*
8057    we output any DIEs needed to represent the types of the formal parameters.
8058    This keeps svr4 SDB happy because it (incorrectly) thinks that the first
8059    non-parameter DIE it sees ends the formal parameter list.  */
8060
8061 static void
8062 gen_formal_types_die (function_or_method_type, context_die)
8063      register tree function_or_method_type;
8064      register dw_die_ref context_die;
8065 {
8066   register tree link;
8067   register tree formal_type = NULL;
8068   register tree first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
8069
8070 #if 0
8071   /* In the case where we are generating a formal types list for a C++
8072      non-static member function type, skip over the first thing on the
8073      TYPE_ARG_TYPES list because it only represents the type of the hidden
8074      `this pointer'.  The debugger should be able to figure out (without
8075      being explicitly told) that this non-static member function type takes a 
8076      `this pointer' and should be able to figure what the type of that hidden 
8077      parameter is from the DW_AT_member attribute of the parent
8078      DW_TAG_subroutine_type DIE.  */
8079   if (TREE_CODE (function_or_method_type) == METHOD_TYPE)
8080     first_parm_type = TREE_CHAIN (first_parm_type);
8081 #endif
8082
8083   /* Make our first pass over the list of formal parameter types and output a 
8084      DW_TAG_formal_parameter DIE for each one.  */
8085   for (link = first_parm_type; link; link = TREE_CHAIN (link))
8086     {
8087       register dw_die_ref parm_die;
8088       
8089       formal_type = TREE_VALUE (link);
8090       if (formal_type == void_type_node)
8091         break;
8092
8093       /* Output a (nameless) DIE to represent the formal parameter itself.  */
8094       parm_die = gen_formal_parameter_die (formal_type, context_die);
8095       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
8096           && link == first_parm_type)
8097         add_AT_flag (parm_die, DW_AT_artificial, 1);
8098     }
8099
8100   /* If this function type has an ellipsis, add a
8101      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
8102   if (formal_type != void_type_node)
8103     gen_unspecified_parameters_die (function_or_method_type, context_die);
8104
8105   /* Make our second (and final) pass over the list of formal parameter types 
8106      and output DIEs to represent those types (as necessary).  */
8107   for (link = TYPE_ARG_TYPES (function_or_method_type);
8108        link;
8109        link = TREE_CHAIN (link))
8110     {
8111       formal_type = TREE_VALUE (link);
8112       if (formal_type == void_type_node)
8113         break;
8114
8115       gen_type_die (formal_type, context_die);
8116     }
8117 }
8118
8119 /* Generate a DIE to represent a declared function (either file-scope or
8120    block-local).  */
8121
8122 static void
8123 gen_subprogram_die (decl, context_die)
8124      register tree decl;
8125      register dw_die_ref context_die;
8126 {
8127   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
8128   register tree origin = decl_ultimate_origin (decl);
8129   register dw_die_ref subr_die;
8130   register rtx fp_reg;
8131   register tree fn_arg_types;
8132   register tree outer_scope;
8133   register dw_die_ref old_die = lookup_decl_die (decl);
8134   register int declaration
8135     = (current_function_decl != decl
8136        || (context_die
8137            && (context_die->die_tag == DW_TAG_structure_type
8138                || context_die->die_tag == DW_TAG_union_type)));
8139
8140   if (origin != NULL)
8141     {
8142       subr_die = new_die (DW_TAG_subprogram, context_die);
8143       add_abstract_origin_attribute (subr_die, origin);
8144     }
8145   else if (old_die && DECL_ABSTRACT (decl)
8146            && get_AT_unsigned (old_die, DW_AT_inline))
8147     {
8148       /* This must be a redefinition of an extern inline function.
8149          We can just reuse the old die here.  */
8150       subr_die = old_die;
8151
8152       /* Clear out the inlined attribute and parm types.  */
8153       remove_AT (subr_die, DW_AT_inline);
8154       remove_children (subr_die);
8155     }
8156   else if (old_die)
8157     {
8158       register unsigned file_index
8159         = lookup_filename (DECL_SOURCE_FILE (decl));
8160
8161       if (get_AT_flag (old_die, DW_AT_declaration) != 1)
8162         abort ();
8163
8164       /* If the definition comes from the same place as the declaration,
8165          maybe use the old DIE.  We always want the DIE for this function
8166          that has the *_pc attributes to be under comp_unit_die so the
8167          debugger can find it.  For inlines, that is the concrete instance,
8168          so we can use the old DIE here.  For non-inline methods, we want a
8169          specification DIE at toplevel, so we need a new DIE.  For local
8170          class methods, this does not apply.  */
8171       if ((DECL_ABSTRACT (decl) || old_die->die_parent == comp_unit_die
8172            || context_die == NULL)
8173           && get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
8174           && (get_AT_unsigned (old_die, DW_AT_decl_line)
8175               == DECL_SOURCE_LINE (decl)))
8176         {
8177           subr_die = old_die;
8178
8179           /* Clear out the declaration attribute and the parm types.  */
8180           remove_AT (subr_die, DW_AT_declaration);
8181           remove_children (subr_die);
8182         }
8183       else
8184         {
8185           subr_die = new_die (DW_TAG_subprogram, context_die);
8186           add_AT_die_ref (subr_die, DW_AT_specification, old_die);
8187           if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
8188             add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
8189           if (get_AT_unsigned (old_die, DW_AT_decl_line)
8190               != DECL_SOURCE_LINE (decl))
8191             add_AT_unsigned
8192               (subr_die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
8193         }
8194     }
8195   else
8196     {
8197       register dw_die_ref scope_die;
8198
8199       if (DECL_CONTEXT (decl))
8200         scope_die = scope_die_for (decl, context_die);
8201       else
8202         /* Don't put block extern declarations under comp_unit_die.  */
8203         scope_die = context_die;
8204
8205       subr_die = new_die (DW_TAG_subprogram, scope_die);
8206                          
8207       if (TREE_PUBLIC (decl))
8208         add_AT_flag (subr_die, DW_AT_external, 1);
8209
8210       add_name_and_src_coords_attributes (subr_die, decl);
8211       if (debug_info_level > DINFO_LEVEL_TERSE)
8212         {
8213           register tree type = TREE_TYPE (decl);
8214
8215           add_prototyped_attribute (subr_die, type);
8216           add_type_attribute (subr_die, TREE_TYPE (type), 0, 0, context_die);
8217         }
8218
8219       add_pure_or_virtual_attribute (subr_die, decl);
8220       if (DECL_ARTIFICIAL (decl))
8221         add_AT_flag (subr_die, DW_AT_artificial, 1);
8222       if (TREE_PROTECTED (decl))
8223         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
8224       else if (TREE_PRIVATE (decl))
8225         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
8226     }
8227
8228   if (declaration)
8229     {
8230       add_AT_flag (subr_die, DW_AT_declaration, 1);
8231
8232       /* The first time we see a member function, it is in the context of
8233          the class to which it belongs.  We make sure of this by emitting
8234          the class first.  The next time is the definition, which is
8235          handled above.  The two may come from the same source text.  */
8236       if (DECL_CONTEXT (decl))
8237         equate_decl_number_to_die (decl, subr_die);
8238     }
8239   else if (DECL_ABSTRACT (decl))
8240     {
8241       /* ??? Checking DECL_DEFER_OUTPUT is correct for static inline functions,
8242          but not for extern inline functions.  We can't get this completely
8243          correct because information about whether the function was declared
8244          inline is not saved anywhere.  */
8245       if (DECL_DEFER_OUTPUT (decl))
8246         {
8247           if (DECL_INLINE (decl))
8248             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
8249           else
8250             add_AT_unsigned (subr_die, DW_AT_inline,
8251                              DW_INL_declared_not_inlined);
8252         }
8253       else if (DECL_INLINE (decl))
8254         add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
8255       else
8256         abort ();
8257
8258       equate_decl_number_to_die (decl, subr_die);
8259     }
8260   else if (!DECL_EXTERNAL (decl))
8261     {
8262       if (origin == NULL_TREE)
8263         equate_decl_number_to_die (decl, subr_die);
8264
8265       ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
8266                                    current_funcdef_number);
8267       add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
8268       ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
8269                                    current_funcdef_number);
8270       add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
8271
8272       add_pubname (decl, subr_die);
8273       add_arange (decl, subr_die);
8274
8275 #ifdef MIPS_DEBUGGING_INFO
8276       /* Add a reference to the FDE for this routine.  */
8277       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
8278 #endif
8279
8280       /* Define the "frame base" location for this routine.  We use the
8281          frame pointer or stack pointer registers, since the RTL for local
8282          variables is relative to one of them.  */
8283       fp_reg
8284         = frame_pointer_needed ? hard_frame_pointer_rtx : stack_pointer_rtx;
8285       add_AT_loc (subr_die, DW_AT_frame_base, reg_loc_descriptor (fp_reg));
8286
8287 #if 0
8288       /* ??? This fails for nested inline functions, because context_display
8289          is not part of the state saved/restored for inline functions.  */
8290       if (current_function_needs_context)
8291         add_AT_location_description (subr_die, DW_AT_static_link,
8292                                      lookup_static_chain (decl));
8293 #endif
8294     }
8295
8296   /* Now output descriptions of the arguments for this function. This gets
8297      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list 
8298      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
8299      `...' at the end of the formal parameter list.  In order to find out if
8300      there was a trailing ellipsis or not, we must instead look at the type
8301      associated with the FUNCTION_DECL.  This will be a node of type
8302      FUNCTION_TYPE. If the chain of type nodes hanging off of this
8303      FUNCTION_TYPE node ends with a void_type_node then there should *not* be 
8304      an ellipsis at the end.  */
8305   push_decl_scope (decl);
8306
8307   /* In the case where we are describing a mere function declaration, all we
8308      need to do here (and all we *can* do here) is to describe the *types* of 
8309      its formal parameters.  */
8310   if (debug_info_level <= DINFO_LEVEL_TERSE)
8311     ;
8312   else if (declaration)
8313     gen_formal_types_die (TREE_TYPE (decl), subr_die);
8314   else
8315     {
8316       /* Generate DIEs to represent all known formal parameters */
8317       register tree arg_decls = DECL_ARGUMENTS (decl);
8318       register tree parm;
8319
8320       /* When generating DIEs, generate the unspecified_parameters DIE
8321          instead if we come across the arg "__builtin_va_alist" */
8322       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
8323         if (TREE_CODE (parm) == PARM_DECL)
8324           {
8325             if (DECL_NAME (parm)
8326                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
8327                             "__builtin_va_alist"))
8328               gen_unspecified_parameters_die (parm, subr_die);
8329             else
8330               gen_decl_die (parm, subr_die);
8331           }
8332
8333       /* Decide whether we need a unspecified_parameters DIE at the end.
8334          There are 2 more cases to do this for: 1) the ansi ... declaration - 
8335          this is detectable when the end of the arg list is not a
8336          void_type_node 2) an unprototyped function declaration (not a
8337          definition).  This just means that we have no info about the
8338          parameters at all.  */
8339       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
8340       if (fn_arg_types != NULL)
8341         {
8342           /* this is the prototyped case, check for ...  */
8343           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
8344             gen_unspecified_parameters_die (decl, subr_die);
8345         }
8346       else if (DECL_INITIAL (decl) == NULL_TREE)
8347         gen_unspecified_parameters_die (decl, subr_die);
8348     }
8349
8350   /* Output Dwarf info for all of the stuff within the body of the function
8351      (if it has one - it may be just a declaration).  */
8352   outer_scope = DECL_INITIAL (decl);
8353
8354   /* Note that here, `outer_scope' is a pointer to the outermost BLOCK
8355      node created to represent a function. This outermost BLOCK actually
8356      represents the outermost binding contour for the function, i.e. the
8357      contour in which the function's formal parameters and labels get
8358      declared. Curiously, it appears that the front end doesn't actually
8359      put the PARM_DECL nodes for the current function onto the BLOCK_VARS
8360      list for this outer scope.  (They are strung off of the DECL_ARGUMENTS
8361      list for the function instead.) The BLOCK_VARS list for the
8362      `outer_scope' does provide us with a list of the LABEL_DECL nodes for
8363      the function however, and we output DWARF info for those in
8364      decls_for_scope.  Just within the `outer_scope' there will be a BLOCK
8365      node representing the function's outermost pair of curly braces, and
8366      any blocks used for the base and member initializers of a C++
8367      constructor function.  */
8368   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
8369     {
8370       current_function_has_inlines = 0;
8371       decls_for_scope (outer_scope, subr_die, 0);
8372
8373 #if 0 && defined (MIPS_DEBUGGING_INFO)
8374       if (current_function_has_inlines)
8375         {
8376           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
8377           if (! comp_unit_has_inlines)
8378             {
8379               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
8380               comp_unit_has_inlines = 1;
8381             }
8382         }
8383 #endif
8384     }
8385
8386   pop_decl_scope ();
8387 }
8388
8389 /* Generate a DIE to represent a declared data object.  */
8390
8391 static void
8392 gen_variable_die (decl, context_die)
8393      register tree decl;
8394      register dw_die_ref context_die;
8395 {
8396   register tree origin = decl_ultimate_origin (decl);
8397   register dw_die_ref var_die = new_die (DW_TAG_variable, context_die);
8398
8399   dw_die_ref old_die = lookup_decl_die (decl);
8400   int declaration
8401     = (DECL_EXTERNAL (decl)
8402        || current_function_decl != decl_function_context (decl)
8403        || context_die->die_tag == DW_TAG_structure_type
8404        || context_die->die_tag == DW_TAG_union_type);
8405
8406   if (origin != NULL)
8407     add_abstract_origin_attribute (var_die, origin);
8408   /* Loop unrolling can create multiple blocks that refer to the same
8409      static variable, so we must test for the DW_AT_declaration flag.  */
8410   /* ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
8411      copy decls and set the DECL_ABSTRACT flag on them instead of
8412      sharing them.  */
8413   else if (old_die && TREE_STATIC (decl)
8414            && get_AT_flag (old_die, DW_AT_declaration) == 1)
8415     {
8416       /* ??? This is an instantiation of a C++ class level static.  */
8417       add_AT_die_ref (var_die, DW_AT_specification, old_die);
8418       if (DECL_NAME (decl))
8419         {
8420           register unsigned file_index
8421             = lookup_filename (DECL_SOURCE_FILE (decl));
8422
8423           if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
8424             add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
8425
8426           if (get_AT_unsigned (old_die, DW_AT_decl_line)
8427               != DECL_SOURCE_LINE (decl))
8428
8429             add_AT_unsigned (var_die, DW_AT_decl_line,
8430                              DECL_SOURCE_LINE (decl));
8431         }
8432     }
8433   else
8434     {
8435       add_name_and_src_coords_attributes (var_die, decl);
8436       add_type_attribute (var_die, TREE_TYPE (decl),
8437                           TREE_READONLY (decl),
8438                           TREE_THIS_VOLATILE (decl), context_die);
8439
8440       if (TREE_PUBLIC (decl))
8441         add_AT_flag (var_die, DW_AT_external, 1);
8442
8443       if (DECL_ARTIFICIAL (decl))
8444         add_AT_flag (var_die, DW_AT_artificial, 1);
8445
8446       if (TREE_PROTECTED (decl))
8447         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
8448
8449       else if (TREE_PRIVATE (decl))
8450         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
8451     }
8452
8453   if (declaration)
8454     add_AT_flag (var_die, DW_AT_declaration, 1);
8455   
8456   if ((declaration && decl_class_context (decl)) || DECL_ABSTRACT (decl))
8457     equate_decl_number_to_die (decl, var_die);
8458
8459   if (! declaration && ! DECL_ABSTRACT (decl))
8460     {
8461       equate_decl_number_to_die (decl, var_die);
8462       add_location_or_const_value_attribute (var_die, decl);
8463       add_pubname (decl, var_die);
8464     }
8465 }
8466
8467 /* Generate a DIE to represent a label identifier.  */
8468
8469 static void
8470 gen_label_die (decl, context_die)
8471      register tree decl;
8472      register dw_die_ref context_die;
8473 {
8474   register tree origin = decl_ultimate_origin (decl);
8475   register dw_die_ref lbl_die = new_die (DW_TAG_label, context_die);
8476   register rtx insn;
8477   char label[MAX_ARTIFICIAL_LABEL_BYTES];
8478   char label2[MAX_ARTIFICIAL_LABEL_BYTES];
8479
8480   if (origin != NULL)
8481     add_abstract_origin_attribute (lbl_die, origin);
8482   else
8483     add_name_and_src_coords_attributes (lbl_die, decl);
8484
8485   if (DECL_ABSTRACT (decl))
8486     equate_decl_number_to_die (decl, lbl_die);
8487   else
8488     {
8489       insn = DECL_RTL (decl);
8490       if (GET_CODE (insn) == CODE_LABEL)
8491         {
8492           /* When optimization is enabled (via -O) some parts of the compiler 
8493              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which 
8494              represent source-level labels which were explicitly declared by
8495              the user.  This really shouldn't be happening though, so catch
8496              it if it ever does happen.  */
8497           if (INSN_DELETED_P (insn))
8498             abort ();
8499
8500           sprintf (label2, INSN_LABEL_FMT, current_funcdef_number);
8501           ASM_GENERATE_INTERNAL_LABEL (label, label2,
8502                                        (unsigned) INSN_UID (insn));
8503           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
8504         }
8505     }
8506 }
8507
8508 /* Generate a DIE for a lexical block.  */
8509
8510 static void
8511 gen_lexical_block_die (stmt, context_die, depth)
8512      register tree stmt;
8513      register dw_die_ref context_die;
8514      int depth;
8515 {
8516   register dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die);
8517   char label[MAX_ARTIFICIAL_LABEL_BYTES];
8518
8519   if (! BLOCK_ABSTRACT (stmt))
8520     {
8521       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
8522                                    next_block_number);
8523       add_AT_lbl_id (stmt_die, DW_AT_low_pc, label);
8524       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL, next_block_number);
8525       add_AT_lbl_id (stmt_die, DW_AT_high_pc, label);
8526     }
8527
8528   push_decl_scope (stmt);
8529   decls_for_scope (stmt, stmt_die, depth);
8530   pop_decl_scope ();
8531 }
8532
8533 /* Generate a DIE for an inlined subprogram.  */
8534
8535 static void
8536 gen_inlined_subroutine_die (stmt, context_die, depth)
8537      register tree stmt;
8538      register dw_die_ref context_die;
8539      int depth;
8540 {
8541   if (! BLOCK_ABSTRACT (stmt))
8542     {
8543       register dw_die_ref subr_die
8544         = new_die (DW_TAG_inlined_subroutine, context_die);
8545       register tree decl = block_ultimate_origin (stmt);
8546       char label[MAX_ARTIFICIAL_LABEL_BYTES];
8547
8548       add_abstract_origin_attribute (subr_die, decl);
8549       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
8550                                    next_block_number);
8551       add_AT_lbl_id (subr_die, DW_AT_low_pc, label);
8552       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL, next_block_number);
8553       add_AT_lbl_id (subr_die, DW_AT_high_pc, label);
8554       push_decl_scope (decl);
8555       decls_for_scope (stmt, subr_die, depth);
8556       pop_decl_scope ();
8557       current_function_has_inlines = 1;
8558     }
8559 }
8560
8561 /* Generate a DIE for a field in a record, or structure.  */
8562
8563 static void
8564 gen_field_die (decl, context_die)
8565      register tree decl;
8566      register dw_die_ref context_die;
8567 {
8568   register dw_die_ref decl_die = new_die (DW_TAG_member, context_die);
8569
8570   add_name_and_src_coords_attributes (decl_die, decl);
8571   add_type_attribute (decl_die, member_declared_type (decl),
8572                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
8573                       context_die);
8574
8575   /* If this is a bit field...  */
8576   if (DECL_BIT_FIELD_TYPE (decl))
8577     {
8578       add_byte_size_attribute (decl_die, decl);
8579       add_bit_size_attribute (decl_die, decl);
8580       add_bit_offset_attribute (decl_die, decl);
8581     }
8582
8583   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
8584     add_data_member_location_attribute (decl_die, decl);
8585
8586   if (DECL_ARTIFICIAL (decl))
8587     add_AT_flag (decl_die, DW_AT_artificial, 1);
8588
8589   if (TREE_PROTECTED (decl))
8590     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
8591
8592   else if (TREE_PRIVATE (decl))
8593     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
8594 }
8595
8596 #if 0
8597 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
8598    Use modified_type_die instead.
8599    We keep this code here just in case these types of DIEs may be needed to
8600    represent certain things in other languages (e.g. Pascal) someday.  */
8601 static void
8602 gen_pointer_type_die (type, context_die)
8603      register tree type;
8604      register dw_die_ref context_die;
8605 {
8606   register dw_die_ref ptr_die
8607     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die));
8608
8609   equate_type_number_to_die (type, ptr_die);
8610   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
8611   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
8612 }
8613
8614 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
8615    Use modified_type_die instead.
8616    We keep this code here just in case these types of DIEs may be needed to
8617    represent certain things in other languages (e.g. Pascal) someday.  */
8618 static void
8619 gen_reference_type_die (type, context_die)
8620      register tree type;
8621      register dw_die_ref context_die;
8622 {
8623   register dw_die_ref ref_die
8624     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die));
8625
8626   equate_type_number_to_die (type, ref_die);
8627   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
8628   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
8629 }
8630 #endif
8631
8632 /* Generate a DIE for a pointer to a member type.  */
8633 static void
8634 gen_ptr_to_mbr_type_die (type, context_die)
8635      register tree type;
8636      register dw_die_ref context_die;
8637 {
8638   register dw_die_ref ptr_die
8639     = new_die (DW_TAG_ptr_to_member_type, scope_die_for (type, context_die));
8640
8641   equate_type_number_to_die (type, ptr_die);
8642   add_AT_die_ref (ptr_die, DW_AT_containing_type,
8643                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
8644   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
8645 }
8646
8647 /* Generate the DIE for the compilation unit.  */
8648
8649 static void
8650 gen_compile_unit_die (main_input_filename)
8651      register char *main_input_filename;
8652 {
8653   char producer[250];
8654   char *wd = getpwd ();
8655
8656   comp_unit_die = new_die (DW_TAG_compile_unit, NULL);
8657   add_name_attribute (comp_unit_die, main_input_filename);
8658
8659   if (wd != NULL)
8660     add_AT_string (comp_unit_die, DW_AT_comp_dir, wd);
8661
8662   sprintf (producer, "%s %s", language_string, version_string);
8663
8664 #ifdef MIPS_DEBUGGING_INFO
8665   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
8666      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
8667      not appear in the producer string, the debugger reaches the conclusion
8668      that the object file is stripped and has no debugging information.
8669      To get the MIPS/SGI debugger to believe that there is debugging
8670      information in the object file, we add a -g to the producer string.  */
8671   if (debug_info_level > DINFO_LEVEL_TERSE)
8672     strcat (producer, " -g");
8673 #endif
8674
8675   add_AT_string (comp_unit_die, DW_AT_producer, producer);
8676
8677   if (strcmp (language_string, "GNU C++") == 0)
8678     add_AT_unsigned (comp_unit_die, DW_AT_language, DW_LANG_C_plus_plus);
8679
8680   else if (strcmp (language_string, "GNU Ada") == 0)
8681     add_AT_unsigned (comp_unit_die, DW_AT_language, DW_LANG_Ada83);
8682
8683   else if (strcmp (language_string, "GNU F77") == 0)
8684     add_AT_unsigned (comp_unit_die, DW_AT_language, DW_LANG_Fortran77);
8685
8686   else if (strcmp (language_string, "GNU Pascal") == 0)
8687     add_AT_unsigned (comp_unit_die, DW_AT_language, DW_LANG_Pascal83);
8688
8689   else if (flag_traditional)
8690     add_AT_unsigned (comp_unit_die, DW_AT_language, DW_LANG_C);
8691
8692   else
8693     add_AT_unsigned (comp_unit_die, DW_AT_language, DW_LANG_C89);
8694
8695 #if 0 /* unimplemented */
8696   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
8697     add_AT_unsigned (comp_unit_die, DW_AT_macro_info, 0);
8698 #endif
8699 }
8700
8701 /* Generate a DIE for a string type.  */
8702
8703 static void
8704 gen_string_type_die (type, context_die)
8705      register tree type;
8706      register dw_die_ref context_die;
8707 {
8708   register dw_die_ref type_die
8709     = new_die (DW_TAG_string_type, scope_die_for (type, context_die));
8710
8711   equate_type_number_to_die (type, type_die);
8712
8713   /* Fudge the string length attribute for now.  */
8714   
8715   /* TODO: add string length info.
8716    string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
8717                               bound_representation (upper_bound, 0, 'u'); */
8718 }
8719
8720 /* Generate the DIE for a base class.  */
8721
8722 static void
8723 gen_inheritance_die (binfo, context_die)
8724      register tree binfo;
8725      register dw_die_ref context_die;
8726 {
8727   dw_die_ref die = new_die (DW_TAG_inheritance, context_die);
8728
8729   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
8730   add_data_member_location_attribute (die, binfo);
8731
8732   if (TREE_VIA_VIRTUAL (binfo))
8733     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
8734   if (TREE_VIA_PUBLIC (binfo))
8735     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
8736   else if (TREE_VIA_PROTECTED (binfo))
8737     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
8738 }
8739
8740 /* Generate a DIE for a class member.  */
8741
8742 static void
8743 gen_member_die (type, context_die)
8744      register tree type;
8745      register dw_die_ref context_die;
8746 {
8747   register tree member;
8748
8749   /* If this is not an incomplete type, output descriptions of each of its
8750      members. Note that as we output the DIEs necessary to represent the
8751      members of this record or union type, we will also be trying to output
8752      DIEs to represent the *types* of those members. However the `type'
8753      function (above) will specifically avoid generating type DIEs for member 
8754      types *within* the list of member DIEs for this (containing) type execpt 
8755      for those types (of members) which are explicitly marked as also being
8756      members of this (containing) type themselves.  The g++ front- end can
8757      force any given type to be treated as a member of some other
8758      (containing) type by setting the TYPE_CONTEXT of the given (member) type 
8759      to point to the TREE node representing the appropriate (containing)
8760      type.  */
8761
8762   /* First output info about the base classes.  */
8763   if (TYPE_BINFO (type) && TYPE_BINFO_BASETYPES (type))
8764     {
8765       register tree bases = TYPE_BINFO_BASETYPES (type);
8766       register int n_bases = TREE_VEC_LENGTH (bases);
8767       register int i;
8768
8769       for (i = 0; i < n_bases; i++)
8770         gen_inheritance_die (TREE_VEC_ELT (bases, i), context_die);
8771     }
8772
8773   /* Now output info about the data members and type members.  */
8774   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
8775     gen_decl_die (member, context_die);
8776
8777   /* Now output info about the function members (if any).  */
8778   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
8779     gen_decl_die (member, context_die);
8780 }
8781
8782 /* Generate a DIE for a structure or union type.  */
8783
8784 static void
8785 gen_struct_or_union_type_die (type, context_die)
8786      register tree type;
8787      register dw_die_ref context_die;
8788 {
8789   register dw_die_ref type_die = lookup_type_die (type);
8790   register dw_die_ref scope_die = 0;
8791   register int nested = 0;
8792
8793   if (type_die && ! TYPE_SIZE (type))
8794     return;
8795
8796   if (TYPE_CONTEXT (type) != NULL_TREE
8797       && TREE_CODE_CLASS (TREE_CODE (TYPE_CONTEXT (type))) == 't')
8798     nested = 1;
8799
8800   scope_die = scope_die_for (type, context_die);
8801
8802   if (! type_die || (nested && scope_die == comp_unit_die))
8803     /* First occurrence of type or toplevel definition of nested class.  */
8804     {
8805       register dw_die_ref old_die = type_die;
8806
8807       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
8808                           ? DW_TAG_structure_type : DW_TAG_union_type,
8809                           scope_die);
8810       equate_type_number_to_die (type, type_die);
8811       add_name_attribute (type_die, type_tag (type));
8812       if (old_die)
8813         add_AT_die_ref (type_die, DW_AT_specification, old_die);
8814     }
8815   else
8816     remove_AT (type_die, DW_AT_declaration);
8817
8818   /* If we're not in the right context to be defining this type, defer to
8819      avoid tricky recursion.  */
8820   if (TYPE_SIZE (type) && decl_scope_depth > 0 && scope_die == comp_unit_die)
8821     {
8822       add_AT_flag (type_die, DW_AT_declaration, 1);
8823       pend_type (type);
8824     }
8825   /* If this type has been completed, then give it a byte_size attribute and
8826      then give a list of members.  */
8827   else if (TYPE_SIZE (type))
8828     {
8829       /* Prevent infinite recursion in cases where the type of some member of 
8830          this type is expressed in terms of this type itself.  */
8831       TREE_ASM_WRITTEN (type) = 1;
8832       add_byte_size_attribute (type_die, type);
8833       if (TYPE_STUB_DECL (type) != NULL_TREE)
8834         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
8835
8836       /* If the first reference to this type was as the return type of an
8837          inline function, then it may not have a parent.  Fix this now.  */
8838       if (type_die->die_parent == NULL)
8839         add_child_die (scope_die, type_die);
8840
8841       push_decl_scope (type);
8842       gen_member_die (type, type_die);
8843       pop_decl_scope ();
8844
8845       /* GNU extension: Record what type our vtable lives in.  */
8846       if (TYPE_VFIELD (type))
8847         {
8848           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
8849
8850           gen_type_die (vtype, context_die);
8851           add_AT_die_ref (type_die, DW_AT_containing_type,
8852                           lookup_type_die (vtype));
8853         }
8854     }
8855   else
8856     add_AT_flag (type_die, DW_AT_declaration, 1);
8857 }
8858
8859 /* Generate a DIE for a subroutine _type_.  */
8860
8861 static void
8862 gen_subroutine_type_die (type, context_die)
8863      register tree type;
8864      register dw_die_ref context_die;
8865 {
8866   register tree return_type = TREE_TYPE (type);
8867   register dw_die_ref subr_die
8868     = new_die (DW_TAG_subroutine_type, scope_die_for (type, context_die));
8869
8870   equate_type_number_to_die (type, subr_die);
8871   add_prototyped_attribute (subr_die, type);
8872   add_type_attribute (subr_die, return_type, 0, 0, context_die);
8873   gen_formal_types_die (type, subr_die);
8874 }
8875
8876 /* Generate a DIE for a type definition */
8877
8878 static void
8879 gen_typedef_die (decl, context_die)
8880      register tree decl;
8881      register dw_die_ref context_die;
8882 {
8883   register dw_die_ref type_die;
8884   register tree origin;
8885
8886   if (TREE_ASM_WRITTEN (decl))
8887     return;
8888   TREE_ASM_WRITTEN (decl) = 1;
8889
8890   type_die = new_die (DW_TAG_typedef, scope_die_for (decl, context_die));
8891   origin = decl_ultimate_origin (decl);
8892   if (origin != NULL)
8893     add_abstract_origin_attribute (type_die, origin);
8894   else
8895     {
8896       register tree type;
8897       add_name_and_src_coords_attributes (type_die, decl);
8898       if (DECL_ORIGINAL_TYPE (decl))
8899         {
8900           type = DECL_ORIGINAL_TYPE (decl);
8901           equate_type_number_to_die (TREE_TYPE (decl), type_die);
8902         }
8903       else
8904         type = TREE_TYPE (decl);
8905       add_type_attribute (type_die, type, TREE_READONLY (decl),
8906                           TREE_THIS_VOLATILE (decl), context_die);
8907     }
8908
8909   if (DECL_ABSTRACT (decl))
8910     equate_decl_number_to_die (decl, type_die);
8911 }
8912
8913 /* Generate a type description DIE.  */
8914
8915 static void
8916 gen_type_die (type, context_die)
8917      register tree type;
8918      register dw_die_ref context_die;
8919 {
8920   if (type == NULL_TREE || type == error_mark_node)
8921     return;
8922
8923   /* We are going to output a DIE to represent the unqualified version of
8924      this type (i.e. without any const or volatile qualifiers) so get the
8925      main variant (i.e. the unqualified version) of this type now.  */
8926   type = type_main_variant (type);
8927
8928   if (TREE_ASM_WRITTEN (type))
8929     return;
8930
8931   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8932       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
8933     { 
8934       TREE_ASM_WRITTEN (type) = 1;
8935       gen_decl_die (TYPE_NAME (type), context_die);
8936       return;
8937     }
8938
8939   switch (TREE_CODE (type))
8940     {
8941     case ERROR_MARK:
8942       break;
8943
8944     case POINTER_TYPE:
8945     case REFERENCE_TYPE:
8946       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
8947          ensures that the gen_type_die recursion will terminate even if the
8948          type is recursive.  Recursive types are possible in Ada.  */
8949       /* ??? We could perhaps do this for all types before the switch
8950          statement.  */
8951       TREE_ASM_WRITTEN (type) = 1;
8952
8953       /* For these types, all that is required is that we output a DIE (or a
8954          set of DIEs) to represent the "basis" type.  */
8955       gen_type_die (TREE_TYPE (type), context_die);
8956       break;
8957
8958     case OFFSET_TYPE:
8959       /* This code is used for C++ pointer-to-data-member types. 
8960          Output a description of the relevant class type.  */
8961       gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
8962
8963       /* Output a description of the type of the object pointed to.  */
8964       gen_type_die (TREE_TYPE (type), context_die);
8965
8966       /* Now output a DIE to represent this pointer-to-data-member type
8967          itself.  */
8968       gen_ptr_to_mbr_type_die (type, context_die);
8969       break;
8970
8971     case SET_TYPE:
8972       gen_type_die (TYPE_DOMAIN (type), context_die);
8973       gen_set_type_die (type, context_die);
8974       break;
8975
8976     case FILE_TYPE:
8977       gen_type_die (TREE_TYPE (type), context_die);
8978       abort ();                 /* No way to represent these in Dwarf yet!  */
8979       break;
8980
8981     case FUNCTION_TYPE:
8982       /* Force out return type (in case it wasn't forced out already).  */
8983       gen_type_die (TREE_TYPE (type), context_die);
8984       gen_subroutine_type_die (type, context_die);
8985       break;
8986
8987     case METHOD_TYPE:
8988       /* Force out return type (in case it wasn't forced out already).  */
8989       gen_type_die (TREE_TYPE (type), context_die);
8990       gen_subroutine_type_die (type, context_die);
8991       break;
8992
8993     case ARRAY_TYPE:
8994       if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
8995         {
8996           gen_type_die (TREE_TYPE (type), context_die);
8997           gen_string_type_die (type, context_die);
8998         }
8999       else
9000         gen_array_type_die (type, context_die);
9001       break;
9002
9003     case ENUMERAL_TYPE:
9004     case RECORD_TYPE:
9005     case UNION_TYPE:
9006     case QUAL_UNION_TYPE:
9007       /* If this is a nested type whose containing class hasn't been
9008          written out yet, writing it out will cover this one, too.  */
9009       if (TYPE_CONTEXT (type)
9010           && TREE_CODE_CLASS (TREE_CODE (TYPE_CONTEXT (type))) == 't'
9011           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
9012         {
9013           gen_type_die (TYPE_CONTEXT (type), context_die);
9014
9015           if (TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
9016             return;
9017
9018           /* If that failed, attach ourselves to the stub.  */
9019           push_decl_scope (TYPE_CONTEXT (type));
9020           context_die = lookup_type_die (TYPE_CONTEXT (type));
9021         }
9022
9023       if (TREE_CODE (type) == ENUMERAL_TYPE)
9024         gen_enumeration_type_die (type, context_die);
9025       else
9026         gen_struct_or_union_type_die (type, context_die);
9027
9028       if (TYPE_CONTEXT (type)
9029           && TREE_CODE_CLASS (TREE_CODE (TYPE_CONTEXT (type))) == 't'
9030           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
9031         pop_decl_scope ();
9032
9033       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
9034          it up if it is ever completed.  gen_*_type_die will set it for us
9035          when appropriate.  */
9036       return;
9037
9038     case VOID_TYPE:
9039     case INTEGER_TYPE:
9040     case REAL_TYPE:
9041     case COMPLEX_TYPE:
9042     case BOOLEAN_TYPE:
9043     case CHAR_TYPE:
9044       /* No DIEs needed for fundamental types.  */
9045       break;
9046
9047     case LANG_TYPE:
9048       /* No Dwarf representation currently defined.  */
9049       break;
9050
9051     default:
9052       abort ();
9053     }
9054
9055   TREE_ASM_WRITTEN (type) = 1;
9056 }
9057
9058 /* Generate a DIE for a tagged type instantiation.  */
9059
9060 static void
9061 gen_tagged_type_instantiation_die (type, context_die)
9062      register tree type;
9063      register dw_die_ref context_die;
9064 {
9065   if (type == NULL_TREE || type == error_mark_node)
9066     return;
9067
9068   /* We are going to output a DIE to represent the unqualified version of
9069      this type (i.e. without any const or volatile qualifiers) so make sure
9070      that we have the main variant (i.e. the unqualified version) of this
9071      type now.  */
9072   if (type != type_main_variant (type)
9073       || !TREE_ASM_WRITTEN (type))
9074     abort ();
9075
9076   switch (TREE_CODE (type))
9077     {
9078     case ERROR_MARK:
9079       break;
9080
9081     case ENUMERAL_TYPE:
9082       gen_inlined_enumeration_type_die (type, context_die);
9083       break;
9084
9085     case RECORD_TYPE:
9086       gen_inlined_structure_type_die (type, context_die);
9087       break;
9088
9089     case UNION_TYPE:
9090     case QUAL_UNION_TYPE:
9091       gen_inlined_union_type_die (type, context_die);
9092       break;
9093
9094     default:
9095       abort ();
9096     }
9097 }
9098
9099 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
9100    things which are local to the given block.  */
9101
9102 static void
9103 gen_block_die (stmt, context_die, depth)
9104      register tree stmt;
9105      register dw_die_ref context_die;
9106      int depth;
9107 {
9108   register int must_output_die = 0;
9109   register tree origin;
9110   register tree decl;
9111   register enum tree_code origin_code;
9112
9113   /* Ignore blocks never really used to make RTL.  */
9114
9115   if (stmt == NULL_TREE || !TREE_USED (stmt))
9116     return;
9117
9118   /* Determine the "ultimate origin" of this block.  This block may be an
9119      inlined instance of an inlined instance of inline function, so we have
9120      to trace all of the way back through the origin chain to find out what
9121      sort of node actually served as the original seed for the creation of
9122      the current block.  */
9123   origin = block_ultimate_origin (stmt);
9124   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
9125
9126   /* Determine if we need to output any Dwarf DIEs at all to represent this
9127      block.  */
9128   if (origin_code == FUNCTION_DECL)
9129     /* The outer scopes for inlinings *must* always be represented.  We
9130        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
9131     must_output_die = 1;
9132   else
9133     {
9134       /* In the case where the current block represents an inlining of the
9135          "body block" of an inline function, we must *NOT* output any DIE for 
9136          this block because we have already output a DIE to represent the
9137          whole inlined function scope and the "body block" of any function
9138          doesn't really represent a different scope according to ANSI C
9139          rules.  So we check here to make sure that this block does not
9140          represent a "body block inlining" before trying to set the
9141          `must_output_die' flag.  */
9142       if (! is_body_block (origin ? origin : stmt))
9143         {
9144           /* Determine if this block directly contains any "significant"
9145              local declarations which we will need to output DIEs for.  */
9146           if (debug_info_level > DINFO_LEVEL_TERSE)
9147             /* We are not in terse mode so *any* local declaration counts
9148                as being a "significant" one.  */
9149             must_output_die = (BLOCK_VARS (stmt) != NULL);
9150           else
9151             /* We are in terse mode, so only local (nested) function
9152                definitions count as "significant" local declarations.  */
9153             for (decl = BLOCK_VARS (stmt);
9154                  decl != NULL; decl = TREE_CHAIN (decl))
9155               if (TREE_CODE (decl) == FUNCTION_DECL
9156                   && DECL_INITIAL (decl))
9157                 {
9158                   must_output_die = 1;
9159                   break;
9160                 }
9161         }
9162     }
9163
9164   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
9165      DIE for any block which contains no significant local declarations at
9166      all.  Rather, in such cases we just call `decls_for_scope' so that any
9167      needed Dwarf info for any sub-blocks will get properly generated. Note
9168      that in terse mode, our definition of what constitutes a "significant"
9169      local declaration gets restricted to include only inlined function
9170      instances and local (nested) function definitions.  */
9171   if (must_output_die)
9172     {
9173       if (origin_code == FUNCTION_DECL)
9174         gen_inlined_subroutine_die (stmt, context_die, depth);
9175       else
9176         gen_lexical_block_die (stmt, context_die, depth);
9177     }
9178   else
9179     decls_for_scope (stmt, context_die, depth);
9180 }
9181
9182 /* Generate all of the decls declared within a given scope and (recursively)
9183    all of it's sub-blocks.  */
9184
9185 static void
9186 decls_for_scope (stmt, context_die, depth)
9187      register tree stmt;
9188      register dw_die_ref context_die;
9189      int depth;
9190 {
9191   register tree decl;
9192   register tree subblocks;
9193
9194   /* Ignore blocks never really used to make RTL.  */
9195   if (stmt == NULL_TREE || ! TREE_USED (stmt))
9196     return;
9197
9198   if (!BLOCK_ABSTRACT (stmt) && depth > 0)
9199     next_block_number++;
9200
9201   /* Output the DIEs to represent all of the data objects and typedefs
9202      declared directly within this block but not within any nested
9203      sub-blocks.  Also, nested function and tag DIEs have been
9204      generated with a parent of NULL; fix that up now.  */
9205   for (decl = BLOCK_VARS (stmt);
9206        decl != NULL; decl = TREE_CHAIN (decl))
9207     {
9208       register dw_die_ref die;
9209
9210       if (TREE_CODE (decl) == FUNCTION_DECL)
9211         die = lookup_decl_die (decl);
9212       else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
9213         die = lookup_type_die (TREE_TYPE (decl));
9214       else
9215         die = NULL;
9216
9217       if (die != NULL && die->die_parent == NULL)
9218         add_child_die (context_die, die);
9219       else
9220         gen_decl_die (decl, context_die);
9221     }
9222
9223   /* Output the DIEs to represent all sub-blocks (and the items declared
9224      therein) of this block.  */
9225   for (subblocks = BLOCK_SUBBLOCKS (stmt);
9226        subblocks != NULL;
9227        subblocks = BLOCK_CHAIN (subblocks))
9228     gen_block_die (subblocks, context_die, depth + 1);
9229 }
9230
9231 /* Is this a typedef we can avoid emitting?  */
9232
9233 static inline int
9234 is_redundant_typedef (decl)
9235      register tree decl;
9236 {
9237   if (TYPE_DECL_IS_STUB (decl))
9238     return 1;
9239
9240   if (DECL_ARTIFICIAL (decl)
9241       && DECL_CONTEXT (decl)
9242       && is_tagged_type (DECL_CONTEXT (decl))
9243       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
9244       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
9245     /* Also ignore the artificial member typedef for the class name.  */
9246     return 1;
9247
9248   return 0;
9249 }
9250
9251 /* Generate Dwarf debug information for a decl described by DECL.  */
9252
9253 static void
9254 gen_decl_die (decl, context_die)
9255      register tree decl;
9256      register dw_die_ref context_die;
9257 {
9258   register tree origin;
9259
9260   /* Make a note of the decl node we are going to be working on.  We may need 
9261      to give the user the source coordinates of where it appeared in case we
9262      notice (later on) that something about it looks screwy.  */
9263   dwarf_last_decl = decl;
9264
9265   if (TREE_CODE (decl) == ERROR_MARK)
9266     return;
9267
9268   /* If this ..._DECL node is marked to be ignored, then ignore it. But don't 
9269      ignore a function definition, since that would screw up our count of
9270      blocks, and that in turn will completely screw up the labels we will 
9271      reference in subsequent DW_AT_low_pc and DW_AT_high_pc attributes (for
9272      subsequent blocks).  */
9273   if (DECL_IGNORED_P (decl) && TREE_CODE (decl) != FUNCTION_DECL)
9274     return;
9275
9276   switch (TREE_CODE (decl))
9277     {
9278     case CONST_DECL:
9279       /* The individual enumerators of an enum type get output when we output 
9280          the Dwarf representation of the relevant enum type itself.  */
9281       break;
9282
9283     case FUNCTION_DECL:
9284       /* Don't output any DIEs to represent mere function declarations,
9285          unless they are class members or explicit block externs.  */
9286       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
9287           && (current_function_decl == NULL_TREE || ! DECL_ARTIFICIAL (decl)))
9288         break;
9289
9290       if (debug_info_level > DINFO_LEVEL_TERSE)
9291         {
9292           /* Before we describe the FUNCTION_DECL itself, make sure that we
9293              have described its return type.  */
9294           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
9295
9296           /* And its containing type.  */
9297           origin = decl_class_context (decl);
9298           if (origin != NULL_TREE)
9299             gen_type_die (origin, context_die);
9300
9301           /* And its virtual context.  */
9302           if (DECL_VINDEX (decl) != NULL_TREE)
9303             gen_type_die (DECL_CONTEXT (decl), context_die);
9304         }
9305
9306       /* Now output a DIE to represent the function itself.  */
9307       gen_subprogram_die (decl, context_die);
9308       break;
9309
9310     case TYPE_DECL:
9311       /* If we are in terse mode, don't generate any DIEs to represent any
9312          actual typedefs.  */
9313       if (debug_info_level <= DINFO_LEVEL_TERSE)
9314         break;
9315
9316       /* In the special case of a TYPE_DECL node representing the 
9317          declaration of some type tag, if the given TYPE_DECL is marked as
9318          having been instantiated from some other (original) TYPE_DECL node
9319          (e.g. one which was generated within the original definition of an
9320          inline function) we have to generate a special (abbreviated)
9321          DW_TAG_structure_type, DW_TAG_union_type, or DW_TAG_enumeration_type 
9322          DIE here.  */
9323       if (TYPE_DECL_IS_STUB (decl) && DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
9324         {
9325           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
9326           break;
9327         }
9328
9329       if (is_redundant_typedef (decl))
9330         gen_type_die (TREE_TYPE (decl), context_die);
9331       else
9332         /* Output a DIE to represent the typedef itself.  */
9333         gen_typedef_die (decl, context_die);
9334       break;
9335
9336     case LABEL_DECL:
9337       if (debug_info_level >= DINFO_LEVEL_NORMAL)
9338         gen_label_die (decl, context_die);
9339       break;
9340
9341     case VAR_DECL:
9342       /* If we are in terse mode, don't generate any DIEs to represent any
9343          variable declarations or definitions.  */
9344       if (debug_info_level <= DINFO_LEVEL_TERSE)
9345         break;
9346
9347       /* Output any DIEs that are needed to specify the type of this data
9348          object.  */
9349       gen_type_die (TREE_TYPE (decl), context_die);
9350
9351       /* And its containing type.  */
9352       origin = decl_class_context (decl);
9353       if (origin != NULL_TREE)
9354         gen_type_die (origin, context_die);
9355
9356       /* Now output the DIE to represent the data object itself.  This gets
9357          complicated because of the possibility that the VAR_DECL really
9358          represents an inlined instance of a formal parameter for an inline
9359          function.  */
9360       origin = decl_ultimate_origin (decl);
9361       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
9362         gen_formal_parameter_die (decl, context_die);
9363       else
9364         gen_variable_die (decl, context_die);
9365       break;
9366
9367     case FIELD_DECL:
9368       /* Ignore the nameless fields that are used to skip bits, but
9369          handle C++ anonymous unions.  */
9370       if (DECL_NAME (decl) != NULL_TREE
9371           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE)
9372         {
9373           gen_type_die (member_declared_type (decl), context_die);
9374           gen_field_die (decl, context_die);
9375         }
9376       break;
9377
9378     case PARM_DECL:
9379       gen_type_die (TREE_TYPE (decl), context_die);
9380       gen_formal_parameter_die (decl, context_die);
9381       break;
9382
9383     default:
9384       abort ();
9385     }
9386 }
9387 \f
9388 /* Write the debugging output for DECL.  */
9389
9390 void
9391 dwarf2out_decl (decl)
9392      register tree decl;
9393 {
9394   register dw_die_ref context_die = comp_unit_die;
9395
9396   if (TREE_CODE (decl) == ERROR_MARK)
9397     return;
9398
9399   /* If this ..._DECL node is marked to be ignored, then ignore it.  We gotta 
9400      hope that the node in question doesn't represent a function definition.
9401      If it does, then totally ignoring it is bound to screw up our count of
9402      blocks, and that in turn will completely screw up the labels we will 
9403      reference in subsequent DW_AT_low_pc and DW_AT_high_pc attributes (for
9404      subsequent blocks).  (It's too bad that BLOCK nodes don't carry their
9405      own sequence numbers with them!) */
9406   if (DECL_IGNORED_P (decl))
9407     {
9408       if (TREE_CODE (decl) == FUNCTION_DECL
9409           && DECL_INITIAL (decl) != NULL)
9410         abort ();
9411
9412       return;
9413     }
9414
9415   switch (TREE_CODE (decl))
9416     {
9417     case FUNCTION_DECL:
9418       /* Ignore this FUNCTION_DECL if it refers to a builtin declaration of a 
9419          builtin function.  Explicit programmer-supplied declarations of
9420          these same functions should NOT be ignored however.  */
9421       if (DECL_EXTERNAL (decl) && DECL_FUNCTION_CODE (decl))
9422         return;
9423
9424       /* What we would really like to do here is to filter out all mere
9425          file-scope declarations of file-scope functions which are never
9426          referenced later within this translation unit (and keep all of ones
9427          that *are* referenced later on) but we aren't clairvoyant, so we have 
9428          no idea which functions will be referenced in the future (i.e. later 
9429          on within the current translation unit). So here we just ignore all
9430          file-scope function declarations which are not also definitions.  If 
9431          and when the debugger needs to know something about these functions,
9432          it wil have to hunt around and find the DWARF information associated 
9433          with the definition of the function. Note that we can't just check
9434          `DECL_EXTERNAL' to find out which FUNCTION_DECL nodes represent
9435          definitions and which ones represent mere declarations.  We have to
9436          check `DECL_INITIAL' instead. That's because the C front-end
9437          supports some weird semantics for "extern inline" function
9438          definitions.  These can get inlined within the current translation
9439          unit (an thus, we need to generate DWARF info for their abstract
9440          instances so that the DWARF info for the concrete inlined instances
9441          can have something to refer to) but the compiler never generates any 
9442          out-of-lines instances of such things (despite the fact that they
9443          *are* definitions).  The important point is that the C front-end
9444          marks these "extern inline" functions as DECL_EXTERNAL, but we need
9445          to generate DWARF for them anyway. Note that the C++ front-end also
9446          plays some similar games for inline function definitions appearing
9447          within include files which also contain 
9448          `#pragma interface' pragmas.  */
9449       if (DECL_INITIAL (decl) == NULL_TREE)
9450         return;
9451
9452       /* If we're a nested function, initially use a parent of NULL; if we're
9453          a plain function, this will be fixed up in decls_for_scope.  If
9454          we're a method, it will be ignored, since we already have a DIE.  */
9455       if (decl_function_context (decl))
9456         context_die = NULL;
9457
9458       break;
9459
9460     case VAR_DECL:
9461       /* Ignore this VAR_DECL if it refers to a file-scope extern data object 
9462          declaration and if the declaration was never even referenced from
9463          within this entire compilation unit.  We suppress these DIEs in
9464          order to save space in the .debug section (by eliminating entries
9465          which are probably useless).  Note that we must not suppress
9466          block-local extern declarations (whether used or not) because that
9467          would screw-up the debugger's name lookup mechanism and cause it to
9468          miss things which really ought to be in scope at a given point.  */
9469       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
9470         return;
9471
9472       /* If we are in terse mode, don't generate any DIEs to represent any
9473          variable declarations or definitions.  */
9474       if (debug_info_level <= DINFO_LEVEL_TERSE)
9475         return;
9476       break;
9477
9478     case TYPE_DECL:
9479       /* Don't bother trying to generate any DIEs to represent any of the
9480          normal built-in types for the language we are compiling.  */
9481       if (DECL_SOURCE_LINE (decl) == 0)
9482         {
9483           /* OK, we need to generate one for `bool' so GDB knows what type
9484              comparisons have.  */
9485           if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
9486                == DW_LANG_C_plus_plus)
9487               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE)
9488             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
9489
9490           return;
9491         }
9492
9493       /* If we are in terse mode, don't generate any DIEs for types.  */
9494       if (debug_info_level <= DINFO_LEVEL_TERSE)
9495         return;
9496
9497       /* If we're a function-scope tag, initially use a parent of NULL;
9498          this will be fixed up in decls_for_scope.  */
9499       if (decl_function_context (decl))
9500         context_die = NULL;
9501
9502       break;
9503
9504     default:
9505       return;
9506     }
9507
9508   gen_decl_die (decl, context_die);
9509   output_pending_types_for_scope (comp_unit_die);
9510 }
9511
9512 /* Output a marker (i.e. a label) for the beginning of the generated code for
9513    a lexical block.  */
9514
9515 void
9516 dwarf2out_begin_block (blocknum)
9517      register unsigned blocknum;
9518 {
9519   function_section (current_function_decl);
9520   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
9521 }
9522
9523 /* Output a marker (i.e. a label) for the end of the generated code for a
9524    lexical block.  */
9525
9526 void
9527 dwarf2out_end_block (blocknum)
9528      register unsigned blocknum;
9529 {
9530   function_section (current_function_decl);
9531   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
9532 }
9533
9534 /* Output a marker (i.e. a label) at a point in the assembly code which
9535    corresponds to a given source level label.  */
9536
9537 void
9538 dwarf2out_label (insn)
9539      register rtx insn;
9540 {
9541   char label[MAX_ARTIFICIAL_LABEL_BYTES];
9542
9543   if (debug_info_level >= DINFO_LEVEL_NORMAL)
9544     {
9545       function_section (current_function_decl);
9546       sprintf (label, INSN_LABEL_FMT, current_funcdef_number);
9547       ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, label,
9548                                  (unsigned) INSN_UID (insn));
9549     }
9550 }
9551
9552 /* Lookup a filename (in the list of filenames that we know about here in
9553    dwarf2out.c) and return its "index".  The index of each (known) filename is
9554    just a unique number which is associated with only that one filename.
9555    We need such numbers for the sake of generating labels
9556    (in the .debug_sfnames section) and references to those
9557    files  numbers (in the .debug_srcinfo and.debug_macinfo sections).
9558    If the filename given as an argument is not found in our current list,
9559    add it to the list and assign it the next available unique index number.
9560    In order to speed up searches, we remember the index of the filename
9561    was looked up last.  This handles the majority of all searches.  */
9562
9563 static unsigned
9564 lookup_filename (file_name)
9565      char *file_name;
9566 {
9567   static unsigned last_file_lookup_index = 0;
9568   register unsigned i;
9569
9570   /* Check to see if the file name that was searched on the previous call
9571      matches this file name. If so, return the index.  */
9572   if (last_file_lookup_index != 0)
9573     if (strcmp (file_name, file_table[last_file_lookup_index]) == 0)
9574       return last_file_lookup_index;
9575
9576   /* Didn't match the previous lookup, search the table */
9577   for (i = 1; i < file_table_in_use; ++i)
9578     if (strcmp (file_name, file_table[i]) == 0)
9579       {
9580         last_file_lookup_index = i;
9581         return i;
9582       }
9583
9584   /* Prepare to add a new table entry by making sure there is enough space in 
9585      the table to do so.  If not, expand the current table.  */
9586   if (file_table_in_use == file_table_allocated)
9587     {
9588       file_table_allocated += FILE_TABLE_INCREMENT;
9589       file_table
9590         = (char **) xrealloc (file_table,
9591                               file_table_allocated * sizeof (char *));
9592     }
9593
9594   /* Add the new entry to the end of the filename table.  */
9595   file_table[file_table_in_use] = xstrdup (file_name);
9596   last_file_lookup_index = file_table_in_use++;
9597
9598   return last_file_lookup_index;
9599 }
9600
9601 /* Output a label to mark the beginning of a source code line entry
9602    and record information relating to this source line, in
9603    'line_info_table' for later output of the .debug_line section.  */
9604
9605 void
9606 dwarf2out_line (filename, line)
9607      register char *filename;
9608      register unsigned line;
9609 {
9610   if (debug_info_level >= DINFO_LEVEL_NORMAL)
9611     {
9612       function_section (current_function_decl);
9613
9614       if (DECL_SECTION_NAME (current_function_decl))
9615         {
9616           register dw_separate_line_info_ref line_info;
9617           ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, SEPARATE_LINE_CODE_LABEL,
9618                                      separate_line_info_table_in_use);
9619           fputc ('\n', asm_out_file);
9620
9621           /* expand the line info table if necessary */
9622           if (separate_line_info_table_in_use
9623               == separate_line_info_table_allocated)
9624             {
9625               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
9626               separate_line_info_table
9627                 = (dw_separate_line_info_ref)
9628                   xrealloc (separate_line_info_table,
9629                             separate_line_info_table_allocated
9630                             * sizeof (dw_separate_line_info_entry));
9631             }
9632
9633           /* Add the new entry at the end of the line_info_table.  */
9634           line_info
9635             = &separate_line_info_table[separate_line_info_table_in_use++];
9636           line_info->dw_file_num = lookup_filename (filename);
9637           line_info->dw_line_num = line;
9638           line_info->function = current_funcdef_number;
9639         }
9640       else
9641         {
9642           register dw_line_info_ref line_info;
9643
9644           ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, LINE_CODE_LABEL,
9645                                      line_info_table_in_use);
9646           fputc ('\n', asm_out_file);
9647
9648           /* Expand the line info table if necessary.  */
9649           if (line_info_table_in_use == line_info_table_allocated)
9650             {
9651               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
9652               line_info_table
9653                 = (dw_line_info_ref)
9654                   xrealloc (line_info_table,
9655                             (line_info_table_allocated
9656                              * sizeof (dw_line_info_entry)));
9657             }
9658
9659           /* Add the new entry at the end of the line_info_table.  */
9660           line_info = &line_info_table[line_info_table_in_use++];
9661           line_info->dw_file_num = lookup_filename (filename);
9662           line_info->dw_line_num = line;
9663         }
9664     }
9665 }
9666
9667 /* Record the beginning of a new source file, for later output
9668    of the .debug_macinfo section.  At present, unimplemented.  */
9669
9670 void
9671 dwarf2out_start_source_file (filename)
9672      register char *filename;
9673 {
9674 }
9675
9676 /* Record the end of a source file, for later output
9677    of the .debug_macinfo section.  At present, unimplemented.  */
9678
9679 void
9680 dwarf2out_end_source_file ()
9681 {
9682 }
9683
9684 /* Called from check_newline in c-parse.y.  The `buffer' parameter contains
9685    the tail part of the directive line, i.e. the part which is past the
9686    initial whitespace, #, whitespace, directive-name, whitespace part.  */
9687
9688 void
9689 dwarf2out_define (lineno, buffer)
9690      register unsigned lineno;
9691      register char *buffer;
9692 {
9693   static int initialized = 0;
9694   if (!initialized)
9695     {
9696       dwarf2out_start_source_file (primary_filename);
9697       initialized = 1;
9698     }
9699 }
9700
9701 /* Called from check_newline in c-parse.y.  The `buffer' parameter contains
9702    the tail part of the directive line, i.e. the part which is past the
9703    initial whitespace, #, whitespace, directive-name, whitespace part.  */
9704
9705 void
9706 dwarf2out_undef (lineno, buffer)
9707      register unsigned lineno;
9708      register char *buffer;
9709 {
9710 }
9711
9712 /* Set up for Dwarf output at the start of compilation.  */
9713
9714 void
9715 dwarf2out_init (asm_out_file, main_input_filename)
9716      register FILE *asm_out_file;
9717      register char *main_input_filename;
9718 {
9719   /* Remember the name of the primary input file.  */
9720   primary_filename = main_input_filename;
9721
9722   /* Allocate the initial hunk of the file_table.  */
9723   file_table = (char **) xmalloc (FILE_TABLE_INCREMENT * sizeof (char *));
9724   bzero ((char *) file_table, FILE_TABLE_INCREMENT * sizeof (char *));
9725   file_table_allocated = FILE_TABLE_INCREMENT;
9726
9727   /* Skip the first entry - file numbers begin at 1.  */
9728   file_table_in_use = 1;
9729
9730   /* Allocate the initial hunk of the decl_die_table.  */
9731   decl_die_table
9732     = (dw_die_ref *) xmalloc (DECL_DIE_TABLE_INCREMENT * sizeof (dw_die_ref));
9733   bzero ((char *) decl_die_table,
9734          DECL_DIE_TABLE_INCREMENT * sizeof (dw_die_ref));
9735   decl_die_table_allocated = DECL_DIE_TABLE_INCREMENT;
9736   decl_die_table_in_use = 0;
9737
9738   /* Allocate the initial hunk of the decl_scope_table.  */
9739   decl_scope_table
9740     = (decl_scope_node *) xmalloc (DECL_SCOPE_TABLE_INCREMENT
9741                                    * sizeof (decl_scope_node));
9742   bzero ((char *) decl_scope_table,
9743          DECL_SCOPE_TABLE_INCREMENT * sizeof (decl_scope_node));
9744   decl_scope_table_allocated = DECL_SCOPE_TABLE_INCREMENT;
9745   decl_scope_depth = 0;
9746
9747   /* Allocate the initial hunk of the abbrev_die_table.  */
9748   abbrev_die_table
9749     = (dw_die_ref *) xmalloc (ABBREV_DIE_TABLE_INCREMENT
9750                               * sizeof (dw_die_ref));
9751   bzero ((char *) abbrev_die_table,
9752          ABBREV_DIE_TABLE_INCREMENT * sizeof (dw_die_ref));
9753   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
9754   /* Zero-th entry is allocated, but unused */
9755   abbrev_die_table_in_use = 1;
9756
9757   /* Allocate the initial hunk of the line_info_table.  */
9758   line_info_table
9759     = (dw_line_info_ref) xmalloc (LINE_INFO_TABLE_INCREMENT
9760                                   * sizeof (dw_line_info_entry));
9761   bzero ((char *) line_info_table,
9762          LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
9763   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
9764   /* Zero-th entry is allocated, but unused */
9765   line_info_table_in_use = 1;
9766
9767   /* Generate the initial DIE for the .debug section.  Note that the (string) 
9768      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
9769      will (typically) be a relative pathname and that this pathname should be 
9770      taken as being relative to the directory from which the compiler was
9771      invoked when the given (base) source file was compiled.  */
9772   gen_compile_unit_die (main_input_filename);
9773
9774   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
9775 }
9776
9777 /* Output stuff that dwarf requires at the end of every file,
9778    and generate the DWARF-2 debugging info.  */
9779
9780 void
9781 dwarf2out_finish ()
9782 {
9783   limbo_die_node *node, *next_node;
9784   dw_die_ref die;
9785   dw_attr_ref a;
9786
9787   /* Traverse the limbo die list, and add parent/child links.  The only
9788      dies without parents that should be here are concrete instances of
9789      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
9790      For concrete instances, we can get the parent die from the abstract
9791      instance.  */
9792   for (node = limbo_die_list; node; node = next_node)
9793     {
9794       next_node = node->next;
9795       die = node->die;
9796
9797       if (die->die_parent == NULL)
9798         {
9799           a = get_AT (die, DW_AT_abstract_origin);
9800           if (a)
9801             add_child_die (a->dw_attr_val.v.val_die_ref->die_parent, die);
9802           else if (die == comp_unit_die)
9803               ;
9804           else
9805             abort ();
9806         }
9807       free (node);
9808     }
9809
9810   /* Traverse the DIE tree and add sibling attributes to those DIE's
9811      that have children.  */
9812   add_sibling_attributes (comp_unit_die);
9813
9814   /* Output a terminator label for the .text section.  */
9815   fputc ('\n', asm_out_file);
9816   ASM_OUTPUT_SECTION (asm_out_file, TEXT_SECTION);
9817   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, TEXT_END_LABEL, 0);
9818
9819 #if 0
9820   /* Output a terminator label for the .data section.  */
9821   fputc ('\n', asm_out_file);
9822   ASM_OUTPUT_SECTION (asm_out_file, DATA_SECTION);
9823   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, DATA_END_LABEL, 0);
9824
9825   /* Output a terminator label for the .bss section.  */
9826   fputc ('\n', asm_out_file);
9827   ASM_OUTPUT_SECTION (asm_out_file, BSS_SECTION);
9828   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, BSS_END_LABEL, 0);
9829 #endif
9830
9831   /* Output the source line correspondence table.  */
9832   if (line_info_table_in_use > 1 || separate_line_info_table_in_use)
9833     {
9834       fputc ('\n', asm_out_file);
9835       ASM_OUTPUT_SECTION (asm_out_file, DEBUG_LINE_SECTION);
9836       output_line_info ();
9837
9838       /* We can only use the low/high_pc attributes if all of the code
9839          was in .text.  */
9840       if (separate_line_info_table_in_use == 0)
9841         {
9842           add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, TEXT_SECTION);
9843           add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
9844         }
9845
9846       add_AT_section_offset (comp_unit_die, DW_AT_stmt_list, DEBUG_LINE_SECTION);
9847     }
9848
9849   /* Output the abbreviation table.  */
9850   fputc ('\n', asm_out_file);
9851   ASM_OUTPUT_SECTION (asm_out_file, ABBREV_SECTION);
9852   build_abbrev_table (comp_unit_die);
9853   output_abbrev_section ();
9854
9855   /* Initialize the beginning DIE offset - and calculate sizes/offsets.   */
9856   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9857   calc_die_sizes (comp_unit_die);
9858
9859   /* Output debugging information.  */
9860   fputc ('\n', asm_out_file);
9861   ASM_OUTPUT_SECTION (asm_out_file, DEBUG_INFO_SECTION);
9862   output_compilation_unit_header ();
9863   output_die (comp_unit_die);
9864
9865   if (pubname_table_in_use)
9866     {
9867       /* Output public names table.  */
9868       fputc ('\n', asm_out_file);
9869       ASM_OUTPUT_SECTION (asm_out_file, PUBNAMES_SECTION);
9870       output_pubnames ();
9871     }
9872
9873   if (fde_table_in_use)
9874     {
9875       /* Output the address range information.  */
9876       fputc ('\n', asm_out_file);
9877       ASM_OUTPUT_SECTION (asm_out_file, ARANGES_SECTION);
9878       output_aranges ();
9879     }
9880 }
9881 #endif /* DWARF2_DEBUGGING_INFO */