OSDN Git Service

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