OSDN Git Service

* emit-rtl.c (gen_lowpart_common) [REAL_ARITHMETICS]: Fix conversion
[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     head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
2831   else
2832     head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
2833   head->dw_loc_oprnd1.val_class = dw_val_class_const;
2834   tmp = new_loc_descr (DW_OP_deref, 0, 0);
2835   add_loc_descr (&head, tmp);
2836   if (cfa->offset != 0)
2837     {
2838       tmp = new_loc_descr (DW_OP_plus_uconst, cfa->offset, 0);
2839       add_loc_descr (&head, tmp);
2840     }
2841   return head;
2842 }
2843
2844 /* This function fills in aa dw_cfa_location structure from a
2845    dwarf location descriptor sequence.  */
2846
2847 static void
2848 get_cfa_from_loc_descr (cfa, loc)
2849      dw_cfa_location *cfa;
2850      struct dw_loc_descr_struct *loc;
2851 {
2852   struct dw_loc_descr_struct *ptr;
2853   cfa->offset = 0;
2854   cfa->base_offset = 0;
2855   cfa->indirect = 0;
2856   cfa->reg = -1;
2857
2858   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
2859     {
2860       enum dwarf_location_atom op = ptr->dw_loc_opc;
2861       switch (op)
2862         {
2863         case DW_OP_reg0:
2864         case DW_OP_reg1:
2865         case DW_OP_reg2:
2866         case DW_OP_reg3:
2867         case DW_OP_reg4:
2868         case DW_OP_reg5:
2869         case DW_OP_reg6:
2870         case DW_OP_reg7:
2871         case DW_OP_reg8:
2872         case DW_OP_reg9:
2873         case DW_OP_reg10:
2874         case DW_OP_reg11:
2875         case DW_OP_reg12:
2876         case DW_OP_reg13:
2877         case DW_OP_reg14:
2878         case DW_OP_reg15:
2879         case DW_OP_reg16:
2880         case DW_OP_reg17:
2881         case DW_OP_reg18:
2882         case DW_OP_reg19:
2883         case DW_OP_reg20:
2884         case DW_OP_reg21:
2885         case DW_OP_reg22:
2886         case DW_OP_reg23:
2887         case DW_OP_reg24:
2888         case DW_OP_reg25:
2889         case DW_OP_reg26:
2890         case DW_OP_reg27:
2891         case DW_OP_reg28:
2892         case DW_OP_reg29:
2893         case DW_OP_reg30:
2894         case DW_OP_reg31:
2895           cfa->reg = op - DW_OP_reg0;
2896           break;
2897         case DW_OP_regx:
2898           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
2899           break;
2900         case DW_OP_breg0:
2901         case DW_OP_breg1:
2902         case DW_OP_breg2:
2903         case DW_OP_breg3:
2904         case DW_OP_breg4:
2905         case DW_OP_breg5:
2906         case DW_OP_breg6:
2907         case DW_OP_breg7:
2908         case DW_OP_breg8:
2909         case DW_OP_breg9:
2910         case DW_OP_breg10:
2911         case DW_OP_breg11:
2912         case DW_OP_breg12:
2913         case DW_OP_breg13:
2914         case DW_OP_breg14:
2915         case DW_OP_breg15:
2916         case DW_OP_breg16:
2917         case DW_OP_breg17:
2918         case DW_OP_breg18:
2919         case DW_OP_breg19:
2920         case DW_OP_breg20:
2921         case DW_OP_breg21:
2922         case DW_OP_breg22:
2923         case DW_OP_breg23:
2924         case DW_OP_breg24:
2925         case DW_OP_breg25:
2926         case DW_OP_breg26:
2927         case DW_OP_breg27:
2928         case DW_OP_breg28:
2929         case DW_OP_breg29:
2930         case DW_OP_breg30:
2931         case DW_OP_breg31:
2932           cfa->reg = op - DW_OP_breg0;
2933           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
2934           break;
2935         case DW_OP_bregx:
2936           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
2937           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
2938           break;
2939         case DW_OP_deref:
2940           cfa->indirect = 1;
2941           break;
2942         case DW_OP_plus_uconst:
2943           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
2944           break;
2945         default:
2946           fatal ("DW_LOC_OP %s not implememnted yet.\n",
2947                  dwarf_stack_op_name (ptr->dw_loc_opc));
2948         }
2949     }
2950 }
2951 #endif /* .debug_frame support */
2952 \f
2953 /* And now, the support for symbolic debugging information.  */
2954 #ifdef DWARF2_DEBUGGING_INFO
2955
2956 /* NOTE: In the comments in this file, many references are made to
2957    "Debugging Information Entries".  This term is abbreviated as `DIE'
2958    throughout the remainder of this file.  */
2959
2960 /* An internal representation of the DWARF output is built, and then
2961    walked to generate the DWARF debugging info.  The walk of the internal
2962    representation is done after the entire program has been compiled.
2963    The types below are used to describe the internal representation.  */
2964
2965 /* Various DIE's use offsets relative to the beginning of the
2966    .debug_info section to refer to each other.  */
2967
2968 typedef long int dw_offset;
2969
2970 /* Define typedefs here to avoid circular dependencies.  */
2971
2972 typedef struct dw_attr_struct *dw_attr_ref;
2973 typedef struct dw_line_info_struct *dw_line_info_ref;
2974 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
2975 typedef struct pubname_struct *pubname_ref;
2976 typedef dw_die_ref *arange_ref;
2977
2978 /* Each entry in the line_info_table maintains the file and
2979    line number associated with the label generated for that
2980    entry.  The label gives the PC value associated with
2981    the line number entry.  */
2982
2983 typedef struct dw_line_info_struct
2984 {
2985   unsigned long dw_file_num;
2986   unsigned long dw_line_num;
2987 }
2988 dw_line_info_entry;
2989
2990 /* Line information for functions in separate sections; each one gets its
2991    own sequence.  */
2992 typedef struct dw_separate_line_info_struct
2993 {
2994   unsigned long dw_file_num;
2995   unsigned long dw_line_num;
2996   unsigned long function;
2997 }
2998 dw_separate_line_info_entry;
2999
3000 /* Each DIE attribute has a field specifying the attribute kind,
3001    a link to the next attribute in the chain, and an attribute value.
3002    Attributes are typically linked below the DIE they modify.  */
3003
3004 typedef struct dw_attr_struct
3005 {
3006   enum dwarf_attribute dw_attr;
3007   dw_attr_ref dw_attr_next;
3008   dw_val_node dw_attr_val;
3009 }
3010 dw_attr_node;
3011
3012 /* The Debugging Information Entry (DIE) structure */
3013
3014 typedef struct die_struct
3015 {
3016   enum dwarf_tag die_tag;
3017   char *die_symbol;
3018   dw_attr_ref die_attr;
3019   dw_die_ref die_parent;
3020   dw_die_ref die_child;
3021   dw_die_ref die_sib;
3022   dw_offset die_offset;
3023   unsigned long die_abbrev;
3024   int die_mark;
3025 }
3026 die_node;
3027
3028 /* The pubname structure */
3029
3030 typedef struct pubname_struct
3031 {
3032   dw_die_ref die;
3033   char *name;
3034 }
3035 pubname_entry;
3036
3037 /* The limbo die list structure.  */
3038 typedef struct limbo_die_struct
3039 {
3040   dw_die_ref die;
3041   struct limbo_die_struct *next;
3042 }
3043 limbo_die_node;
3044
3045 /* How to start an assembler comment.  */
3046 #ifndef ASM_COMMENT_START
3047 #define ASM_COMMENT_START ";#"
3048 #endif
3049
3050 /* Define a macro which returns non-zero for a TYPE_DECL which was
3051    implicitly generated for a tagged type.
3052
3053    Note that unlike the gcc front end (which generates a NULL named
3054    TYPE_DECL node for each complete tagged type, each array type, and
3055    each function type node created) the g++ front end generates a
3056    _named_ TYPE_DECL node for each tagged type node created.
3057    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3058    generate a DW_TAG_typedef DIE for them.  */
3059
3060 #define TYPE_DECL_IS_STUB(decl)                         \
3061   (DECL_NAME (decl) == NULL_TREE                        \
3062    || (DECL_ARTIFICIAL (decl)                           \
3063        && is_tagged_type (TREE_TYPE (decl))             \
3064        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
3065            /* This is necessary for stub decls that     \
3066               appear in nested inline functions.  */    \
3067            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3068                && (decl_ultimate_origin (decl)          \
3069                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3070
3071 /* Information concerning the compilation unit's programming
3072    language, and compiler version.  */
3073
3074 extern int flag_traditional;
3075
3076 /* Fixed size portion of the DWARF compilation unit header.  */
3077 #define DWARF_COMPILE_UNIT_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 3)
3078
3079 /* Fixed size portion of debugging line information prolog.  */
3080 #define DWARF_LINE_PROLOG_HEADER_SIZE 5
3081
3082 /* Fixed size portion of public names info.  */
3083 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3084
3085 /* Fixed size portion of the address range info.  */
3086 #define DWARF_ARANGES_HEADER_SIZE                                       \
3087   (DWARF_ROUND (2 * DWARF_OFFSET_SIZE + 4, DWARF2_ADDR_SIZE * 2)        \
3088    - DWARF_OFFSET_SIZE)
3089
3090 /* Size of padding portion in the address range info.  It must be
3091    aligned to twice the pointer size.  */
3092 #define DWARF_ARANGES_PAD_SIZE \
3093   (DWARF_ROUND (2 * DWARF_OFFSET_SIZE + 4, DWARF2_ADDR_SIZE * 2) \
3094    - (2 * DWARF_OFFSET_SIZE + 4))
3095
3096 /* The default is to have gcc emit the line number tables.  */
3097 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3098 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3099 #endif
3100
3101 /* Define the architecture-dependent minimum instruction length (in bytes).
3102    In this implementation of DWARF, this field is used for information
3103    purposes only.  Since GCC generates assembly language, we have
3104    no a priori knowledge of how many instruction bytes are generated
3105    for each source line, and therefore can use only the  DW_LNE_set_address
3106    and DW_LNS_fixed_advance_pc line information commands.  */
3107
3108 #ifndef DWARF_LINE_MIN_INSTR_LENGTH
3109 #define DWARF_LINE_MIN_INSTR_LENGTH 4
3110 #endif
3111
3112 /* Minimum line offset in a special line info. opcode.
3113    This value was chosen to give a reasonable range of values.  */
3114 #define DWARF_LINE_BASE  -10
3115
3116 /* First special line opcde - leave room for the standard opcodes.  */
3117 #define DWARF_LINE_OPCODE_BASE  10
3118
3119 /* Range of line offsets in a special line info. opcode.  */
3120 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
3121
3122 /* Flag that indicates the initial value of the is_stmt_start flag.
3123    In the present implementation, we do not mark any lines as
3124    the beginning of a source statement, because that information
3125    is not made available by the GCC front-end.  */
3126 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3127
3128 /* This location is used by calc_die_sizes() to keep track
3129    the offset of each DIE within the .debug_info section.  */
3130 static unsigned long next_die_offset;
3131
3132 /* Record the root of the DIE's built for the current compilation unit.  */
3133 static dw_die_ref comp_unit_die;
3134
3135 /* A list of DIEs with a NULL parent waiting to be relocated.  */
3136 static limbo_die_node *limbo_die_list = 0;
3137
3138 /* Pointer to an array of filenames referenced by this compilation unit.  */
3139 static char **file_table;
3140
3141 /* Total number of entries in the table (i.e. array) pointed to by
3142    `file_table'.  This is the *total* and includes both used and unused
3143    slots.  */
3144 static unsigned file_table_allocated;
3145
3146 /* Number of entries in the file_table which are actually in use.  */
3147 static unsigned file_table_in_use;
3148
3149 /* Size (in elements) of increments by which we may expand the filename
3150    table.  */
3151 #define FILE_TABLE_INCREMENT 64
3152
3153 /* Local pointer to the name of the main input file.  Initialized in
3154    dwarf2out_init.  */
3155 static const char *primary_filename;
3156
3157 /* A pointer to the base of a table of references to DIE's that describe
3158    declarations.  The table is indexed by DECL_UID() which is a unique
3159    number identifying each decl.  */
3160 static dw_die_ref *decl_die_table;
3161
3162 /* Number of elements currently allocated for the decl_die_table.  */
3163 static unsigned decl_die_table_allocated;
3164
3165 /* Number of elements in decl_die_table currently in use.  */
3166 static unsigned decl_die_table_in_use;
3167
3168 /* Size (in elements) of increments by which we may expand the
3169    decl_die_table.  */
3170 #define DECL_DIE_TABLE_INCREMENT 256
3171
3172 /* A pointer to the base of a table of references to declaration
3173    scopes.  This table is a display which tracks the nesting
3174    of declaration scopes at the current scope and containing
3175    scopes.  This table is used to find the proper place to
3176    define type declaration DIE's.  */
3177 static tree *decl_scope_table;
3178
3179 /* Number of elements currently allocated for the decl_scope_table.  */
3180 static int decl_scope_table_allocated;
3181
3182 /* Current level of nesting of declaration scopes.  */
3183 static int decl_scope_depth;
3184
3185 /* Size (in elements) of increments by which we may expand the
3186    decl_scope_table.  */
3187 #define DECL_SCOPE_TABLE_INCREMENT 64
3188
3189 /* A pointer to the base of a list of references to DIE's that
3190    are uniquely identified by their tag, presence/absence of
3191    children DIE's, and list of attribute/value pairs.  */
3192 static dw_die_ref *abbrev_die_table;
3193
3194 /* Number of elements currently allocated for abbrev_die_table.  */
3195 static unsigned abbrev_die_table_allocated;
3196
3197 /* Number of elements in type_die_table currently in use.  */
3198 static unsigned abbrev_die_table_in_use;
3199
3200 /* Size (in elements) of increments by which we may expand the
3201    abbrev_die_table.  */
3202 #define ABBREV_DIE_TABLE_INCREMENT 256
3203
3204 /* A pointer to the base of a table that contains line information
3205    for each source code line in .text in the compilation unit.  */
3206 static dw_line_info_ref line_info_table;
3207
3208 /* Number of elements currently allocated for line_info_table.  */
3209 static unsigned line_info_table_allocated;
3210
3211 /* Number of elements in separate_line_info_table currently in use.  */
3212 static unsigned separate_line_info_table_in_use;
3213
3214 /* A pointer to the base of a table that contains line information
3215    for each source code line outside of .text in the compilation unit.  */
3216 static dw_separate_line_info_ref separate_line_info_table;
3217
3218 /* Number of elements currently allocated for separate_line_info_table.  */
3219 static unsigned separate_line_info_table_allocated;
3220
3221 /* Number of elements in line_info_table currently in use.  */
3222 static unsigned line_info_table_in_use;
3223
3224 /* Size (in elements) of increments by which we may expand the
3225    line_info_table.  */
3226 #define LINE_INFO_TABLE_INCREMENT 1024
3227
3228 /* A pointer to the base of a table that contains a list of publicly
3229    accessible names.  */
3230 static pubname_ref pubname_table;
3231
3232 /* Number of elements currently allocated for pubname_table.  */
3233 static unsigned pubname_table_allocated;
3234
3235 /* Number of elements in pubname_table currently in use.  */
3236 static unsigned pubname_table_in_use;
3237
3238 /* Size (in elements) of increments by which we may expand the
3239    pubname_table.  */
3240 #define PUBNAME_TABLE_INCREMENT 64
3241
3242 /* A pointer to the base of a table that contains a list of publicly
3243    accessible names.  */
3244 static arange_ref arange_table;
3245
3246 /* Number of elements currently allocated for arange_table.  */
3247 static unsigned arange_table_allocated;
3248
3249 /* Number of elements in arange_table currently in use.  */
3250 static unsigned arange_table_in_use;
3251
3252 /* Size (in elements) of increments by which we may expand the
3253    arange_table.  */
3254 #define ARANGE_TABLE_INCREMENT 64
3255
3256 /* A pointer to the base of a list of incomplete types which might be
3257    completed at some later time.  */
3258
3259 static tree *incomplete_types_list;
3260
3261 /* Number of elements currently allocated for the incomplete_types_list.  */
3262 static unsigned incomplete_types_allocated;
3263
3264 /* Number of elements of incomplete_types_list currently in use.  */
3265 static unsigned incomplete_types;
3266
3267 /* Size (in elements) of increments by which we may expand the incomplete
3268    types list.  Actually, a single hunk of space of this size should
3269    be enough for most typical programs.  */
3270 #define INCOMPLETE_TYPES_INCREMENT 64
3271
3272 /* Record whether the function being analyzed contains inlined functions.  */
3273 static int current_function_has_inlines;
3274 #if 0 && defined (MIPS_DEBUGGING_INFO)
3275 static int comp_unit_has_inlines;
3276 #endif
3277
3278 /* Array of RTXes referenced by the debugging information, which therefore
3279    must be kept around forever.  We do this rather than perform GC on
3280    the dwarf info because almost all of the dwarf info lives forever, and
3281    it's easier to support non-GC frontends this way.  */
3282 static varray_type used_rtx_varray;
3283
3284 /* Forward declarations for functions defined in this file.  */
3285
3286 static int is_pseudo_reg                PARAMS ((rtx));
3287 static tree type_main_variant           PARAMS ((tree));
3288 static int is_tagged_type               PARAMS ((tree));
3289 static const char *dwarf_tag_name       PARAMS ((unsigned));
3290 static const char *dwarf_attr_name      PARAMS ((unsigned));
3291 static const char *dwarf_form_name      PARAMS ((unsigned));
3292 #if 0
3293 static const char *dwarf_type_encoding_name PARAMS ((unsigned));
3294 #endif
3295 static tree decl_ultimate_origin        PARAMS ((tree));
3296 static tree block_ultimate_origin       PARAMS ((tree));
3297 static tree decl_class_context          PARAMS ((tree));
3298 static void add_dwarf_attr              PARAMS ((dw_die_ref, dw_attr_ref));
3299 static void add_AT_flag                 PARAMS ((dw_die_ref,
3300                                                  enum dwarf_attribute,
3301                                                  unsigned));
3302 static void add_AT_int                  PARAMS ((dw_die_ref,
3303                                                  enum dwarf_attribute, long));
3304 static void add_AT_unsigned             PARAMS ((dw_die_ref,
3305                                                  enum dwarf_attribute,
3306                                                  unsigned long));
3307 static void add_AT_long_long            PARAMS ((dw_die_ref,
3308                                                  enum dwarf_attribute,
3309                                                  unsigned long,
3310                                                  unsigned long));
3311 static void add_AT_float                PARAMS ((dw_die_ref,
3312                                                  enum dwarf_attribute,
3313                                                  unsigned, long *));
3314 static void add_AT_string               PARAMS ((dw_die_ref,
3315                                                  enum dwarf_attribute,
3316                                                  const char *));
3317 static void add_AT_die_ref              PARAMS ((dw_die_ref,
3318                                                  enum dwarf_attribute,
3319                                                  dw_die_ref));
3320 static void add_AT_fde_ref              PARAMS ((dw_die_ref,
3321                                                  enum dwarf_attribute,
3322                                                  unsigned));
3323 static void add_AT_loc                  PARAMS ((dw_die_ref,
3324                                                  enum dwarf_attribute,
3325                                                  dw_loc_descr_ref));
3326 static void add_AT_addr                 PARAMS ((dw_die_ref,
3327                                                  enum dwarf_attribute,
3328                                                  rtx));
3329 static void add_AT_lbl_id               PARAMS ((dw_die_ref,
3330                                                  enum dwarf_attribute,
3331                                                  const char *));
3332 static void add_AT_lbl_offset           PARAMS ((dw_die_ref,
3333                                                  enum dwarf_attribute,
3334                                                  const char *));
3335 static dw_attr_ref get_AT               PARAMS ((dw_die_ref,
3336                                                  enum dwarf_attribute));
3337 static const char *get_AT_low_pc        PARAMS ((dw_die_ref));
3338 static const char *get_AT_hi_pc         PARAMS ((dw_die_ref));
3339 static const char *get_AT_string        PARAMS ((dw_die_ref,
3340                                                  enum dwarf_attribute));
3341 static int get_AT_flag                  PARAMS ((dw_die_ref,
3342                                                  enum dwarf_attribute));
3343 static unsigned get_AT_unsigned         PARAMS ((dw_die_ref,
3344                                                  enum dwarf_attribute));
3345 static inline dw_die_ref get_AT_ref     PARAMS ((dw_die_ref,
3346                                                  enum dwarf_attribute));
3347 static int is_c_family                  PARAMS ((void));
3348 static int is_java                      PARAMS ((void));
3349 static int is_fortran                   PARAMS ((void));
3350 static void remove_AT                   PARAMS ((dw_die_ref,
3351                                                  enum dwarf_attribute));
3352 static void remove_children             PARAMS ((dw_die_ref));
3353 static void add_child_die               PARAMS ((dw_die_ref, dw_die_ref));
3354 static dw_die_ref new_die               PARAMS ((enum dwarf_tag, dw_die_ref));
3355 static dw_die_ref lookup_type_die       PARAMS ((tree));
3356 static void equate_type_number_to_die   PARAMS ((tree, dw_die_ref));
3357 static dw_die_ref lookup_decl_die       PARAMS ((tree));
3358 static void equate_decl_number_to_die   PARAMS ((tree, dw_die_ref));
3359 static void print_spaces                PARAMS ((FILE *));
3360 static void print_die                   PARAMS ((dw_die_ref, FILE *));
3361 static void print_dwarf_line_table      PARAMS ((FILE *));
3362 static void reverse_die_lists           PARAMS ((dw_die_ref));
3363 static void reverse_all_dies            PARAMS ((dw_die_ref));
3364 static dw_die_ref push_new_compile_unit PARAMS ((dw_die_ref, dw_die_ref));
3365 static dw_die_ref pop_compile_unit      PARAMS ((dw_die_ref));
3366 static void loc_checksum         PARAMS ((dw_loc_descr_ref, struct md5_ctx *));
3367 static void attr_checksum             PARAMS ((dw_attr_ref, struct md5_ctx *));
3368 static void die_checksum               PARAMS ((dw_die_ref, struct md5_ctx *));
3369 static void compute_section_prefix      PARAMS ((dw_die_ref));
3370 static int is_type_die                  PARAMS ((dw_die_ref));
3371 static int is_comdat_die                PARAMS ((dw_die_ref));
3372 static int is_symbol_die                PARAMS ((dw_die_ref));
3373 static char *gen_internal_sym           PARAMS ((void));
3374 static void assign_symbol_names         PARAMS ((dw_die_ref));
3375 static void break_out_includes          PARAMS ((dw_die_ref));
3376 static void add_sibling_attributes      PARAMS ((dw_die_ref));
3377 static void build_abbrev_table          PARAMS ((dw_die_ref));
3378 static unsigned long size_of_string     PARAMS ((const char *));
3379 static int constant_size                PARAMS ((long unsigned));
3380 static unsigned long size_of_die        PARAMS ((dw_die_ref));
3381 static void calc_die_sizes              PARAMS ((dw_die_ref));
3382 static void mark_dies                   PARAMS ((dw_die_ref));
3383 static void unmark_dies                 PARAMS ((dw_die_ref));
3384 static unsigned long size_of_line_prolog PARAMS ((void));
3385 static unsigned long size_of_pubnames   PARAMS ((void));
3386 static unsigned long size_of_aranges    PARAMS ((void));
3387 static enum dwarf_form value_format     PARAMS ((dw_attr_ref));
3388 static void output_value_format         PARAMS ((dw_attr_ref));
3389 static void output_abbrev_section       PARAMS ((void));
3390 static void output_die_symbol           PARAMS ((dw_die_ref));
3391 static void output_symbolic_ref         PARAMS ((dw_die_ref));
3392 static void output_die                  PARAMS ((dw_die_ref));
3393 static void output_compilation_unit_header PARAMS ((void));
3394 static void output_comp_unit            PARAMS ((dw_die_ref));
3395 static const char *dwarf2_name          PARAMS ((tree, int));
3396 static void add_pubname                 PARAMS ((tree, dw_die_ref));
3397 static void output_pubnames             PARAMS ((void));
3398 static void add_arange                  PARAMS ((tree, dw_die_ref));
3399 static void output_aranges              PARAMS ((void));
3400 static void output_line_info            PARAMS ((void));
3401 static dw_die_ref base_type_die         PARAMS ((tree));
3402 static tree root_type                   PARAMS ((tree));
3403 static int is_base_type                 PARAMS ((tree));
3404 static dw_die_ref modified_type_die     PARAMS ((tree, int, int, dw_die_ref));
3405 static int type_is_enum                 PARAMS ((tree));
3406 static unsigned int reg_number          PARAMS ((rtx));
3407 static dw_loc_descr_ref reg_loc_descriptor PARAMS ((rtx));
3408 static dw_loc_descr_ref based_loc_descr PARAMS ((unsigned, long));
3409 static int is_based_loc                 PARAMS ((rtx));
3410 static dw_loc_descr_ref mem_loc_descriptor PARAMS ((rtx, enum machine_mode mode));
3411 static dw_loc_descr_ref concat_loc_descriptor PARAMS ((rtx, rtx));
3412 static dw_loc_descr_ref loc_descriptor  PARAMS ((rtx));
3413 static HOST_WIDE_INT ceiling            PARAMS ((HOST_WIDE_INT, unsigned int));
3414 static tree field_type                  PARAMS ((tree));
3415 static unsigned int simple_type_align_in_bits PARAMS ((tree));
3416 static unsigned HOST_WIDE_INT simple_type_size_in_bits PARAMS ((tree));
3417 static HOST_WIDE_INT field_byte_offset  PARAMS ((tree));
3418 static void add_AT_location_description PARAMS ((dw_die_ref,
3419                                                  enum dwarf_attribute, rtx));
3420 static void add_data_member_location_attribute PARAMS ((dw_die_ref, tree));
3421 static void add_const_value_attribute   PARAMS ((dw_die_ref, rtx));
3422 static void add_location_or_const_value_attribute PARAMS ((dw_die_ref, tree));
3423 static void tree_add_const_value_attribute PARAMS ((dw_die_ref, tree));
3424 static void add_name_attribute          PARAMS ((dw_die_ref, const char *));
3425 static void add_bound_info              PARAMS ((dw_die_ref,
3426                                                  enum dwarf_attribute, tree));
3427 static void add_subscript_info          PARAMS ((dw_die_ref, tree));
3428 static void add_byte_size_attribute     PARAMS ((dw_die_ref, tree));
3429 static void add_bit_offset_attribute    PARAMS ((dw_die_ref, tree));
3430 static void add_bit_size_attribute      PARAMS ((dw_die_ref, tree));
3431 static void add_prototyped_attribute    PARAMS ((dw_die_ref, tree));
3432 static void add_abstract_origin_attribute PARAMS ((dw_die_ref, tree));
3433 static void add_pure_or_virtual_attribute PARAMS ((dw_die_ref, tree));
3434 static void add_src_coords_attributes   PARAMS ((dw_die_ref, tree));
3435 static void add_name_and_src_coords_attributes PARAMS ((dw_die_ref, tree));
3436 static void push_decl_scope             PARAMS ((tree));
3437 static dw_die_ref scope_die_for         PARAMS ((tree, dw_die_ref));
3438 static void pop_decl_scope              PARAMS ((void));
3439 static void add_type_attribute          PARAMS ((dw_die_ref, tree, int, int,
3440                                                  dw_die_ref));
3441 static const char *type_tag             PARAMS ((tree));
3442 static tree member_declared_type        PARAMS ((tree));
3443 #if 0
3444 static const char *decl_start_label     PARAMS ((tree));
3445 #endif
3446 static void gen_array_type_die          PARAMS ((tree, dw_die_ref));
3447 static void gen_set_type_die            PARAMS ((tree, dw_die_ref));
3448 #if 0
3449 static void gen_entry_point_die         PARAMS ((tree, dw_die_ref));
3450 #endif
3451 static void gen_inlined_enumeration_type_die PARAMS ((tree, dw_die_ref));
3452 static void gen_inlined_structure_type_die PARAMS ((tree, dw_die_ref));
3453 static void gen_inlined_union_type_die  PARAMS ((tree, dw_die_ref));
3454 static void gen_enumeration_type_die    PARAMS ((tree, dw_die_ref));
3455 static dw_die_ref gen_formal_parameter_die PARAMS ((tree, dw_die_ref));
3456 static void gen_unspecified_parameters_die PARAMS ((tree, dw_die_ref));
3457 static void gen_formal_types_die        PARAMS ((tree, dw_die_ref));
3458 static void gen_subprogram_die          PARAMS ((tree, dw_die_ref));
3459 static void gen_variable_die            PARAMS ((tree, dw_die_ref));
3460 static void gen_label_die               PARAMS ((tree, dw_die_ref));
3461 static void gen_lexical_block_die       PARAMS ((tree, dw_die_ref, int));
3462 static void gen_inlined_subroutine_die  PARAMS ((tree, dw_die_ref, int));
3463 static void gen_field_die               PARAMS ((tree, dw_die_ref));
3464 static void gen_ptr_to_mbr_type_die     PARAMS ((tree, dw_die_ref));
3465 static dw_die_ref gen_compile_unit_die  PARAMS ((const char *));
3466 static void gen_string_type_die         PARAMS ((tree, dw_die_ref));
3467 static void gen_inheritance_die         PARAMS ((tree, dw_die_ref));
3468 static void gen_member_die              PARAMS ((tree, dw_die_ref));
3469 static void gen_struct_or_union_type_die PARAMS ((tree, dw_die_ref));
3470 static void gen_subroutine_type_die     PARAMS ((tree, dw_die_ref));
3471 static void gen_typedef_die             PARAMS ((tree, dw_die_ref));
3472 static void gen_type_die                PARAMS ((tree, dw_die_ref));
3473 static void gen_tagged_type_instantiation_die PARAMS ((tree, dw_die_ref));
3474 static void gen_block_die               PARAMS ((tree, dw_die_ref, int));
3475 static void decls_for_scope             PARAMS ((tree, dw_die_ref, int));
3476 static int is_redundant_typedef         PARAMS ((tree));
3477 static void gen_decl_die                PARAMS ((tree, dw_die_ref));
3478 static unsigned lookup_filename         PARAMS ((const char *));
3479 static void add_incomplete_type         PARAMS ((tree));
3480 static void retry_incomplete_types      PARAMS ((void));
3481 static void gen_type_die_for_member     PARAMS ((tree, tree, dw_die_ref));
3482 static void gen_abstract_function       PARAMS ((tree));
3483 static rtx save_rtx                     PARAMS ((rtx));
3484 static void splice_child_die            PARAMS ((dw_die_ref, dw_die_ref));
3485
3486 /* Section names used to hold DWARF debugging information.  */
3487 #ifndef DEBUG_INFO_SECTION
3488 #define DEBUG_INFO_SECTION      ".debug_info"
3489 #endif
3490 #ifndef ABBREV_SECTION
3491 #define ABBREV_SECTION          ".debug_abbrev"
3492 #endif
3493 #ifndef ARANGES_SECTION
3494 #define ARANGES_SECTION         ".debug_aranges"
3495 #endif
3496 #ifndef DW_MACINFO_SECTION
3497 #define DW_MACINFO_SECTION      ".debug_macinfo"
3498 #endif
3499 #ifndef DEBUG_LINE_SECTION
3500 #define DEBUG_LINE_SECTION      ".debug_line"
3501 #endif
3502 #ifndef LOC_SECTION
3503 #define LOC_SECTION             ".debug_loc"
3504 #endif
3505 #ifndef PUBNAMES_SECTION
3506 #define PUBNAMES_SECTION        ".debug_pubnames"
3507 #endif
3508 #ifndef STR_SECTION
3509 #define STR_SECTION             ".debug_str"
3510 #endif
3511
3512 /* Standard ELF section names for compiled code and data.  */
3513 #ifndef TEXT_SECTION
3514 #define TEXT_SECTION            ".text"
3515 #endif
3516 #ifndef DATA_SECTION
3517 #define DATA_SECTION            ".data"
3518 #endif
3519 #ifndef BSS_SECTION
3520 #define BSS_SECTION             ".bss"
3521 #endif
3522
3523 /* Labels we insert at beginning sections we can reference instead of
3524    the section names themselves.  */
3525
3526 #ifndef TEXT_SECTION_LABEL
3527 #define TEXT_SECTION_LABEL       "Ltext"
3528 #endif
3529 #ifndef DEBUG_LINE_SECTION_LABEL
3530 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3531 #endif
3532 #ifndef DEBUG_INFO_SECTION_LABEL
3533 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3534 #endif
3535 #ifndef ABBREV_SECTION_LABEL
3536 #define ABBREV_SECTION_LABEL     "Ldebug_abbrev"
3537 #endif
3538
3539 /* Definitions of defaults for formats and names of various special
3540    (artificial) labels which may be generated within this file (when the -g
3541    options is used and DWARF_DEBUGGING_INFO is in effect.
3542    If necessary, these may be overridden from within the tm.h file, but
3543    typically, overriding these defaults is unnecessary.  */
3544
3545 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3546 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3547 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3548 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3549 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3550
3551 #ifndef TEXT_END_LABEL
3552 #define TEXT_END_LABEL          "Letext"
3553 #endif
3554 #ifndef DATA_END_LABEL
3555 #define DATA_END_LABEL          "Ledata"
3556 #endif
3557 #ifndef BSS_END_LABEL
3558 #define BSS_END_LABEL           "Lebss"
3559 #endif
3560 #ifndef INSN_LABEL_FMT
3561 #define INSN_LABEL_FMT          "LI%u_"
3562 #endif
3563 #ifndef BLOCK_BEGIN_LABEL
3564 #define BLOCK_BEGIN_LABEL       "LBB"
3565 #endif
3566 #ifndef BLOCK_END_LABEL
3567 #define BLOCK_END_LABEL         "LBE"
3568 #endif
3569 #ifndef BODY_BEGIN_LABEL
3570 #define BODY_BEGIN_LABEL        "Lbb"
3571 #endif
3572 #ifndef BODY_END_LABEL
3573 #define BODY_END_LABEL          "Lbe"
3574 #endif
3575 #ifndef LINE_CODE_LABEL
3576 #define LINE_CODE_LABEL         "LM"
3577 #endif
3578 #ifndef SEPARATE_LINE_CODE_LABEL
3579 #define SEPARATE_LINE_CODE_LABEL        "LSM"
3580 #endif
3581 \f
3582 /* We allow a language front-end to designate a function that is to be
3583    called to "demangle" any name before it it put into a DIE.  */
3584
3585 static const char *(*demangle_name_func) PARAMS ((const char *));
3586
3587 void
3588 dwarf2out_set_demangle_name_func (func)
3589      const char *(*func) PARAMS ((const char *));
3590 {
3591   demangle_name_func = func;
3592 }
3593 \f
3594 /* Return an rtx like ORIG which lives forever.  If we're doing GC,
3595    that means adding it to used_rtx_varray.  If not, that means making
3596    a copy on the permanent_obstack.  */
3597
3598 static rtx
3599 save_rtx (orig)
3600      register rtx orig;
3601 {
3602   if (ggc_p)
3603     VARRAY_PUSH_RTX (used_rtx_varray, orig);
3604   else
3605     {
3606       push_obstacks_nochange ();
3607       end_temporary_allocation ();
3608       orig = copy_rtx (orig);
3609       pop_obstacks ();
3610     }
3611
3612   return orig;
3613 }
3614
3615 /* Test if rtl node points to a pseudo register.  */
3616
3617 static inline int
3618 is_pseudo_reg (rtl)
3619      register rtx rtl;
3620 {
3621   return ((GET_CODE (rtl) == REG && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3622           || (GET_CODE (rtl) == SUBREG
3623               && REGNO (XEXP (rtl, 0)) >= FIRST_PSEUDO_REGISTER));
3624 }
3625
3626 /* Return a reference to a type, with its const and volatile qualifiers
3627    removed.  */
3628
3629 static inline tree
3630 type_main_variant (type)
3631      register tree type;
3632 {
3633   type = TYPE_MAIN_VARIANT (type);
3634
3635   /* There really should be only one main variant among any group of variants
3636      of a given type (and all of the MAIN_VARIANT values for all members of
3637      the group should point to that one type) but sometimes the C front-end
3638      messes this up for array types, so we work around that bug here.  */
3639
3640   if (TREE_CODE (type) == ARRAY_TYPE)
3641     while (type != TYPE_MAIN_VARIANT (type))
3642       type = TYPE_MAIN_VARIANT (type);
3643
3644   return type;
3645 }
3646
3647 /* Return non-zero if the given type node represents a tagged type.  */
3648
3649 static inline int
3650 is_tagged_type (type)
3651      register tree type;
3652 {
3653   register enum tree_code code = TREE_CODE (type);
3654
3655   return (code == RECORD_TYPE || code == UNION_TYPE
3656           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3657 }
3658
3659 /* Convert a DIE tag into its string name.  */
3660
3661 static const char *
3662 dwarf_tag_name (tag)
3663      register unsigned tag;
3664 {
3665   switch (tag)
3666     {
3667     case DW_TAG_padding:
3668       return "DW_TAG_padding";
3669     case DW_TAG_array_type:
3670       return "DW_TAG_array_type";
3671     case DW_TAG_class_type:
3672       return "DW_TAG_class_type";
3673     case DW_TAG_entry_point:
3674       return "DW_TAG_entry_point";
3675     case DW_TAG_enumeration_type:
3676       return "DW_TAG_enumeration_type";
3677     case DW_TAG_formal_parameter:
3678       return "DW_TAG_formal_parameter";
3679     case DW_TAG_imported_declaration:
3680       return "DW_TAG_imported_declaration";
3681     case DW_TAG_label:
3682       return "DW_TAG_label";
3683     case DW_TAG_lexical_block:
3684       return "DW_TAG_lexical_block";
3685     case DW_TAG_member:
3686       return "DW_TAG_member";
3687     case DW_TAG_pointer_type:
3688       return "DW_TAG_pointer_type";
3689     case DW_TAG_reference_type:
3690       return "DW_TAG_reference_type";
3691     case DW_TAG_compile_unit:
3692       return "DW_TAG_compile_unit";
3693     case DW_TAG_string_type:
3694       return "DW_TAG_string_type";
3695     case DW_TAG_structure_type:
3696       return "DW_TAG_structure_type";
3697     case DW_TAG_subroutine_type:
3698       return "DW_TAG_subroutine_type";
3699     case DW_TAG_typedef:
3700       return "DW_TAG_typedef";
3701     case DW_TAG_union_type:
3702       return "DW_TAG_union_type";
3703     case DW_TAG_unspecified_parameters:
3704       return "DW_TAG_unspecified_parameters";
3705     case DW_TAG_variant:
3706       return "DW_TAG_variant";
3707     case DW_TAG_common_block:
3708       return "DW_TAG_common_block";
3709     case DW_TAG_common_inclusion:
3710       return "DW_TAG_common_inclusion";
3711     case DW_TAG_inheritance:
3712       return "DW_TAG_inheritance";
3713     case DW_TAG_inlined_subroutine:
3714       return "DW_TAG_inlined_subroutine";
3715     case DW_TAG_module:
3716       return "DW_TAG_module";
3717     case DW_TAG_ptr_to_member_type:
3718       return "DW_TAG_ptr_to_member_type";
3719     case DW_TAG_set_type:
3720       return "DW_TAG_set_type";
3721     case DW_TAG_subrange_type:
3722       return "DW_TAG_subrange_type";
3723     case DW_TAG_with_stmt:
3724       return "DW_TAG_with_stmt";
3725     case DW_TAG_access_declaration:
3726       return "DW_TAG_access_declaration";
3727     case DW_TAG_base_type:
3728       return "DW_TAG_base_type";
3729     case DW_TAG_catch_block:
3730       return "DW_TAG_catch_block";
3731     case DW_TAG_const_type:
3732       return "DW_TAG_const_type";
3733     case DW_TAG_constant:
3734       return "DW_TAG_constant";
3735     case DW_TAG_enumerator:
3736       return "DW_TAG_enumerator";
3737     case DW_TAG_file_type:
3738       return "DW_TAG_file_type";
3739     case DW_TAG_friend:
3740       return "DW_TAG_friend";
3741     case DW_TAG_namelist:
3742       return "DW_TAG_namelist";
3743     case DW_TAG_namelist_item:
3744       return "DW_TAG_namelist_item";
3745     case DW_TAG_packed_type:
3746       return "DW_TAG_packed_type";
3747     case DW_TAG_subprogram:
3748       return "DW_TAG_subprogram";
3749     case DW_TAG_template_type_param:
3750       return "DW_TAG_template_type_param";
3751     case DW_TAG_template_value_param:
3752       return "DW_TAG_template_value_param";
3753     case DW_TAG_thrown_type:
3754       return "DW_TAG_thrown_type";
3755     case DW_TAG_try_block:
3756       return "DW_TAG_try_block";
3757     case DW_TAG_variant_part:
3758       return "DW_TAG_variant_part";
3759     case DW_TAG_variable:
3760       return "DW_TAG_variable";
3761     case DW_TAG_volatile_type:
3762       return "DW_TAG_volatile_type";
3763     case DW_TAG_MIPS_loop:
3764       return "DW_TAG_MIPS_loop";
3765     case DW_TAG_format_label:
3766       return "DW_TAG_format_label";
3767     case DW_TAG_function_template:
3768       return "DW_TAG_function_template";
3769     case DW_TAG_class_template:
3770       return "DW_TAG_class_template";
3771     case DW_TAG_GNU_BINCL:
3772       return "DW_TAG_GNU_BINCL";
3773     case DW_TAG_GNU_EINCL:
3774       return "DW_TAG_GNU_EINCL";
3775     default:
3776       return "DW_TAG_<unknown>";
3777     }
3778 }
3779
3780 /* Convert a DWARF attribute code into its string name.  */
3781
3782 static const char *
3783 dwarf_attr_name (attr)
3784      register unsigned attr;
3785 {
3786   switch (attr)
3787     {
3788     case DW_AT_sibling:
3789       return "DW_AT_sibling";
3790     case DW_AT_location:
3791       return "DW_AT_location";
3792     case DW_AT_name:
3793       return "DW_AT_name";
3794     case DW_AT_ordering:
3795       return "DW_AT_ordering";
3796     case DW_AT_subscr_data:
3797       return "DW_AT_subscr_data";
3798     case DW_AT_byte_size:
3799       return "DW_AT_byte_size";
3800     case DW_AT_bit_offset:
3801       return "DW_AT_bit_offset";
3802     case DW_AT_bit_size:
3803       return "DW_AT_bit_size";
3804     case DW_AT_element_list:
3805       return "DW_AT_element_list";
3806     case DW_AT_stmt_list:
3807       return "DW_AT_stmt_list";
3808     case DW_AT_low_pc:
3809       return "DW_AT_low_pc";
3810     case DW_AT_high_pc:
3811       return "DW_AT_high_pc";
3812     case DW_AT_language:
3813       return "DW_AT_language";
3814     case DW_AT_member:
3815       return "DW_AT_member";
3816     case DW_AT_discr:
3817       return "DW_AT_discr";
3818     case DW_AT_discr_value:
3819       return "DW_AT_discr_value";
3820     case DW_AT_visibility:
3821       return "DW_AT_visibility";
3822     case DW_AT_import:
3823       return "DW_AT_import";
3824     case DW_AT_string_length:
3825       return "DW_AT_string_length";
3826     case DW_AT_common_reference:
3827       return "DW_AT_common_reference";
3828     case DW_AT_comp_dir:
3829       return "DW_AT_comp_dir";
3830     case DW_AT_const_value:
3831       return "DW_AT_const_value";
3832     case DW_AT_containing_type:
3833       return "DW_AT_containing_type";
3834     case DW_AT_default_value:
3835       return "DW_AT_default_value";
3836     case DW_AT_inline:
3837       return "DW_AT_inline";
3838     case DW_AT_is_optional:
3839       return "DW_AT_is_optional";
3840     case DW_AT_lower_bound:
3841       return "DW_AT_lower_bound";
3842     case DW_AT_producer:
3843       return "DW_AT_producer";
3844     case DW_AT_prototyped:
3845       return "DW_AT_prototyped";
3846     case DW_AT_return_addr:
3847       return "DW_AT_return_addr";
3848     case DW_AT_start_scope:
3849       return "DW_AT_start_scope";
3850     case DW_AT_stride_size:
3851       return "DW_AT_stride_size";
3852     case DW_AT_upper_bound:
3853       return "DW_AT_upper_bound";
3854     case DW_AT_abstract_origin:
3855       return "DW_AT_abstract_origin";
3856     case DW_AT_accessibility:
3857       return "DW_AT_accessibility";
3858     case DW_AT_address_class:
3859       return "DW_AT_address_class";
3860     case DW_AT_artificial:
3861       return "DW_AT_artificial";
3862     case DW_AT_base_types:
3863       return "DW_AT_base_types";
3864     case DW_AT_calling_convention:
3865       return "DW_AT_calling_convention";
3866     case DW_AT_count:
3867       return "DW_AT_count";
3868     case DW_AT_data_member_location:
3869       return "DW_AT_data_member_location";
3870     case DW_AT_decl_column:
3871       return "DW_AT_decl_column";
3872     case DW_AT_decl_file:
3873       return "DW_AT_decl_file";
3874     case DW_AT_decl_line:
3875       return "DW_AT_decl_line";
3876     case DW_AT_declaration:
3877       return "DW_AT_declaration";
3878     case DW_AT_discr_list:
3879       return "DW_AT_discr_list";
3880     case DW_AT_encoding:
3881       return "DW_AT_encoding";
3882     case DW_AT_external:
3883       return "DW_AT_external";
3884     case DW_AT_frame_base:
3885       return "DW_AT_frame_base";
3886     case DW_AT_friend:
3887       return "DW_AT_friend";
3888     case DW_AT_identifier_case:
3889       return "DW_AT_identifier_case";
3890     case DW_AT_macro_info:
3891       return "DW_AT_macro_info";
3892     case DW_AT_namelist_items:
3893       return "DW_AT_namelist_items";
3894     case DW_AT_priority:
3895       return "DW_AT_priority";
3896     case DW_AT_segment:
3897       return "DW_AT_segment";
3898     case DW_AT_specification:
3899       return "DW_AT_specification";
3900     case DW_AT_static_link:
3901       return "DW_AT_static_link";
3902     case DW_AT_type:
3903       return "DW_AT_type";
3904     case DW_AT_use_location:
3905       return "DW_AT_use_location";
3906     case DW_AT_variable_parameter:
3907       return "DW_AT_variable_parameter";
3908     case DW_AT_virtuality:
3909       return "DW_AT_virtuality";
3910     case DW_AT_vtable_elem_location:
3911       return "DW_AT_vtable_elem_location";
3912
3913     case DW_AT_MIPS_fde:
3914       return "DW_AT_MIPS_fde";
3915     case DW_AT_MIPS_loop_begin:
3916       return "DW_AT_MIPS_loop_begin";
3917     case DW_AT_MIPS_tail_loop_begin:
3918       return "DW_AT_MIPS_tail_loop_begin";
3919     case DW_AT_MIPS_epilog_begin:
3920       return "DW_AT_MIPS_epilog_begin";
3921     case DW_AT_MIPS_loop_unroll_factor:
3922       return "DW_AT_MIPS_loop_unroll_factor";
3923     case DW_AT_MIPS_software_pipeline_depth:
3924       return "DW_AT_MIPS_software_pipeline_depth";
3925     case DW_AT_MIPS_linkage_name:
3926       return "DW_AT_MIPS_linkage_name";
3927     case DW_AT_MIPS_stride:
3928       return "DW_AT_MIPS_stride";
3929     case DW_AT_MIPS_abstract_name:
3930       return "DW_AT_MIPS_abstract_name";
3931     case DW_AT_MIPS_clone_origin:
3932       return "DW_AT_MIPS_clone_origin";
3933     case DW_AT_MIPS_has_inlines:
3934       return "DW_AT_MIPS_has_inlines";
3935
3936     case DW_AT_sf_names:
3937       return "DW_AT_sf_names";
3938     case DW_AT_src_info:
3939       return "DW_AT_src_info";
3940     case DW_AT_mac_info:
3941       return "DW_AT_mac_info";
3942     case DW_AT_src_coords:
3943       return "DW_AT_src_coords";
3944     case DW_AT_body_begin:
3945       return "DW_AT_body_begin";
3946     case DW_AT_body_end:
3947       return "DW_AT_body_end";
3948     default:
3949       return "DW_AT_<unknown>";
3950     }
3951 }
3952
3953 /* Convert a DWARF value form code into its string name.  */
3954
3955 static const char *
3956 dwarf_form_name (form)
3957      register unsigned form;
3958 {
3959   switch (form)
3960     {
3961     case DW_FORM_addr:
3962       return "DW_FORM_addr";
3963     case DW_FORM_block2:
3964       return "DW_FORM_block2";
3965     case DW_FORM_block4:
3966       return "DW_FORM_block4";
3967     case DW_FORM_data2:
3968       return "DW_FORM_data2";
3969     case DW_FORM_data4:
3970       return "DW_FORM_data4";
3971     case DW_FORM_data8:
3972       return "DW_FORM_data8";
3973     case DW_FORM_string:
3974       return "DW_FORM_string";
3975     case DW_FORM_block:
3976       return "DW_FORM_block";
3977     case DW_FORM_block1:
3978       return "DW_FORM_block1";
3979     case DW_FORM_data1:
3980       return "DW_FORM_data1";
3981     case DW_FORM_flag:
3982       return "DW_FORM_flag";
3983     case DW_FORM_sdata:
3984       return "DW_FORM_sdata";
3985     case DW_FORM_strp:
3986       return "DW_FORM_strp";
3987     case DW_FORM_udata:
3988       return "DW_FORM_udata";
3989     case DW_FORM_ref_addr:
3990       return "DW_FORM_ref_addr";
3991     case DW_FORM_ref1:
3992       return "DW_FORM_ref1";
3993     case DW_FORM_ref2:
3994       return "DW_FORM_ref2";
3995     case DW_FORM_ref4:
3996       return "DW_FORM_ref4";
3997     case DW_FORM_ref8:
3998       return "DW_FORM_ref8";
3999     case DW_FORM_ref_udata:
4000       return "DW_FORM_ref_udata";
4001     case DW_FORM_indirect:
4002       return "DW_FORM_indirect";
4003     default:
4004       return "DW_FORM_<unknown>";
4005     }
4006 }
4007
4008 /* Convert a DWARF type code into its string name.  */
4009
4010 #if 0
4011 static const char *
4012 dwarf_type_encoding_name (enc)
4013      register unsigned enc;
4014 {
4015   switch (enc)
4016     {
4017     case DW_ATE_address:
4018       return "DW_ATE_address";
4019     case DW_ATE_boolean:
4020       return "DW_ATE_boolean";
4021     case DW_ATE_complex_float:
4022       return "DW_ATE_complex_float";
4023     case DW_ATE_float:
4024       return "DW_ATE_float";
4025     case DW_ATE_signed:
4026       return "DW_ATE_signed";
4027     case DW_ATE_signed_char:
4028       return "DW_ATE_signed_char";
4029     case DW_ATE_unsigned:
4030       return "DW_ATE_unsigned";
4031     case DW_ATE_unsigned_char:
4032       return "DW_ATE_unsigned_char";
4033     default:
4034       return "DW_ATE_<unknown>";
4035     }
4036 }
4037 #endif
4038 \f
4039 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4040    instance of an inlined instance of a decl which is local to an inline
4041    function, so we have to trace all of the way back through the origin chain
4042    to find out what sort of node actually served as the original seed for the
4043    given block.  */
4044
4045 static tree
4046 decl_ultimate_origin (decl)
4047      register tree decl;
4048 {
4049   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4050      nodes in the function to point to themselves; ignore that if
4051      we're trying to output the abstract instance of this function.  */
4052   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4053     return NULL_TREE;
4054
4055 #ifdef ENABLE_CHECKING
4056   if (DECL_FROM_INLINE (DECL_ORIGIN (decl)))
4057     /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4058        most distant ancestor, this should never happen.  */
4059     abort ();
4060 #endif
4061
4062   return DECL_ABSTRACT_ORIGIN (decl);
4063 }
4064
4065 /* Determine the "ultimate origin" of a block.  The block may be an inlined
4066    instance of an inlined instance of a block which is local to an inline
4067    function, so we have to trace all of the way back through the origin chain
4068    to find out what sort of node actually served as the original seed for the
4069    given block.  */
4070
4071 static tree
4072 block_ultimate_origin (block)
4073      register tree block;
4074 {
4075   register tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4076
4077   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4078      nodes in the function to point to themselves; ignore that if
4079      we're trying to output the abstract instance of this function.  */
4080   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4081     return NULL_TREE;
4082
4083   if (immediate_origin == NULL_TREE)
4084     return NULL_TREE;
4085   else
4086     {
4087       register tree ret_val;
4088       register tree lookahead = immediate_origin;
4089
4090       do
4091         {
4092           ret_val = lookahead;
4093           lookahead = (TREE_CODE (ret_val) == BLOCK)
4094             ? BLOCK_ABSTRACT_ORIGIN (ret_val)
4095             : NULL;
4096         }
4097       while (lookahead != NULL && lookahead != ret_val);
4098
4099       return ret_val;
4100     }
4101 }
4102
4103 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4104    of a virtual function may refer to a base class, so we check the 'this'
4105    parameter.  */
4106
4107 static tree
4108 decl_class_context (decl)
4109      tree decl;
4110 {
4111   tree context = NULL_TREE;
4112
4113   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4114     context = DECL_CONTEXT (decl);
4115   else
4116     context = TYPE_MAIN_VARIANT
4117       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4118
4119   if (context && !TYPE_P (context))
4120     context = NULL_TREE;
4121
4122   return context;
4123 }
4124 \f
4125 /* Add an attribute/value pair to a DIE.  We build the lists up in reverse
4126    addition order, and correct that in reverse_all_dies.  */
4127
4128 static inline void
4129 add_dwarf_attr (die, attr)
4130      register dw_die_ref die;
4131      register dw_attr_ref attr;
4132 {
4133   if (die != NULL && attr != NULL)
4134     {
4135       attr->dw_attr_next = die->die_attr;
4136       die->die_attr = attr;
4137     }
4138 }
4139
4140 static inline dw_val_class AT_class PARAMS ((dw_attr_ref));
4141 static inline dw_val_class
4142 AT_class (a)
4143      dw_attr_ref a;
4144 {
4145   return a->dw_attr_val.val_class;
4146 }
4147
4148 /* Add a flag value attribute to a DIE.  */
4149
4150 static inline void
4151 add_AT_flag (die, attr_kind, flag)
4152      register dw_die_ref die;
4153      register enum dwarf_attribute attr_kind;
4154      register unsigned flag;
4155 {
4156   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4157
4158   attr->dw_attr_next = NULL;
4159   attr->dw_attr = attr_kind;
4160   attr->dw_attr_val.val_class = dw_val_class_flag;
4161   attr->dw_attr_val.v.val_flag = flag;
4162   add_dwarf_attr (die, attr);
4163 }
4164
4165 static inline unsigned AT_flag PARAMS ((dw_attr_ref));
4166 static inline unsigned
4167 AT_flag (a)
4168      register dw_attr_ref a;
4169 {
4170   if (a && AT_class (a) == dw_val_class_flag)
4171     return a->dw_attr_val.v.val_flag;
4172
4173   abort ();
4174 }
4175
4176 /* Add a signed integer attribute value to a DIE.  */
4177
4178 static inline void
4179 add_AT_int (die, attr_kind, int_val)
4180      register dw_die_ref die;
4181      register enum dwarf_attribute attr_kind;
4182      register long int int_val;
4183 {
4184   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4185
4186   attr->dw_attr_next = NULL;
4187   attr->dw_attr = attr_kind;
4188   attr->dw_attr_val.val_class = dw_val_class_const;
4189   attr->dw_attr_val.v.val_int = int_val;
4190   add_dwarf_attr (die, attr);
4191 }
4192
4193 static inline long int AT_int PARAMS ((dw_attr_ref));
4194 static inline long int
4195 AT_int (a)
4196      register dw_attr_ref a;
4197 {
4198   if (a && AT_class (a) == dw_val_class_const)
4199     return a->dw_attr_val.v.val_int;
4200
4201   abort ();
4202 }
4203
4204 /* Add an unsigned integer attribute value to a DIE.  */
4205
4206 static inline void
4207 add_AT_unsigned (die, attr_kind, unsigned_val)
4208      register dw_die_ref die;
4209      register enum dwarf_attribute attr_kind;
4210      register unsigned long unsigned_val;
4211 {
4212   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4213
4214   attr->dw_attr_next = NULL;
4215   attr->dw_attr = attr_kind;
4216   attr->dw_attr_val.val_class = dw_val_class_unsigned_const;
4217   attr->dw_attr_val.v.val_unsigned = unsigned_val;
4218   add_dwarf_attr (die, attr);
4219 }
4220
4221 static inline unsigned long AT_unsigned PARAMS ((dw_attr_ref));
4222 static inline unsigned long
4223 AT_unsigned (a)
4224      register dw_attr_ref a;
4225 {
4226   if (a && AT_class (a) == dw_val_class_unsigned_const)
4227     return a->dw_attr_val.v.val_unsigned;
4228
4229   abort ();
4230 }
4231
4232 /* Add an unsigned double integer attribute value to a DIE.  */
4233
4234 static inline void
4235 add_AT_long_long (die, attr_kind, val_hi, val_low)
4236      register dw_die_ref die;
4237      register enum dwarf_attribute attr_kind;
4238      register unsigned long val_hi;
4239      register unsigned long val_low;
4240 {
4241   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4242
4243   attr->dw_attr_next = NULL;
4244   attr->dw_attr = attr_kind;
4245   attr->dw_attr_val.val_class = dw_val_class_long_long;
4246   attr->dw_attr_val.v.val_long_long.hi = val_hi;
4247   attr->dw_attr_val.v.val_long_long.low = val_low;
4248   add_dwarf_attr (die, attr);
4249 }
4250
4251 /* Add a floating point attribute value to a DIE and return it.  */
4252
4253 static inline void
4254 add_AT_float (die, attr_kind, length, array)
4255      register dw_die_ref die;
4256      register enum dwarf_attribute attr_kind;
4257      register unsigned length;
4258      register long *array;
4259 {
4260   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4261
4262   attr->dw_attr_next = NULL;
4263   attr->dw_attr = attr_kind;
4264   attr->dw_attr_val.val_class = dw_val_class_float;
4265   attr->dw_attr_val.v.val_float.length = length;
4266   attr->dw_attr_val.v.val_float.array = array;
4267   add_dwarf_attr (die, attr);
4268 }
4269
4270 /* Add a string attribute value to a DIE.  */
4271
4272 static inline void
4273 add_AT_string (die, attr_kind, str)
4274      register dw_die_ref die;
4275      register enum dwarf_attribute attr_kind;
4276      register const char *str;
4277 {
4278   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4279
4280   attr->dw_attr_next = NULL;
4281   attr->dw_attr = attr_kind;
4282   attr->dw_attr_val.val_class = dw_val_class_str;
4283   attr->dw_attr_val.v.val_str = xstrdup (str);
4284   add_dwarf_attr (die, attr);
4285 }
4286
4287 static inline const char *AT_string PARAMS ((dw_attr_ref));
4288 static inline const char *
4289 AT_string (a)
4290      register dw_attr_ref a;
4291 {
4292   if (a && AT_class (a) == dw_val_class_str)
4293     return a->dw_attr_val.v.val_str;
4294
4295   abort ();
4296 }
4297
4298 /* Add a DIE reference attribute value to a DIE.  */
4299
4300 static inline void
4301 add_AT_die_ref (die, attr_kind, targ_die)
4302      register dw_die_ref die;
4303      register enum dwarf_attribute attr_kind;
4304      register dw_die_ref targ_die;
4305 {
4306   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4307
4308   attr->dw_attr_next = NULL;
4309   attr->dw_attr = attr_kind;
4310   attr->dw_attr_val.val_class = dw_val_class_die_ref;
4311   attr->dw_attr_val.v.val_die_ref.die = targ_die;
4312   attr->dw_attr_val.v.val_die_ref.external = 0;
4313   add_dwarf_attr (die, attr);
4314 }
4315
4316 static inline dw_die_ref AT_ref PARAMS ((dw_attr_ref));
4317 static inline dw_die_ref
4318 AT_ref (a)
4319      register dw_attr_ref a;
4320 {
4321   if (a && AT_class (a) == dw_val_class_die_ref)
4322     return a->dw_attr_val.v.val_die_ref.die;
4323
4324   abort ();
4325 }
4326
4327 static inline int AT_ref_external PARAMS ((dw_attr_ref));
4328 static inline int
4329 AT_ref_external (a)
4330      register dw_attr_ref a;
4331 {
4332   if (a && AT_class (a) == dw_val_class_die_ref)
4333     return a->dw_attr_val.v.val_die_ref.external;
4334
4335   return 0;
4336 }
4337
4338 static inline void set_AT_ref_external PARAMS ((dw_attr_ref, int));
4339 static inline void
4340 set_AT_ref_external (a, i)
4341      register dw_attr_ref a;
4342      int i;
4343 {
4344   if (a && AT_class (a) == dw_val_class_die_ref)
4345     a->dw_attr_val.v.val_die_ref.external = i;
4346   else
4347     abort ();
4348 }
4349
4350 /* Add an FDE reference attribute value to a DIE.  */
4351
4352 static inline void
4353 add_AT_fde_ref (die, attr_kind, targ_fde)
4354      register dw_die_ref die;
4355      register enum dwarf_attribute attr_kind;
4356      register unsigned targ_fde;
4357 {
4358   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4359
4360   attr->dw_attr_next = NULL;
4361   attr->dw_attr = attr_kind;
4362   attr->dw_attr_val.val_class = dw_val_class_fde_ref;
4363   attr->dw_attr_val.v.val_fde_index = targ_fde;
4364   add_dwarf_attr (die, attr);
4365 }
4366
4367 /* Add a location description attribute value to a DIE.  */
4368
4369 static inline void
4370 add_AT_loc (die, attr_kind, loc)
4371      register dw_die_ref die;
4372      register enum dwarf_attribute attr_kind;
4373      register dw_loc_descr_ref loc;
4374 {
4375   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4376
4377   attr->dw_attr_next = NULL;
4378   attr->dw_attr = attr_kind;
4379   attr->dw_attr_val.val_class = dw_val_class_loc;
4380   attr->dw_attr_val.v.val_loc = loc;
4381   add_dwarf_attr (die, attr);
4382 }
4383
4384 static inline dw_loc_descr_ref AT_loc PARAMS ((dw_attr_ref));
4385 static inline dw_loc_descr_ref
4386 AT_loc (a)
4387      register dw_attr_ref a;
4388 {
4389   if (a && AT_class (a) == dw_val_class_loc)
4390     return a->dw_attr_val.v.val_loc;
4391
4392   abort ();
4393 }
4394
4395 /* Add an address constant attribute value to a DIE.  */
4396
4397 static inline void
4398 add_AT_addr (die, attr_kind, addr)
4399      register dw_die_ref die;
4400      register enum dwarf_attribute attr_kind;
4401      rtx addr;
4402 {
4403   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4404
4405   attr->dw_attr_next = NULL;
4406   attr->dw_attr = attr_kind;
4407   attr->dw_attr_val.val_class = dw_val_class_addr;
4408   attr->dw_attr_val.v.val_addr = addr;
4409   add_dwarf_attr (die, attr);
4410 }
4411
4412 static inline rtx AT_addr PARAMS ((dw_attr_ref));
4413 static inline rtx
4414 AT_addr (a)
4415      register dw_attr_ref a;
4416 {
4417   if (a && AT_class (a) == dw_val_class_addr)
4418     return a->dw_attr_val.v.val_addr;
4419
4420   abort ();
4421 }
4422
4423 /* Add a label identifier attribute value to a DIE.  */
4424
4425 static inline void
4426 add_AT_lbl_id (die, attr_kind, lbl_id)
4427      register dw_die_ref die;
4428      register enum dwarf_attribute attr_kind;
4429      register const char *lbl_id;
4430 {
4431   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4432
4433   attr->dw_attr_next = NULL;
4434   attr->dw_attr = attr_kind;
4435   attr->dw_attr_val.val_class = dw_val_class_lbl_id;
4436   attr->dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4437   add_dwarf_attr (die, attr);
4438 }
4439
4440 /* Add a section offset attribute value to a DIE.  */
4441
4442 static inline void
4443 add_AT_lbl_offset (die, attr_kind, label)
4444      register dw_die_ref die;
4445      register enum dwarf_attribute attr_kind;
4446      register const char *label;
4447 {
4448   register dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4449
4450   attr->dw_attr_next = NULL;
4451   attr->dw_attr = attr_kind;
4452   attr->dw_attr_val.val_class = dw_val_class_lbl_offset;
4453   attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
4454   add_dwarf_attr (die, attr);
4455 }
4456
4457 static inline const char *AT_lbl PARAMS ((dw_attr_ref));
4458 static inline const char *
4459 AT_lbl (a)
4460      register dw_attr_ref a;
4461 {
4462   if (a && (AT_class (a) == dw_val_class_lbl_id
4463             || AT_class (a) == dw_val_class_lbl_offset))
4464     return a->dw_attr_val.v.val_lbl_id;
4465
4466   abort ();
4467 }
4468
4469 /* Get the attribute of type attr_kind.  */
4470
4471 static inline dw_attr_ref
4472 get_AT (die, attr_kind)
4473      register dw_die_ref die;
4474      register enum dwarf_attribute attr_kind;
4475 {
4476   register dw_attr_ref a;
4477   register dw_die_ref spec = NULL;
4478
4479   if (die != NULL)
4480     {
4481       for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
4482         {
4483           if (a->dw_attr == attr_kind)
4484             return a;
4485
4486           if (a->dw_attr == DW_AT_specification
4487               || a->dw_attr == DW_AT_abstract_origin)
4488             spec = AT_ref (a);
4489         }
4490
4491       if (spec)
4492         return get_AT (spec, attr_kind);
4493     }
4494
4495   return NULL;
4496 }
4497
4498 /* Return the "low pc" attribute value, typically associated with
4499    a subprogram DIE.  Return null if the "low pc" attribute is
4500    either not prsent, or if it cannot be represented as an
4501    assembler label identifier.  */
4502
4503 static inline const char *
4504 get_AT_low_pc (die)
4505      register dw_die_ref die;
4506 {
4507   register dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4508   return a ? AT_lbl (a) : NULL;
4509 }
4510
4511 /* Return the "high pc" attribute value, typically associated with
4512    a subprogram DIE.  Return null if the "high pc" attribute is
4513    either not prsent, or if it cannot be represented as an
4514    assembler label identifier.  */
4515
4516 static inline const char *
4517 get_AT_hi_pc (die)
4518      register dw_die_ref die;
4519 {
4520   register dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4521   return a ? AT_lbl (a) : NULL;
4522 }
4523
4524 /* Return the value of the string attribute designated by ATTR_KIND, or
4525    NULL if it is not present.  */
4526
4527 static inline const char *
4528 get_AT_string (die, attr_kind)
4529      register dw_die_ref die;
4530      register enum dwarf_attribute attr_kind;
4531 {
4532   register dw_attr_ref a = get_AT (die, attr_kind);
4533   return a ? AT_string (a) : NULL;
4534 }
4535
4536 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4537    if it is not present.  */
4538
4539 static inline int
4540 get_AT_flag (die, attr_kind)
4541      register dw_die_ref die;
4542      register enum dwarf_attribute attr_kind;
4543 {
4544   register dw_attr_ref a = get_AT (die, attr_kind);
4545   return a ? AT_flag (a) : 0;
4546 }
4547
4548 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4549    if it is not present.  */
4550
4551 static inline unsigned
4552 get_AT_unsigned (die, attr_kind)
4553      register dw_die_ref die;
4554      register enum dwarf_attribute attr_kind;
4555 {
4556   register dw_attr_ref a = get_AT (die, attr_kind);
4557   return a ? AT_unsigned (a) : 0;
4558 }
4559
4560 static inline dw_die_ref
4561 get_AT_ref (die, attr_kind)
4562      dw_die_ref die;
4563      register enum dwarf_attribute attr_kind;
4564 {
4565   register dw_attr_ref a = get_AT (die, attr_kind);
4566   return a ? AT_ref (a) : NULL;
4567 }
4568
4569 static inline int
4570 is_c_family ()
4571 {
4572   register unsigned lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
4573
4574   return (lang == DW_LANG_C || lang == DW_LANG_C89
4575           || lang == DW_LANG_C_plus_plus);
4576 }
4577
4578 static inline int
4579 is_fortran ()
4580 {
4581   register unsigned lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
4582
4583   return (lang == DW_LANG_Fortran77 || lang == DW_LANG_Fortran90);
4584 }
4585
4586 static inline int
4587 is_java ()
4588 {
4589   register unsigned lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
4590
4591   return (lang == DW_LANG_Java);
4592 }
4593
4594 /* Free up the memory used by A.  */
4595
4596 static inline void free_AT PARAMS ((dw_attr_ref));
4597 static inline void
4598 free_AT (a)
4599      dw_attr_ref a;
4600 {
4601   switch (AT_class (a))
4602     {
4603     case dw_val_class_str:
4604     case dw_val_class_lbl_id:
4605     case dw_val_class_lbl_offset:
4606       free (a->dw_attr_val.v.val_str);
4607       break;
4608
4609     case dw_val_class_float:
4610       free (a->dw_attr_val.v.val_float.array);
4611       break;
4612       
4613     default:
4614       break;
4615     }
4616
4617   free (a);
4618 }
4619
4620 /* Remove the specified attribute if present.  */
4621
4622 static void
4623 remove_AT (die, attr_kind)
4624      register dw_die_ref die;
4625      register enum dwarf_attribute attr_kind;
4626 {
4627   register dw_attr_ref *p;
4628   register dw_attr_ref removed = NULL;
4629
4630   if (die != NULL)
4631     {
4632       for (p = &(die->die_attr); *p; p = &((*p)->dw_attr_next))
4633         if ((*p)->dw_attr == attr_kind)
4634           {
4635             removed = *p;
4636             *p = (*p)->dw_attr_next;
4637             break;
4638           }
4639
4640       if (removed != 0)
4641         free_AT (removed);
4642     }
4643 }
4644
4645 /* Free up the memory used by DIE.  */
4646
4647 static inline void free_die PARAMS ((dw_die_ref));
4648 static inline void
4649 free_die (die)
4650      dw_die_ref die;
4651 {
4652   remove_children (die);
4653   free (die);
4654 }
4655
4656 /* Discard the children of this DIE.  */
4657
4658 static void
4659 remove_children (die)
4660      register dw_die_ref die;
4661 {
4662   register dw_die_ref child_die = die->die_child;
4663
4664   die->die_child = NULL;
4665
4666   while (child_die != NULL)
4667     {
4668       register dw_die_ref tmp_die = child_die;
4669       register dw_attr_ref a;
4670
4671       child_die = child_die->die_sib;
4672
4673       for (a = tmp_die->die_attr; a != NULL;)
4674         {
4675           register dw_attr_ref tmp_a = a;
4676
4677           a = a->dw_attr_next;
4678           free_AT (tmp_a);
4679         }
4680
4681       free_die (tmp_die);
4682     }
4683 }
4684
4685 /* Add a child DIE below its parent.  We build the lists up in reverse
4686    addition order, and correct that in reverse_all_dies.  */
4687
4688 static inline void
4689 add_child_die (die, child_die)
4690      register dw_die_ref die;
4691      register dw_die_ref child_die;
4692 {
4693   if (die != NULL && child_die != NULL)
4694     {
4695       if (die == child_die)
4696         abort ();
4697       child_die->die_parent = die;
4698       child_die->die_sib = die->die_child;
4699       die->die_child = child_die;
4700     }
4701 }
4702
4703 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4704    is the specification, to the front of PARENT's list of children.  */
4705
4706 static void
4707 splice_child_die (parent, child)
4708      dw_die_ref parent, child;
4709 {
4710   dw_die_ref *p;
4711
4712   /* We want the declaration DIE from inside the class, not the
4713      specification DIE at toplevel.  */
4714   if (child->die_parent != parent)
4715     {
4716       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4717       if (tmp)
4718         child = tmp;
4719     }
4720
4721   if (child->die_parent != parent
4722       && child->die_parent != get_AT_ref (parent, DW_AT_specification))
4723     abort ();
4724
4725   for (p = &(child->die_parent->die_child); *p; p = &((*p)->die_sib))
4726     if (*p == child)
4727       {
4728         *p = child->die_sib;
4729         break;
4730       }
4731
4732   child->die_sib = parent->die_child;
4733   parent->die_child = child;
4734 }
4735
4736 /* Return a pointer to a newly created DIE node.  */
4737
4738 static inline dw_die_ref
4739 new_die (tag_value, parent_die)
4740      register enum dwarf_tag tag_value;
4741      register dw_die_ref parent_die;
4742 {
4743   register dw_die_ref die = (dw_die_ref) xmalloc (sizeof (die_node));
4744
4745   die->die_tag = tag_value;
4746   die->die_abbrev = 0;
4747   die->die_offset = 0;
4748   die->die_child = NULL;
4749   die->die_parent = NULL;
4750   die->die_sib = NULL;
4751   die->die_attr = NULL;
4752   die->die_symbol = NULL;
4753
4754   if (parent_die != NULL)
4755     add_child_die (parent_die, die);
4756   else
4757     {
4758       limbo_die_node *limbo_node;
4759
4760       limbo_node = (limbo_die_node *) xmalloc (sizeof (limbo_die_node));
4761       limbo_node->die = die;
4762       limbo_node->next = limbo_die_list;
4763       limbo_die_list = limbo_node;
4764     }
4765
4766   return die;
4767 }
4768
4769 /* Return the DIE associated with the given type specifier.  */
4770
4771 static inline dw_die_ref
4772 lookup_type_die (type)
4773      register tree type;
4774 {
4775   if (TREE_CODE (type) == VECTOR_TYPE)
4776     type = TYPE_DEBUG_REPRESENTATION_TYPE (type);
4777   return (dw_die_ref) TYPE_SYMTAB_POINTER (type);
4778 }
4779
4780 /* Equate a DIE to a given type specifier.  */
4781
4782 static inline void
4783 equate_type_number_to_die (type, type_die)
4784      register tree type;
4785      register dw_die_ref type_die;
4786 {
4787   TYPE_SYMTAB_POINTER (type) = (char *) type_die;
4788 }
4789
4790 /* Return the DIE associated with a given declaration.  */
4791
4792 static inline dw_die_ref
4793 lookup_decl_die (decl)
4794      register tree decl;
4795 {
4796   register unsigned decl_id = DECL_UID (decl);
4797
4798   return (decl_id < decl_die_table_in_use
4799           ? decl_die_table[decl_id] : NULL);
4800 }
4801
4802 /* Equate a DIE to a particular declaration.  */
4803
4804 static void
4805 equate_decl_number_to_die (decl, decl_die)
4806      register tree decl;
4807      register dw_die_ref decl_die;
4808 {
4809   register unsigned decl_id = DECL_UID (decl);
4810   register unsigned num_allocated;
4811
4812   if (decl_id >= decl_die_table_allocated)
4813     {
4814       num_allocated
4815         = ((decl_id + 1 + DECL_DIE_TABLE_INCREMENT - 1)
4816            / DECL_DIE_TABLE_INCREMENT)
4817           * DECL_DIE_TABLE_INCREMENT;
4818
4819       decl_die_table
4820         = (dw_die_ref *) xrealloc (decl_die_table,
4821                                    sizeof (dw_die_ref) * num_allocated);
4822
4823       bzero ((char *) &decl_die_table[decl_die_table_allocated],
4824              (num_allocated - decl_die_table_allocated) * sizeof (dw_die_ref));
4825       decl_die_table_allocated = num_allocated;
4826     }
4827
4828   if (decl_id >= decl_die_table_in_use)
4829     decl_die_table_in_use = (decl_id + 1);
4830
4831   decl_die_table[decl_id] = decl_die;
4832 }
4833 \f
4834 /* Keep track of the number of spaces used to indent the
4835    output of the debugging routines that print the structure of
4836    the DIE internal representation.  */
4837 static int print_indent;
4838
4839 /* Indent the line the number of spaces given by print_indent.  */
4840
4841 static inline void
4842 print_spaces (outfile)
4843      FILE *outfile;
4844 {
4845   fprintf (outfile, "%*s", print_indent, "");
4846 }
4847
4848 /* Print the information associated with a given DIE, and its children.
4849    This routine is a debugging aid only.  */
4850
4851 static void
4852 print_die (die, outfile)
4853      dw_die_ref die;
4854      FILE *outfile;
4855 {
4856   register dw_attr_ref a;
4857   register dw_die_ref c;
4858
4859   print_spaces (outfile);
4860   fprintf (outfile, "DIE %4lu: %s\n",
4861            die->die_offset, dwarf_tag_name (die->die_tag));
4862   print_spaces (outfile);
4863   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
4864   fprintf (outfile, " offset: %lu\n", die->die_offset);
4865
4866   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
4867     {
4868       print_spaces (outfile);
4869       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
4870
4871       switch (AT_class (a))
4872         {
4873         case dw_val_class_addr:
4874           fprintf (outfile, "address");
4875           break;
4876         case dw_val_class_loc:
4877           fprintf (outfile, "location descriptor");
4878           break;
4879         case dw_val_class_const:
4880           fprintf (outfile, "%ld", AT_int (a));
4881           break;
4882         case dw_val_class_unsigned_const:
4883           fprintf (outfile, "%lu", AT_unsigned (a));
4884           break;
4885         case dw_val_class_long_long:
4886           fprintf (outfile, "constant (%lu,%lu)",
4887                    a->dw_attr_val.v.val_long_long.hi,
4888                    a->dw_attr_val.v.val_long_long.low);
4889           break;
4890         case dw_val_class_float:
4891           fprintf (outfile, "floating-point constant");
4892           break;
4893         case dw_val_class_flag:
4894           fprintf (outfile, "%u", AT_flag (a));
4895           break;
4896         case dw_val_class_die_ref:
4897           if (AT_ref (a) != NULL)
4898             {
4899               if (AT_ref (a)->die_symbol)
4900                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
4901               else
4902                 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
4903             }
4904           else
4905             fprintf (outfile, "die -> <null>");
4906           break;
4907         case dw_val_class_lbl_id:
4908         case dw_val_class_lbl_offset:
4909           fprintf (outfile, "label: %s", AT_lbl (a));
4910           break;
4911         case dw_val_class_str:
4912           if (AT_string (a) != NULL)
4913             fprintf (outfile, "\"%s\"", AT_string (a));
4914           else
4915             fprintf (outfile, "<null>");
4916           break;
4917         default:
4918           break;
4919         }
4920
4921       fprintf (outfile, "\n");
4922     }
4923
4924   if (die->die_child != NULL)
4925     {
4926       print_indent += 4;
4927       for (c = die->die_child; c != NULL; c = c->die_sib)
4928         print_die (c, outfile);
4929
4930       print_indent -= 4;
4931     }
4932   if (print_indent == 0)
4933     fprintf (outfile, "\n");
4934 }
4935
4936 /* Print the contents of the source code line number correspondence table.
4937    This routine is a debugging aid only.  */
4938
4939 static void
4940 print_dwarf_line_table (outfile)
4941      FILE *outfile;
4942 {
4943   register unsigned i;
4944   register dw_line_info_ref line_info;
4945
4946   fprintf (outfile, "\n\nDWARF source line information\n");
4947   for (i = 1; i < line_info_table_in_use; ++i)
4948     {
4949       line_info = &line_info_table[i];
4950       fprintf (outfile, "%5d: ", i);
4951       fprintf (outfile, "%-20s", file_table[line_info->dw_file_num]);
4952       fprintf (outfile, "%6ld", line_info->dw_line_num);
4953       fprintf (outfile, "\n");
4954     }
4955
4956   fprintf (outfile, "\n\n");
4957 }
4958
4959 /* Print the information collected for a given DIE.  */
4960
4961 void
4962 debug_dwarf_die (die)
4963      dw_die_ref die;
4964 {
4965   print_die (die, stderr);
4966 }
4967
4968 /* Print all DWARF information collected for the compilation unit.
4969    This routine is a debugging aid only.  */
4970
4971 void
4972 debug_dwarf ()
4973 {
4974   print_indent = 0;
4975   print_die (comp_unit_die, stderr);
4976   if (! DWARF2_ASM_LINE_DEBUG_INFO)
4977     print_dwarf_line_table (stderr);
4978 }
4979 \f
4980 /* We build up the lists of children and attributes by pushing new ones
4981    onto the beginning of the list.  Reverse the lists for DIE so that
4982    they are in order of addition.  */
4983
4984 static void
4985 reverse_die_lists (die)
4986      register dw_die_ref die;
4987 {
4988   register dw_die_ref c, cp, cn;
4989   register dw_attr_ref a, ap, an;
4990
4991   for (a = die->die_attr, ap = 0; a; a = an)
4992     {
4993       an = a->dw_attr_next;
4994       a->dw_attr_next = ap;
4995       ap = a;
4996     }
4997   die->die_attr = ap;
4998
4999   for (c = die->die_child, cp = 0; c; c = cn)
5000     {
5001       cn = c->die_sib;
5002       c->die_sib = cp;
5003       cp = c;
5004     }
5005   die->die_child = cp;
5006 }
5007
5008 /* reverse_die_lists only reverses the single die you pass it. Since
5009    we used to reverse all dies in add_sibling_attributes, which runs
5010    through all the dies, it would reverse all the dies.  Now, however,
5011    since we don't call reverse_die_lists in add_sibling_attributes, we
5012    need a routine to recursively reverse all the dies. This is that
5013    routine.  */
5014
5015 static void
5016 reverse_all_dies (die)
5017      register dw_die_ref die;
5018 {
5019   register dw_die_ref c;
5020
5021   reverse_die_lists (die);
5022
5023   for (c = die->die_child; c; c = c->die_sib)
5024     reverse_all_dies (c);
5025 }
5026
5027 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is
5028    the CU for the enclosing include file, if any.  BINCL_DIE is the
5029    DW_TAG_GNU_BINCL DIE that marks the start of the DIEs for this
5030    include file.  */
5031
5032 static dw_die_ref
5033 push_new_compile_unit (old_unit, bincl_die)
5034      dw_die_ref old_unit, bincl_die;
5035 {
5036   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5037   dw_die_ref new_unit = gen_compile_unit_die (filename);
5038   new_unit->die_sib = old_unit;
5039   return new_unit;
5040 }
5041
5042 /* Close an include-file CU and reopen the enclosing one.  */
5043
5044 static dw_die_ref
5045 pop_compile_unit (old_unit)
5046      dw_die_ref old_unit;
5047 {
5048   dw_die_ref new_unit = old_unit->die_sib;
5049   old_unit->die_sib = NULL;
5050   return new_unit;
5051 }
5052
5053 #define PROCESS(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5054 #define PROCESS_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5055
5056 /* Calculate the checksum of a location expression.  */
5057
5058 static inline void
5059 loc_checksum (loc, ctx)
5060      dw_loc_descr_ref loc;
5061      struct md5_ctx *ctx;
5062 {
5063   PROCESS (loc->dw_loc_opc);
5064   PROCESS (loc->dw_loc_oprnd1);
5065   PROCESS (loc->dw_loc_oprnd2);
5066 }
5067
5068 /* Calculate the checksum of an attribute.  */
5069
5070 static void
5071 attr_checksum (at, ctx)
5072      dw_attr_ref at;
5073      struct md5_ctx *ctx;
5074 {
5075   dw_loc_descr_ref loc;
5076   rtx r;
5077
5078   PROCESS (at->dw_attr);
5079
5080   /* We don't care about differences in file numbering.  */
5081   if (at->dw_attr == DW_AT_decl_file
5082       /* Or that this was compiled with a different compiler snapshot; if
5083          the output is the same, that's what matters.  */
5084       || at->dw_attr == DW_AT_producer)
5085     return;
5086
5087   switch (AT_class (at))
5088     {
5089     case dw_val_class_const:
5090       PROCESS (at->dw_attr_val.v.val_int);
5091       break;
5092     case dw_val_class_unsigned_const:
5093       PROCESS (at->dw_attr_val.v.val_unsigned);
5094       break;
5095     case dw_val_class_long_long:
5096       PROCESS (at->dw_attr_val.v.val_long_long);
5097       break;
5098     case dw_val_class_float:
5099       PROCESS (at->dw_attr_val.v.val_float);
5100       break;
5101     case dw_val_class_flag:
5102       PROCESS (at->dw_attr_val.v.val_flag);
5103       break;
5104
5105     case dw_val_class_str:
5106       PROCESS_STRING (AT_string (at));
5107       break;
5108     case dw_val_class_addr:
5109       r = AT_addr (at);
5110       switch (GET_CODE (r))
5111         {
5112         case SYMBOL_REF:
5113           PROCESS_STRING (XSTR (r, 0));
5114           break;
5115
5116         default:
5117           abort ();
5118         }
5119       break;
5120
5121     case dw_val_class_loc:
5122       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5123         loc_checksum (loc, ctx);
5124       break;
5125
5126     case dw_val_class_die_ref:
5127       if (AT_ref (at)->die_offset)
5128         PROCESS (AT_ref (at)->die_offset);
5129       /* FIXME else use target die name or something.  */
5130
5131     case dw_val_class_fde_ref:
5132     case dw_val_class_lbl_id:
5133     case dw_val_class_lbl_offset:
5134
5135     default:
5136       break;
5137     }
5138 }
5139
5140 /* Calculate the checksum of a DIE.  */
5141
5142 static void
5143 die_checksum (die, ctx)
5144      dw_die_ref die;
5145      struct md5_ctx *ctx;
5146 {
5147   dw_die_ref c;
5148   dw_attr_ref a;
5149
5150   PROCESS (die->die_tag);
5151
5152   for (a = die->die_attr; a; a = a->dw_attr_next)
5153     attr_checksum (a, ctx);
5154
5155   for (c = die->die_child; c; c = c->die_sib)
5156     die_checksum (c, ctx);
5157 }
5158
5159 #undef PROCESS
5160 #undef PROCESS_STRING
5161
5162 /* The prefix to attach to symbols on DIEs in the current comdat debug
5163    info section.  */
5164 static char *comdat_symbol_id;
5165
5166 /* The index of the current symbol within the current comdat CU.  */
5167 static unsigned int comdat_symbol_number;
5168
5169 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
5170    children, and set comdat_symbol_id accordingly.  */
5171
5172 static void
5173 compute_section_prefix (unit_die)
5174      dw_die_ref unit_die;
5175 {
5176   char *p, *name;
5177   int i;
5178   unsigned char checksum[16];
5179   struct md5_ctx ctx;
5180
5181   md5_init_ctx (&ctx);
5182   die_checksum (unit_die, &ctx);
5183   md5_finish_ctx (&ctx, checksum);
5184
5185   p = file_name_nondirectory (get_AT_string (unit_die, DW_AT_name));
5186   name = (char *) alloca (strlen (p) + 64);
5187   sprintf (name, "%s.", p);
5188
5189   clean_symbol_name (name);
5190
5191   p = name + strlen (name);
5192   for (i = 0; i < 4; ++i)
5193     {
5194       sprintf (p, "%.2x", checksum[i]);
5195       p += 2;
5196     }
5197
5198   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
5199   comdat_symbol_number = 0;
5200 }
5201
5202 /* Returns nonzero iff DIE represents a type, in the sense of TYPE_P.  */
5203
5204 static int
5205 is_type_die (die)
5206      dw_die_ref die;
5207 {
5208   switch (die->die_tag)
5209     {
5210     case DW_TAG_array_type:
5211     case DW_TAG_class_type:
5212     case DW_TAG_enumeration_type:
5213     case DW_TAG_pointer_type:
5214     case DW_TAG_reference_type:
5215     case DW_TAG_string_type:
5216     case DW_TAG_structure_type:
5217     case DW_TAG_subroutine_type:
5218     case DW_TAG_union_type:
5219     case DW_TAG_ptr_to_member_type:
5220     case DW_TAG_set_type:
5221     case DW_TAG_subrange_type:
5222     case DW_TAG_base_type:
5223     case DW_TAG_const_type:
5224     case DW_TAG_file_type:
5225     case DW_TAG_packed_type:
5226     case DW_TAG_volatile_type:
5227       return 1;
5228     default:
5229       return 0;
5230     }
5231 }
5232
5233 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
5234    Basically, we want to choose the bits that are likely to be shared between
5235    compilations (types) and leave out the bits that are specific to individual
5236    compilations (functions).  */
5237
5238 static int
5239 is_comdat_die (c)
5240      dw_die_ref c;
5241 {
5242 #if 1
5243   /* I think we want to leave base types and __vtbl_ptr_type in the
5244      main CU, as we do for stabs.  The advantage is a greater
5245      likelihood of sharing between objects that don't include headers
5246      in the same order (and therefore would put the base types in a
5247      different comdat).  jason 8/28/00 */
5248   if (c->die_tag == DW_TAG_base_type)
5249     return 0;
5250
5251   if (c->die_tag == DW_TAG_pointer_type
5252       || c->die_tag == DW_TAG_reference_type
5253       || c->die_tag == DW_TAG_const_type
5254       || c->die_tag == DW_TAG_volatile_type)
5255     {
5256       dw_die_ref t = get_AT_ref (c, DW_AT_type);
5257       return t ? is_comdat_die (t) : 0;
5258     }
5259 #endif
5260
5261   return is_type_die (c);
5262 }
5263
5264 /* Returns 1 iff C is the sort of DIE that might be referred to from another
5265    compilation unit.  */
5266
5267 static int
5268 is_symbol_die (c)
5269      dw_die_ref c;
5270 {
5271   if (is_type_die (c))
5272     return 1;
5273   if (get_AT (c, DW_AT_declaration)
5274       && ! get_AT (c, DW_AT_specification))
5275     return 1;
5276   return 0;
5277 }
5278
5279 static char *
5280 gen_internal_sym ()
5281 {
5282   char buf[256];
5283   static int label_num;
5284   ASM_GENERATE_INTERNAL_LABEL (buf, "LDIE", label_num++);
5285   return xstrdup (buf);
5286 }
5287
5288 /* Assign symbols to all worthy DIEs under DIE.  */
5289
5290 static void
5291 assign_symbol_names (die)
5292      register dw_die_ref die;
5293 {
5294   register dw_die_ref c;
5295
5296   if (is_symbol_die (die))
5297     {
5298       if (comdat_symbol_id)
5299         {
5300           char *p = alloca (strlen (comdat_symbol_id) + 64);
5301           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
5302                    comdat_symbol_id, comdat_symbol_number++);
5303           die->die_symbol = xstrdup (p);
5304         }
5305       else
5306         die->die_symbol = gen_internal_sym ();
5307     }
5308
5309   for (c = die->die_child; c != NULL; c = c->die_sib)
5310     assign_symbol_names (c);
5311 }
5312
5313 /* Traverse the DIE (which is always comp_unit_die), and set up
5314    additional compilation units for each of the include files we see
5315    bracketed by BINCL/EINCL.  */
5316
5317 static void
5318 break_out_includes (die)
5319      register dw_die_ref die;
5320 {
5321   dw_die_ref *ptr;
5322   register dw_die_ref unit = NULL;
5323   limbo_die_node *node;
5324
5325   for (ptr = &(die->die_child); *ptr; )
5326     {
5327       register dw_die_ref c = *ptr;
5328
5329       if (c->die_tag == DW_TAG_GNU_BINCL
5330           || c->die_tag == DW_TAG_GNU_EINCL
5331           || (unit && is_comdat_die (c)))
5332         {
5333           /* This DIE is for a secondary CU; remove it from the main one.  */
5334           *ptr = c->die_sib;
5335
5336           if (c->die_tag == DW_TAG_GNU_BINCL)
5337             {
5338               unit = push_new_compile_unit (unit, c);
5339               free_die (c);
5340             }
5341           else if (c->die_tag == DW_TAG_GNU_EINCL)
5342             {
5343               unit = pop_compile_unit (unit);
5344               free_die (c);
5345             }
5346           else
5347             add_child_die (unit, c);
5348         }
5349       else
5350         {
5351           /* Leave this DIE in the main CU.  */
5352           ptr = &(c->die_sib);
5353           continue;
5354         }
5355     }
5356
5357 #if 0
5358   /* We can only use this in debugging, since the frontend doesn't check
5359      to make sure that we leave every include file we enter.  */     
5360   if (unit != NULL)
5361     abort ();
5362 #endif
5363
5364   assign_symbol_names (die);
5365   for (node = limbo_die_list; node; node = node->next)
5366     {
5367       compute_section_prefix (node->die);
5368       assign_symbol_names (node->die);
5369     }
5370 }
5371
5372 /* Traverse the DIE and add a sibling attribute if it may have the
5373    effect of speeding up access to siblings.  To save some space,
5374    avoid generating sibling attributes for DIE's without children.  */
5375
5376 static void
5377 add_sibling_attributes (die)
5378      register dw_die_ref die;
5379 {
5380   register dw_die_ref c;
5381
5382   if (die->die_tag != DW_TAG_compile_unit
5383       && die->die_sib && die->die_child != NULL)
5384     /* Add the sibling link to the front of the attribute list.  */
5385     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
5386
5387   for (c = die->die_child; c != NULL; c = c->die_sib)
5388     add_sibling_attributes (c);
5389 }
5390
5391 /* The format of each DIE (and its attribute value pairs)
5392    is encoded in an abbreviation table.  This routine builds the
5393    abbreviation table and assigns a unique abbreviation id for
5394    each abbreviation entry.  The children of each die are visited
5395    recursively.  */
5396
5397 static void
5398 build_abbrev_table (die)
5399      register dw_die_ref die;
5400 {
5401   register unsigned long abbrev_id;
5402   register unsigned long n_alloc;
5403   register dw_die_ref c;
5404   register dw_attr_ref d_attr, a_attr;
5405
5406   /* Scan the DIE references, and mark as external any that refer to
5407      DIEs from other CUs (i.e. those which are not marked).  */
5408   for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
5409     {
5410       if (AT_class (d_attr) == dw_val_class_die_ref
5411           && AT_ref (d_attr)->die_mark == 0)
5412         {
5413           if (AT_ref (d_attr)->die_symbol == 0)
5414             abort ();
5415           set_AT_ref_external (d_attr, 1);
5416         }
5417     }
5418
5419   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
5420     {
5421       register dw_die_ref abbrev = abbrev_die_table[abbrev_id];
5422
5423       if (abbrev->die_tag == die->die_tag)
5424         {
5425           if ((abbrev->die_child != NULL) == (die->die_child != NULL))
5426             {
5427               a_attr = abbrev->die_attr;
5428               d_attr = die->die_attr;
5429
5430               while (a_attr != NULL && d_attr != NULL)
5431                 {
5432                   if ((a_attr->dw_attr != d_attr->dw_attr)
5433                       || (value_format (a_attr) != value_format (d_attr)))
5434                     break;
5435
5436                   a_attr = a_attr->dw_attr_next;
5437                   d_attr = d_attr->dw_attr_next;
5438                 }
5439
5440               if (a_attr == NULL && d_attr == NULL)
5441                 break;
5442             }
5443         }
5444     }
5445
5446   if (abbrev_id >= abbrev_die_table_in_use)
5447     {
5448       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
5449         {
5450           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
5451           abbrev_die_table
5452             = (dw_die_ref *) xrealloc (abbrev_die_table,
5453                                        sizeof (dw_die_ref) * n_alloc);
5454
5455           bzero ((char *) &abbrev_die_table[abbrev_die_table_allocated],
5456                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
5457           abbrev_die_table_allocated = n_alloc;
5458         }
5459
5460       ++abbrev_die_table_in_use;
5461       abbrev_die_table[abbrev_id] = die;
5462     }
5463
5464   die->die_abbrev = abbrev_id;
5465   for (c = die->die_child; c != NULL; c = c->die_sib)
5466     build_abbrev_table (c);
5467 }
5468 \f
5469 /* Return the size of a string, including the null byte.
5470
5471    This used to treat backslashes as escapes, and hence they were not included
5472    in the count.  However, that conflicts with what ASM_OUTPUT_ASCII does,
5473    which treats a backslash as a backslash, escaping it if necessary, and hence
5474    we must include them in the count.  */
5475
5476 static unsigned long
5477 size_of_string (str)
5478      register const char *str;
5479 {
5480   return strlen (str) + 1;
5481 }
5482
5483 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
5484
5485 static int
5486 constant_size (value)
5487      long unsigned value;
5488 {
5489   int log;
5490
5491   if (value == 0)
5492     log = 0;
5493   else
5494     log = floor_log2 (value);
5495
5496   log = log / 8;
5497   log = 1 << (floor_log2 (log) + 1);
5498
5499   return log;
5500 }
5501
5502 /* Return the size of a DIE, as it is represented in the
5503    .debug_info section.  */
5504
5505 static unsigned long
5506 size_of_die (die)
5507      register dw_die_ref die;
5508 {
5509   register unsigned long size = 0;
5510   register dw_attr_ref a;
5511
5512   size += size_of_uleb128 (die->die_abbrev);
5513   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5514     {
5515       switch (AT_class (a))
5516         {
5517         case dw_val_class_addr:
5518           size += DWARF2_ADDR_SIZE;
5519           break;
5520         case dw_val_class_loc:
5521           {
5522             register unsigned long lsize = size_of_locs (AT_loc (a));
5523
5524             /* Block length.  */
5525             size += constant_size (lsize);
5526             size += lsize;
5527           }
5528           break;
5529         case dw_val_class_const:
5530           size += size_of_sleb128 (AT_int (a));
5531           break;
5532         case dw_val_class_unsigned_const:
5533           size += constant_size (AT_unsigned (a));
5534           break;
5535         case dw_val_class_long_long:
5536           size += 1 + 8; /* block */
5537           break;
5538         case dw_val_class_float:
5539           size += 1 + a->dw_attr_val.v.val_float.length * 4; /* block */
5540           break;
5541         case dw_val_class_flag:
5542           size += 1;
5543           break;
5544         case dw_val_class_die_ref:
5545           size += DWARF_OFFSET_SIZE;
5546           break;
5547         case dw_val_class_fde_ref:
5548           size += DWARF_OFFSET_SIZE;
5549           break;
5550         case dw_val_class_lbl_id:
5551           size += DWARF2_ADDR_SIZE;
5552           break;
5553         case dw_val_class_lbl_offset:
5554           size += DWARF_OFFSET_SIZE;
5555           break;
5556         case dw_val_class_str:
5557           size += size_of_string (AT_string (a));
5558           break;
5559         default:
5560           abort ();
5561         }
5562     }
5563
5564   return size;
5565 }
5566
5567 /* Size the debugging information associated with a given DIE.
5568    Visits the DIE's children recursively.  Updates the global
5569    variable next_die_offset, on each time through.  Uses the
5570    current value of next_die_offset to update the die_offset
5571    field in each DIE.  */
5572
5573 static void
5574 calc_die_sizes (die)
5575      dw_die_ref die;
5576 {
5577   register dw_die_ref c;
5578   die->die_offset = next_die_offset;
5579   next_die_offset += size_of_die (die);
5580
5581   for (c = die->die_child; c != NULL; c = c->die_sib)
5582     calc_die_sizes (c);
5583
5584   if (die->die_child != NULL)
5585     /* Count the null byte used to terminate sibling lists.  */
5586     next_die_offset += 1;
5587 }
5588
5589 /* Set the marks for a die and its children.  We do this so
5590    that we know whether or not a reference needs to use FORM_ref_addr; only
5591    DIEs in the same CU will be marked.  We used to clear out the offset
5592    and use that as the flag, but ran into ordering problems.  */
5593
5594 static void
5595 mark_dies (die)
5596      dw_die_ref die;
5597 {
5598   register dw_die_ref c;
5599   die->die_mark = 1;
5600   for (c = die->die_child; c; c = c->die_sib)
5601     mark_dies (c);
5602 }
5603
5604 /* Clear the marks for a die and its children.  */
5605
5606 static void
5607 unmark_dies (die)
5608      dw_die_ref die;
5609 {
5610   register dw_die_ref c;
5611   die->die_mark = 0;
5612   for (c = die->die_child; c; c = c->die_sib)
5613     unmark_dies (c);
5614 }
5615
5616 /* Return the size of the line information prolog generated for the
5617    compilation unit.  */
5618
5619 static unsigned long
5620 size_of_line_prolog ()
5621 {
5622   register unsigned long size;
5623   register unsigned long ft_index;
5624
5625   size = DWARF_LINE_PROLOG_HEADER_SIZE;
5626
5627   /* Count the size of the table giving number of args for each
5628      standard opcode.  */
5629   size += DWARF_LINE_OPCODE_BASE - 1;
5630
5631   /* Include directory table is empty (at present).  Count only the
5632      null byte used to terminate the table.  */
5633   size += 1;
5634
5635   for (ft_index = 1; ft_index < file_table_in_use; ++ft_index)
5636     {
5637       /* File name entry.  */
5638       size += size_of_string (file_table[ft_index]);
5639
5640       /* Include directory index.  */
5641       size += size_of_uleb128 (0);
5642
5643       /* Modification time.  */
5644       size += size_of_uleb128 (0);
5645
5646       /* File length in bytes.  */
5647       size += size_of_uleb128 (0);
5648     }
5649
5650   /* Count the file table terminator.  */
5651   size += 1;
5652   return size;
5653 }
5654
5655 /* Return the size of the .debug_pubnames table  generated for the
5656    compilation unit.  */
5657
5658 static unsigned long
5659 size_of_pubnames ()
5660 {
5661   register unsigned long size;
5662   register unsigned i;
5663
5664   size = DWARF_PUBNAMES_HEADER_SIZE;
5665   for (i = 0; i < pubname_table_in_use; ++i)
5666     {
5667       register pubname_ref p = &pubname_table[i];
5668       size += DWARF_OFFSET_SIZE + size_of_string (p->name);
5669     }
5670
5671   size += DWARF_OFFSET_SIZE;
5672   return size;
5673 }
5674
5675 /* Return the size of the information in the .debug_aranges section.  */
5676
5677 static unsigned long
5678 size_of_aranges ()
5679 {
5680   register unsigned long size;
5681
5682   size = DWARF_ARANGES_HEADER_SIZE;
5683
5684   /* Count the address/length pair for this compilation unit.  */
5685   size += 2 * DWARF2_ADDR_SIZE;
5686   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
5687
5688   /* Count the two zero words used to terminated the address range table.  */
5689   size += 2 * DWARF2_ADDR_SIZE;
5690   return size;
5691 }
5692 \f
5693 /* Select the encoding of an attribute value.  */
5694
5695 static enum dwarf_form
5696 value_format (a)
5697      dw_attr_ref a;
5698 {
5699   switch (a->dw_attr_val.val_class)
5700     {
5701     case dw_val_class_addr:
5702       return DW_FORM_addr;
5703     case dw_val_class_loc:
5704       switch (constant_size (size_of_locs (AT_loc (a))))
5705         {
5706         case 1:
5707           return DW_FORM_block1;
5708         case 2:
5709           return DW_FORM_block2;
5710         default:
5711           abort ();
5712         }
5713     case dw_val_class_const:
5714       return DW_FORM_sdata;
5715     case dw_val_class_unsigned_const:
5716       switch (constant_size (AT_unsigned (a)))
5717         {
5718         case 1:
5719           return DW_FORM_data1;
5720         case 2:
5721           return DW_FORM_data2;
5722         case 4:
5723           return DW_FORM_data4;
5724         case 8:
5725           return DW_FORM_data8;
5726         default:
5727           abort ();
5728         }
5729     case dw_val_class_long_long:
5730       return DW_FORM_block1;
5731     case dw_val_class_float:
5732       return DW_FORM_block1;
5733     case dw_val_class_flag:
5734       return DW_FORM_flag;
5735     case dw_val_class_die_ref:
5736       if (AT_ref_external (a))
5737         return DW_FORM_ref_addr;
5738       else
5739         return DW_FORM_ref;
5740     case dw_val_class_fde_ref:
5741       return DW_FORM_data;
5742     case dw_val_class_lbl_id:
5743       return DW_FORM_addr;
5744     case dw_val_class_lbl_offset:
5745       return DW_FORM_data;
5746     case dw_val_class_str:
5747       return DW_FORM_string;
5748     default:
5749       abort ();
5750     }
5751 }
5752
5753 /* Output the encoding of an attribute value.  */
5754
5755 static void
5756 output_value_format (a)
5757      dw_attr_ref a;
5758 {
5759   enum dwarf_form form = value_format (a);
5760
5761   output_uleb128 (form);
5762   if (flag_debug_asm)
5763     fprintf (asm_out_file, " (%s)", dwarf_form_name (form));
5764
5765   fputc ('\n', asm_out_file);
5766 }
5767
5768 /* Output the .debug_abbrev section which defines the DIE abbreviation
5769    table.  */
5770
5771 static void
5772 output_abbrev_section ()
5773 {
5774   unsigned long abbrev_id;
5775
5776   dw_attr_ref a_attr;
5777   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
5778     {
5779       register dw_die_ref abbrev = abbrev_die_table[abbrev_id];
5780
5781       output_uleb128 (abbrev_id);
5782       if (flag_debug_asm)
5783         fprintf (asm_out_file, " (abbrev code)");
5784
5785       fputc ('\n', asm_out_file);
5786       output_uleb128 (abbrev->die_tag);
5787       if (flag_debug_asm)
5788         fprintf (asm_out_file, " (TAG: %s)",
5789                  dwarf_tag_name (abbrev->die_tag));
5790
5791       fputc ('\n', asm_out_file);
5792       fprintf (asm_out_file, "%s0x%x", ASM_BYTE_OP,
5793                abbrev->die_child != NULL ? DW_children_yes : DW_children_no);
5794
5795       if (flag_debug_asm)
5796         fprintf (asm_out_file, "\t%s %s",
5797                  ASM_COMMENT_START,
5798                  (abbrev->die_child != NULL
5799                   ? "DW_children_yes" : "DW_children_no"));
5800
5801       fputc ('\n', asm_out_file);
5802
5803       for (a_attr = abbrev->die_attr; a_attr != NULL;
5804            a_attr = a_attr->dw_attr_next)
5805         {
5806           output_uleb128 (a_attr->dw_attr);
5807           if (flag_debug_asm)
5808             fprintf (asm_out_file, " (%s)",
5809                      dwarf_attr_name (a_attr->dw_attr));
5810
5811           fputc ('\n', asm_out_file);
5812           output_value_format (a_attr);
5813         }
5814
5815       fprintf (asm_out_file, "%s0,0\n", ASM_BYTE_OP);
5816     }
5817
5818   /* Terminate the table.  */
5819   fprintf (asm_out_file, "%s0\n", ASM_BYTE_OP);
5820 }
5821
5822 /* Output a symbol we can use to refer to this DIE from another CU.  */
5823
5824 static inline void
5825 output_die_symbol (die)
5826      register dw_die_ref die;
5827 {
5828   char *sym = die->die_symbol;
5829
5830   if (sym == 0)
5831     return;
5832
5833   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
5834     /* We make these global, not weak; if the target doesn't support
5835        .linkonce, it doesn't support combining the sections, so debugging
5836        will break.  */
5837     ASM_GLOBALIZE_LABEL (asm_out_file, sym);
5838   ASM_OUTPUT_LABEL (asm_out_file, sym);
5839 }
5840
5841 /* Output a symbolic (i.e. FORM_ref_addr) reference to TARGET_DIE.  */
5842
5843 static inline void
5844 output_symbolic_ref (target_die)
5845      dw_die_ref target_die;
5846 {
5847   char *sym = target_die->die_symbol;
5848
5849   if (sym == 0)
5850     abort ();
5851
5852   ASM_OUTPUT_DWARF_OFFSET (asm_out_file, sym);
5853 }
5854
5855 /* Output the DIE and its attributes.  Called recursively to generate
5856    the definitions of each child DIE.  */
5857
5858 static void
5859 output_die (die)
5860      register dw_die_ref die;
5861 {
5862   register dw_attr_ref a;
5863   register dw_die_ref c;
5864   register unsigned long size;
5865
5866   /* If someone in another CU might refer to us, set up a symbol for
5867      them to point to.  */
5868   if (die->die_symbol)
5869     output_die_symbol (die);
5870
5871   output_uleb128 (die->die_abbrev);
5872   if (flag_debug_asm)
5873     fprintf (asm_out_file, " (DIE (0x%lx) %s)",
5874              die->die_offset, dwarf_tag_name (die->die_tag));
5875
5876   fputc ('\n', asm_out_file);
5877
5878   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5879     {
5880       switch (AT_class (a))
5881         {
5882         case dw_val_class_addr:
5883           ASM_OUTPUT_DWARF_ADDR_CONST (asm_out_file, AT_addr (a));
5884           break;
5885
5886         case dw_val_class_loc:
5887           size = size_of_locs (AT_loc (a));
5888
5889           /* Output the block length for this list of location operations.  */
5890           switch (constant_size (size))
5891             {
5892             case 1:
5893               ASM_OUTPUT_DWARF_DATA1 (asm_out_file, size);
5894               break;
5895             case 2:
5896               ASM_OUTPUT_DWARF_DATA2 (asm_out_file, size);
5897               break;
5898             default:
5899               abort ();
5900             }
5901
5902           if (flag_debug_asm)
5903             fprintf (asm_out_file, "\t%s %s",
5904                      ASM_COMMENT_START, dwarf_attr_name (a->dw_attr));
5905
5906           fputc ('\n', asm_out_file);
5907
5908           output_loc_sequence (AT_loc (a));
5909           break;
5910
5911         case dw_val_class_const:
5912           /* ??? It would be slightly more efficient to use a scheme like is
5913              used for unsigned constants below, but gdb 4.x does not sign
5914              extend.  Gdb 5.x does sign extend.  */
5915           output_sleb128 (AT_int (a));
5916           break;
5917
5918         case dw_val_class_unsigned_const:
5919           switch (constant_size (AT_unsigned (a)))
5920             {
5921             case 1:
5922               ASM_OUTPUT_DWARF_DATA1 (asm_out_file, AT_unsigned (a));
5923               break;
5924             case 2:
5925               ASM_OUTPUT_DWARF_DATA2 (asm_out_file, AT_unsigned (a));
5926               break;
5927             case 4:
5928               ASM_OUTPUT_DWARF_DATA4 (asm_out_file, AT_unsigned (a));
5929               break;
5930             case 8:
5931               ASM_OUTPUT_DWARF_DATA8 (asm_out_file, AT_unsigned (a));
5932               break;
5933             default:
5934               abort ();
5935             }
5936           break;
5937
5938         case dw_val_class_long_long:
5939           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 8);
5940           if (flag_debug_asm)
5941             fprintf (asm_out_file, "\t%s %s",
5942                      ASM_COMMENT_START, dwarf_attr_name (a->dw_attr));
5943
5944           fputc ('\n', asm_out_file);
5945           ASM_OUTPUT_DWARF_CONST_DOUBLE (asm_out_file,
5946                                          a->dw_attr_val.v.val_long_long.hi,
5947                                          a->dw_attr_val.v.val_long_long.low);
5948
5949           if (flag_debug_asm)
5950             fprintf (asm_out_file,
5951                      "\t%s long long constant", ASM_COMMENT_START);
5952
5953           fputc ('\n', asm_out_file);
5954           break;
5955
5956         case dw_val_class_float:
5957           {
5958             register unsigned int i;
5959             ASM_OUTPUT_DWARF_DATA1 (asm_out_file,
5960                                     a->dw_attr_val.v.val_float.length * 4);
5961             if (flag_debug_asm)
5962               fprintf (asm_out_file, "\t%s %s",
5963                        ASM_COMMENT_START, dwarf_attr_name (a->dw_attr));
5964
5965             fputc ('\n', asm_out_file);
5966             for (i = 0; i < a->dw_attr_val.v.val_float.length; ++i)
5967               {
5968                 ASM_OUTPUT_DWARF_DATA4 (asm_out_file,
5969                                         a->dw_attr_val.v.val_float.array[i]);
5970                 if (flag_debug_asm)
5971                   fprintf (asm_out_file, "\t%s fp constant word %u",
5972                            ASM_COMMENT_START, i);
5973
5974                 fputc ('\n', asm_out_file);
5975               }
5976             break;
5977           }
5978
5979         case dw_val_class_flag:
5980           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, AT_flag (a));
5981           break;
5982
5983         case dw_val_class_die_ref:
5984           if (AT_ref_external (a))
5985             output_symbolic_ref (AT_ref (a));
5986           else
5987             ASM_OUTPUT_DWARF_DATA (asm_out_file, AT_ref (a)->die_offset);
5988           break;
5989
5990         case dw_val_class_fde_ref:
5991           {
5992             char l1[20];
5993             ASM_GENERATE_INTERNAL_LABEL
5994               (l1, FDE_AFTER_SIZE_LABEL, a->dw_attr_val.v.val_fde_index * 2);
5995             ASM_OUTPUT_DWARF_OFFSET (asm_out_file, l1);
5996             fprintf (asm_out_file, " - %d", DWARF_OFFSET_SIZE);
5997           }
5998           break;
5999
6000         case dw_val_class_lbl_id:
6001           ASM_OUTPUT_DWARF_ADDR (asm_out_file, AT_lbl (a));
6002           break;
6003
6004         case dw_val_class_lbl_offset:
6005           ASM_OUTPUT_DWARF_OFFSET (asm_out_file, AT_lbl (a));
6006           break;
6007
6008         case dw_val_class_str:
6009           if (flag_debug_asm)
6010             ASM_OUTPUT_DWARF_STRING (asm_out_file, AT_string (a));
6011           else
6012             ASM_OUTPUT_ASCII (asm_out_file, AT_string (a),
6013                               (int) strlen (AT_string (a)) + 1);
6014           break;
6015
6016         default:
6017           abort ();
6018         }
6019
6020       if (AT_class (a) != dw_val_class_loc
6021           && AT_class (a) != dw_val_class_long_long
6022           && AT_class (a) != dw_val_class_float)
6023         {
6024           if (flag_debug_asm)
6025             fprintf (asm_out_file, "\t%s %s",
6026                      ASM_COMMENT_START, dwarf_attr_name (a->dw_attr));
6027
6028           fputc ('\n', asm_out_file);
6029         }
6030     }
6031
6032   for (c = die->die_child; c != NULL; c = c->die_sib)
6033     output_die (c);
6034
6035   if (die->die_child != NULL)
6036     {
6037       /* Add null byte to terminate sibling list.  */
6038       ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
6039       if (flag_debug_asm)
6040         fprintf (asm_out_file, "\t%s end of children of DIE 0x%lx",
6041                  ASM_COMMENT_START, die->die_offset);
6042
6043       fputc ('\n', asm_out_file);
6044     }
6045 }
6046
6047 /* Output the compilation unit that appears at the beginning of the
6048    .debug_info section, and precedes the DIE descriptions.  */
6049
6050 static void
6051 output_compilation_unit_header ()
6052 {
6053   ASM_OUTPUT_DWARF_DATA (asm_out_file, next_die_offset - DWARF_OFFSET_SIZE);
6054   if (flag_debug_asm)
6055     fprintf (asm_out_file, "\t%s Length of Compilation Unit Info.",
6056              ASM_COMMENT_START);
6057
6058   fputc ('\n', asm_out_file);
6059   ASM_OUTPUT_DWARF_DATA2 (asm_out_file, DWARF_VERSION);
6060   if (flag_debug_asm)
6061     fprintf (asm_out_file, "\t%s DWARF version number", ASM_COMMENT_START);
6062
6063   fputc ('\n', asm_out_file);
6064   ASM_OUTPUT_DWARF_OFFSET (asm_out_file, abbrev_section_label);
6065   if (flag_debug_asm)
6066     fprintf (asm_out_file, "\t%s Offset Into Abbrev. Section",
6067              ASM_COMMENT_START);
6068
6069   fputc ('\n', asm_out_file);
6070   ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DWARF2_ADDR_SIZE);
6071   if (flag_debug_asm)
6072     fprintf (asm_out_file, "\t%s Pointer Size (in bytes)", ASM_COMMENT_START);
6073
6074   fputc ('\n', asm_out_file);
6075 }
6076
6077 /* Output the compilation unit DIE and its children.  */
6078
6079 static void
6080 output_comp_unit (die)
6081      dw_die_ref die;
6082 {
6083   char *secname;
6084
6085   if (die->die_child == 0)
6086     return;
6087
6088   /* Mark all the DIEs in this CU so we know which get local refs.  */
6089   mark_dies (die);
6090
6091   build_abbrev_table (die);
6092
6093   /* Initialize the beginning DIE offset - and calculate sizes/offsets.   */
6094   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
6095   calc_die_sizes (die);
6096
6097   if (die->die_symbol)
6098     {
6099       secname = (char *) alloca (strlen (die->die_symbol) + 24);
6100       sprintf (secname, ".gnu.linkonce.wi.%s", die->die_symbol);
6101       die->die_symbol = NULL;
6102     }
6103   else
6104     secname = (char *) DEBUG_INFO_SECTION;
6105
6106   /* Output debugging information.  */
6107   fputc ('\n', asm_out_file);
6108   ASM_OUTPUT_SECTION (asm_out_file, secname);
6109   output_compilation_unit_header ();
6110   output_die (die);
6111
6112   /* Leave the marks on the main CU, so we can check them in
6113      output_pubnames.  */
6114   if (die->die_symbol)
6115     unmark_dies (die);
6116 }
6117
6118 /* The DWARF2 pubname for a nested thingy looks like "A::f".  The output
6119    of decl_printable_name for C++ looks like "A::f(int)".  Let's drop the
6120    argument list, and maybe the scope.  */
6121
6122 static const char *
6123 dwarf2_name (decl, scope)
6124      tree decl;
6125      int scope;
6126 {
6127   return (*decl_printable_name) (decl, scope ? 1 : 0);
6128 }
6129
6130 /* Add a new entry to .debug_pubnames if appropriate.  */
6131
6132 static void
6133 add_pubname (decl, die)
6134      tree decl;
6135      dw_die_ref die;
6136 {
6137   pubname_ref p;
6138
6139   if (! TREE_PUBLIC (decl))
6140     return;
6141
6142   if (pubname_table_in_use == pubname_table_allocated)
6143     {
6144       pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
6145       pubname_table = (pubname_ref) xrealloc
6146         (pubname_table, pubname_table_allocated * sizeof (pubname_entry));
6147     }
6148
6149   p = &pubname_table[pubname_table_in_use++];
6150   p->die = die;
6151
6152   p->name = xstrdup (dwarf2_name (decl, 1));
6153 }
6154
6155 /* Output the public names table used to speed up access to externally
6156    visible names.  For now, only generate entries for externally
6157    visible procedures.  */
6158
6159 static void
6160 output_pubnames ()
6161 {
6162   register unsigned i;
6163   register unsigned long pubnames_length = size_of_pubnames ();
6164
6165   ASM_OUTPUT_DWARF_DATA (asm_out_file, pubnames_length);
6166
6167   if (flag_debug_asm)
6168     fprintf (asm_out_file, "\t%s Length of Public Names Info.",
6169              ASM_COMMENT_START);
6170
6171   fputc ('\n', asm_out_file);
6172   ASM_OUTPUT_DWARF_DATA2 (asm_out_file, DWARF_VERSION);
6173
6174   if (flag_debug_asm)
6175     fprintf (asm_out_file, "\t%s DWARF Version", ASM_COMMENT_START);
6176
6177   fputc ('\n', asm_out_file);
6178   ASM_OUTPUT_DWARF_OFFSET (asm_out_file, debug_info_section_label);
6179   if (flag_debug_asm)
6180     fprintf (asm_out_file, "\t%s Offset of Compilation Unit Info.",
6181              ASM_COMMENT_START);
6182
6183   fputc ('\n', asm_out_file);
6184   ASM_OUTPUT_DWARF_DATA (asm_out_file, next_die_offset);
6185   if (flag_debug_asm)
6186     fprintf (asm_out_file, "\t%s Compilation Unit Length", ASM_COMMENT_START);
6187
6188   fputc ('\n', asm_out_file);
6189   for (i = 0; i < pubname_table_in_use; ++i)
6190     {
6191       register pubname_ref pub = &pubname_table[i];
6192
6193       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
6194       if (pub->die->die_mark == 0)
6195         abort ();
6196
6197       ASM_OUTPUT_DWARF_DATA (asm_out_file, pub->die->die_offset);
6198       if (flag_debug_asm)
6199         fprintf (asm_out_file, "\t%s DIE offset", ASM_COMMENT_START);
6200
6201       fputc ('\n', asm_out_file);
6202
6203       if (flag_debug_asm)
6204         {
6205           ASM_OUTPUT_DWARF_STRING (asm_out_file, pub->name);
6206           fprintf (asm_out_file, "%s external name", ASM_COMMENT_START);
6207         }
6208       else
6209         {
6210           ASM_OUTPUT_ASCII (asm_out_file, pub->name,
6211                             (int) strlen (pub->name) + 1);
6212         }
6213
6214       fputc ('\n', asm_out_file);
6215     }
6216
6217   ASM_OUTPUT_DWARF_DATA (asm_out_file, 0);
6218   fputc ('\n', asm_out_file);
6219 }
6220
6221 /* Add a new entry to .debug_aranges if appropriate.  */
6222
6223 static void
6224 add_arange (decl, die)
6225      tree decl;
6226      dw_die_ref die;
6227 {
6228   if (! DECL_SECTION_NAME (decl))
6229     return;
6230
6231   if (arange_table_in_use == arange_table_allocated)
6232     {
6233       arange_table_allocated += ARANGE_TABLE_INCREMENT;
6234       arange_table
6235         = (arange_ref) xrealloc (arange_table,
6236                                  arange_table_allocated * sizeof (dw_die_ref));
6237     }
6238
6239   arange_table[arange_table_in_use++] = die;
6240 }
6241
6242 /* Output the information that goes into the .debug_aranges table.
6243    Namely, define the beginning and ending address range of the
6244    text section generated for this compilation unit.  */
6245
6246 static void
6247 output_aranges ()
6248 {
6249   register unsigned i;
6250   register unsigned long aranges_length = size_of_aranges ();
6251
6252   ASM_OUTPUT_DWARF_DATA (asm_out_file, aranges_length);
6253   if (flag_debug_asm)
6254     fprintf (asm_out_file, "\t%s Length of Address Ranges Info.",
6255              ASM_COMMENT_START);
6256
6257   fputc ('\n', asm_out_file);
6258   ASM_OUTPUT_DWARF_DATA2 (asm_out_file, DWARF_VERSION);
6259   if (flag_debug_asm)
6260     fprintf (asm_out_file, "\t%s DWARF Version", ASM_COMMENT_START);
6261
6262   fputc ('\n', asm_out_file);
6263   ASM_OUTPUT_DWARF_OFFSET (asm_out_file, debug_info_section_label);
6264   if (flag_debug_asm)
6265     fprintf (asm_out_file, "\t%s Offset of Compilation Unit Info.",
6266              ASM_COMMENT_START);
6267
6268   fputc ('\n', asm_out_file);
6269   ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DWARF2_ADDR_SIZE);
6270   if (flag_debug_asm)
6271     fprintf (asm_out_file, "\t%s Size of Address", ASM_COMMENT_START);
6272
6273   fputc ('\n', asm_out_file);
6274   ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
6275   if (flag_debug_asm)
6276     fprintf (asm_out_file, "\t%s Size of Segment Descriptor",
6277              ASM_COMMENT_START);
6278
6279   fputc ('\n', asm_out_file);
6280
6281   /* We need to align to twice the pointer size here.  */
6282   if (DWARF_ARANGES_PAD_SIZE)
6283     {
6284       /* Pad using a 2 bytes word so that padding is correct
6285          for any pointer size.  */
6286       ASM_OUTPUT_DWARF_DATA2 (asm_out_file, 0);
6287       for (i = 2; i < DWARF_ARANGES_PAD_SIZE; i += 2)
6288         fprintf (asm_out_file, ",0");
6289       if (flag_debug_asm)
6290         fprintf (asm_out_file, "\t%s Pad to %d byte boundary",
6291                  ASM_COMMENT_START, 2 * DWARF2_ADDR_SIZE);
6292     }
6293
6294   fputc ('\n', asm_out_file);
6295   ASM_OUTPUT_DWARF_ADDR (asm_out_file, text_section_label);
6296   if (flag_debug_asm)
6297     fprintf (asm_out_file, "\t%s Address", ASM_COMMENT_START);
6298
6299   fputc ('\n', asm_out_file);
6300   ASM_OUTPUT_DWARF_ADDR_DELTA (asm_out_file, text_end_label,
6301                                text_section_label);
6302   if (flag_debug_asm)
6303     fprintf (asm_out_file, "%s Length", ASM_COMMENT_START);
6304
6305   fputc ('\n', asm_out_file);
6306   for (i = 0; i < arange_table_in_use; ++i)
6307     {
6308       dw_die_ref die = arange_table[i];
6309
6310       /* We shouldn't see aranges for DIEs outside of the main CU.  */
6311       if (die->die_mark == 0)
6312         abort ();
6313
6314       if (die->die_tag == DW_TAG_subprogram)
6315         ASM_OUTPUT_DWARF_ADDR (asm_out_file, get_AT_low_pc (die));
6316       else
6317         {
6318           /* A static variable; extract the symbol from DW_AT_location.
6319              Note that this code isn't currently hit, as we only emit
6320              aranges for functions (jason 9/23/99).  */
6321
6322           dw_attr_ref a = get_AT (die, DW_AT_location);
6323           dw_loc_descr_ref loc;
6324           if (! a || AT_class (a) != dw_val_class_loc)
6325             abort ();
6326
6327           loc = AT_loc (a);
6328           if (loc->dw_loc_opc != DW_OP_addr)
6329             abort ();
6330
6331           ASM_OUTPUT_DWARF_ADDR_CONST (asm_out_file,
6332                                        loc->dw_loc_oprnd1.v.val_addr);
6333         }
6334
6335       if (flag_debug_asm)
6336         fprintf (asm_out_file, "\t%s Address", ASM_COMMENT_START);
6337
6338       fputc ('\n', asm_out_file);
6339       if (die->die_tag == DW_TAG_subprogram)
6340         ASM_OUTPUT_DWARF_ADDR_DELTA (asm_out_file, get_AT_hi_pc (die),
6341                                      get_AT_low_pc (die));
6342       else
6343         ASM_OUTPUT_DWARF_ADDR_DATA (asm_out_file,
6344                                     get_AT_unsigned (die, DW_AT_byte_size));
6345
6346       if (flag_debug_asm)
6347         fprintf (asm_out_file, "%s Length", ASM_COMMENT_START);
6348
6349       fputc ('\n', asm_out_file);
6350     }
6351
6352   /* Output the terminator words.  */
6353   ASM_OUTPUT_DWARF_ADDR_DATA (asm_out_file, 0);
6354   fputc ('\n', asm_out_file);
6355   ASM_OUTPUT_DWARF_ADDR_DATA (asm_out_file, 0);
6356   fputc ('\n', asm_out_file);
6357 }
6358
6359 /* Output the source line number correspondence information.  This
6360    information goes into the .debug_line section.  */
6361
6362 static void
6363 output_line_info ()
6364 {
6365   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
6366   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
6367   register unsigned opc;
6368   register unsigned n_op_args;
6369   register unsigned long ft_index;
6370   register unsigned long lt_index;
6371   register unsigned long current_line;
6372   register long line_offset;
6373   register long line_delta;
6374   register unsigned long current_file;
6375   register unsigned long function;
6376
6377   ASM_OUTPUT_DWARF_DELTA (asm_out_file, ".LTEND", ".LTSTART");
6378   if (flag_debug_asm)
6379     fprintf (asm_out_file, "\t%s Length of Source Line Info.",
6380              ASM_COMMENT_START);
6381
6382   fputc ('\n', asm_out_file);
6383   ASM_OUTPUT_LABEL (asm_out_file, ".LTSTART");
6384   ASM_OUTPUT_DWARF_DATA2 (asm_out_file, DWARF_VERSION);
6385   if (flag_debug_asm)
6386     fprintf (asm_out_file, "\t%s DWARF Version", ASM_COMMENT_START);
6387
6388   fputc ('\n', asm_out_file);
6389   ASM_OUTPUT_DWARF_DATA (asm_out_file, size_of_line_prolog ());
6390   if (flag_debug_asm)
6391     fprintf (asm_out_file, "\t%s Prolog Length", ASM_COMMENT_START);
6392
6393   fputc ('\n', asm_out_file);
6394   ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DWARF_LINE_MIN_INSTR_LENGTH);
6395   if (flag_debug_asm)
6396     fprintf (asm_out_file, "\t%s Minimum Instruction Length",
6397              ASM_COMMENT_START);
6398
6399   fputc ('\n', asm_out_file);
6400   ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DWARF_LINE_DEFAULT_IS_STMT_START);
6401   if (flag_debug_asm)
6402     fprintf (asm_out_file, "\t%s Default is_stmt_start flag",
6403              ASM_COMMENT_START);
6404
6405   fputc ('\n', asm_out_file);
6406   fprintf (asm_out_file, "%s%d", ASM_BYTE_OP, DWARF_LINE_BASE);
6407   if (flag_debug_asm)
6408     fprintf (asm_out_file, "\t%s Line Base Value (Special Opcodes)",
6409              ASM_COMMENT_START);
6410
6411   fputc ('\n', asm_out_file);
6412   fprintf (asm_out_file, "%s%u", ASM_BYTE_OP, DWARF_LINE_RANGE);
6413   if (flag_debug_asm)
6414     fprintf (asm_out_file, "\t%s Line Range Value (Special Opcodes)",
6415              ASM_COMMENT_START);
6416
6417   fputc ('\n', asm_out_file);
6418   fprintf (asm_out_file, "%s%u", ASM_BYTE_OP, DWARF_LINE_OPCODE_BASE);
6419   if (flag_debug_asm)
6420     fprintf (asm_out_file, "\t%s Special Opcode Base", ASM_COMMENT_START);
6421
6422   fputc ('\n', asm_out_file);
6423   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; ++opc)
6424     {
6425       switch (opc)
6426         {
6427         case DW_LNS_advance_pc:
6428         case DW_LNS_advance_line:
6429         case DW_LNS_set_file:
6430         case DW_LNS_set_column:
6431         case DW_LNS_fixed_advance_pc:
6432           n_op_args = 1;
6433           break;
6434         default:
6435           n_op_args = 0;
6436           break;
6437         }
6438       ASM_OUTPUT_DWARF_DATA1 (asm_out_file, n_op_args);
6439       if (flag_debug_asm)
6440         fprintf (asm_out_file, "\t%s opcode: 0x%x has %d args",
6441                  ASM_COMMENT_START, opc, n_op_args);
6442       fputc ('\n', asm_out_file);
6443     }
6444
6445   if (flag_debug_asm)
6446     fprintf (asm_out_file, "%s Include Directory Table\n", ASM_COMMENT_START);
6447
6448   /* Include directory table is empty, at present */
6449   ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
6450   fputc ('\n', asm_out_file);
6451   if (flag_debug_asm)
6452     fprintf (asm_out_file, "%s File Name Table\n", ASM_COMMENT_START);
6453
6454   for (ft_index = 1; ft_index < file_table_in_use; ++ft_index)
6455     {
6456       if (flag_debug_asm)
6457         {
6458           ASM_OUTPUT_DWARF_STRING (asm_out_file, file_table[ft_index]);
6459           fprintf (asm_out_file, "%s File Entry: 0x%lx",
6460                    ASM_COMMENT_START, ft_index);
6461         }
6462       else
6463         {
6464           ASM_OUTPUT_ASCII (asm_out_file,
6465                             file_table[ft_index],
6466                             (int) strlen (file_table[ft_index]) + 1);
6467         }
6468
6469       fputc ('\n', asm_out_file);
6470
6471       /* Include directory index */
6472       output_uleb128 (0);
6473       fputc ('\n', asm_out_file);
6474
6475       /* Modification time */
6476       output_uleb128 (0);
6477       fputc ('\n', asm_out_file);
6478
6479       /* File length in bytes */
6480       output_uleb128 (0);
6481       fputc ('\n', asm_out_file);
6482     }
6483
6484   /* Terminate the file name table */
6485   ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
6486   fputc ('\n', asm_out_file);
6487
6488   /* We used to set the address register to the first location in the text
6489      section here, but that didn't accomplish anything since we already
6490      have a line note for the opening brace of the first function.  */
6491
6492   /* Generate the line number to PC correspondence table, encoded as
6493      a series of state machine operations.  */
6494   current_file = 1;
6495   current_line = 1;
6496   strcpy (prev_line_label, text_section_label);
6497   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
6498     {
6499       register dw_line_info_ref line_info = &line_info_table[lt_index];
6500
6501 #if 0
6502       /* Disable this optimization for now; GDB wants to see two line notes
6503          at the beginning of a function so it can find the end of the
6504          prologue.  */
6505
6506       /* Don't emit anything for redundant notes.  Just updating the
6507          address doesn't accomplish anything, because we already assume
6508          that anything after the last address is this line.  */
6509       if (line_info->dw_line_num == current_line
6510           && line_info->dw_file_num == current_file)
6511         continue;
6512 #endif
6513
6514       /* Emit debug info for the address of the current line, choosing
6515          the encoding that uses the least amount of space.  */
6516       /* ??? Unfortunately, we have little choice here currently, and must
6517          always use the most general form.  Gcc does not know the address
6518          delta itself, so we can't use DW_LNS_advance_pc.  There are no known
6519          dwarf2 aware assemblers at this time, so we can't use any special
6520          pseudo ops that would allow the assembler to optimally encode this for
6521          us.  Many ports do have length attributes which will give an upper
6522          bound on the address range.  We could perhaps use length attributes
6523          to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
6524       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
6525       if (0)
6526         {
6527           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
6528           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_fixed_advance_pc);
6529           if (flag_debug_asm)
6530             fprintf (asm_out_file, "\t%s DW_LNS_fixed_advance_pc",
6531                      ASM_COMMENT_START);
6532
6533           fputc ('\n', asm_out_file);
6534           ASM_OUTPUT_DWARF_DELTA2 (asm_out_file, line_label, prev_line_label);
6535           fputc ('\n', asm_out_file);
6536         }
6537       else
6538         {
6539           /* This can handle any delta.  This takes
6540              4+DWARF2_ADDR_SIZE bytes.  */
6541           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
6542           if (flag_debug_asm)
6543             fprintf (asm_out_file, "\t%s DW_LNE_set_address",
6544                      ASM_COMMENT_START);
6545           fputc ('\n', asm_out_file);
6546           output_uleb128 (1 + DWARF2_ADDR_SIZE);
6547           fputc ('\n', asm_out_file);
6548           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNE_set_address);
6549           fputc ('\n', asm_out_file);
6550           ASM_OUTPUT_DWARF_ADDR (asm_out_file, line_label);
6551           fputc ('\n', asm_out_file);
6552         }
6553       strcpy (prev_line_label, line_label);
6554
6555       /* Emit debug info for the source file of the current line, if
6556          different from the previous line.  */
6557       if (line_info->dw_file_num != current_file)
6558         {
6559           current_file = line_info->dw_file_num;
6560           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_set_file);
6561           if (flag_debug_asm)
6562             fprintf (asm_out_file, "\t%s DW_LNS_set_file", ASM_COMMENT_START);
6563
6564           fputc ('\n', asm_out_file);
6565           output_uleb128 (current_file);
6566           if (flag_debug_asm)
6567             fprintf (asm_out_file, " (\"%s\")", file_table[current_file]);
6568
6569           fputc ('\n', asm_out_file);
6570         }
6571
6572       /* Emit debug info for the current line number, choosing the encoding
6573          that uses the least amount of space.  */
6574       if (line_info->dw_line_num != current_line)
6575         {
6576           line_offset = line_info->dw_line_num - current_line;
6577           line_delta = line_offset - DWARF_LINE_BASE;
6578           current_line = line_info->dw_line_num;
6579           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
6580             {
6581               /* This can handle deltas from -10 to 234, using the current
6582                  definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
6583                  takes 1 byte.  */
6584               ASM_OUTPUT_DWARF_DATA1 (asm_out_file,
6585                                       DWARF_LINE_OPCODE_BASE + line_delta);
6586               if (flag_debug_asm)
6587                 fprintf (asm_out_file,
6588                          "\t%s line %ld", ASM_COMMENT_START, current_line);
6589
6590               fputc ('\n', asm_out_file);
6591             }
6592           else
6593             {
6594               /* This can handle any delta.  This takes at least 4 bytes,
6595                  depending on the value being encoded.  */
6596               ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_advance_line);
6597               if (flag_debug_asm)
6598                 fprintf (asm_out_file, "\t%s advance to line %ld",
6599                          ASM_COMMENT_START, current_line);
6600
6601               fputc ('\n', asm_out_file);
6602               output_sleb128 (line_offset);
6603               fputc ('\n', asm_out_file);
6604               ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_copy);
6605               if (flag_debug_asm)
6606                 fprintf (asm_out_file, "\t%s DW_LNS_copy", ASM_COMMENT_START);
6607               fputc ('\n', asm_out_file);
6608             }
6609         }
6610       else
6611         {
6612           /* We still need to start a new row, so output a copy insn.  */
6613           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_copy);
6614           if (flag_debug_asm)
6615             fprintf (asm_out_file, "\t%s DW_LNS_copy", ASM_COMMENT_START);
6616           fputc ('\n', asm_out_file);
6617         }
6618     }
6619
6620   /* Emit debug info for the address of the end of the function.  */
6621   if (0)
6622     {
6623       ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_fixed_advance_pc);
6624       if (flag_debug_asm)
6625         fprintf (asm_out_file, "\t%s DW_LNS_fixed_advance_pc",
6626                  ASM_COMMENT_START);
6627
6628       fputc ('\n', asm_out_file);
6629       ASM_OUTPUT_DWARF_DELTA2 (asm_out_file, text_end_label, prev_line_label);
6630       fputc ('\n', asm_out_file);
6631     }
6632   else
6633     {
6634       ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
6635       if (flag_debug_asm)
6636         fprintf (asm_out_file, "\t%s DW_LNE_set_address", ASM_COMMENT_START);
6637       fputc ('\n', asm_out_file);
6638       output_uleb128 (1 + DWARF2_ADDR_SIZE);
6639       fputc ('\n', asm_out_file);
6640       ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNE_set_address);
6641       fputc ('\n', asm_out_file);
6642       ASM_OUTPUT_DWARF_ADDR (asm_out_file, text_end_label);
6643       fputc ('\n', asm_out_file);
6644     }
6645
6646   ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
6647   if (flag_debug_asm)
6648     fprintf (asm_out_file, "\t%s DW_LNE_end_sequence", ASM_COMMENT_START);
6649
6650   fputc ('\n', asm_out_file);
6651   output_uleb128 (1);
6652   fputc ('\n', asm_out_file);
6653   ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNE_end_sequence);
6654   fputc ('\n', asm_out_file);
6655
6656   function = 0;
6657   current_file = 1;
6658   current_line = 1;
6659   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
6660     {
6661       register dw_separate_line_info_ref line_info
6662         = &separate_line_info_table[lt_index];
6663
6664 #if 0
6665       /* Don't emit anything for redundant notes.  */
6666       if (line_info->dw_line_num == current_line
6667           && line_info->dw_file_num == current_file
6668           && line_info->function == function)
6669         goto cont;
6670 #endif
6671
6672       /* Emit debug info for the address of the current line.  If this is
6673          a new function, or the first line of a function, then we need
6674          to handle it differently.  */
6675       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
6676                                    lt_index);
6677       if (function != line_info->function)
6678         {
6679           function = line_info->function;
6680
6681           /* Set the address register to the first line in the function */
6682           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
6683           if (flag_debug_asm)
6684             fprintf (asm_out_file, "\t%s DW_LNE_set_address",
6685                      ASM_COMMENT_START);
6686
6687           fputc ('\n', asm_out_file);
6688           output_uleb128 (1 + DWARF2_ADDR_SIZE);
6689           fputc ('\n', asm_out_file);
6690           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNE_set_address);
6691           fputc ('\n', asm_out_file);
6692           ASM_OUTPUT_DWARF_ADDR (asm_out_file, line_label);
6693           fputc ('\n', asm_out_file);
6694         }
6695       else
6696         {
6697           /* ??? See the DW_LNS_advance_pc comment above.  */
6698           if (0)
6699             {
6700               ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_fixed_advance_pc);
6701               if (flag_debug_asm)
6702                 fprintf (asm_out_file, "\t%s DW_LNS_fixed_advance_pc",
6703                          ASM_COMMENT_START);
6704
6705               fputc ('\n', asm_out_file);
6706               ASM_OUTPUT_DWARF_DELTA2 (asm_out_file, line_label,
6707                                        prev_line_label);
6708               fputc ('\n', asm_out_file);
6709             }
6710           else
6711             {
6712               ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
6713               if (flag_debug_asm)
6714                 fprintf (asm_out_file, "\t%s DW_LNE_set_address",
6715                          ASM_COMMENT_START);
6716               fputc ('\n', asm_out_file);
6717               output_uleb128 (1 + DWARF2_ADDR_SIZE);
6718               fputc ('\n', asm_out_file);
6719               ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNE_set_address);
6720               fputc ('\n', asm_out_file);
6721               ASM_OUTPUT_DWARF_ADDR (asm_out_file, line_label);
6722               fputc ('\n', asm_out_file);
6723             }
6724         }
6725       strcpy (prev_line_label, line_label);
6726
6727       /* Emit debug info for the source file of the current line, if
6728          different from the previous line.  */
6729       if (line_info->dw_file_num != current_file)
6730         {
6731           current_file = line_info->dw_file_num;
6732           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_set_file);
6733           if (flag_debug_asm)
6734             fprintf (asm_out_file, "\t%s DW_LNS_set_file", ASM_COMMENT_START);
6735
6736           fputc ('\n', asm_out_file);
6737           output_uleb128 (current_file);
6738           if (flag_debug_asm)
6739             fprintf (asm_out_file, " (\"%s\")", file_table[current_file]);
6740
6741           fputc ('\n', asm_out_file);
6742         }
6743
6744       /* Emit debug info for the current line number, choosing the encoding
6745          that uses the least amount of space.  */
6746       if (line_info->dw_line_num != current_line)
6747         {
6748           line_offset = line_info->dw_line_num - current_line;
6749           line_delta = line_offset - DWARF_LINE_BASE;
6750           current_line = line_info->dw_line_num;
6751           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
6752             {
6753               ASM_OUTPUT_DWARF_DATA1 (asm_out_file,
6754                                       DWARF_LINE_OPCODE_BASE + line_delta);
6755               if (flag_debug_asm)
6756                 fprintf (asm_out_file,
6757                          "\t%s line %ld", ASM_COMMENT_START, current_line);
6758
6759               fputc ('\n', asm_out_file);
6760             }
6761           else
6762             {
6763               ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_advance_line);
6764               if (flag_debug_asm)
6765                 fprintf (asm_out_file, "\t%s advance to line %ld",
6766                          ASM_COMMENT_START, current_line);
6767
6768               fputc ('\n', asm_out_file);
6769               output_sleb128 (line_offset);
6770               fputc ('\n', asm_out_file);
6771               ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_copy);
6772               if (flag_debug_asm)
6773                 fprintf (asm_out_file, "\t%s DW_LNS_copy", ASM_COMMENT_START);
6774               fputc ('\n', asm_out_file);
6775             }
6776         }
6777       else
6778         {
6779           /* We still need to start a new row, so output a copy insn.  */
6780           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_copy);
6781           if (flag_debug_asm)
6782             fprintf (asm_out_file, "\t%s DW_LNS_copy", ASM_COMMENT_START);
6783           fputc ('\n', asm_out_file);
6784         }
6785
6786 #if 0
6787     cont:
6788 #endif
6789       ++lt_index;
6790
6791       /* If we're done with a function, end its sequence.  */
6792       if (lt_index == separate_line_info_table_in_use
6793           || separate_line_info_table[lt_index].function != function)
6794         {
6795           current_file = 1;
6796           current_line = 1;
6797
6798           /* Emit debug info for the address of the end of the function.  */
6799           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
6800           if (0)
6801             {
6802               ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNS_fixed_advance_pc);
6803               if (flag_debug_asm)
6804                 fprintf (asm_out_file, "\t%s DW_LNS_fixed_advance_pc",
6805                          ASM_COMMENT_START);
6806
6807               fputc ('\n', asm_out_file);
6808               ASM_OUTPUT_DWARF_DELTA2 (asm_out_file, line_label,
6809                                        prev_line_label);
6810               fputc ('\n', asm_out_file);
6811             }
6812           else
6813             {
6814               ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
6815               if (flag_debug_asm)
6816                 fprintf (asm_out_file, "\t%s DW_LNE_set_address",
6817                          ASM_COMMENT_START);
6818               fputc ('\n', asm_out_file);
6819               output_uleb128 (1 + DWARF2_ADDR_SIZE);
6820               fputc ('\n', asm_out_file);
6821               ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNE_set_address);
6822               fputc ('\n', asm_out_file);
6823               ASM_OUTPUT_DWARF_ADDR (asm_out_file, line_label);
6824               fputc ('\n', asm_out_file);
6825             }
6826
6827           /* Output the marker for the end of this sequence.  */
6828           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 0);
6829           if (flag_debug_asm)
6830             fprintf (asm_out_file, "\t%s DW_LNE_end_sequence",
6831                      ASM_COMMENT_START);
6832
6833           fputc ('\n', asm_out_file);
6834           output_uleb128 (1);
6835           fputc ('\n', asm_out_file);
6836           ASM_OUTPUT_DWARF_DATA1 (asm_out_file, DW_LNE_end_sequence);
6837           fputc ('\n', asm_out_file);
6838         }
6839     }
6840
6841   /* Output the marker for the end of the line number info.  */
6842   ASM_OUTPUT_LABEL (asm_out_file, ".LTEND");
6843 }
6844 \f
6845 /* Given a pointer to a tree node for some base type, return a pointer to
6846    a DIE that describes the given type.
6847
6848    This routine must only be called for GCC type nodes that correspond to
6849    Dwarf base (fundamental) types.  */
6850
6851 static dw_die_ref
6852 base_type_die (type)
6853      register tree type;
6854 {
6855   register dw_die_ref base_type_result;
6856   register const char *type_name;
6857   register enum dwarf_type encoding;
6858   register tree name = TYPE_NAME (type);
6859
6860   if (TREE_CODE (type) == ERROR_MARK
6861       || TREE_CODE (type) == VOID_TYPE)
6862     return 0;
6863
6864   if (name)
6865     {
6866       if (TREE_CODE (name) == TYPE_DECL)
6867         name = DECL_NAME (name);
6868
6869       type_name = IDENTIFIER_POINTER (name);
6870     }
6871   else
6872     type_name = "__unknown__";
6873
6874   switch (TREE_CODE (type))
6875     {
6876     case INTEGER_TYPE:
6877       /* Carefully distinguish the C character types, without messing
6878          up if the language is not C. Note that we check only for the names
6879          that contain spaces; other names might occur by coincidence in other
6880          languages.  */
6881       if (! (TYPE_PRECISION (type) == CHAR_TYPE_SIZE
6882              && (type == char_type_node
6883                  || ! strcmp (type_name, "signed char")
6884                  || ! strcmp (type_name, "unsigned char"))))
6885         {
6886           if (TREE_UNSIGNED (type))
6887             encoding = DW_ATE_unsigned;
6888           else
6889             encoding = DW_ATE_signed;
6890           break;
6891         }
6892       /* else fall through.  */
6893
6894     case CHAR_TYPE:
6895       /* GNU Pascal/Ada CHAR type.  Not used in C.  */
6896       if (TREE_UNSIGNED (type))
6897         encoding = DW_ATE_unsigned_char;
6898       else
6899         encoding = DW_ATE_signed_char;
6900       break;
6901
6902     case REAL_TYPE:
6903       encoding = DW_ATE_float;
6904       break;
6905
6906       /* Dwarf2 doesn't know anything about complex ints, so use
6907          a user defined type for it.  */
6908     case COMPLEX_TYPE:
6909       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
6910         encoding = DW_ATE_complex_float;
6911       else
6912         encoding = DW_ATE_lo_user;
6913       break;
6914
6915     case BOOLEAN_TYPE:
6916       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
6917       encoding = DW_ATE_boolean;
6918       break;
6919
6920     default:
6921       abort (); /* No other TREE_CODEs are Dwarf fundamental types.  */
6922     }
6923
6924   base_type_result = new_die (DW_TAG_base_type, comp_unit_die);
6925   if (demangle_name_func)
6926     type_name = (*demangle_name_func) (type_name);
6927
6928   add_AT_string (base_type_result, DW_AT_name, type_name);
6929   add_AT_unsigned (base_type_result, DW_AT_byte_size,
6930                    int_size_in_bytes (type));
6931   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
6932
6933   return base_type_result;
6934 }
6935
6936 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
6937    the Dwarf "root" type for the given input type.  The Dwarf "root" type of
6938    a given type is generally the same as the given type, except that if the
6939    given type is a pointer or reference type, then the root type of the given
6940    type is the root type of the "basis" type for the pointer or reference
6941    type.  (This definition of the "root" type is recursive.) Also, the root
6942    type of a `const' qualified type or a `volatile' qualified type is the
6943    root type of the given type without the qualifiers.  */
6944
6945 static tree
6946 root_type (type)
6947      register tree type;
6948 {
6949   if (TREE_CODE (type) == ERROR_MARK)
6950     return error_mark_node;
6951
6952   switch (TREE_CODE (type))
6953     {
6954     case ERROR_MARK:
6955       return error_mark_node;
6956
6957     case POINTER_TYPE:
6958     case REFERENCE_TYPE:
6959       return type_main_variant (root_type (TREE_TYPE (type)));
6960
6961     default:
6962       return type_main_variant (type);
6963     }
6964 }
6965
6966 /* Given a pointer to an arbitrary ..._TYPE tree node, return non-zero if the
6967    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
6968
6969 static inline int
6970 is_base_type (type)
6971      register tree type;
6972 {
6973   switch (TREE_CODE (type))
6974     {
6975     case ERROR_MARK:
6976     case VOID_TYPE:
6977     case INTEGER_TYPE:
6978     case REAL_TYPE:
6979     case COMPLEX_TYPE:
6980     case BOOLEAN_TYPE:
6981     case CHAR_TYPE:
6982       return 1;
6983
6984     case SET_TYPE:
6985     case ARRAY_TYPE:
6986     case RECORD_TYPE:
6987     case UNION_TYPE:
6988     case QUAL_UNION_TYPE:
6989     case ENUMERAL_TYPE:
6990     case FUNCTION_TYPE:
6991     case METHOD_TYPE:
6992     case POINTER_TYPE:
6993     case REFERENCE_TYPE:
6994     case FILE_TYPE:
6995     case OFFSET_TYPE:
6996     case LANG_TYPE:
6997       return 0;
6998
6999     default:
7000       abort ();
7001     }
7002
7003   return 0;
7004 }
7005
7006 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
7007    entry that chains various modifiers in front of the given type.  */
7008
7009 static dw_die_ref
7010 modified_type_die (type, is_const_type, is_volatile_type, context_die)
7011      register tree type;
7012      register int is_const_type;
7013      register int is_volatile_type;
7014      register dw_die_ref context_die;
7015 {
7016   register enum tree_code code = TREE_CODE (type);
7017   register dw_die_ref mod_type_die = NULL;
7018   register dw_die_ref sub_die = NULL;
7019   register tree item_type = NULL;
7020
7021   if (code != ERROR_MARK)
7022     {
7023       type = build_type_variant (type, is_const_type, is_volatile_type);
7024
7025       mod_type_die = lookup_type_die (type);
7026       if (mod_type_die)
7027         return mod_type_die;
7028
7029       /* Handle C typedef types.  */
7030       if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
7031           && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
7032         {
7033           tree dtype = TREE_TYPE (TYPE_NAME (type));
7034           if (type == dtype)
7035             {
7036               /* For a named type, use the typedef.  */
7037               gen_type_die (type, context_die);
7038               mod_type_die = lookup_type_die (type);
7039             }
7040
7041           else if (is_const_type < TYPE_READONLY (dtype)
7042                    || is_volatile_type < TYPE_VOLATILE (dtype))
7043             /* cv-unqualified version of named type.  Just use the unnamed
7044                type to which it refers.  */
7045             mod_type_die
7046               = modified_type_die (DECL_ORIGINAL_TYPE (TYPE_NAME (type)),
7047                                    is_const_type, is_volatile_type,
7048                                    context_die);
7049           /* Else cv-qualified version of named type; fall through.  */
7050         }
7051
7052       if (mod_type_die)
7053         /* OK.  */
7054         ;
7055       else if (is_const_type)
7056         {
7057           mod_type_die = new_die (DW_TAG_const_type, comp_unit_die);
7058           sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
7059         }
7060       else if (is_volatile_type)
7061         {
7062           mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die);
7063           sub_die = modified_type_die (type, 0, 0, context_die);
7064         }
7065       else if (code == POINTER_TYPE)
7066         {
7067           mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die);
7068           add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
7069 #if 0
7070           add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
7071 #endif
7072           item_type = TREE_TYPE (type);
7073         }
7074       else if (code == REFERENCE_TYPE)
7075         {
7076           mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die);
7077           add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
7078 #if 0
7079           add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
7080 #endif
7081           item_type = TREE_TYPE (type);
7082         }
7083       else if (is_base_type (type))
7084         mod_type_die = base_type_die (type);
7085       else
7086         {
7087           gen_type_die (type, context_die);
7088
7089           /* We have to get the type_main_variant here (and pass that to the
7090              `lookup_type_die' routine) because the ..._TYPE node we have
7091              might simply be a *copy* of some original type node (where the
7092              copy was created to help us keep track of typedef names) and
7093              that copy might have a different TYPE_UID from the original
7094              ..._TYPE node.  */
7095           mod_type_die = lookup_type_die (type_main_variant (type));
7096           if (mod_type_die == NULL)
7097             abort ();
7098         }
7099     }
7100
7101   equate_type_number_to_die (type, mod_type_die);
7102   if (item_type)
7103     /* We must do this after the equate_type_number_to_die call, in case
7104        this is a recursive type.  This ensures that the modified_type_die
7105        recursion will terminate even if the type is recursive.  Recursive
7106        types are possible in Ada.  */
7107     sub_die = modified_type_die (item_type,
7108                                  TYPE_READONLY (item_type),
7109                                  TYPE_VOLATILE (item_type),
7110                                  context_die);
7111
7112   if (sub_die != NULL)
7113     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
7114
7115   return mod_type_die;
7116 }
7117
7118 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
7119    an enumerated type.   */
7120
7121 static inline int
7122 type_is_enum (type)
7123      register tree type;
7124 {
7125   return TREE_CODE (type) == ENUMERAL_TYPE;
7126 }
7127
7128 /* Return the register number described by a given RTL node.  */
7129
7130 static unsigned int
7131 reg_number (rtl)
7132      register rtx rtl;
7133 {
7134   register unsigned regno = REGNO (rtl);
7135
7136   if (regno >= FIRST_PSEUDO_REGISTER)
7137     {
7138       warning ("internal regno botch: regno = %d\n", regno);
7139       regno = 0;
7140     }
7141
7142   regno = DBX_REGISTER_NUMBER (regno);
7143   return regno;
7144 }
7145
7146 /* Return a location descriptor that designates a machine register.  */
7147
7148 static dw_loc_descr_ref
7149 reg_loc_descriptor (rtl)
7150      register rtx rtl;
7151 {
7152   register dw_loc_descr_ref loc_result = NULL;
7153   register unsigned reg = reg_number (rtl);
7154
7155   if (reg <= 31)
7156     loc_result = new_loc_descr (DW_OP_reg0 + reg, 0, 0);
7157   else
7158     loc_result = new_loc_descr (DW_OP_regx, reg, 0);
7159
7160   return loc_result;
7161 }
7162
7163 /* Return a location descriptor that designates a base+offset location.  */
7164
7165 static dw_loc_descr_ref
7166 based_loc_descr (reg, offset)
7167      unsigned reg;
7168      long int offset;
7169 {
7170   register dw_loc_descr_ref loc_result;
7171   /* For the "frame base", we use the frame pointer or stack pointer
7172      registers, since the RTL for local variables is relative to one of
7173      them.  */
7174   register unsigned fp_reg = DBX_REGISTER_NUMBER (frame_pointer_needed
7175                                                   ? HARD_FRAME_POINTER_REGNUM
7176                                                   : STACK_POINTER_REGNUM);
7177
7178   if (reg == fp_reg)
7179     loc_result = new_loc_descr (DW_OP_fbreg, offset, 0);
7180   else if (reg <= 31)
7181     loc_result = new_loc_descr (DW_OP_breg0 + reg, offset, 0);
7182   else
7183     loc_result = new_loc_descr (DW_OP_bregx, reg, offset);
7184
7185   return loc_result;
7186 }
7187
7188 /* Return true if this RTL expression describes a base+offset calculation.  */
7189
7190 static inline int
7191 is_based_loc (rtl)
7192      register rtx rtl;
7193 {
7194     return (GET_CODE (rtl) == PLUS
7195             && ((GET_CODE (XEXP (rtl, 0)) == REG
7196                  && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
7197 }
7198
7199 /* The following routine converts the RTL for a variable or parameter
7200    (resident in memory) into an equivalent Dwarf representation of a
7201    mechanism for getting the address of that same variable onto the top of a
7202    hypothetical "address evaluation" stack.
7203
7204    When creating memory location descriptors, we are effectively transforming
7205    the RTL for a memory-resident object into its Dwarf postfix expression
7206    equivalent.  This routine recursively descends an RTL tree, turning
7207    it into Dwarf postfix code as it goes.
7208
7209    MODE is the mode of the memory reference, needed to handle some
7210    autoincrement addressing modes.  */
7211
7212 static dw_loc_descr_ref
7213 mem_loc_descriptor (rtl, mode)
7214      register rtx rtl;
7215      enum machine_mode mode;
7216 {
7217   dw_loc_descr_ref mem_loc_result = NULL;
7218   /* Note that for a dynamically sized array, the location we will generate a
7219      description of here will be the lowest numbered location which is
7220      actually within the array.  That's *not* necessarily the same as the
7221      zeroth element of the array.  */
7222
7223 #ifdef ASM_SIMPLIFY_DWARF_ADDR
7224   rtl = ASM_SIMPLIFY_DWARF_ADDR (rtl);
7225 #endif
7226
7227   switch (GET_CODE (rtl))
7228     {
7229     case POST_INC:
7230     case POST_DEC:
7231       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
7232          just fall into the SUBREG code.  */
7233
7234       /* Fall through.  */
7235
7236     case SUBREG:
7237       /* The case of a subreg may arise when we have a local (register)
7238          variable or a formal (register) parameter which doesn't quite fill
7239          up an entire register.  For now, just assume that it is
7240          legitimate to make the Dwarf info refer to the whole register which
7241          contains the given subreg.  */
7242       rtl = XEXP (rtl, 0);
7243
7244       /* Fall through.  */
7245
7246     case REG:
7247       /* Whenever a register number forms a part of the description of the
7248          method for calculating the (dynamic) address of a memory resident
7249          object, DWARF rules require the register number be referred to as
7250          a "base register".  This distinction is not based in any way upon
7251          what category of register the hardware believes the given register
7252          belongs to.  This is strictly DWARF terminology we're dealing with
7253          here. Note that in cases where the location of a memory-resident
7254          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
7255          OP_CONST (0)) the actual DWARF location descriptor that we generate
7256          may just be OP_BASEREG (basereg).  This may look deceptively like
7257          the object in question was allocated to a register (rather than in
7258          memory) so DWARF consumers need to be aware of the subtle
7259          distinction between OP_REG and OP_BASEREG.  */
7260       mem_loc_result = based_loc_descr (reg_number (rtl), 0);
7261       break;
7262
7263     case MEM:
7264       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
7265       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
7266       break;
7267
7268      case LABEL_REF:
7269        /* Some ports can transform a symbol ref into a label ref, because
7270          the symbol ref is too far away and has to be dumped into a constant
7271          pool.  */
7272     case CONST:
7273     case SYMBOL_REF:
7274       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
7275       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
7276       mem_loc_result->dw_loc_oprnd1.v.val_addr = save_rtx (rtl);
7277       break;
7278
7279     case PRE_INC:
7280     case PRE_DEC:
7281       /* Turn these into a PLUS expression and fall into the PLUS code
7282          below.  */
7283       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
7284                           GEN_INT (GET_CODE (rtl) == PRE_INC
7285                                    ? GET_MODE_UNIT_SIZE (mode)
7286                                    : -GET_MODE_UNIT_SIZE (mode)));
7287
7288       /* Fall through.  */
7289
7290     case PLUS:
7291       if (is_based_loc (rtl))
7292         mem_loc_result = based_loc_descr (reg_number (XEXP (rtl, 0)),
7293                                           INTVAL (XEXP (rtl, 1)));
7294       else
7295         {
7296           add_loc_descr (&mem_loc_result, mem_loc_descriptor (XEXP (rtl, 0),
7297                                                               mode));
7298           add_loc_descr (&mem_loc_result, mem_loc_descriptor (XEXP (rtl, 1),
7299                                                               mode));
7300           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_plus, 0, 0));
7301         }
7302       break;
7303
7304     case MULT:
7305       /* If a pseudo-reg is optimized away, it is possible for it to
7306          be replaced with a MEM containing a multiply.  */
7307       add_loc_descr (&mem_loc_result, mem_loc_descriptor (XEXP (rtl, 0), mode));
7308       add_loc_descr (&mem_loc_result, mem_loc_descriptor (XEXP (rtl, 1), mode));
7309       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
7310       break;
7311
7312     case CONST_INT:
7313       mem_loc_result = new_loc_descr (DW_OP_constu, INTVAL (rtl), 0);
7314       break;
7315
7316     default:
7317       abort ();
7318     }
7319
7320   return mem_loc_result;
7321 }
7322
7323 /* Return a descriptor that describes the concatenation of two locations.
7324    This is typically a complex variable.  */
7325
7326 static dw_loc_descr_ref
7327 concat_loc_descriptor (x0, x1)
7328      register rtx x0, x1;
7329 {
7330   dw_loc_descr_ref cc_loc_result = NULL;
7331
7332   if (!is_pseudo_reg (x0)
7333       && (GET_CODE (x0) != MEM || !is_pseudo_reg (XEXP (x0, 0))))
7334     add_loc_descr (&cc_loc_result, loc_descriptor (x0));
7335   add_loc_descr (&cc_loc_result,
7336                  new_loc_descr (DW_OP_piece, GET_MODE_SIZE (GET_MODE (x0)), 0));
7337
7338   if (!is_pseudo_reg (x1)
7339       && (GET_CODE (x1) != MEM || !is_pseudo_reg (XEXP (x1, 0))))
7340     add_loc_descr (&cc_loc_result, loc_descriptor (x1));
7341   add_loc_descr (&cc_loc_result,
7342                  new_loc_descr (DW_OP_piece, GET_MODE_SIZE (GET_MODE (x1)), 0));
7343
7344   return cc_loc_result;
7345 }
7346
7347 /* Output a proper Dwarf location descriptor for a variable or parameter
7348    which is either allocated in a register or in a memory location.  For a
7349    register, we just generate an OP_REG and the register number.  For a
7350    memory location we provide a Dwarf postfix expression describing how to
7351    generate the (dynamic) address of the object onto the address stack.  */
7352
7353 static dw_loc_descr_ref
7354 loc_descriptor (rtl)
7355      register rtx rtl;
7356 {
7357   dw_loc_descr_ref loc_result = NULL;
7358   switch (GET_CODE (rtl))
7359     {
7360     case SUBREG:
7361       /* The case of a subreg may arise when we have a local (register)
7362          variable or a formal (register) parameter which doesn't quite fill
7363          up an entire register.  For now, just assume that it is
7364          legitimate to make the Dwarf info refer to the whole register which
7365          contains the given subreg.  */
7366       rtl = XEXP (rtl, 0);
7367
7368       /* Fall through.  */
7369
7370     case REG:
7371       loc_result = reg_loc_descriptor (rtl);
7372       break;
7373
7374     case MEM:
7375       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
7376       break;
7377
7378     case CONCAT:
7379       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
7380       break;
7381
7382     default:
7383       abort ();
7384     }
7385
7386   return loc_result;
7387 }
7388
7389 /* Given a value, round it up to the lowest multiple of `boundary'
7390    which is not less than the value itself.  */
7391
7392 static inline HOST_WIDE_INT
7393 ceiling (value, boundary)
7394      HOST_WIDE_INT value;
7395      unsigned int boundary;
7396 {
7397   return (((value + boundary - 1) / boundary) * boundary);
7398 }
7399
7400 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
7401    pointer to the declared type for the relevant field variable, or return
7402    `integer_type_node' if the given node turns out to be an
7403    ERROR_MARK node.  */
7404
7405 static inline tree
7406 field_type (decl)
7407      register tree decl;
7408 {
7409   register tree type;
7410
7411   if (TREE_CODE (decl) == ERROR_MARK)
7412     return integer_type_node;
7413
7414   type = DECL_BIT_FIELD_TYPE (decl);
7415   if (type == NULL_TREE)
7416     type = TREE_TYPE (decl);
7417
7418   return type;
7419 }
7420
7421 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
7422    node, return the alignment in bits for the type, or else return
7423    BITS_PER_WORD if the node actually turns out to be an
7424    ERROR_MARK node.  */
7425
7426 static inline unsigned
7427 simple_type_align_in_bits (type)
7428      register tree type;
7429 {
7430   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
7431 }
7432
7433 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
7434    node, return the size in bits for the type if it is a constant, or else
7435    return the alignment for the type if the type's size is not constant, or
7436    else return BITS_PER_WORD if the type actually turns out to be an
7437    ERROR_MARK node.  */
7438
7439 static inline unsigned HOST_WIDE_INT
7440 simple_type_size_in_bits (type)
7441      register tree type;
7442 {
7443   if (TREE_CODE (type) == ERROR_MARK)
7444     return BITS_PER_WORD;
7445   else
7446     {
7447       register tree type_size_tree = TYPE_SIZE (type);
7448
7449       if (! host_integerp (type_size_tree, 1))
7450         return TYPE_ALIGN (type);
7451
7452       return tree_low_cst (type_size_tree, 1);
7453     }
7454 }
7455
7456 /* Given a pointer to what is assumed to be a FIELD_DECL node, compute and
7457    return the byte offset of the lowest addressed byte of the "containing
7458    object" for the given FIELD_DECL, or return 0 if we are unable to
7459    determine what that offset is, either because the argument turns out to
7460    be a pointer to an ERROR_MARK node, or because the offset is actually
7461    variable.  (We can't handle the latter case just yet).  */
7462
7463 static HOST_WIDE_INT
7464 field_byte_offset (decl)
7465      register tree decl;
7466 {
7467   unsigned int type_align_in_bytes;
7468   unsigned int type_align_in_bits;
7469   unsigned HOST_WIDE_INT type_size_in_bits;
7470   HOST_WIDE_INT object_offset_in_align_units;
7471   HOST_WIDE_INT object_offset_in_bits;
7472   HOST_WIDE_INT object_offset_in_bytes;
7473   tree type;
7474   tree field_size_tree;
7475   HOST_WIDE_INT bitpos_int;
7476   HOST_WIDE_INT deepest_bitpos;
7477   unsigned HOST_WIDE_INT field_size_in_bits;
7478
7479   if (TREE_CODE (decl) == ERROR_MARK)
7480     return 0;
7481
7482   if (TREE_CODE (decl) != FIELD_DECL)
7483     abort ();
7484
7485   type = field_type (decl);
7486   field_size_tree = DECL_SIZE (decl);
7487
7488   /* If there was an error, the size could be zero.  */
7489   if (! field_size_tree)
7490     {
7491       if (errorcount)
7492         return 0;
7493
7494       abort ();
7495     }
7496
7497   /* We cannot yet cope with fields whose positions are variable, so
7498      for now, when we see such things, we simply return 0.  Someday, we may
7499      be able to handle such cases, but it will be damn difficult.  */
7500   if (! host_integerp (bit_position (decl), 0))
7501     return 0;
7502
7503   bitpos_int = int_bit_position (decl);
7504
7505     /* If we don't know the size of the field, pretend it's a full word.  */
7506   if (host_integerp (field_size_tree, 1))
7507     field_size_in_bits = tree_low_cst (field_size_tree, 1);
7508   else
7509     field_size_in_bits = BITS_PER_WORD;
7510
7511   type_size_in_bits = simple_type_size_in_bits (type);
7512   type_align_in_bits = simple_type_align_in_bits (type);
7513   type_align_in_bytes = type_align_in_bits / BITS_PER_UNIT;
7514
7515   /* Note that the GCC front-end doesn't make any attempt to keep track of
7516      the starting bit offset (relative to the start of the containing
7517      structure type) of the hypothetical "containing object" for a bit-
7518      field.  Thus, when computing the byte offset value for the start of the
7519      "containing object" of a bit-field, we must deduce this information on
7520      our own. This can be rather tricky to do in some cases.  For example,
7521      handling the following structure type definition when compiling for an
7522      i386/i486 target (which only aligns long long's to 32-bit boundaries)
7523      can be very tricky:
7524
7525          struct S { int field1; long long field2:31; };
7526
7527      Fortunately, there is a simple rule-of-thumb which can be
7528      used in such cases.  When compiling for an i386/i486, GCC will allocate
7529      8 bytes for the structure shown above.  It decides to do this based upon
7530      one simple rule for bit-field allocation.  Quite simply, GCC allocates
7531      each "containing object" for each bit-field at the first (i.e. lowest
7532      addressed) legitimate alignment boundary (based upon the required
7533      minimum alignment for the declared type of the field) which it can
7534      possibly use, subject to the condition that there is still enough
7535      available space remaining in the containing object (when allocated at
7536      the selected point) to fully accommodate all of the bits of the
7537      bit-field itself.  This simple rule makes it obvious why GCC allocates
7538      8 bytes for each object of the structure type shown above.  When looking
7539      for a place to allocate the "containing object" for `field2', the
7540      compiler simply tries to allocate a 64-bit "containing object" at each
7541      successive 32-bit boundary (starting at zero) until it finds a place to
7542      allocate that 64- bit field such that at least 31 contiguous (and
7543      previously unallocated) bits remain within that selected 64 bit field.
7544      (As it turns out, for the example above, the compiler finds that it is
7545      OK to allocate the "containing object" 64-bit field at bit-offset zero
7546      within the structure type.) Here we attempt to work backwards from the
7547      limited set of facts we're given, and we try to deduce from those facts,
7548      where GCC must have believed that the containing object started (within
7549      the structure type). The value we deduce is then used (by the callers of
7550      this routine) to generate DW_AT_location and DW_AT_bit_offset attributes
7551      for fields (both bit-fields and, in the case of DW_AT_location, regular
7552      fields as well).  */
7553
7554   /* Figure out the bit-distance from the start of the structure to the
7555      "deepest" bit of the bit-field.  */
7556   deepest_bitpos = bitpos_int + field_size_in_bits;
7557
7558   /* This is the tricky part.  Use some fancy footwork to deduce where the
7559      lowest addressed bit of the containing object must be.  */
7560   object_offset_in_bits
7561     = ceiling (deepest_bitpos, type_align_in_bits) - type_size_in_bits;
7562
7563   /* Compute the offset of the containing object in "alignment units".  */
7564   object_offset_in_align_units = object_offset_in_bits / type_align_in_bits;
7565
7566   /* Compute the offset of the containing object in bytes.  */
7567   object_offset_in_bytes = object_offset_in_align_units * type_align_in_bytes;
7568
7569   return object_offset_in_bytes;
7570 }
7571 \f
7572 /* The following routines define various Dwarf attributes and any data
7573    associated with them.  */
7574
7575 /* Add a location description attribute value to a DIE.
7576
7577    This emits location attributes suitable for whole variables and
7578    whole parameters.  Note that the location attributes for struct fields are
7579    generated by the routine `data_member_location_attribute' below.  */
7580
7581 static void
7582 add_AT_location_description (die, attr_kind, rtl)
7583      dw_die_ref die;
7584      enum dwarf_attribute attr_kind;
7585      register rtx rtl;
7586 {
7587   /* Handle a special case.  If we are about to output a location descriptor
7588      for a variable or parameter which has been optimized out of existence,
7589      don't do that.  A variable which has been optimized out
7590      of existence will have a DECL_RTL value which denotes a pseudo-reg.
7591      Currently, in some rare cases, variables can have DECL_RTL values which
7592      look like (MEM (REG pseudo-reg#)).  These cases are due to bugs
7593      elsewhere in the compiler.  We treat such cases as if the variable(s) in
7594      question had been optimized out of existence.  */
7595
7596   if (is_pseudo_reg (rtl)
7597       || (GET_CODE (rtl) == MEM
7598           && is_pseudo_reg (XEXP (rtl, 0)))
7599       /* This can happen for a PARM_DECL with a DECL_INCOMING_RTL which
7600          references the internal argument pointer (a pseudo) in a function
7601          where all references to the internal argument pointer were
7602          eliminated via the optimizers.  */
7603       || (GET_CODE (rtl) == MEM
7604           && GET_CODE (XEXP (rtl, 0)) == PLUS
7605           && is_pseudo_reg (XEXP (XEXP (rtl, 0), 0)))
7606       || (GET_CODE (rtl) == CONCAT
7607           && is_pseudo_reg (XEXP (rtl, 0))
7608           && is_pseudo_reg (XEXP (rtl, 1))))
7609     return;
7610
7611   add_AT_loc (die, attr_kind, loc_descriptor (rtl));
7612 }
7613
7614 /* Attach the specialized form of location attribute used for data
7615    members of struct and union types.  In the special case of a
7616    FIELD_DECL node which represents a bit-field, the "offset" part
7617    of this special location descriptor must indicate the distance
7618    in bytes from the lowest-addressed byte of the containing struct
7619    or union type to the lowest-addressed byte of the "containing
7620    object" for the bit-field.  (See the `field_byte_offset' function
7621    above).. For any given bit-field, the "containing object" is a
7622    hypothetical object (of some integral or enum type) within which
7623    the given bit-field lives.  The type of this hypothetical
7624    "containing object" is always the same as the declared type of
7625    the individual bit-field itself (for GCC anyway... the DWARF
7626    spec doesn't actually mandate this).  Note that it is the size
7627    (in bytes) of the hypothetical "containing object" which will
7628    be given in the DW_AT_byte_size attribute for this bit-field.
7629    (See the `byte_size_attribute' function below.)  It is also used
7630    when calculating the value of the DW_AT_bit_offset attribute.
7631    (See the `bit_offset_attribute' function below).  */
7632
7633 static void
7634 add_data_member_location_attribute (die, decl)
7635      register dw_die_ref die;
7636      register tree decl;
7637 {
7638   register unsigned long offset;
7639   register dw_loc_descr_ref loc_descr;
7640   register enum dwarf_location_atom op;
7641
7642   if (TREE_CODE (decl) == TREE_VEC)
7643     offset = tree_low_cst (BINFO_OFFSET (decl), 0);
7644   else
7645     offset = field_byte_offset (decl);
7646
7647   /* The DWARF2 standard says that we should assume that the structure address
7648      is already on the stack, so we can specify a structure field address
7649      by using DW_OP_plus_uconst.  */
7650
7651 #ifdef MIPS_DEBUGGING_INFO
7652   /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst operator
7653      correctly.  It works only if we leave the offset on the stack.  */
7654   op = DW_OP_constu;
7655 #else
7656   op = DW_OP_plus_uconst;
7657 #endif
7658
7659   loc_descr = new_loc_descr (op, offset, 0);
7660   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
7661 }
7662
7663 /* Attach an DW_AT_const_value attribute for a variable or a parameter which
7664    does not have a "location" either in memory or in a register.  These
7665    things can arise in GNU C when a constant is passed as an actual parameter
7666    to an inlined function.  They can also arise in C++ where declared
7667    constants do not necessarily get memory "homes".  */
7668
7669 static void
7670 add_const_value_attribute (die, rtl)
7671      register dw_die_ref die;
7672      register rtx rtl;
7673 {
7674   switch (GET_CODE (rtl))
7675     {
7676     case CONST_INT:
7677       /* Note that a CONST_INT rtx could represent either an integer or a
7678          floating-point constant.  A CONST_INT is used whenever the constant
7679          will fit into a single word.  In all such cases, the original mode
7680          of the constant value is wiped out, and the CONST_INT rtx is
7681          assigned VOIDmode.  */
7682       add_AT_unsigned (die, DW_AT_const_value, (unsigned) INTVAL (rtl));
7683       break;
7684
7685     case CONST_DOUBLE:
7686       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
7687          floating-point constant.  A CONST_DOUBLE is used whenever the
7688          constant requires more than one word in order to be adequately
7689          represented.  We output CONST_DOUBLEs as blocks.  */
7690       {
7691         register enum machine_mode mode = GET_MODE (rtl);
7692
7693         if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7694           {
7695             register unsigned length = GET_MODE_SIZE (mode) / 4;
7696             long *array = (long *) xmalloc (sizeof (long) * length);
7697             REAL_VALUE_TYPE rv;
7698
7699             REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
7700             switch (mode)
7701               {
7702               case SFmode:
7703                 REAL_VALUE_TO_TARGET_SINGLE (rv, array[0]);
7704                 break;
7705
7706               case DFmode:
7707                 REAL_VALUE_TO_TARGET_DOUBLE (rv, array);
7708                 break;
7709
7710               case XFmode:
7711               case TFmode:
7712                 REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, array);
7713                 break;
7714
7715               default:
7716                 abort ();
7717               }
7718
7719             add_AT_float (die, DW_AT_const_value, length, array);
7720           }
7721         else
7722           add_AT_long_long (die, DW_AT_const_value,
7723                             CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
7724       }
7725       break;
7726
7727     case CONST_STRING:
7728       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
7729       break;
7730
7731     case SYMBOL_REF:
7732     case LABEL_REF:
7733     case CONST:
7734       add_AT_addr (die, DW_AT_const_value, save_rtx (rtl));
7735       break;
7736
7737     case PLUS:
7738       /* In cases where an inlined instance of an inline function is passed
7739          the address of an `auto' variable (which is local to the caller) we
7740          can get a situation where the DECL_RTL of the artificial local
7741          variable (for the inlining) which acts as a stand-in for the
7742          corresponding formal parameter (of the inline function) will look
7743          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
7744          exactly a compile-time constant expression, but it isn't the address
7745          of the (artificial) local variable either.  Rather, it represents the
7746          *value* which the artificial local variable always has during its
7747          lifetime.  We currently have no way to represent such quasi-constant
7748          values in Dwarf, so for now we just punt and generate nothing.  */
7749       break;
7750
7751     default:
7752       /* No other kinds of rtx should be possible here.  */
7753       abort ();
7754     }
7755
7756 }
7757
7758 /* Generate *either* an DW_AT_location attribute or else an DW_AT_const_value
7759    data attribute for a variable or a parameter.  We generate the
7760    DW_AT_const_value attribute only in those cases where the given variable
7761    or parameter does not have a true "location" either in memory or in a
7762    register.  This can happen (for example) when a constant is passed as an
7763    actual argument in a call to an inline function.  (It's possible that
7764    these things can crop up in other ways also.)  Note that one type of
7765    constant value which can be passed into an inlined function is a constant
7766    pointer.  This can happen for example if an actual argument in an inlined
7767    function call evaluates to a compile-time constant address.  */
7768
7769 static void
7770 add_location_or_const_value_attribute (die, decl)
7771      register dw_die_ref die;
7772      register tree decl;
7773 {
7774   register rtx rtl;
7775   register tree declared_type;
7776   register tree passed_type;
7777
7778   if (TREE_CODE (decl) == ERROR_MARK)
7779     return;
7780
7781   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != PARM_DECL)
7782     abort ();
7783
7784   /* Here we have to decide where we are going to say the parameter "lives"
7785      (as far as the debugger is concerned).  We only have a couple of
7786      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
7787
7788      DECL_RTL normally indicates where the parameter lives during most of the
7789      activation of the function.  If optimization is enabled however, this
7790      could be either NULL or else a pseudo-reg.  Both of those cases indicate
7791      that the parameter doesn't really live anywhere (as far as the code
7792      generation parts of GCC are concerned) during most of the function's
7793      activation.  That will happen (for example) if the parameter is never
7794      referenced within the function.
7795
7796      We could just generate a location descriptor here for all non-NULL
7797      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
7798      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
7799      where DECL_RTL is NULL or is a pseudo-reg.
7800
7801      Note however that we can only get away with using DECL_INCOMING_RTL as
7802      a backup substitute for DECL_RTL in certain limited cases.  In cases
7803      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
7804      we can be sure that the parameter was passed using the same type as it is
7805      declared to have within the function, and that its DECL_INCOMING_RTL
7806      points us to a place where a value of that type is passed.
7807
7808      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
7809      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
7810      because in these cases DECL_INCOMING_RTL points us to a value of some
7811      type which is *different* from the type of the parameter itself.  Thus,
7812      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
7813      such cases, the debugger would end up (for example) trying to fetch a
7814      `float' from a place which actually contains the first part of a
7815      `double'.  That would lead to really incorrect and confusing
7816      output at debug-time.
7817
7818      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
7819      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
7820      are a couple of exceptions however.  On little-endian machines we can
7821      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
7822      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
7823      an integral type that is smaller than TREE_TYPE (decl). These cases arise
7824      when (on a little-endian machine) a non-prototyped function has a
7825      parameter declared to be of type `short' or `char'.  In such cases,
7826      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
7827      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
7828      passed `int' value.  If the debugger then uses that address to fetch
7829      a `short' or a `char' (on a little-endian machine) the result will be
7830      the correct data, so we allow for such exceptional cases below.
7831
7832      Note that our goal here is to describe the place where the given formal
7833      parameter lives during most of the function's activation (i.e. between
7834      the end of the prologue and the start of the epilogue).  We'll do that
7835      as best as we can. Note however that if the given formal parameter is
7836      modified sometime during the execution of the function, then a stack
7837      backtrace (at debug-time) will show the function as having been
7838      called with the *new* value rather than the value which was
7839      originally passed in.  This happens rarely enough that it is not
7840      a major problem, but it *is* a problem, and I'd like to fix it.
7841
7842      A future version of dwarf2out.c may generate two additional
7843      attributes for any given DW_TAG_formal_parameter DIE which will
7844      describe the "passed type" and the "passed location" for the
7845      given formal parameter in addition to the attributes we now
7846      generate to indicate the "declared type" and the "active
7847      location" for each parameter.  This additional set of attributes
7848      could be used by debuggers for stack backtraces. Separately, note
7849      that sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be
7850      NULL also.  This happens (for example) for inlined-instances of
7851      inline function formal parameters which are never referenced.
7852      This really shouldn't be happening.  All PARM_DECL nodes should
7853      get valid non-NULL DECL_INCOMING_RTL values, but integrate.c
7854      doesn't currently generate these values for inlined instances of
7855      inline function parameters, so when we see such cases, we are
7856      just out-of-luck for the time being (until integrate.c
7857      gets fixed).  */
7858
7859   /* Use DECL_RTL as the "location" unless we find something better.  */
7860   rtl = DECL_RTL (decl);
7861
7862   if (TREE_CODE (decl) == PARM_DECL)
7863     {
7864       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
7865         {
7866           declared_type = type_main_variant (TREE_TYPE (decl));
7867           passed_type = type_main_variant (DECL_ARG_TYPE (decl));
7868
7869           /* This decl represents a formal parameter which was optimized out.
7870              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
7871              all* cases where (rtl == NULL_RTX) just below.  */
7872           if (declared_type == passed_type)
7873             rtl = DECL_INCOMING_RTL (decl);
7874           else if (! BYTES_BIG_ENDIAN
7875                    && TREE_CODE (declared_type) == INTEGER_TYPE
7876                    && (GET_MODE_SIZE (TYPE_MODE (declared_type))
7877                        <= GET_MODE_SIZE (TYPE_MODE (passed_type))))
7878             rtl = DECL_INCOMING_RTL (decl);
7879         }
7880
7881       /* If the parm was passed in registers, but lives on the stack, then
7882          make a big endian correction if the mode of the type of the
7883          parameter is not the same as the mode of the rtl.  */
7884       /* ??? This is the same series of checks that are made in dbxout.c before
7885          we reach the big endian correction code there.  It isn't clear if all
7886          of these checks are necessary here, but keeping them all is the safe
7887          thing to do.  */
7888       else if (GET_CODE (rtl) == MEM
7889                && XEXP (rtl, 0) != const0_rtx
7890                && ! CONSTANT_P (XEXP (rtl, 0))
7891                /* Not passed in memory.  */
7892                && GET_CODE (DECL_INCOMING_RTL (decl)) != MEM
7893                /* Not passed by invisible reference.  */
7894                && (GET_CODE (XEXP (rtl, 0)) != REG
7895                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
7896                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
7897 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
7898                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
7899 #endif
7900                      )
7901                /* Big endian correction check.  */
7902                && BYTES_BIG_ENDIAN
7903                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
7904                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
7905                    < UNITS_PER_WORD))
7906         {
7907           int offset = (UNITS_PER_WORD
7908                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
7909           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
7910                              plus_constant (XEXP (rtl, 0), offset));
7911         }
7912     }
7913
7914   if (rtl == NULL_RTX)
7915     return;
7916
7917   rtl = eliminate_regs (rtl, 0, NULL_RTX);
7918 #ifdef LEAF_REG_REMAP
7919   if (current_function_uses_only_leaf_regs)
7920     leaf_renumber_regs_insn (rtl);
7921 #endif
7922
7923   switch (GET_CODE (rtl))
7924     {
7925     case ADDRESSOF:
7926       /* The address of a variable that was optimized away; don't emit
7927          anything.  */
7928       break;
7929
7930     case CONST_INT:
7931     case CONST_DOUBLE:
7932     case CONST_STRING:
7933     case SYMBOL_REF:
7934     case LABEL_REF:
7935     case CONST:
7936     case PLUS:
7937       /* DECL_RTL could be (plus (reg ...) (const_int ...)) */
7938       add_const_value_attribute (die, rtl);
7939       break;
7940
7941     case MEM:
7942     case REG:
7943     case SUBREG:
7944     case CONCAT:
7945       add_AT_location_description (die, DW_AT_location, rtl);
7946       break;
7947
7948     default:
7949       abort ();
7950     }
7951 }
7952
7953 /* If we don't have a copy of this variable in memory for some reason (such
7954    as a C++ member constant that doesn't have an out-of-line definition),
7955    we should tell the debugger about the constant value.  */
7956
7957 static void
7958 tree_add_const_value_attribute (var_die, decl)
7959      dw_die_ref var_die;
7960      tree decl;
7961 {
7962   tree init = DECL_INITIAL (decl);
7963   tree type = TREE_TYPE (decl);
7964
7965   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init
7966       && initializer_constant_valid_p (init, type) == null_pointer_node)
7967     /* OK */;
7968   else
7969     return;
7970
7971   switch (TREE_CODE (type))
7972     {
7973     case INTEGER_TYPE:
7974       if (host_integerp (init, 0))
7975         add_AT_unsigned (var_die, DW_AT_const_value,
7976                          TREE_INT_CST_LOW (init));
7977       else
7978         add_AT_long_long (var_die, DW_AT_const_value,
7979                           TREE_INT_CST_HIGH (init),
7980                           TREE_INT_CST_LOW (init));
7981       break;
7982
7983     default:;
7984     }
7985 }
7986      
7987 /* Generate an DW_AT_name attribute given some string value to be included as
7988    the value of the attribute.  */
7989
7990 static inline void
7991 add_name_attribute (die, name_string)
7992      register dw_die_ref die;
7993      register const char *name_string;
7994 {
7995   if (name_string != NULL && *name_string != 0)
7996     {
7997       if (demangle_name_func)
7998         name_string = (*demangle_name_func) (name_string);
7999
8000       add_AT_string (die, DW_AT_name, name_string);
8001     }
8002 }
8003
8004 /* Given a tree node describing an array bound (either lower or upper) output
8005    a representation for that bound.  */
8006
8007 static void
8008 add_bound_info (subrange_die, bound_attr, bound)
8009      register dw_die_ref subrange_die;
8010      register enum dwarf_attribute bound_attr;
8011      register tree bound;
8012 {
8013   /* If this is an Ada unconstrained array type, then don't emit any debug
8014      info because the array bounds are unknown.  They are parameterized when
8015      the type is instantiated.  */
8016   if (contains_placeholder_p (bound))
8017     return;
8018
8019   switch (TREE_CODE (bound))
8020     {
8021     case ERROR_MARK:
8022       return;
8023
8024     /* All fixed-bounds are represented by INTEGER_CST nodes.        */
8025     case INTEGER_CST:
8026       if (! host_integerp (bound, 0)
8027           || (bound_attr == DW_AT_lower_bound
8028               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
8029                   || (is_fortran () && integer_onep (bound)))))
8030         /* use the default */
8031         ;
8032       else
8033         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
8034       break;
8035
8036     case CONVERT_EXPR:
8037     case NOP_EXPR:
8038     case NON_LVALUE_EXPR:
8039       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
8040       break;
8041
8042     case SAVE_EXPR:
8043       /* If optimization is turned on, the SAVE_EXPRs that describe how to
8044          access the upper bound values may be bogus.  If they refer to a
8045          register, they may only describe how to get at these values at the
8046          points in the generated code right after they have just been
8047          computed.  Worse yet, in the typical case, the upper bound values
8048          will not even *be* computed in the optimized code (though the
8049          number of elements will), so these SAVE_EXPRs are entirely
8050          bogus. In order to compensate for this fact, we check here to see
8051          if optimization is enabled, and if so, we don't add an attribute
8052          for the (unknown and unknowable) upper bound.  This should not
8053          cause too much trouble for existing (stupid?)  debuggers because
8054          they have to deal with empty upper bounds location descriptions
8055          anyway in order to be able to deal with incomplete array types.
8056          Of course an intelligent debugger (GDB?)  should be able to
8057          comprehend that a missing upper bound specification in a array
8058          type used for a storage class `auto' local array variable
8059          indicates that the upper bound is both unknown (at compile- time)
8060          and unknowable (at run-time) due to optimization.
8061
8062          We assume that a MEM rtx is safe because gcc wouldn't put the
8063          value there unless it was going to be used repeatedly in the
8064          function, i.e. for cleanups.  */
8065       if (! optimize || (SAVE_EXPR_RTL (bound)
8066                          && GET_CODE (SAVE_EXPR_RTL (bound)) == MEM))
8067         {
8068           register dw_die_ref ctx = lookup_decl_die (current_function_decl);
8069           register dw_die_ref decl_die = new_die (DW_TAG_variable, ctx);
8070           register rtx loc = SAVE_EXPR_RTL (bound);
8071
8072           /* If the RTL for the SAVE_EXPR is memory, handle the case where
8073              it references an outer function's frame.  */
8074
8075           if (GET_CODE (loc) == MEM)
8076             {
8077               rtx new_addr = fix_lexical_addr (XEXP (loc, 0), bound);
8078
8079               if (XEXP (loc, 0) != new_addr)
8080                 loc = gen_rtx_MEM (GET_MODE (loc), new_addr);
8081             }
8082
8083           add_AT_flag (decl_die, DW_AT_artificial, 1);
8084           add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
8085           add_AT_location_description (decl_die, DW_AT_location, loc);
8086           add_AT_die_ref (subrange_die, bound_attr, decl_die);
8087         }
8088
8089       /* Else leave out the attribute.  */
8090       break;
8091
8092     case MAX_EXPR:
8093     case VAR_DECL:
8094     case COMPONENT_REF:
8095       /* ??? These types of bounds can be created by the Ada front end,
8096          and it isn't clear how to emit debug info for them.  */
8097       break;
8098
8099     default:
8100       abort ();
8101     }
8102 }
8103
8104 /* Note that the block of subscript information for an array type also
8105    includes information about the element type of type given array type.  */
8106
8107 static void
8108 add_subscript_info (type_die, type)
8109      register dw_die_ref type_die;
8110      register tree type;
8111 {
8112 #ifndef MIPS_DEBUGGING_INFO
8113   register unsigned dimension_number;
8114 #endif
8115   register tree lower, upper;
8116   register dw_die_ref subrange_die;
8117
8118   /* The GNU compilers represent multidimensional array types as sequences of
8119      one dimensional array types whose element types are themselves array
8120      types.  Here we squish that down, so that each multidimensional array
8121      type gets only one array_type DIE in the Dwarf debugging info. The draft
8122      Dwarf specification say that we are allowed to do this kind of
8123      compression in C (because there is no difference between an array or
8124      arrays and a multidimensional array in C) but for other source languages
8125      (e.g. Ada) we probably shouldn't do this.  */
8126
8127   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
8128      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
8129      We work around this by disabling this feature.  See also
8130      gen_array_type_die.  */
8131 #ifndef MIPS_DEBUGGING_INFO
8132   for (dimension_number = 0;
8133        TREE_CODE (type) == ARRAY_TYPE;
8134        type = TREE_TYPE (type), dimension_number++)
8135     {
8136 #endif
8137       register tree domain = TYPE_DOMAIN (type);
8138
8139       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
8140          and (in GNU C only) variable bounds.  Handle all three forms
8141          here.  */
8142       subrange_die = new_die (DW_TAG_subrange_type, type_die);
8143       if (domain)
8144         {
8145           /* We have an array type with specified bounds.  */
8146           lower = TYPE_MIN_VALUE (domain);
8147           upper = TYPE_MAX_VALUE (domain);
8148
8149           /* define the index type.  */
8150           if (TREE_TYPE (domain))
8151             {
8152               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
8153                  TREE_TYPE field.  We can't emit debug info for this
8154                  because it is an unnamed integral type.  */
8155               if (TREE_CODE (domain) == INTEGER_TYPE
8156                   && TYPE_NAME (domain) == NULL_TREE
8157                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
8158                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
8159                 ;
8160               else
8161                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
8162                                     type_die);
8163             }
8164
8165           /* ??? If upper is NULL, the array has unspecified length,
8166              but it does have a lower bound.  This happens with Fortran
8167                dimension arr(N:*)
8168              Since the debugger is definitely going to need to know N
8169              to produce useful results, go ahead and output the lower
8170              bound solo, and hope the debugger can cope.  */
8171
8172           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
8173           if (upper)
8174             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
8175         }
8176       else
8177         /* We have an array type with an unspecified length.  The DWARF-2
8178              spec does not say how to handle this; let's just leave out the
8179              bounds.  */
8180         {;}
8181
8182 #ifndef MIPS_DEBUGGING_INFO
8183     }
8184 #endif
8185 }
8186
8187 static void
8188 add_byte_size_attribute (die, tree_node)
8189      dw_die_ref die;
8190      register tree tree_node;
8191 {
8192   register unsigned size;
8193
8194   switch (TREE_CODE (tree_node))
8195     {
8196     case ERROR_MARK:
8197       size = 0;
8198       break;
8199     case ENUMERAL_TYPE:
8200     case RECORD_TYPE:
8201     case UNION_TYPE:
8202     case QUAL_UNION_TYPE:
8203       size = int_size_in_bytes (tree_node);
8204       break;
8205     case FIELD_DECL:
8206       /* For a data member of a struct or union, the DW_AT_byte_size is
8207          generally given as the number of bytes normally allocated for an
8208          object of the *declared* type of the member itself.  This is true
8209          even for bit-fields.  */
8210       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
8211       break;
8212     default:
8213       abort ();
8214     }
8215
8216   /* Note that `size' might be -1 when we get to this point.  If it is, that
8217      indicates that the byte size of the entity in question is variable.  We
8218      have no good way of expressing this fact in Dwarf at the present time,
8219      so just let the -1 pass on through.  */
8220
8221   add_AT_unsigned (die, DW_AT_byte_size, size);
8222 }
8223
8224 /* For a FIELD_DECL node which represents a bit-field, output an attribute
8225    which specifies the distance in bits from the highest order bit of the
8226    "containing object" for the bit-field to the highest order bit of the
8227    bit-field itself.
8228
8229    For any given bit-field, the "containing object" is a hypothetical
8230    object (of some integral or enum type) within which the given bit-field
8231    lives.  The type of this hypothetical "containing object" is always the
8232    same as the declared type of the individual bit-field itself.  The
8233    determination of the exact location of the "containing object" for a
8234    bit-field is rather complicated.  It's handled by the
8235    `field_byte_offset' function (above).
8236
8237    Note that it is the size (in bytes) of the hypothetical "containing object"
8238    which will be given in the DW_AT_byte_size attribute for this bit-field.
8239    (See `byte_size_attribute' above).  */
8240
8241 static inline void
8242 add_bit_offset_attribute (die, decl)
8243      register dw_die_ref die;
8244      register tree decl;
8245 {
8246   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
8247   tree type = DECL_BIT_FIELD_TYPE (decl);
8248   HOST_WIDE_INT bitpos_int;
8249   HOST_WIDE_INT highest_order_object_bit_offset;
8250   HOST_WIDE_INT highest_order_field_bit_offset;
8251   HOST_WIDE_INT unsigned bit_offset;
8252
8253   /* Must be a field and a bit field.  */
8254   if (!type
8255       || TREE_CODE (decl) != FIELD_DECL)
8256     abort ();
8257
8258   /* We can't yet handle bit-fields whose offsets are variable, so if we
8259      encounter such things, just return without generating any attribute
8260      whatsoever.  Likewise for variable or too large size.  */
8261   if (! host_integerp (bit_position (decl), 0)
8262       || ! host_integerp (DECL_SIZE (decl), 1))
8263     return;
8264
8265   bitpos_int = int_bit_position (decl);
8266
8267   /* Note that the bit offset is always the distance (in bits) from the
8268      highest-order bit of the "containing object" to the highest-order bit of
8269      the bit-field itself.  Since the "high-order end" of any object or field
8270      is different on big-endian and little-endian machines, the computation
8271      below must take account of these differences.  */
8272   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
8273   highest_order_field_bit_offset = bitpos_int;
8274
8275   if (! BYTES_BIG_ENDIAN)
8276     {
8277       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
8278       highest_order_object_bit_offset += simple_type_size_in_bits (type);
8279     }
8280
8281   bit_offset
8282     = (! BYTES_BIG_ENDIAN
8283        ? highest_order_object_bit_offset - highest_order_field_bit_offset
8284        : highest_order_field_bit_offset - highest_order_object_bit_offset);
8285
8286   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
8287 }
8288
8289 /* For a FIELD_DECL node which represents a bit field, output an attribute
8290    which specifies the length in bits of the given field.  */
8291
8292 static inline void
8293 add_bit_size_attribute (die, decl)
8294      register dw_die_ref die;
8295      register tree decl;
8296 {
8297   /* Must be a field and a bit field.  */
8298   if (TREE_CODE (decl) != FIELD_DECL
8299       || ! DECL_BIT_FIELD_TYPE (decl))
8300     abort ();
8301
8302   if (host_integerp (DECL_SIZE (decl), 1))
8303     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
8304 }
8305
8306 /* If the compiled language is ANSI C, then add a 'prototyped'
8307    attribute, if arg types are given for the parameters of a function.  */
8308
8309 static inline void
8310 add_prototyped_attribute (die, func_type)
8311      register dw_die_ref die;
8312      register tree func_type;
8313 {
8314   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
8315       && TYPE_ARG_TYPES (func_type) != NULL)
8316     add_AT_flag (die, DW_AT_prototyped, 1);
8317 }
8318
8319 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
8320    by looking in either the type declaration or object declaration
8321    equate table.  */
8322
8323 static inline void
8324 add_abstract_origin_attribute (die, origin)
8325      register dw_die_ref die;
8326      register tree origin;
8327 {
8328   dw_die_ref origin_die = NULL;
8329
8330   if (TREE_CODE (origin) != FUNCTION_DECL)
8331     {
8332       /* We may have gotten separated from the block for the inlined
8333          function, if we're in an exception handler or some such; make
8334          sure that the abstract function has been written out.
8335
8336          Doing this for nested functions is wrong, however; functions are
8337          distinct units, and our context might not even be inline.  */
8338       tree fn = origin;
8339       if (TYPE_P (fn))
8340         fn = TYPE_STUB_DECL (fn);
8341       fn = decl_function_context (fn);
8342       if (fn)
8343         gen_abstract_function (fn);
8344     }
8345
8346   if (DECL_P (origin))
8347     origin_die = lookup_decl_die (origin);
8348   else if (TYPE_P (origin))
8349     origin_die = lookup_type_die (origin);
8350
8351   if (origin_die == NULL)
8352     abort ();
8353
8354   add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
8355 }
8356
8357 /* We do not currently support the pure_virtual attribute.  */
8358
8359 static inline void
8360 add_pure_or_virtual_attribute (die, func_decl)
8361      register dw_die_ref die;
8362      register tree func_decl;
8363 {
8364   if (DECL_VINDEX (func_decl))
8365     {
8366       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
8367
8368       if (host_integerp (DECL_VINDEX (func_decl), 0))
8369         add_AT_loc (die, DW_AT_vtable_elem_location,
8370                     new_loc_descr (DW_OP_constu,
8371                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
8372                                    0));
8373
8374       /* GNU extension: Record what type this method came from originally.  */
8375       if (debug_info_level > DINFO_LEVEL_TERSE)
8376         add_AT_die_ref (die, DW_AT_containing_type,
8377                         lookup_type_die (DECL_CONTEXT (func_decl)));
8378     }
8379 }
8380 \f
8381 /* Add source coordinate attributes for the given decl.  */
8382
8383 static void
8384 add_src_coords_attributes (die, decl)
8385      register dw_die_ref die;
8386      register tree decl;
8387 {
8388   register unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
8389
8390   add_AT_unsigned (die, DW_AT_decl_file, file_index);
8391   add_AT_unsigned (die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
8392 }
8393
8394 /* Add an DW_AT_name attribute and source coordinate attribute for the
8395    given decl, but only if it actually has a name.  */
8396
8397 static void
8398 add_name_and_src_coords_attributes (die, decl)
8399      register dw_die_ref die;
8400      register tree decl;
8401 {
8402   register tree decl_name;
8403
8404   decl_name = DECL_NAME (decl);
8405   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
8406     {
8407       add_name_attribute (die, dwarf2_name (decl, 0));
8408       if (! DECL_ARTIFICIAL (decl))
8409         add_src_coords_attributes (die, decl);
8410
8411       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
8412           && TREE_PUBLIC (decl)
8413           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
8414         add_AT_string (die, DW_AT_MIPS_linkage_name,
8415                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
8416     }
8417 }
8418
8419 /* Push a new declaration scope.  */
8420
8421 static void
8422 push_decl_scope (scope)
8423      tree scope;
8424 {
8425   /* Make room in the decl_scope_table, if necessary.  */
8426   if (decl_scope_table_allocated == decl_scope_depth)
8427     {
8428       decl_scope_table_allocated += DECL_SCOPE_TABLE_INCREMENT;
8429       decl_scope_table
8430         = (tree *) xrealloc (decl_scope_table,
8431                              decl_scope_table_allocated * sizeof (tree));
8432     }
8433
8434   decl_scope_table[decl_scope_depth] = scope;
8435   decl_scope_depth++;
8436 }
8437
8438 /* Pop a declaration scope.  */
8439 static inline void
8440 pop_decl_scope ()
8441 {
8442   if (decl_scope_depth <= 0)
8443     abort ();
8444   --decl_scope_depth;
8445 }
8446
8447 /* Return the DIE for the scope that immediately contains this type.
8448    Non-named types get global scope.  Named types nested in other
8449    types get their containing scope if it's open, or global scope
8450    otherwise.  All other types (i.e. function-local named types) get
8451    the current active scope.  */
8452
8453 static dw_die_ref
8454 scope_die_for (t, context_die)
8455      register tree t;
8456      register dw_die_ref context_die;
8457 {
8458   register dw_die_ref scope_die = NULL;
8459   register tree containing_scope;
8460   register int i;
8461
8462   /* Non-types always go in the current scope.  */
8463   if (! TYPE_P (t))
8464     abort ();
8465
8466   containing_scope = TYPE_CONTEXT (t);
8467
8468   /* Ignore namespaces for the moment.  */
8469   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
8470     containing_scope = NULL_TREE;
8471
8472   /* Ignore function type "scopes" from the C frontend.  They mean that
8473      a tagged type is local to a parmlist of a function declarator, but
8474      that isn't useful to DWARF.  */
8475   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
8476     containing_scope = NULL_TREE;
8477
8478   if (containing_scope == NULL_TREE)
8479     scope_die = comp_unit_die;
8480   else if (TYPE_P (containing_scope))
8481     {
8482       /* For types, we can just look up the appropriate DIE.  But
8483          first we check to see if we're in the middle of emitting it
8484          so we know where the new DIE should go.  */
8485
8486       for (i = decl_scope_depth - 1; i >= 0; --i)
8487         if (decl_scope_table[i] == containing_scope)
8488           break;
8489
8490       if (i < 0)
8491         {
8492           if (debug_info_level > DINFO_LEVEL_TERSE
8493               && !TREE_ASM_WRITTEN (containing_scope))
8494             abort ();
8495
8496           /* If none of the current dies are suitable, we get file scope.  */
8497           scope_die = comp_unit_die;
8498         }
8499       else
8500         scope_die = lookup_type_die (containing_scope);
8501     }
8502   else
8503     scope_die = context_die;
8504
8505   return scope_die;
8506 }
8507
8508 /* Returns nonzero iff CONTEXT_DIE is internal to a function.  */
8509
8510 static inline int local_scope_p PARAMS ((dw_die_ref));
8511 static inline int
8512 local_scope_p (context_die)
8513      dw_die_ref context_die;
8514 {
8515   for (; context_die; context_die = context_die->die_parent)
8516     if (context_die->die_tag == DW_TAG_inlined_subroutine
8517         || context_die->die_tag == DW_TAG_subprogram)
8518       return 1;
8519   return 0;
8520 }
8521
8522 /* Returns nonzero iff CONTEXT_DIE is a class.  */
8523
8524 static inline int class_scope_p PARAMS ((dw_die_ref));
8525 static inline int
8526 class_scope_p (context_die)
8527      dw_die_ref context_die;
8528 {
8529   return (context_die
8530           && (context_die->die_tag == DW_TAG_structure_type
8531               || context_die->die_tag == DW_TAG_union_type));
8532 }
8533
8534 /* Many forms of DIEs require a "type description" attribute.  This
8535    routine locates the proper "type descriptor" die for the type given
8536    by 'type', and adds an DW_AT_type attribute below the given die.  */
8537
8538 static void
8539 add_type_attribute (object_die, type, decl_const, decl_volatile, context_die)
8540      register dw_die_ref object_die;
8541      register tree type;
8542      register int decl_const;
8543      register int decl_volatile;
8544      register dw_die_ref context_die;
8545 {
8546   register enum tree_code code  = TREE_CODE (type);
8547   register dw_die_ref type_die  = NULL;
8548
8549   /* ??? If this type is an unnamed subrange type of an integral or
8550      floating-point type, use the inner type.  This is because we have no
8551      support for unnamed types in base_type_die.  This can happen if this is
8552      an Ada subrange type.  Correct solution is emit a subrange type die.  */
8553   if ((code == INTEGER_TYPE || code == REAL_TYPE)
8554       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
8555     type = TREE_TYPE (type), code = TREE_CODE (type);
8556
8557   if (code == ERROR_MARK)
8558     return;
8559
8560   /* Handle a special case.  For functions whose return type is void, we
8561      generate *no* type attribute.  (Note that no object may have type
8562      `void', so this only applies to function return types).  */
8563   if (code == VOID_TYPE)
8564     return;
8565
8566   type_die = modified_type_die (type,
8567                                 decl_const || TYPE_READONLY (type),
8568                                 decl_volatile || TYPE_VOLATILE (type),
8569                                 context_die);
8570   if (type_die != NULL)
8571     add_AT_die_ref (object_die, DW_AT_type, type_die);
8572 }
8573
8574 /* Given a tree pointer to a struct, class, union, or enum type node, return
8575    a pointer to the (string) tag name for the given type, or zero if the type
8576    was declared without a tag.  */
8577
8578 static const char *
8579 type_tag (type)
8580      register tree type;
8581 {
8582   register const char *name = 0;
8583
8584   if (TYPE_NAME (type) != 0)
8585     {
8586       register tree t = 0;
8587
8588       /* Find the IDENTIFIER_NODE for the type name.  */
8589       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8590         t = TYPE_NAME (type);
8591
8592       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
8593          a TYPE_DECL node, regardless of whether or not a `typedef' was
8594          involved.  */
8595       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8596                && ! DECL_IGNORED_P (TYPE_NAME (type)))
8597         t = DECL_NAME (TYPE_NAME (type));
8598
8599       /* Now get the name as a string, or invent one.  */
8600       if (t != 0)
8601         name = IDENTIFIER_POINTER (t);
8602     }
8603
8604   return (name == 0 || *name == '\0') ? 0 : name;
8605 }
8606
8607 /* Return the type associated with a data member, make a special check
8608    for bit field types.  */
8609
8610 static inline tree
8611 member_declared_type (member)
8612      register tree member;
8613 {
8614   return (DECL_BIT_FIELD_TYPE (member)
8615           ? DECL_BIT_FIELD_TYPE (member)
8616           : TREE_TYPE (member));
8617 }
8618
8619 /* Get the decl's label, as described by its RTL. This may be different
8620    from the DECL_NAME name used in the source file.  */
8621
8622 #if 0
8623 static const char *
8624 decl_start_label (decl)
8625      register tree decl;
8626 {
8627   rtx x;
8628   const char *fnname;
8629   x = DECL_RTL (decl);
8630   if (GET_CODE (x) != MEM)
8631     abort ();
8632
8633   x = XEXP (x, 0);
8634   if (GET_CODE (x) != SYMBOL_REF)
8635     abort ();
8636
8637   fnname = XSTR (x, 0);
8638   return fnname;
8639 }
8640 #endif
8641 \f
8642 /* These routines generate the internal representation of the DIE's for
8643    the compilation unit.  Debugging information is collected by walking
8644    the declaration trees passed in from dwarf2out_decl().  */
8645
8646 static void
8647 gen_array_type_die (type, context_die)
8648      register tree type;
8649      register dw_die_ref context_die;
8650 {
8651   register dw_die_ref scope_die = scope_die_for (type, context_die);
8652   register dw_die_ref array_die;
8653   register tree element_type;
8654
8655   /* ??? The SGI dwarf reader fails for array of array of enum types unless
8656      the inner array type comes before the outer array type.  Thus we must
8657      call gen_type_die before we call new_die.  See below also.  */
8658 #ifdef MIPS_DEBUGGING_INFO
8659   gen_type_die (TREE_TYPE (type), context_die);
8660 #endif
8661
8662   array_die = new_die (DW_TAG_array_type, scope_die);
8663
8664 #if 0
8665   /* We default the array ordering.  SDB will probably do
8666      the right things even if DW_AT_ordering is not present.  It's not even
8667      an issue until we start to get into multidimensional arrays anyway.  If
8668      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
8669      then we'll have to put the DW_AT_ordering attribute back in.  (But if
8670      and when we find out that we need to put these in, we will only do so
8671      for multidimensional arrays.  */
8672   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
8673 #endif
8674
8675 #ifdef MIPS_DEBUGGING_INFO
8676   /* The SGI compilers handle arrays of unknown bound by setting
8677      AT_declaration and not emitting any subrange DIEs.  */
8678   if (! TYPE_DOMAIN (type))
8679     add_AT_unsigned (array_die, DW_AT_declaration, 1);
8680   else
8681 #endif
8682     add_subscript_info (array_die, type);
8683
8684   add_name_attribute (array_die, type_tag (type));
8685   equate_type_number_to_die (type, array_die);
8686
8687   /* Add representation of the type of the elements of this array type.  */
8688   element_type = TREE_TYPE (type);
8689
8690   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
8691      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
8692      We work around this by disabling this feature.  See also
8693      add_subscript_info.  */
8694 #ifndef MIPS_DEBUGGING_INFO
8695   while (TREE_CODE (element_type) == ARRAY_TYPE)
8696     element_type = TREE_TYPE (element_type);
8697
8698   gen_type_die (element_type, context_die);
8699 #endif
8700
8701   add_type_attribute (array_die, element_type, 0, 0, context_die);
8702 }
8703
8704 static void
8705 gen_set_type_die (type, context_die)
8706      register tree type;
8707      register dw_die_ref context_die;
8708 {
8709   register dw_die_ref type_die
8710     = new_die (DW_TAG_set_type, scope_die_for (type, context_die));
8711
8712   equate_type_number_to_die (type, type_die);
8713   add_type_attribute (type_die, TREE_TYPE (type), 0, 0, context_die);
8714 }
8715
8716 #if 0
8717 static void
8718 gen_entry_point_die (decl, context_die)
8719      register tree decl;
8720      register dw_die_ref context_die;
8721 {
8722   register tree origin = decl_ultimate_origin (decl);
8723   register dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die);
8724   if (origin != NULL)
8725     add_abstract_origin_attribute (decl_die, origin);
8726   else
8727     {
8728       add_name_and_src_coords_attributes (decl_die, decl);
8729       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
8730                           0, 0, context_die);
8731     }
8732
8733   if (DECL_ABSTRACT (decl))
8734     equate_decl_number_to_die (decl, decl_die);
8735   else
8736     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
8737 }
8738 #endif
8739
8740 /* Remember a type in the incomplete_types_list.  */
8741
8742 static void
8743 add_incomplete_type (type)
8744      tree type;
8745 {
8746   if (incomplete_types == incomplete_types_allocated)
8747     {
8748       incomplete_types_allocated += INCOMPLETE_TYPES_INCREMENT;
8749       incomplete_types_list
8750         = (tree *) xrealloc (incomplete_types_list,
8751                              sizeof (tree) * incomplete_types_allocated);
8752     }
8753
8754   incomplete_types_list[incomplete_types++] = type;
8755 }
8756
8757 /* Walk through the list of incomplete types again, trying once more to
8758    emit full debugging info for them.  */
8759
8760 static void
8761 retry_incomplete_types ()
8762 {
8763   register tree type;
8764
8765   while (incomplete_types)
8766     {
8767       --incomplete_types;
8768       type = incomplete_types_list[incomplete_types];
8769       gen_type_die (type, comp_unit_die);
8770     }
8771 }
8772
8773 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
8774
8775 static void
8776 gen_inlined_enumeration_type_die (type, context_die)
8777      register tree type;
8778      register dw_die_ref context_die;
8779 {
8780   register dw_die_ref type_die = new_die (DW_TAG_enumeration_type,
8781                                           context_die);
8782   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
8783      be incomplete and such types are not marked.  */
8784   add_abstract_origin_attribute (type_die, type);
8785 }
8786
8787 /* Generate a DIE to represent an inlined instance of a structure type.  */
8788
8789 static void
8790 gen_inlined_structure_type_die (type, context_die)
8791      register tree type;
8792      register dw_die_ref context_die;
8793 {
8794   register dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die);
8795
8796   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
8797      be incomplete and such types are not marked.  */
8798   add_abstract_origin_attribute (type_die, type);
8799 }
8800
8801 /* Generate a DIE to represent an inlined instance of a union type.  */
8802
8803 static void
8804 gen_inlined_union_type_die (type, context_die)
8805      register tree type;
8806      register dw_die_ref context_die;
8807 {
8808   register dw_die_ref type_die = new_die (DW_TAG_union_type, context_die);
8809
8810   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
8811      be incomplete and such types are not marked.  */
8812   add_abstract_origin_attribute (type_die, type);
8813 }
8814
8815 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
8816    include all of the information about the enumeration values also. Each
8817    enumerated type name/value is listed as a child of the enumerated type
8818    DIE.  */
8819
8820 static void
8821 gen_enumeration_type_die (type, context_die)
8822      register tree type;
8823      register dw_die_ref context_die;
8824 {
8825   register dw_die_ref type_die = lookup_type_die (type);
8826
8827   if (type_die == NULL)
8828     {
8829       type_die = new_die (DW_TAG_enumeration_type,
8830                           scope_die_for (type, context_die));
8831       equate_type_number_to_die (type, type_die);
8832       add_name_attribute (type_die, type_tag (type));
8833     }
8834   else if (! TYPE_SIZE (type))
8835     return;
8836   else
8837     remove_AT (type_die, DW_AT_declaration);
8838
8839   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
8840      given enum type is incomplete, do not generate the DW_AT_byte_size
8841      attribute or the DW_AT_element_list attribute.  */
8842   if (TYPE_SIZE (type))
8843     {
8844       register tree link;
8845
8846       TREE_ASM_WRITTEN (type) = 1;
8847       add_byte_size_attribute (type_die, type);
8848       if (TYPE_STUB_DECL (type) != NULL_TREE)
8849         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
8850
8851       /* If the first reference to this type was as the return type of an
8852          inline function, then it may not have a parent.  Fix this now.  */
8853       if (type_die->die_parent == NULL)
8854         add_child_die (scope_die_for (type, context_die), type_die);
8855
8856       for (link = TYPE_FIELDS (type);
8857            link != NULL; link = TREE_CHAIN (link))
8858         {
8859           register dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die);
8860
8861           add_name_attribute (enum_die,
8862                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
8863
8864           if (host_integerp (TREE_VALUE (link), 0))
8865             {
8866               if (tree_int_cst_sgn (TREE_VALUE (link)) < 0)
8867                 add_AT_int (enum_die, DW_AT_const_value,
8868                             tree_low_cst (TREE_VALUE (link), 0));
8869               else
8870                 add_AT_unsigned (enum_die, DW_AT_const_value,
8871                                  tree_low_cst (TREE_VALUE (link), 0));
8872             }
8873         }
8874     }
8875   else
8876     add_AT_flag (type_die, DW_AT_declaration, 1);
8877 }
8878
8879 /* Generate a DIE to represent either a real live formal parameter decl or to
8880    represent just the type of some formal parameter position in some function
8881    type.
8882
8883    Note that this routine is a bit unusual because its argument may be a
8884    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
8885    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
8886    node.  If it's the former then this function is being called to output a
8887    DIE to represent a formal parameter object (or some inlining thereof).  If
8888    it's the latter, then this function is only being called to output a
8889    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
8890    argument type of some subprogram type.  */
8891
8892 static dw_die_ref
8893 gen_formal_parameter_die (node, context_die)
8894      register tree node;
8895      register dw_die_ref context_die;
8896 {
8897   register dw_die_ref parm_die
8898     = new_die (DW_TAG_formal_parameter, context_die);
8899   register tree origin;
8900
8901   switch (TREE_CODE_CLASS (TREE_CODE (node)))
8902     {
8903     case 'd':
8904       origin = decl_ultimate_origin (node);
8905       if (origin != NULL)
8906         add_abstract_origin_attribute (parm_die, origin);
8907       else
8908         {
8909           add_name_and_src_coords_attributes (parm_die, node);
8910           add_type_attribute (parm_die, TREE_TYPE (node),
8911                               TREE_READONLY (node),
8912                               TREE_THIS_VOLATILE (node),
8913                               context_die);
8914           if (DECL_ARTIFICIAL (node))
8915             add_AT_flag (parm_die, DW_AT_artificial, 1);
8916         }
8917
8918       equate_decl_number_to_die (node, parm_die);
8919       if (! DECL_ABSTRACT (node))
8920         add_location_or_const_value_attribute (parm_die, node);
8921
8922       break;
8923
8924     case 't':
8925       /* We were called with some kind of a ..._TYPE node.  */
8926       add_type_attribute (parm_die, node, 0, 0, context_die);
8927       break;
8928
8929     default:
8930       abort ();
8931     }
8932
8933   return parm_die;
8934 }
8935
8936 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
8937    at the end of an (ANSI prototyped) formal parameters list.  */
8938
8939 static void
8940 gen_unspecified_parameters_die (decl_or_type, context_die)
8941      register tree decl_or_type ATTRIBUTE_UNUSED;
8942      register dw_die_ref context_die;
8943 {
8944   new_die (DW_TAG_unspecified_parameters, context_die);
8945 }
8946
8947 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
8948    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
8949    parameters as specified in some function type specification (except for
8950    those which appear as part of a function *definition*).  */
8951
8952 static void
8953 gen_formal_types_die (function_or_method_type, context_die)
8954      register tree function_or_method_type;
8955      register dw_die_ref context_die;
8956 {
8957   register tree link;
8958   register tree formal_type = NULL;
8959   register tree first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
8960
8961 #if 0
8962   /* In the case where we are generating a formal types list for a C++
8963      non-static member function type, skip over the first thing on the
8964      TYPE_ARG_TYPES list because it only represents the type of the hidden
8965      `this pointer'.  The debugger should be able to figure out (without
8966      being explicitly told) that this non-static member function type takes a
8967      `this pointer' and should be able to figure what the type of that hidden
8968      parameter is from the DW_AT_member attribute of the parent
8969      DW_TAG_subroutine_type DIE.  */
8970   if (TREE_CODE (function_or_method_type) == METHOD_TYPE)
8971     first_parm_type = TREE_CHAIN (first_parm_type);
8972 #endif
8973
8974   /* Make our first pass over the list of formal parameter types and output a
8975      DW_TAG_formal_parameter DIE for each one.  */
8976   for (link = first_parm_type; link; link = TREE_CHAIN (link))
8977     {
8978       register dw_die_ref parm_die;
8979
8980       formal_type = TREE_VALUE (link);
8981       if (formal_type == void_type_node)
8982         break;
8983
8984       /* Output a (nameless) DIE to represent the formal parameter itself.  */
8985       parm_die = gen_formal_parameter_die (formal_type, context_die);
8986       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
8987           && link == first_parm_type)
8988         add_AT_flag (parm_die, DW_AT_artificial, 1);
8989     }
8990
8991   /* If this function type has an ellipsis, add a
8992      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
8993   if (formal_type != void_type_node)
8994     gen_unspecified_parameters_die (function_or_method_type, context_die);
8995
8996   /* Make our second (and final) pass over the list of formal parameter types
8997      and output DIEs to represent those types (as necessary).  */
8998   for (link = TYPE_ARG_TYPES (function_or_method_type);
8999        link;
9000        link = TREE_CHAIN (link))
9001     {
9002       formal_type = TREE_VALUE (link);
9003       if (formal_type == void_type_node)
9004         break;
9005
9006       gen_type_die (formal_type, context_die);
9007     }
9008 }
9009
9010 /* We want to generate the DIE for TYPE so that we can generate the
9011    die for MEMBER, which has been defined; we will need to refer back
9012    to the member declaration nested within TYPE.  If we're trying to
9013    generate minimal debug info for TYPE, processing TYPE won't do the
9014    trick; we need to attach the member declaration by hand.  */
9015
9016 static void
9017 gen_type_die_for_member (type, member, context_die)
9018      tree type, member;
9019      dw_die_ref context_die;
9020 {
9021   gen_type_die (type, context_die);
9022
9023   /* If we're trying to avoid duplicate debug info, we may not have
9024      emitted the member decl for this function.  Emit it now.  */
9025   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
9026       && ! lookup_decl_die (member))
9027     {
9028       if (decl_ultimate_origin (member))
9029         abort ();
9030
9031       push_decl_scope (type);
9032       if (TREE_CODE (member) == FUNCTION_DECL)
9033         gen_subprogram_die (member, lookup_type_die (type));
9034       else
9035         gen_variable_die (member, lookup_type_die (type));
9036       pop_decl_scope ();
9037     }
9038 }
9039
9040 /* Generate the DWARF2 info for the "abstract" instance
9041    of a function which we may later generate inlined and/or
9042    out-of-line instances of.  */
9043
9044 static void
9045 gen_abstract_function (decl)
9046      tree decl;
9047 {
9048   register dw_die_ref old_die = lookup_decl_die (decl);
9049   tree save_fn;
9050
9051   if (old_die && get_AT_unsigned (old_die, DW_AT_inline))
9052     /* We've already generated the abstract instance.  */
9053     return;
9054
9055   save_fn = current_function_decl;
9056   current_function_decl = decl;
9057
9058   set_decl_abstract_flags (decl, 1);
9059   dwarf2out_decl (decl);
9060   set_decl_abstract_flags (decl, 0);
9061
9062   current_function_decl = save_fn;
9063 }
9064
9065 /* Generate a DIE to represent a declared function (either file-scope or
9066    block-local).  */
9067
9068 static void
9069 gen_subprogram_die (decl, context_die)
9070      register tree decl;
9071      register dw_die_ref context_die;
9072 {
9073   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
9074   register tree origin = decl_ultimate_origin (decl);
9075   register dw_die_ref subr_die;
9076   register rtx fp_reg;
9077   register tree fn_arg_types;
9078   register tree outer_scope;
9079   register dw_die_ref old_die = lookup_decl_die (decl);
9080   register int declaration = (current_function_decl != decl
9081                               || class_scope_p (context_die));
9082
9083   /* Note that it is possible to have both DECL_ABSTRACT and `declaration'
9084      be true, if we started to generate the abstract instance of an inline,
9085      decided to output its containing class, and proceeded to emit the
9086      declaration of the inline from the member list for the class.  In that
9087      case, `declaration' takes priority; we'll get back to the abstract
9088      instance when we're done with the class.  */
9089
9090   /* The class-scope declaration DIE must be the primary DIE.  */
9091   if (origin && declaration && class_scope_p (context_die))
9092     {
9093       origin = NULL;
9094       if (old_die)
9095         abort ();
9096     }
9097
9098   if (origin != NULL)
9099     {
9100       if (declaration && ! local_scope_p (context_die))
9101         abort ();
9102
9103       /* Fixup die_parent for the abstract instance of a nested
9104          inline function.  */
9105       if (old_die && old_die->die_parent == NULL)
9106         add_child_die (context_die, old_die);
9107
9108       subr_die = new_die (DW_TAG_subprogram, context_die);
9109       add_abstract_origin_attribute (subr_die, origin);
9110     }
9111   else if (old_die && DECL_ABSTRACT (decl)
9112            && get_AT_unsigned (old_die, DW_AT_inline))
9113     {
9114       /* This must be a redefinition of an extern inline function.
9115          We can just reuse the old die here.  */
9116       subr_die = old_die;
9117
9118       /* Clear out the inlined attribute and parm types.  */
9119       remove_AT (subr_die, DW_AT_inline);
9120       remove_children (subr_die);
9121     }
9122   else if (old_die)
9123     {
9124       register unsigned file_index
9125         = lookup_filename (DECL_SOURCE_FILE (decl));
9126
9127       if (get_AT_flag (old_die, DW_AT_declaration) != 1)
9128         {
9129           /* ??? This can happen if there is a bug in the program, for
9130              instance, if it has duplicate function definitions.  Ideally,
9131              we should detect this case and ignore it.  For now, if we have
9132              already reported an error, any error at all, then assume that
9133              we got here because of a input error, not a dwarf2 bug.  */
9134           if (errorcount)
9135             return;
9136           abort ();
9137         }
9138
9139       /* If the definition comes from the same place as the declaration,
9140          maybe use the old DIE.  We always want the DIE for this function
9141          that has the *_pc attributes to be under comp_unit_die so the
9142          debugger can find it.  For inlines, that is the concrete instance,
9143          so we can use the old DIE here.  For non-inline methods, we want a
9144          specification DIE at toplevel, so we need a new DIE.  For local
9145          class methods, this doesn't apply; we just use the old DIE.  */
9146       if ((DECL_ABSTRACT (decl) || old_die->die_parent == comp_unit_die
9147            || context_die == NULL)
9148           && (DECL_ARTIFICIAL (decl)
9149               || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
9150                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
9151                       == (unsigned) DECL_SOURCE_LINE (decl)))))
9152         {
9153           subr_die = old_die;
9154
9155           /* Clear out the declaration attribute and the parm types.  */
9156           remove_AT (subr_die, DW_AT_declaration);
9157           remove_children (subr_die);
9158         }
9159       else
9160         {
9161           subr_die = new_die (DW_TAG_subprogram, context_die);
9162           add_AT_die_ref (subr_die, DW_AT_specification, old_die);
9163           if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
9164             add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
9165           if (get_AT_unsigned (old_die, DW_AT_decl_line)
9166               != (unsigned) DECL_SOURCE_LINE (decl))
9167             add_AT_unsigned
9168               (subr_die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
9169         }
9170     }
9171   else
9172     {
9173       subr_die = new_die (DW_TAG_subprogram, context_die);
9174
9175       if (TREE_PUBLIC (decl))
9176         add_AT_flag (subr_die, DW_AT_external, 1);
9177
9178       add_name_and_src_coords_attributes (subr_die, decl);
9179       if (debug_info_level > DINFO_LEVEL_TERSE)
9180         {
9181           register tree type = TREE_TYPE (decl);
9182
9183           add_prototyped_attribute (subr_die, type);
9184           add_type_attribute (subr_die, TREE_TYPE (type), 0, 0, context_die);
9185         }
9186
9187       add_pure_or_virtual_attribute (subr_die, decl);
9188       if (DECL_ARTIFICIAL (decl))
9189         add_AT_flag (subr_die, DW_AT_artificial, 1);
9190       if (TREE_PROTECTED (decl))
9191         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
9192       else if (TREE_PRIVATE (decl))
9193         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
9194     }
9195
9196   if (declaration)
9197     {
9198       if (! origin)
9199         add_AT_flag (subr_die, DW_AT_declaration, 1);
9200
9201       /* The first time we see a member function, it is in the context of
9202          the class to which it belongs.  We make sure of this by emitting
9203          the class first.  The next time is the definition, which is
9204          handled above.  The two may come from the same source text.  */
9205       if (DECL_CONTEXT (decl) || DECL_ABSTRACT (decl))
9206         equate_decl_number_to_die (decl, subr_die);
9207     }
9208   else if (DECL_ABSTRACT (decl))
9209     {
9210       if (DECL_INLINE (decl) && !flag_no_inline)
9211         {
9212           /* ??? Checking DECL_DEFER_OUTPUT is correct for static
9213              inline functions, but not for extern inline functions.
9214              We can't get this completely correct because information
9215              about whether the function was declared inline is not
9216              saved anywhere.  */
9217           if (DECL_DEFER_OUTPUT (decl))
9218             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
9219           else
9220             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
9221         }
9222       else
9223         add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
9224
9225       equate_decl_number_to_die (decl, subr_die);
9226     }
9227   else if (!DECL_EXTERNAL (decl))
9228     {
9229       if (origin == NULL_TREE)
9230         equate_decl_number_to_die (decl, subr_die);
9231
9232       ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
9233                                    current_funcdef_number);
9234       add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
9235       ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
9236                                    current_funcdef_number);
9237       add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
9238
9239       add_pubname (decl, subr_die);
9240       add_arange (decl, subr_die);
9241
9242 #ifdef MIPS_DEBUGGING_INFO
9243       /* Add a reference to the FDE for this routine.  */
9244       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
9245 #endif
9246
9247       /* Define the "frame base" location for this routine.  We use the
9248          frame pointer or stack pointer registers, since the RTL for local
9249          variables is relative to one of them.  */
9250       fp_reg
9251         = frame_pointer_needed ? hard_frame_pointer_rtx : stack_pointer_rtx;
9252       add_AT_loc (subr_die, DW_AT_frame_base, reg_loc_descriptor (fp_reg));
9253
9254 #if 0
9255       /* ??? This fails for nested inline functions, because context_display
9256          is not part of the state saved/restored for inline functions.  */
9257       if (current_function_needs_context)
9258         add_AT_location_description (subr_die, DW_AT_static_link,
9259                                      lookup_static_chain (decl));
9260 #endif
9261     }
9262
9263   /* Now output descriptions of the arguments for this function. This gets
9264      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
9265      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
9266      `...' at the end of the formal parameter list.  In order to find out if
9267      there was a trailing ellipsis or not, we must instead look at the type
9268      associated with the FUNCTION_DECL.  This will be a node of type
9269      FUNCTION_TYPE. If the chain of type nodes hanging off of this
9270      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
9271      an ellipsis at the end.  */
9272
9273   /* In the case where we are describing a mere function declaration, all we
9274      need to do here (and all we *can* do here) is to describe the *types* of
9275      its formal parameters.  */
9276   if (debug_info_level <= DINFO_LEVEL_TERSE)
9277     ;
9278   else if (declaration)
9279     gen_formal_types_die (TREE_TYPE (decl), subr_die);
9280   else
9281     {
9282       /* Generate DIEs to represent all known formal parameters */
9283       register tree arg_decls = DECL_ARGUMENTS (decl);
9284       register tree parm;
9285
9286       /* When generating DIEs, generate the unspecified_parameters DIE
9287          instead if we come across the arg "__builtin_va_alist" */
9288       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
9289         if (TREE_CODE (parm) == PARM_DECL)
9290           {
9291             if (DECL_NAME (parm)
9292                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
9293                             "__builtin_va_alist"))
9294               gen_unspecified_parameters_die (parm, subr_die);
9295             else
9296               gen_decl_die (parm, subr_die);
9297           }
9298
9299       /* Decide whether we need a unspecified_parameters DIE at the end.
9300          There are 2 more cases to do this for: 1) the ansi ... declaration -
9301          this is detectable when the end of the arg list is not a
9302          void_type_node 2) an unprototyped function declaration (not a
9303          definition).  This just means that we have no info about the
9304          parameters at all.  */
9305       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
9306       if (fn_arg_types != NULL)
9307         {
9308           /* this is the prototyped case, check for ...  */
9309           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
9310             gen_unspecified_parameters_die (decl, subr_die);
9311         }
9312       else if (DECL_INITIAL (decl) == NULL_TREE)
9313         gen_unspecified_parameters_die (decl, subr_die);
9314     }
9315
9316   /* Output Dwarf info for all of the stuff within the body of the function
9317      (if it has one - it may be just a declaration).  */
9318   outer_scope = DECL_INITIAL (decl);
9319
9320   /* Note that here, `outer_scope' is a pointer to the outermost BLOCK
9321      node created to represent a function. This outermost BLOCK actually
9322      represents the outermost binding contour for the function, i.e. the
9323      contour in which the function's formal parameters and labels get
9324      declared. Curiously, it appears that the front end doesn't actually
9325      put the PARM_DECL nodes for the current function onto the BLOCK_VARS
9326      list for this outer scope.  (They are strung off of the DECL_ARGUMENTS
9327      list for the function instead.) The BLOCK_VARS list for the
9328      `outer_scope' does provide us with a list of the LABEL_DECL nodes for
9329      the function however, and we output DWARF info for those in
9330      decls_for_scope.  Just within the `outer_scope' there will be a BLOCK
9331      node representing the function's outermost pair of curly braces, and
9332      any blocks used for the base and member initializers of a C++
9333      constructor function.  */
9334   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
9335     {
9336       current_function_has_inlines = 0;
9337       decls_for_scope (outer_scope, subr_die, 0);
9338
9339 #if 0 && defined (MIPS_DEBUGGING_INFO)
9340       if (current_function_has_inlines)
9341         {
9342           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
9343           if (! comp_unit_has_inlines)
9344             {
9345               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
9346               comp_unit_has_inlines = 1;
9347             }
9348         }
9349 #endif
9350     }
9351 }
9352
9353 /* Generate a DIE to represent a declared data object.  */
9354
9355 static void
9356 gen_variable_die (decl, context_die)
9357      register tree decl;
9358      register dw_die_ref context_die;
9359 {
9360   register tree origin = decl_ultimate_origin (decl);
9361   register dw_die_ref var_die = new_die (DW_TAG_variable, context_die);
9362
9363   dw_die_ref old_die = lookup_decl_die (decl);
9364   int declaration = (DECL_EXTERNAL (decl)
9365                      || class_scope_p (context_die));
9366
9367   if (origin != NULL)
9368     add_abstract_origin_attribute (var_die, origin);
9369   /* Loop unrolling can create multiple blocks that refer to the same
9370      static variable, so we must test for the DW_AT_declaration flag.  */
9371   /* ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
9372      copy decls and set the DECL_ABSTRACT flag on them instead of
9373      sharing them.  */
9374   else if (old_die && TREE_STATIC (decl)
9375            && get_AT_flag (old_die, DW_AT_declaration) == 1)
9376     {
9377       /* This is a definition of a C++ class level static.  */
9378       add_AT_die_ref (var_die, DW_AT_specification, old_die);
9379       if (DECL_NAME (decl))
9380         {
9381           register unsigned file_index
9382             = lookup_filename (DECL_SOURCE_FILE (decl));
9383
9384           if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
9385             add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
9386
9387           if (get_AT_unsigned (old_die, DW_AT_decl_line)
9388               != (unsigned) DECL_SOURCE_LINE (decl))
9389
9390             add_AT_unsigned (var_die, DW_AT_decl_line,
9391                              DECL_SOURCE_LINE (decl));
9392         }
9393     }
9394   else
9395     {
9396       add_name_and_src_coords_attributes (var_die, decl);
9397       add_type_attribute (var_die, TREE_TYPE (decl),
9398                           TREE_READONLY (decl),
9399                           TREE_THIS_VOLATILE (decl), context_die);
9400
9401       if (TREE_PUBLIC (decl))
9402         add_AT_flag (var_die, DW_AT_external, 1);
9403
9404       if (DECL_ARTIFICIAL (decl))
9405         add_AT_flag (var_die, DW_AT_artificial, 1);
9406
9407       if (TREE_PROTECTED (decl))
9408         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
9409
9410       else if (TREE_PRIVATE (decl))
9411         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
9412     }
9413
9414   if (declaration)
9415     add_AT_flag (var_die, DW_AT_declaration, 1);
9416
9417   if (class_scope_p (context_die) || DECL_ABSTRACT (decl))
9418     equate_decl_number_to_die (decl, var_die);
9419
9420   if (! declaration && ! DECL_ABSTRACT (decl))
9421     {
9422       add_location_or_const_value_attribute (var_die, decl);
9423       add_pubname (decl, var_die);
9424     }
9425   else
9426     tree_add_const_value_attribute (var_die, decl);
9427 }
9428
9429 /* Generate a DIE to represent a label identifier.  */
9430
9431 static void
9432 gen_label_die (decl, context_die)
9433      register tree decl;
9434      register dw_die_ref context_die;
9435 {
9436   register tree origin = decl_ultimate_origin (decl);
9437   register dw_die_ref lbl_die = new_die (DW_TAG_label, context_die);
9438   register rtx insn;
9439   char label[MAX_ARTIFICIAL_LABEL_BYTES];
9440   char label2[MAX_ARTIFICIAL_LABEL_BYTES];
9441
9442   if (origin != NULL)
9443     add_abstract_origin_attribute (lbl_die, origin);
9444   else
9445     add_name_and_src_coords_attributes (lbl_die, decl);
9446
9447   if (DECL_ABSTRACT (decl))
9448     equate_decl_number_to_die (decl, lbl_die);
9449   else
9450     {
9451       insn = DECL_RTL (decl);
9452
9453       /* Deleted labels are programmer specified labels which have been
9454          eliminated because of various optimisations.  We still emit them
9455          here so that it is possible to put breakpoints on them.  */
9456       if (GET_CODE (insn) == CODE_LABEL
9457           || ((GET_CODE (insn) == NOTE
9458                && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL)))
9459         {
9460           /* When optimization is enabled (via -O) some parts of the compiler
9461              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
9462              represent source-level labels which were explicitly declared by
9463              the user.  This really shouldn't be happening though, so catch
9464              it if it ever does happen.  */
9465           if (INSN_DELETED_P (insn))
9466             abort ();
9467
9468           sprintf (label2, INSN_LABEL_FMT, current_funcdef_number);
9469           ASM_GENERATE_INTERNAL_LABEL (label, label2,
9470                                        (unsigned) INSN_UID (insn));
9471           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
9472         }
9473     }
9474 }
9475
9476 /* Generate a DIE for a lexical block.  */
9477
9478 static void
9479 gen_lexical_block_die (stmt, context_die, depth)
9480      register tree stmt;
9481      register dw_die_ref context_die;
9482      int depth;
9483 {
9484   register dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die);
9485   char label[MAX_ARTIFICIAL_LABEL_BYTES];
9486
9487   if (! BLOCK_ABSTRACT (stmt))
9488     {
9489       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
9490                                    BLOCK_NUMBER (stmt));
9491       add_AT_lbl_id (stmt_die, DW_AT_low_pc, label);
9492       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
9493                                    BLOCK_NUMBER (stmt));
9494       add_AT_lbl_id (stmt_die, DW_AT_high_pc, label);
9495     }
9496
9497   decls_for_scope (stmt, stmt_die, depth);
9498 }
9499
9500 /* Generate a DIE for an inlined subprogram.  */
9501
9502 static void
9503 gen_inlined_subroutine_die (stmt, context_die, depth)
9504      register tree stmt;
9505      register dw_die_ref context_die;
9506      int depth;
9507 {
9508   if (! BLOCK_ABSTRACT (stmt))
9509     {
9510       register dw_die_ref subr_die
9511         = new_die (DW_TAG_inlined_subroutine, context_die);
9512       register tree decl = block_ultimate_origin (stmt);
9513       char label[MAX_ARTIFICIAL_LABEL_BYTES];
9514
9515       /* Emit info for the abstract instance first, if we haven't yet.  */
9516       gen_abstract_function (decl);
9517
9518       add_abstract_origin_attribute (subr_die, decl);
9519       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
9520                                    BLOCK_NUMBER (stmt));
9521       add_AT_lbl_id (subr_die, DW_AT_low_pc, label);
9522       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
9523                                    BLOCK_NUMBER (stmt));
9524       add_AT_lbl_id (subr_die, DW_AT_high_pc, label);
9525       decls_for_scope (stmt, subr_die, depth);
9526       current_function_has_inlines = 1;
9527     }
9528 }
9529
9530 /* Generate a DIE for a field in a record, or structure.  */
9531
9532 static void
9533 gen_field_die (decl, context_die)
9534      register tree decl;
9535      register dw_die_ref context_die;
9536 {
9537   register dw_die_ref decl_die = new_die (DW_TAG_member, context_die);
9538
9539   add_name_and_src_coords_attributes (decl_die, decl);
9540   add_type_attribute (decl_die, member_declared_type (decl),
9541                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
9542                       context_die);
9543
9544   /* If this is a bit field...  */
9545   if (DECL_BIT_FIELD_TYPE (decl))
9546     {
9547       add_byte_size_attribute (decl_die, decl);
9548       add_bit_size_attribute (decl_die, decl);
9549       add_bit_offset_attribute (decl_die, decl);
9550     }
9551
9552   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
9553     add_data_member_location_attribute (decl_die, decl);
9554
9555   if (DECL_ARTIFICIAL (decl))
9556     add_AT_flag (decl_die, DW_AT_artificial, 1);
9557
9558   if (TREE_PROTECTED (decl))
9559     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
9560
9561   else if (TREE_PRIVATE (decl))
9562     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
9563 }
9564
9565 #if 0
9566 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
9567    Use modified_type_die instead.
9568    We keep this code here just in case these types of DIEs may be needed to
9569    represent certain things in other languages (e.g. Pascal) someday.  */
9570 static void
9571 gen_pointer_type_die (type, context_die)
9572      register tree type;
9573      register dw_die_ref context_die;
9574 {
9575   register dw_die_ref ptr_die
9576     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die));
9577
9578   equate_type_number_to_die (type, ptr_die);
9579   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
9580   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
9581 }
9582
9583 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
9584    Use modified_type_die instead.
9585    We keep this code here just in case these types of DIEs may be needed to
9586    represent certain things in other languages (e.g. Pascal) someday.  */
9587 static void
9588 gen_reference_type_die (type, context_die)
9589      register tree type;
9590      register dw_die_ref context_die;
9591 {
9592   register dw_die_ref ref_die
9593     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die));
9594
9595   equate_type_number_to_die (type, ref_die);
9596   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
9597   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
9598 }
9599 #endif
9600
9601 /* Generate a DIE for a pointer to a member type.  */
9602 static void
9603 gen_ptr_to_mbr_type_die (type, context_die)
9604      register tree type;
9605      register dw_die_ref context_die;
9606 {
9607   register dw_die_ref ptr_die
9608     = new_die (DW_TAG_ptr_to_member_type, scope_die_for (type, context_die));
9609
9610   equate_type_number_to_die (type, ptr_die);
9611   add_AT_die_ref (ptr_die, DW_AT_containing_type,
9612                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
9613   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
9614 }
9615
9616 /* Generate the DIE for the compilation unit.  */
9617
9618 static dw_die_ref
9619 gen_compile_unit_die (filename)
9620      register const char *filename;
9621 {
9622   register dw_die_ref die;
9623   char producer[250];
9624   const char *wd = getpwd ();
9625   int language;
9626
9627   die = new_die (DW_TAG_compile_unit, NULL);
9628   add_name_attribute (die, filename);
9629
9630   if (wd != NULL && filename[0] != DIR_SEPARATOR)
9631     add_AT_string (die, DW_AT_comp_dir, wd);
9632
9633   sprintf (producer, "%s %s", language_string, version_string);
9634
9635 #ifdef MIPS_DEBUGGING_INFO
9636   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
9637      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
9638      not appear in the producer string, the debugger reaches the conclusion
9639      that the object file is stripped and has no debugging information.
9640      To get the MIPS/SGI debugger to believe that there is debugging
9641      information in the object file, we add a -g to the producer string.  */
9642   if (debug_info_level > DINFO_LEVEL_TERSE)
9643     strcat (producer, " -g");
9644 #endif
9645
9646   add_AT_string (die, DW_AT_producer, producer);
9647
9648   if (strcmp (language_string, "GNU C++") == 0)
9649     language = DW_LANG_C_plus_plus;
9650   else if (strcmp (language_string, "GNU Ada") == 0)
9651     language = DW_LANG_Ada83;
9652   else if (strcmp (language_string, "GNU F77") == 0)
9653     language = DW_LANG_Fortran77;
9654   else if (strcmp (language_string, "GNU Pascal") == 0)
9655     language = DW_LANG_Pascal83;
9656   else if (strcmp (language_string, "GNU Java") == 0)
9657     language = DW_LANG_Java;
9658   else if (flag_traditional)
9659     language = DW_LANG_C;
9660   else
9661     language = DW_LANG_C89;
9662
9663   add_AT_unsigned (die, DW_AT_language, language);
9664
9665   return die;
9666 }
9667
9668 /* Generate a DIE for a string type.  */
9669
9670 static void
9671 gen_string_type_die (type, context_die)
9672      register tree type;
9673      register dw_die_ref context_die;
9674 {
9675   register dw_die_ref type_die
9676     = new_die (DW_TAG_string_type, scope_die_for (type, context_die));
9677
9678   equate_type_number_to_die (type, type_die);
9679
9680   /* Fudge the string length attribute for now.  */
9681
9682   /* TODO: add string length info.
9683    string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
9684                               bound_representation (upper_bound, 0, 'u'); */
9685 }
9686
9687 /* Generate the DIE for a base class.  */
9688
9689 static void
9690 gen_inheritance_die (binfo, context_die)
9691      register tree binfo;
9692      register dw_die_ref context_die;
9693 {
9694   dw_die_ref die = new_die (DW_TAG_inheritance, context_die);
9695
9696   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
9697   add_data_member_location_attribute (die, binfo);
9698
9699   if (TREE_VIA_VIRTUAL (binfo))
9700     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
9701   if (TREE_VIA_PUBLIC (binfo))
9702     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
9703   else if (TREE_VIA_PROTECTED (binfo))
9704     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
9705 }
9706
9707 /* Generate a DIE for a class member.  */
9708
9709 static void
9710 gen_member_die (type, context_die)
9711      register tree type;
9712      register dw_die_ref context_die;
9713 {
9714   register tree member;
9715   dw_die_ref child;
9716
9717   /* If this is not an incomplete type, output descriptions of each of its
9718      members. Note that as we output the DIEs necessary to represent the
9719      members of this record or union type, we will also be trying to output
9720      DIEs to represent the *types* of those members. However the `type'
9721      function (above) will specifically avoid generating type DIEs for member
9722      types *within* the list of member DIEs for this (containing) type execpt
9723      for those types (of members) which are explicitly marked as also being
9724      members of this (containing) type themselves.  The g++ front- end can
9725      force any given type to be treated as a member of some other
9726      (containing) type by setting the TYPE_CONTEXT of the given (member) type
9727      to point to the TREE node representing the appropriate (containing)
9728      type.  */
9729
9730   /* First output info about the base classes.  */
9731   if (TYPE_BINFO (type) && TYPE_BINFO_BASETYPES (type))
9732     {
9733       register tree bases = TYPE_BINFO_BASETYPES (type);
9734       register int n_bases = TREE_VEC_LENGTH (bases);
9735       register int i;
9736
9737       for (i = 0; i < n_bases; i++)
9738         gen_inheritance_die (TREE_VEC_ELT (bases, i), context_die);
9739     }
9740
9741   /* Now output info about the data members and type members.  */
9742   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
9743     {
9744       /* If we thought we were generating minimal debug info for TYPE
9745          and then changed our minds, some of the member declarations
9746          may have already been defined.  Don't define them again, but
9747          do put them in the right order.  */
9748
9749       child = lookup_decl_die (member);
9750       if (child)
9751         splice_child_die (context_die, child);
9752       else
9753         gen_decl_die (member, context_die);
9754     }
9755
9756   /* Now output info about the function members (if any).  */
9757   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
9758     {
9759       child = lookup_decl_die (member);
9760       if (child)
9761         splice_child_die (context_die, child);
9762       else
9763         gen_decl_die (member, context_die);
9764     }
9765 }
9766
9767 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
9768    is set, we pretend that the type was never defined, so we only get the
9769    member DIEs needed by later specification DIEs.  */
9770
9771 static void
9772 gen_struct_or_union_type_die (type, context_die)
9773      register tree type;
9774      register dw_die_ref context_die;
9775 {
9776   register dw_die_ref type_die = lookup_type_die (type);
9777   register dw_die_ref scope_die = 0;
9778   register int nested = 0;
9779   int complete = (TYPE_SIZE (type)
9780                   && (! TYPE_STUB_DECL (type)
9781                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
9782
9783   if (type_die && ! complete)
9784     return;
9785
9786   if (TYPE_CONTEXT (type) != NULL_TREE
9787       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type)))
9788     nested = 1;
9789
9790   scope_die = scope_die_for (type, context_die);
9791
9792   if (! type_die || (nested && scope_die == comp_unit_die))
9793     /* First occurrence of type or toplevel definition of nested class.  */
9794     {
9795       register dw_die_ref old_die = type_die;
9796
9797       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
9798                           ? DW_TAG_structure_type : DW_TAG_union_type,
9799                           scope_die);
9800       equate_type_number_to_die (type, type_die);
9801       if (old_die)
9802         add_AT_die_ref (type_die, DW_AT_specification, old_die);
9803       else
9804         add_name_attribute (type_die, type_tag (type));
9805     }
9806   else
9807     remove_AT (type_die, DW_AT_declaration);
9808
9809   /* If this type has been completed, then give it a byte_size attribute and
9810      then give a list of members.  */
9811   if (complete)
9812     {
9813       /* Prevent infinite recursion in cases where the type of some member of
9814          this type is expressed in terms of this type itself.  */
9815       TREE_ASM_WRITTEN (type) = 1;
9816       add_byte_size_attribute (type_die, type);
9817       if (TYPE_STUB_DECL (type) != NULL_TREE)
9818         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
9819
9820       /* If the first reference to this type was as the return type of an
9821          inline function, then it may not have a parent.  Fix this now.  */
9822       if (type_die->die_parent == NULL)
9823         add_child_die (scope_die, type_die);
9824
9825       push_decl_scope (type);
9826       gen_member_die (type, type_die);
9827       pop_decl_scope ();
9828
9829       /* GNU extension: Record what type our vtable lives in.  */
9830       if (TYPE_VFIELD (type))
9831         {
9832           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
9833
9834           gen_type_die (vtype, context_die);
9835           add_AT_die_ref (type_die, DW_AT_containing_type,
9836                           lookup_type_die (vtype));
9837         }
9838     }
9839   else
9840     {
9841       add_AT_flag (type_die, DW_AT_declaration, 1);
9842
9843       /* We don't need to do this for function-local types.  */
9844       if (! decl_function_context (TYPE_STUB_DECL (type)))
9845         add_incomplete_type (type);
9846     }
9847 }
9848
9849 /* Generate a DIE for a subroutine _type_.  */
9850
9851 static void
9852 gen_subroutine_type_die (type, context_die)
9853      register tree type;
9854      register dw_die_ref context_die;
9855 {
9856   register tree return_type = TREE_TYPE (type);
9857   register dw_die_ref subr_die
9858     = new_die (DW_TAG_subroutine_type, scope_die_for (type, context_die));
9859
9860   equate_type_number_to_die (type, subr_die);
9861   add_prototyped_attribute (subr_die, type);
9862   add_type_attribute (subr_die, return_type, 0, 0, context_die);
9863   gen_formal_types_die (type, subr_die);
9864 }
9865
9866 /* Generate a DIE for a type definition */
9867
9868 static void
9869 gen_typedef_die (decl, context_die)
9870      register tree decl;
9871      register dw_die_ref context_die;
9872 {
9873   register dw_die_ref type_die;
9874   register tree origin;
9875
9876   if (TREE_ASM_WRITTEN (decl))
9877     return;
9878   TREE_ASM_WRITTEN (decl) = 1;
9879
9880   type_die = new_die (DW_TAG_typedef, context_die);
9881   origin = decl_ultimate_origin (decl);
9882   if (origin != NULL)
9883     add_abstract_origin_attribute (type_die, origin);
9884   else
9885     {
9886       register tree type;
9887       add_name_and_src_coords_attributes (type_die, decl);
9888       if (DECL_ORIGINAL_TYPE (decl))
9889         {
9890           type = DECL_ORIGINAL_TYPE (decl);
9891
9892           if (type == TREE_TYPE (decl))
9893             abort ();
9894           else
9895             equate_type_number_to_die (TREE_TYPE (decl), type_die);
9896         }
9897       else
9898         type = TREE_TYPE (decl);
9899       add_type_attribute (type_die, type, TREE_READONLY (decl),
9900                           TREE_THIS_VOLATILE (decl), context_die);
9901     }
9902
9903   if (DECL_ABSTRACT (decl))
9904     equate_decl_number_to_die (decl, type_die);
9905 }
9906
9907 /* Generate a type description DIE.  */
9908
9909 static void
9910 gen_type_die (type, context_die)
9911      register tree type;
9912      register dw_die_ref context_die;
9913 {
9914   int need_pop;
9915
9916   if (type == NULL_TREE || type == error_mark_node)
9917     return;
9918
9919   /* We are going to output a DIE to represent the unqualified version of
9920      this type (i.e. without any const or volatile qualifiers) so get the
9921      main variant (i.e. the unqualified version) of this type now.  */
9922   type = type_main_variant (type);
9923
9924   if (TREE_ASM_WRITTEN (type))
9925     return;
9926
9927   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9928       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
9929     {
9930       TREE_ASM_WRITTEN (type) = 1;
9931       gen_decl_die (TYPE_NAME (type), context_die);
9932       return;
9933     }
9934
9935   switch (TREE_CODE (type))
9936     {
9937     case ERROR_MARK:
9938       break;
9939
9940     case POINTER_TYPE:
9941     case REFERENCE_TYPE:
9942       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
9943          ensures that the gen_type_die recursion will terminate even if the
9944          type is recursive.  Recursive types are possible in Ada.  */
9945       /* ??? We could perhaps do this for all types before the switch
9946          statement.  */
9947       TREE_ASM_WRITTEN (type) = 1;
9948
9949       /* For these types, all that is required is that we output a DIE (or a
9950          set of DIEs) to represent the "basis" type.  */
9951       gen_type_die (TREE_TYPE (type), context_die);
9952       break;
9953
9954     case OFFSET_TYPE:
9955       /* This code is used for C++ pointer-to-data-member types.
9956          Output a description of the relevant class type.  */
9957       gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
9958
9959       /* Output a description of the type of the object pointed to.  */
9960       gen_type_die (TREE_TYPE (type), context_die);
9961
9962       /* Now output a DIE to represent this pointer-to-data-member type
9963          itself.  */
9964       gen_ptr_to_mbr_type_die (type, context_die);
9965       break;
9966
9967     case SET_TYPE:
9968       gen_type_die (TYPE_DOMAIN (type), context_die);
9969       gen_set_type_die (type, context_die);
9970       break;
9971
9972     case FILE_TYPE:
9973       gen_type_die (TREE_TYPE (type), context_die);
9974       abort ();                 /* No way to represent these in Dwarf yet!  */
9975       break;
9976
9977     case FUNCTION_TYPE:
9978       /* Force out return type (in case it wasn't forced out already).  */
9979       gen_type_die (TREE_TYPE (type), context_die);
9980       gen_subroutine_type_die (type, context_die);
9981       break;
9982
9983     case METHOD_TYPE:
9984       /* Force out return type (in case it wasn't forced out already).  */
9985       gen_type_die (TREE_TYPE (type), context_die);
9986       gen_subroutine_type_die (type, context_die);
9987       break;
9988
9989     case ARRAY_TYPE:
9990       if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
9991         {
9992           gen_type_die (TREE_TYPE (type), context_die);
9993           gen_string_type_die (type, context_die);
9994         }
9995       else
9996         gen_array_type_die (type, context_die);
9997       break;
9998
9999     case VECTOR_TYPE:
10000       gen_type_die (TYPE_DEBUG_REPRESENTATION_TYPE (type), context_die);
10001       break;
10002
10003     case ENUMERAL_TYPE:
10004     case RECORD_TYPE:
10005     case UNION_TYPE:
10006     case QUAL_UNION_TYPE:
10007       /* If this is a nested type whose containing class hasn't been
10008          written out yet, writing it out will cover this one, too.
10009          This does not apply to instantiations of member class templates;
10010          they need to be added to the containing class as they are
10011          generated.  FIXME: This hurts the idea of combining type decls
10012          from multiple TUs, since we can't predict what set of template
10013          instantiations we'll get.  */
10014       if (TYPE_CONTEXT (type)
10015           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
10016           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
10017         {
10018           gen_type_die (TYPE_CONTEXT (type), context_die);
10019
10020           if (TREE_ASM_WRITTEN (type))
10021             return;
10022
10023           /* If that failed, attach ourselves to the stub.  */
10024           push_decl_scope (TYPE_CONTEXT (type));
10025           context_die = lookup_type_die (TYPE_CONTEXT (type));
10026           need_pop = 1;
10027         }
10028       else
10029         need_pop = 0;
10030
10031       if (TREE_CODE (type) == ENUMERAL_TYPE)
10032         gen_enumeration_type_die (type, context_die);
10033       else
10034         gen_struct_or_union_type_die (type, context_die);
10035
10036       if (need_pop)
10037         pop_decl_scope ();
10038
10039       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
10040          it up if it is ever completed.  gen_*_type_die will set it for us
10041          when appropriate.  */
10042       return;
10043
10044     case VOID_TYPE:
10045     case INTEGER_TYPE:
10046     case REAL_TYPE:
10047     case COMPLEX_TYPE:
10048     case BOOLEAN_TYPE:
10049     case CHAR_TYPE:
10050       /* No DIEs needed for fundamental types.  */
10051       break;
10052
10053     case LANG_TYPE:
10054       /* No Dwarf representation currently defined.  */
10055       break;
10056
10057     default:
10058       abort ();
10059     }
10060
10061   TREE_ASM_WRITTEN (type) = 1;
10062 }
10063
10064 /* Generate a DIE for a tagged type instantiation.  */
10065
10066 static void
10067 gen_tagged_type_instantiation_die (type, context_die)
10068      register tree type;
10069      register dw_die_ref context_die;
10070 {
10071   if (type == NULL_TREE || type == error_mark_node)
10072     return;
10073
10074   /* We are going to output a DIE to represent the unqualified version of
10075      this type (i.e. without any const or volatile qualifiers) so make sure
10076      that we have the main variant (i.e. the unqualified version) of this
10077      type now.  */
10078   if (type != type_main_variant (type))
10079     abort ();
10080
10081   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
10082      an instance of an unresolved type.  */
10083
10084   switch (TREE_CODE (type))
10085     {
10086     case ERROR_MARK:
10087       break;
10088
10089     case ENUMERAL_TYPE:
10090       gen_inlined_enumeration_type_die (type, context_die);
10091       break;
10092
10093     case RECORD_TYPE:
10094       gen_inlined_structure_type_die (type, context_die);
10095       break;
10096
10097     case UNION_TYPE:
10098     case QUAL_UNION_TYPE:
10099       gen_inlined_union_type_die (type, context_die);
10100       break;
10101
10102     default:
10103       abort ();
10104     }
10105 }
10106
10107 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
10108    things which are local to the given block.  */
10109
10110 static void
10111 gen_block_die (stmt, context_die, depth)
10112      register tree stmt;
10113      register dw_die_ref context_die;
10114      int depth;
10115 {
10116   register int must_output_die = 0;
10117   register tree origin;
10118   register tree decl;
10119   register enum tree_code origin_code;
10120
10121   /* Ignore blocks never really used to make RTL.  */
10122
10123   if (stmt == NULL_TREE || !TREE_USED (stmt)
10124       || (!TREE_ASM_WRITTEN (stmt) && !BLOCK_ABSTRACT (stmt)))
10125     return;
10126
10127   /* Determine the "ultimate origin" of this block.  This block may be an
10128      inlined instance of an inlined instance of inline function, so we have
10129      to trace all of the way back through the origin chain to find out what
10130      sort of node actually served as the original seed for the creation of
10131      the current block.  */
10132   origin = block_ultimate_origin (stmt);
10133   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
10134
10135   /* Determine if we need to output any Dwarf DIEs at all to represent this
10136      block.  */
10137   if (origin_code == FUNCTION_DECL)
10138     /* The outer scopes for inlinings *must* always be represented.  We
10139        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
10140     must_output_die = 1;
10141   else
10142     {
10143       /* In the case where the current block represents an inlining of the
10144          "body block" of an inline function, we must *NOT* output any DIE for
10145          this block because we have already output a DIE to represent the
10146          whole inlined function scope and the "body block" of any function
10147          doesn't really represent a different scope according to ANSI C
10148          rules.  So we check here to make sure that this block does not
10149          represent a "body block inlining" before trying to set the
10150          `must_output_die' flag.  */
10151       if (! is_body_block (origin ? origin : stmt))
10152         {
10153           /* Determine if this block directly contains any "significant"
10154              local declarations which we will need to output DIEs for.  */
10155           if (debug_info_level > DINFO_LEVEL_TERSE)
10156             /* We are not in terse mode so *any* local declaration counts
10157                as being a "significant" one.  */
10158             must_output_die = (BLOCK_VARS (stmt) != NULL);
10159           else
10160             /* We are in terse mode, so only local (nested) function
10161                definitions count as "significant" local declarations.  */
10162             for (decl = BLOCK_VARS (stmt);
10163                  decl != NULL; decl = TREE_CHAIN (decl))
10164               if (TREE_CODE (decl) == FUNCTION_DECL
10165                   && DECL_INITIAL (decl))
10166                 {
10167                   must_output_die = 1;
10168                   break;
10169                 }
10170         }
10171     }
10172
10173   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
10174      DIE for any block which contains no significant local declarations at
10175      all.  Rather, in such cases we just call `decls_for_scope' so that any
10176      needed Dwarf info for any sub-blocks will get properly generated. Note
10177      that in terse mode, our definition of what constitutes a "significant"
10178      local declaration gets restricted to include only inlined function
10179      instances and local (nested) function definitions.  */
10180   if (must_output_die)
10181     {
10182       if (origin_code == FUNCTION_DECL)
10183         gen_inlined_subroutine_die (stmt, context_die, depth);
10184       else
10185         gen_lexical_block_die (stmt, context_die, depth);
10186     }
10187   else
10188     decls_for_scope (stmt, context_die, depth);
10189 }
10190
10191 /* Generate all of the decls declared within a given scope and (recursively)
10192    all of its sub-blocks.  */
10193
10194 static void
10195 decls_for_scope (stmt, context_die, depth)
10196      register tree stmt;
10197      register dw_die_ref context_die;
10198      int depth;
10199 {
10200   register tree decl;
10201   register tree subblocks;
10202
10203   /* Ignore blocks never really used to make RTL.  */
10204   if (stmt == NULL_TREE || ! TREE_USED (stmt))
10205     return;
10206
10207   /* Output the DIEs to represent all of the data objects and typedefs
10208      declared directly within this block but not within any nested
10209      sub-blocks.  Also, nested function and tag DIEs have been
10210      generated with a parent of NULL; fix that up now.  */
10211   for (decl = BLOCK_VARS (stmt);
10212        decl != NULL; decl = TREE_CHAIN (decl))
10213     {
10214       register dw_die_ref die;
10215
10216       if (TREE_CODE (decl) == FUNCTION_DECL)
10217         die = lookup_decl_die (decl);
10218       else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
10219         die = lookup_type_die (TREE_TYPE (decl));
10220       else
10221         die = NULL;
10222
10223       if (die != NULL && die->die_parent == NULL)
10224         add_child_die (context_die, die);
10225       else
10226         gen_decl_die (decl, context_die);
10227     }
10228
10229   /* Output the DIEs to represent all sub-blocks (and the items declared
10230      therein) of this block.  */
10231   for (subblocks = BLOCK_SUBBLOCKS (stmt);
10232        subblocks != NULL;
10233        subblocks = BLOCK_CHAIN (subblocks))
10234     gen_block_die (subblocks, context_die, depth + 1);
10235 }
10236
10237 /* Is this a typedef we can avoid emitting?  */
10238
10239 static inline int
10240 is_redundant_typedef (decl)
10241      register tree decl;
10242 {
10243   if (TYPE_DECL_IS_STUB (decl))
10244     return 1;
10245
10246   if (DECL_ARTIFICIAL (decl)
10247       && DECL_CONTEXT (decl)
10248       && is_tagged_type (DECL_CONTEXT (decl))
10249       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
10250       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
10251     /* Also ignore the artificial member typedef for the class name.  */
10252     return 1;
10253
10254   return 0;
10255 }
10256
10257 /* Generate Dwarf debug information for a decl described by DECL.  */
10258
10259 static void
10260 gen_decl_die (decl, context_die)
10261      register tree decl;
10262      register dw_die_ref context_die;
10263 {
10264   register tree origin;
10265
10266   if (TREE_CODE (decl) == ERROR_MARK)
10267     return;
10268
10269   /* If this ..._DECL node is marked to be ignored, then ignore it.  */
10270   if (DECL_IGNORED_P (decl))
10271     return;
10272
10273   switch (TREE_CODE (decl))
10274     {
10275     case CONST_DECL:
10276       /* The individual enumerators of an enum type get output when we output
10277          the Dwarf representation of the relevant enum type itself.  */
10278       break;
10279
10280     case FUNCTION_DECL:
10281       /* Don't output any DIEs to represent mere function declarations,
10282          unless they are class members or explicit block externs.  */
10283       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
10284           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
10285         break;
10286
10287       /* If we're emitting an out-of-line copy of an inline function,
10288          emit info for the abstract instance and set up to refer to it.  */
10289       if (DECL_INLINE (decl) && ! DECL_ABSTRACT (decl)
10290           && ! class_scope_p (context_die)
10291           /* gen_abstract_function won't emit a die if this is just a
10292              declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
10293              that case, because that works only if we have a die.  */
10294           && DECL_INITIAL (decl) != NULL_TREE)
10295         {
10296           gen_abstract_function (decl);
10297           set_decl_origin_self (decl);
10298         }
10299
10300       if (debug_info_level > DINFO_LEVEL_TERSE)
10301         {
10302           /* Before we describe the FUNCTION_DECL itself, make sure that we
10303              have described its return type.  */
10304           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
10305
10306           /* And its virtual context.  */
10307           if (DECL_VINDEX (decl) != NULL_TREE)
10308             gen_type_die (DECL_CONTEXT (decl), context_die);
10309
10310           /* And its containing type.  */
10311           origin = decl_class_context (decl);
10312           if (origin != NULL_TREE)
10313             gen_type_die_for_member (origin, decl, context_die);
10314         }
10315
10316       /* Now output a DIE to represent the function itself.  */
10317       gen_subprogram_die (decl, context_die);
10318       break;
10319
10320     case TYPE_DECL:
10321       /* If we are in terse mode, don't generate any DIEs to represent any
10322          actual typedefs.  */
10323       if (debug_info_level <= DINFO_LEVEL_TERSE)
10324         break;
10325
10326       /* In the special case of a TYPE_DECL node representing the
10327          declaration of some type tag, if the given TYPE_DECL is marked as
10328          having been instantiated from some other (original) TYPE_DECL node
10329          (e.g. one which was generated within the original definition of an
10330          inline function) we have to generate a special (abbreviated)
10331          DW_TAG_structure_type, DW_TAG_union_type, or DW_TAG_enumeration_type
10332          DIE here.  */
10333       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
10334         {
10335           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
10336           break;
10337         }
10338
10339       if (is_redundant_typedef (decl))
10340         gen_type_die (TREE_TYPE (decl), context_die);
10341       else
10342         /* Output a DIE to represent the typedef itself.  */
10343         gen_typedef_die (decl, context_die);
10344       break;
10345
10346     case LABEL_DECL:
10347       if (debug_info_level >= DINFO_LEVEL_NORMAL)
10348         gen_label_die (decl, context_die);
10349       break;
10350
10351     case VAR_DECL:
10352       /* If we are in terse mode, don't generate any DIEs to represent any
10353          variable declarations or definitions.  */
10354       if (debug_info_level <= DINFO_LEVEL_TERSE)
10355         break;
10356
10357       /* Output any DIEs that are needed to specify the type of this data
10358          object.  */
10359       gen_type_die (TREE_TYPE (decl), context_die);
10360
10361       /* And its containing type.  */
10362       origin = decl_class_context (decl);
10363       if (origin != NULL_TREE)
10364         gen_type_die_for_member (origin, decl, context_die);
10365
10366       /* Now output the DIE to represent the data object itself.  This gets
10367          complicated because of the possibility that the VAR_DECL really
10368          represents an inlined instance of a formal parameter for an inline
10369          function.  */
10370       origin = decl_ultimate_origin (decl);
10371       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
10372         gen_formal_parameter_die (decl, context_die);
10373       else
10374         gen_variable_die (decl, context_die);
10375       break;
10376
10377     case FIELD_DECL:
10378       /* Ignore the nameless fields that are used to skip bits, but
10379          handle C++ anonymous unions.  */
10380       if (DECL_NAME (decl) != NULL_TREE
10381           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE)
10382         {
10383           gen_type_die (member_declared_type (decl), context_die);
10384           gen_field_die (decl, context_die);
10385         }
10386       break;
10387
10388     case PARM_DECL:
10389       gen_type_die (TREE_TYPE (decl), context_die);
10390       gen_formal_parameter_die (decl, context_die);
10391       break;
10392
10393     case NAMESPACE_DECL:
10394       /* Ignore for now.  */
10395       break;
10396
10397     default:
10398       abort ();
10399     }
10400 }
10401 \f
10402 /* Add Ada "use" clause information for SGI Workshop debugger.  */
10403
10404 void
10405 dwarf2out_add_library_unit_info (filename, context_list)
10406      const char *filename;
10407      const char *context_list;
10408 {
10409   unsigned int file_index;
10410
10411   if (filename != NULL)
10412     {
10413       dw_die_ref unit_die = new_die (DW_TAG_module, comp_unit_die);
10414       tree context_list_decl
10415         = build_decl (LABEL_DECL, get_identifier (context_list),
10416                       void_type_node);
10417
10418       TREE_PUBLIC (context_list_decl) = TRUE;
10419       add_name_attribute (unit_die, context_list);
10420       file_index = lookup_filename (filename);
10421       add_AT_unsigned (unit_die, DW_AT_decl_file, file_index);
10422       add_pubname (context_list_decl, unit_die);
10423     }
10424 }
10425
10426 /* Write the debugging output for DECL.  */
10427
10428 void
10429 dwarf2out_decl (decl)
10430      register tree decl;
10431 {
10432   register dw_die_ref context_die = comp_unit_die;
10433
10434   if (TREE_CODE (decl) == ERROR_MARK)
10435     return;
10436
10437   /* If this ..._DECL node is marked to be ignored, then ignore it.  */
10438   if (DECL_IGNORED_P (decl))
10439     return;
10440
10441   switch (TREE_CODE (decl))
10442     {
10443     case FUNCTION_DECL:
10444       /* Ignore this FUNCTION_DECL if it refers to a builtin declaration of a
10445          builtin function.  Explicit programmer-supplied declarations of
10446          these same functions should NOT be ignored however.  */
10447       if (DECL_EXTERNAL (decl) && DECL_BUILT_IN (decl))
10448         return;
10449
10450       /* What we would really like to do here is to filter out all mere
10451          file-scope declarations of file-scope functions which are never
10452          referenced later within this translation unit (and keep all of ones
10453          that *are* referenced later on) but we aren't clairvoyant, so we have
10454          no idea which functions will be referenced in the future (i.e. later
10455          on within the current translation unit). So here we just ignore all
10456          file-scope function declarations which are not also definitions.  If
10457          and when the debugger needs to know something about these functions,
10458          it will have to hunt around and find the DWARF information associated
10459          with the definition of the function.  Note that we can't just check
10460          `DECL_EXTERNAL' to find out which FUNCTION_DECL nodes represent
10461          definitions and which ones represent mere declarations.  We have to
10462          check `DECL_INITIAL' instead. That's because the C front-end
10463          supports some weird semantics for "extern inline" function
10464          definitions.  These can get inlined within the current translation
10465          unit (an thus, we need to generate DWARF info for their abstract
10466          instances so that the DWARF info for the concrete inlined instances
10467          can have something to refer to) but the compiler never generates any
10468          out-of-lines instances of such things (despite the fact that they
10469          *are* definitions).  The important point is that the C front-end
10470          marks these "extern inline" functions as DECL_EXTERNAL, but we need
10471          to generate DWARF for them anyway. Note that the C++ front-end also
10472          plays some similar games for inline function definitions appearing
10473          within include files which also contain
10474          `#pragma interface' pragmas.  */
10475       if (DECL_INITIAL (decl) == NULL_TREE)
10476         return;
10477
10478       /* If we're a nested function, initially use a parent of NULL; if we're
10479          a plain function, this will be fixed up in decls_for_scope.  If
10480          we're a method, it will be ignored, since we already have a DIE.  */
10481       if (decl_function_context (decl))
10482         context_die = NULL;
10483
10484       break;
10485
10486     case VAR_DECL:
10487       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
10488          declaration and if the declaration was never even referenced from
10489          within this entire compilation unit.  We suppress these DIEs in
10490          order to save space in the .debug section (by eliminating entries
10491          which are probably useless).  Note that we must not suppress
10492          block-local extern declarations (whether used or not) because that
10493          would screw-up the debugger's name lookup mechanism and cause it to
10494          miss things which really ought to be in scope at a given point.  */
10495       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
10496         return;
10497
10498       /* If we are in terse mode, don't generate any DIEs to represent any
10499          variable declarations or definitions.  */
10500       if (debug_info_level <= DINFO_LEVEL_TERSE)
10501         return;
10502       break;
10503
10504     case TYPE_DECL:
10505       /* Don't emit stubs for types unless they are needed by other DIEs.  */
10506       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
10507         return;
10508
10509       /* Don't bother trying to generate any DIEs to represent any of the
10510          normal built-in types for the language we are compiling.  */
10511       if (DECL_SOURCE_LINE (decl) == 0)
10512         {
10513           /* OK, we need to generate one for `bool' so GDB knows what type
10514              comparisons have.  */
10515           if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
10516                == DW_LANG_C_plus_plus)
10517               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE)
10518             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
10519
10520           return;
10521         }
10522
10523       /* If we are in terse mode, don't generate any DIEs for types.  */
10524       if (debug_info_level <= DINFO_LEVEL_TERSE)
10525         return;
10526
10527       /* If we're a function-scope tag, initially use a parent of NULL;
10528          this will be fixed up in decls_for_scope.  */
10529       if (decl_function_context (decl))
10530         context_die = NULL;
10531
10532       break;
10533
10534     default:
10535       return;
10536     }
10537
10538   gen_decl_die (decl, context_die);
10539 }
10540
10541 /* Output a marker (i.e. a label) for the beginning of the generated code for
10542    a lexical block.  */
10543
10544 void
10545 dwarf2out_begin_block (blocknum)
10546      register unsigned blocknum;
10547 {
10548   function_section (current_function_decl);
10549   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
10550 }
10551
10552 /* Output a marker (i.e. a label) for the end of the generated code for a
10553    lexical block.  */
10554
10555 void
10556 dwarf2out_end_block (blocknum)
10557      register unsigned blocknum;
10558 {
10559   function_section (current_function_decl);
10560   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
10561 }
10562
10563 /* Returns nonzero if it is appropriate not to emit any debugging
10564    information for BLOCK, because it doesn't contain any instructions.
10565
10566    Don't allow this for blocks with nested functions or local classes
10567    as we would end up with orphans, and in the presence of scheduling
10568    we may end up calling them anyway.  */
10569
10570 int
10571 dwarf2out_ignore_block (block)
10572      tree block;
10573 {
10574   tree decl;
10575   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
10576     if (TREE_CODE (decl) == FUNCTION_DECL
10577         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
10578       return 0;
10579   return 1;
10580 }
10581
10582 /* Output a marker (i.e. a label) at a point in the assembly code which
10583    corresponds to a given source level label.  */
10584
10585 void
10586 dwarf2out_label (insn)
10587      register rtx insn;
10588 {
10589   char label[MAX_ARTIFICIAL_LABEL_BYTES];
10590
10591   if (debug_info_level >= DINFO_LEVEL_NORMAL)
10592     {
10593       function_section (current_function_decl);
10594       sprintf (label, INSN_LABEL_FMT, current_funcdef_number);
10595       ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, label,
10596                                  (unsigned) INSN_UID (insn));
10597     }
10598 }
10599
10600 /* Lookup a filename (in the list of filenames that we know about here in
10601    dwarf2out.c) and return its "index".  The index of each (known) filename is
10602    just a unique number which is associated with only that one filename.
10603    We need such numbers for the sake of generating labels
10604    (in the .debug_sfnames section) and references to those
10605    files  numbers (in the .debug_srcinfo and.debug_macinfo sections).
10606    If the filename given as an argument is not found in our current list,
10607    add it to the list and assign it the next available unique index number.
10608    In order to speed up searches, we remember the index of the filename
10609    was looked up last.  This handles the majority of all searches.  */
10610
10611 static unsigned
10612 lookup_filename (file_name)
10613      const char *file_name;
10614 {
10615   static unsigned last_file_lookup_index = 0;
10616   register unsigned i;
10617
10618   /* Check to see if the file name that was searched on the previous call
10619      matches this file name. If so, return the index.  */
10620   if (last_file_lookup_index != 0)
10621     if (strcmp (file_name, file_table[last_file_lookup_index]) == 0)
10622       return last_file_lookup_index;
10623
10624   /* Didn't match the previous lookup, search the table */
10625   for (i = 1; i < file_table_in_use; ++i)
10626     if (strcmp (file_name, file_table[i]) == 0)
10627       {
10628         last_file_lookup_index = i;
10629         return i;
10630       }
10631
10632   /* Prepare to add a new table entry by making sure there is enough space in
10633      the table to do so.  If not, expand the current table.  */
10634   if (file_table_in_use == file_table_allocated)
10635     {
10636       file_table_allocated += FILE_TABLE_INCREMENT;
10637       file_table
10638         = (char **) xrealloc (file_table,
10639                               file_table_allocated * sizeof (char *));
10640     }
10641
10642   /* Add the new entry to the end of the filename table.  */
10643   file_table[file_table_in_use] = xstrdup (file_name);
10644   last_file_lookup_index = file_table_in_use++;
10645
10646   return last_file_lookup_index;
10647 }
10648
10649 /* Output a label to mark the beginning of a source code line entry
10650    and record information relating to this source line, in
10651    'line_info_table' for later output of the .debug_line section.  */
10652
10653 void
10654 dwarf2out_line (filename, line)
10655      register const char *filename;
10656      register unsigned line;
10657 {
10658   if (debug_info_level >= DINFO_LEVEL_NORMAL)
10659     {
10660       function_section (current_function_decl);
10661
10662       if (DWARF2_ASM_LINE_DEBUG_INFO)
10663         {
10664           static const char *lastfile;
10665
10666           /* Emit the .file and .loc directives understood by GNU as.  */
10667           if (lastfile == 0 || strcmp (filename, lastfile))
10668             {
10669               if (lastfile == 0)
10670                 ggc_add_string_root ((char **) &lastfile, 1);
10671
10672               fprintf (asm_out_file, "\t.file 0 \"%s\"\n", filename);
10673               lastfile = filename;
10674             }
10675
10676           fprintf (asm_out_file, "\t.loc 0 %d 0\n", line);
10677
10678           /* Indicate that line number info exists.  */
10679           ++line_info_table_in_use;
10680
10681           /* Indicate that multiple line number tables exist.  */
10682           if (DECL_SECTION_NAME (current_function_decl))
10683             ++separate_line_info_table_in_use;
10684         }
10685       else if (DECL_SECTION_NAME (current_function_decl))
10686         {
10687           register dw_separate_line_info_ref line_info;
10688           ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, SEPARATE_LINE_CODE_LABEL,
10689                                      separate_line_info_table_in_use);
10690           if (flag_debug_asm)
10691             fprintf (asm_out_file, "\t%s line %d", ASM_COMMENT_START, line);
10692           fputc ('\n', asm_out_file);
10693
10694           /* expand the line info table if necessary */
10695           if (separate_line_info_table_in_use
10696               == separate_line_info_table_allocated)
10697             {
10698               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
10699               separate_line_info_table
10700                 = (dw_separate_line_info_ref)
10701                   xrealloc (separate_line_info_table,
10702                             separate_line_info_table_allocated
10703                             * sizeof (dw_separate_line_info_entry));
10704             }
10705
10706           /* Add the new entry at the end of the line_info_table.  */
10707           line_info
10708             = &separate_line_info_table[separate_line_info_table_in_use++];
10709           line_info->dw_file_num = lookup_filename (filename);
10710           line_info->dw_line_num = line;
10711           line_info->function = current_funcdef_number;
10712         }
10713       else
10714         {
10715           register dw_line_info_ref line_info;
10716
10717           ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, LINE_CODE_LABEL,
10718                                      line_info_table_in_use);
10719           if (flag_debug_asm)
10720             fprintf (asm_out_file, "\t%s line %d", ASM_COMMENT_START, line);
10721           fputc ('\n', asm_out_file);
10722
10723           /* Expand the line info table if necessary.  */
10724           if (line_info_table_in_use == line_info_table_allocated)
10725             {
10726               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
10727               line_info_table
10728                 = (dw_line_info_ref)
10729                   xrealloc (line_info_table,
10730                             (line_info_table_allocated
10731                              * sizeof (dw_line_info_entry)));
10732             }
10733
10734           /* Add the new entry at the end of the line_info_table.  */
10735           line_info = &line_info_table[line_info_table_in_use++];
10736           line_info->dw_file_num = lookup_filename (filename);
10737           line_info->dw_line_num = line;
10738         }
10739     }
10740 }
10741
10742 /* Record the beginning of a new source file, for later output
10743    of the .debug_macinfo section.  At present, unimplemented.  */
10744
10745 void
10746 dwarf2out_start_source_file (filename)
10747      register const char *filename ATTRIBUTE_UNUSED;
10748 {
10749   if (flag_eliminate_dwarf2_dups)
10750     {
10751       /* Record the beginning of the file for break_out_includes.  */
10752       dw_die_ref bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die);
10753       add_AT_string (bincl_die, DW_AT_name, filename);
10754     }
10755 }
10756
10757 /* Record the end of a source file, for later output
10758    of the .debug_macinfo section.  At present, unimplemented.  */
10759
10760 void
10761 dwarf2out_end_source_file ()
10762 {
10763   if (flag_eliminate_dwarf2_dups)
10764     {
10765       /* Record the end of the file for break_out_includes.  */
10766       new_die (DW_TAG_GNU_EINCL, comp_unit_die);
10767     }      
10768 }
10769
10770 /* Called from check_newline in c-parse.y.  The `buffer' parameter contains
10771    the tail part of the directive line, i.e. the part which is past the
10772    initial whitespace, #, whitespace, directive-name, whitespace part.  */
10773
10774 void
10775 dwarf2out_define (lineno, buffer)
10776      register unsigned lineno ATTRIBUTE_UNUSED;
10777      register const char *buffer ATTRIBUTE_UNUSED;
10778 {
10779   static int initialized = 0;
10780   if (!initialized)
10781     {
10782       dwarf2out_start_source_file (primary_filename);
10783       initialized = 1;
10784     }
10785 }
10786
10787 /* Called from check_newline in c-parse.y.  The `buffer' parameter contains
10788    the tail part of the directive line, i.e. the part which is past the
10789    initial whitespace, #, whitespace, directive-name, whitespace part.  */
10790
10791 void
10792 dwarf2out_undef (lineno, buffer)
10793      register unsigned lineno ATTRIBUTE_UNUSED;
10794      register const char *buffer ATTRIBUTE_UNUSED;
10795 {
10796 }
10797
10798 /* Set up for Dwarf output at the start of compilation.  */
10799
10800 void
10801 dwarf2out_init (asm_out_file, main_input_filename)
10802      register FILE *asm_out_file;
10803      register const char *main_input_filename;
10804 {
10805   /* Remember the name of the primary input file.  */
10806   primary_filename = main_input_filename;
10807
10808   /* Allocate the initial hunk of the file_table.  */
10809   file_table = (char **) xcalloc (FILE_TABLE_INCREMENT, sizeof (char *));
10810   file_table_allocated = FILE_TABLE_INCREMENT;
10811
10812   /* Skip the first entry - file numbers begin at 1.  */
10813   file_table_in_use = 1;
10814
10815   /* Allocate the initial hunk of the decl_die_table.  */
10816   decl_die_table
10817     = (dw_die_ref *) xcalloc (DECL_DIE_TABLE_INCREMENT, sizeof (dw_die_ref));
10818   decl_die_table_allocated = DECL_DIE_TABLE_INCREMENT;
10819   decl_die_table_in_use = 0;
10820
10821   /* Allocate the initial hunk of the decl_scope_table.  */
10822   decl_scope_table
10823     = (tree *) xcalloc (DECL_SCOPE_TABLE_INCREMENT, sizeof (tree));
10824   decl_scope_table_allocated = DECL_SCOPE_TABLE_INCREMENT;
10825   decl_scope_depth = 0;
10826
10827   /* Allocate the initial hunk of the abbrev_die_table.  */
10828   abbrev_die_table
10829     = (dw_die_ref *) xcalloc (ABBREV_DIE_TABLE_INCREMENT,
10830                               sizeof (dw_die_ref));
10831   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
10832   /* Zero-th entry is allocated, but unused */
10833   abbrev_die_table_in_use = 1;
10834
10835   /* Allocate the initial hunk of the line_info_table.  */
10836   line_info_table
10837     = (dw_line_info_ref) xcalloc (LINE_INFO_TABLE_INCREMENT,
10838                                   sizeof (dw_line_info_entry));
10839   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
10840   /* Zero-th entry is allocated, but unused */
10841   line_info_table_in_use = 1;
10842
10843   /* Generate the initial DIE for the .debug section.  Note that the (string)
10844      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
10845      will (typically) be a relative pathname and that this pathname should be
10846      taken as being relative to the directory from which the compiler was
10847      invoked when the given (base) source file was compiled.  */
10848   comp_unit_die = gen_compile_unit_die (main_input_filename);
10849
10850   if (ggc_p)
10851     {
10852       VARRAY_RTX_INIT (used_rtx_varray, 32, "used_rtx_varray");
10853       ggc_add_rtx_varray_root (&used_rtx_varray, 1);
10854     }
10855
10856   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
10857   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label, ABBREV_SECTION_LABEL, 0);
10858   if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
10859     ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
10860   else
10861     strcpy (text_section_label, stripattributes (TEXT_SECTION));
10862   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
10863                                DEBUG_INFO_SECTION_LABEL, 0);
10864   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
10865                                DEBUG_LINE_SECTION_LABEL, 0);
10866
10867   ASM_OUTPUT_SECTION (asm_out_file, ABBREV_SECTION);
10868   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
10869   if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
10870     {
10871       ASM_OUTPUT_SECTION (asm_out_file, TEXT_SECTION);
10872       ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
10873     }
10874   ASM_OUTPUT_SECTION (asm_out_file, DEBUG_INFO_SECTION);
10875   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10876   ASM_OUTPUT_SECTION (asm_out_file, DEBUG_LINE_SECTION);
10877   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
10878 }
10879
10880 /* Output stuff that dwarf requires at the end of every file,
10881    and generate the DWARF-2 debugging info.  */
10882
10883 void
10884 dwarf2out_finish ()
10885 {
10886   limbo_die_node *node, *next_node;
10887   dw_die_ref die;
10888
10889   /* Traverse the limbo die list, and add parent/child links.  The only
10890      dies without parents that should be here are concrete instances of
10891      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
10892      For concrete instances, we can get the parent die from the abstract
10893      instance.  */
10894   for (node = limbo_die_list; node; node = next_node)
10895     {
10896       next_node = node->next;
10897       die = node->die;
10898
10899       if (die->die_parent == NULL)
10900         {
10901           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
10902           if (origin)
10903             add_child_die (origin->die_parent, die);
10904           else if (die == comp_unit_die)
10905             ;
10906           else
10907             abort ();
10908         }
10909       free (node);
10910     }
10911   limbo_die_list = NULL;
10912
10913   /* Walk through the list of incomplete types again, trying once more to
10914      emit full debugging info for them.  */
10915   retry_incomplete_types ();
10916
10917   /* We need to reverse all the dies before break_out_includes, or
10918      we'll see the end of an include file before the beginning.  */
10919   reverse_all_dies (comp_unit_die);
10920
10921   /* Generate separate CUs for each of the include files we've seen.
10922      They will go into limbo_die_list.  */
10923   if (flag_eliminate_dwarf2_dups)
10924     break_out_includes (comp_unit_die);
10925
10926   /* Traverse the DIE's and add add sibling attributes to those DIE's
10927      that have children.  */
10928   add_sibling_attributes (comp_unit_die);
10929   for (node = limbo_die_list; node; node = node->next)
10930     add_sibling_attributes (node->die);
10931
10932   /* Output a terminator label for the .text section.  */
10933   fputc ('\n', asm_out_file);
10934   ASM_OUTPUT_SECTION (asm_out_file, TEXT_SECTION);
10935   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, TEXT_END_LABEL, 0);
10936
10937 #if 0
10938   /* Output a terminator label for the .data section.  */
10939   fputc ('\n', asm_out_file);
10940   ASM_OUTPUT_SECTION (asm_out_file, DATA_SECTION);
10941   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, DATA_END_LABEL, 0);
10942
10943   /* Output a terminator label for the .bss section.  */
10944   fputc ('\n', asm_out_file);
10945   ASM_OUTPUT_SECTION (asm_out_file, BSS_SECTION);
10946   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, BSS_END_LABEL, 0);
10947 #endif
10948
10949   /* Output the source line correspondence table.  */
10950   if (line_info_table_in_use > 1 || separate_line_info_table_in_use)
10951     {
10952       if (! DWARF2_ASM_LINE_DEBUG_INFO)
10953         {
10954           fputc ('\n', asm_out_file);
10955           ASM_OUTPUT_SECTION (asm_out_file, DEBUG_LINE_SECTION);
10956           output_line_info ();
10957         }
10958
10959       /* We can only use the low/high_pc attributes if all of the code
10960          was in .text.  */
10961       if (separate_line_info_table_in_use == 0)
10962         {
10963           add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
10964           add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
10965         }
10966
10967       add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
10968                          debug_line_section_label);
10969     }
10970
10971 #if 0 /* unimplemented */
10972   if (debug_info_level >= DINFO_LEVEL_VERBOSE && primary)
10973     add_AT_unsigned (die, DW_AT_macro_info, 0);
10974 #endif
10975
10976   /* Output all of the compilation units.  We put the main one last so that
10977      the offsets are available to output_pubnames.  */
10978   for (node = limbo_die_list; node; node = node->next)
10979     output_comp_unit (node->die);
10980   output_comp_unit (comp_unit_die);
10981
10982   /* Output the abbreviation table.  */
10983   fputc ('\n', asm_out_file);
10984   ASM_OUTPUT_SECTION (asm_out_file, ABBREV_SECTION);
10985   output_abbrev_section ();
10986
10987   if (pubname_table_in_use)
10988     {
10989       /* Output public names table.  */
10990       fputc ('\n', asm_out_file);
10991       ASM_OUTPUT_SECTION (asm_out_file, PUBNAMES_SECTION);
10992       output_pubnames ();
10993     }
10994
10995   /* We only put functions in the arange table, so don't write it out if
10996      we don't have any.  */
10997   if (fde_table_in_use)
10998     {
10999       /* Output the address range information.  */
11000       fputc ('\n', asm_out_file);
11001       ASM_OUTPUT_SECTION (asm_out_file, ARANGES_SECTION);
11002       output_aranges ();
11003     }
11004 }
11005 #endif /* DWARF2_DEBUGGING_INFO */