OSDN Git Service

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