OSDN Git Service

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