OSDN Git Service

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