OSDN Git Service

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