OSDN Git Service

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