OSDN Git Service

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