OSDN Git Service

* recog.c (general_operand, immediate_operand,
[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       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
7121       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
7122       mem_loc_result->dw_loc_oprnd1.v.val_addr = save_rtx (rtl);
7123       break;
7124
7125     case PRE_MODIFY:
7126       /* Extract the PLUS expression nested inside and fall into
7127          PLUS code bellow.  */
7128       rtl = XEXP (rtl, 1);
7129       goto plus;
7130
7131     case PRE_INC:
7132     case PRE_DEC:
7133       /* Turn these into a PLUS expression and fall into the PLUS code
7134          below.  */
7135       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
7136                           GEN_INT (GET_CODE (rtl) == PRE_INC
7137                                    ? GET_MODE_UNIT_SIZE (mode)
7138                                    : -GET_MODE_UNIT_SIZE (mode)));
7139
7140       /* Fall through.  */
7141
7142     case PLUS:
7143     plus:
7144       if (is_based_loc (rtl))
7145         mem_loc_result = based_loc_descr (reg_number (XEXP (rtl, 0)),
7146                                           INTVAL (XEXP (rtl, 1)));
7147       else
7148         {
7149           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
7150
7151           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
7152               && INTVAL (XEXP (rtl, 1)) >= 0)
7153             {
7154               add_loc_descr (&mem_loc_result,
7155                              new_loc_descr (DW_OP_plus_uconst,
7156                                             INTVAL (XEXP (rtl, 1)), 0));
7157             }
7158           else
7159             {
7160               add_loc_descr (&mem_loc_result,
7161                              mem_loc_descriptor (XEXP (rtl, 1), mode));
7162               add_loc_descr (&mem_loc_result,
7163                              new_loc_descr (DW_OP_plus, 0, 0));
7164             }
7165         }
7166       break;
7167
7168     case MULT:
7169       /* If a pseudo-reg is optimized away, it is possible for it to
7170          be replaced with a MEM containing a multiply.  */
7171       add_loc_descr (&mem_loc_result,
7172                      mem_loc_descriptor (XEXP (rtl, 0), mode));
7173       add_loc_descr (&mem_loc_result,
7174                      mem_loc_descriptor (XEXP (rtl, 1), mode));
7175       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
7176       break;
7177
7178     case CONST_INT:
7179       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
7180       break;
7181
7182     default:
7183       abort ();
7184     }
7185
7186   return mem_loc_result;
7187 }
7188
7189 /* Return a descriptor that describes the concatenation of two locations.
7190    This is typically a complex variable.  */
7191
7192 static dw_loc_descr_ref
7193 concat_loc_descriptor (x0, x1)
7194      register rtx x0, x1;
7195 {
7196   dw_loc_descr_ref cc_loc_result = NULL;
7197
7198   if (!is_pseudo_reg (x0)
7199       && (GET_CODE (x0) != MEM || !is_pseudo_reg (XEXP (x0, 0))))
7200     add_loc_descr (&cc_loc_result, loc_descriptor (x0));
7201   add_loc_descr (&cc_loc_result,
7202                  new_loc_descr (DW_OP_piece, GET_MODE_SIZE (GET_MODE (x0)), 0));
7203
7204   if (!is_pseudo_reg (x1)
7205       && (GET_CODE (x1) != MEM || !is_pseudo_reg (XEXP (x1, 0))))
7206     add_loc_descr (&cc_loc_result, loc_descriptor (x1));
7207   add_loc_descr (&cc_loc_result,
7208                  new_loc_descr (DW_OP_piece, GET_MODE_SIZE (GET_MODE (x1)), 0));
7209
7210   return cc_loc_result;
7211 }
7212
7213 /* Output a proper Dwarf location descriptor for a variable or parameter
7214    which is either allocated in a register or in a memory location.  For a
7215    register, we just generate an OP_REG and the register number.  For a
7216    memory location we provide a Dwarf postfix expression describing how to
7217    generate the (dynamic) address of the object onto the address stack.  */
7218
7219 static dw_loc_descr_ref
7220 loc_descriptor (rtl)
7221      register rtx rtl;
7222 {
7223   dw_loc_descr_ref loc_result = NULL;
7224   switch (GET_CODE (rtl))
7225     {
7226     case SUBREG:
7227       /* The case of a subreg may arise when we have a local (register)
7228          variable or a formal (register) parameter which doesn't quite fill
7229          up an entire register.  For now, just assume that it is
7230          legitimate to make the Dwarf info refer to the whole register which
7231          contains the given subreg.  */
7232       rtl = SUBREG_REG (rtl);
7233
7234       /* Fall through.  */
7235
7236     case REG:
7237       loc_result = reg_loc_descriptor (rtl);
7238       break;
7239
7240     case MEM:
7241       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
7242       break;
7243
7244     case CONCAT:
7245       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
7246       break;
7247
7248     default:
7249       abort ();
7250     }
7251
7252   return loc_result;
7253 }
7254
7255 /* Similar, but generate the descriptor from trees instead of rtl.
7256    This comes up particularly with variable length arrays.  */
7257
7258 static dw_loc_descr_ref
7259 loc_descriptor_from_tree (loc, addressp)
7260      tree loc;
7261      int addressp;
7262 {
7263   dw_loc_descr_ref ret = NULL;
7264   int indirect_size = 0;
7265   int unsignedp = TREE_UNSIGNED (TREE_TYPE (loc));
7266   enum dwarf_location_atom op;
7267
7268   /* ??? Most of the time we do not take proper care for sign/zero
7269      extending the values properly.  Hopefully this won't be a real
7270      problem...  */
7271
7272   switch (TREE_CODE (loc))
7273     {
7274     case ERROR_MARK:
7275       break;
7276
7277     case WITH_RECORD_EXPR:
7278       /* This case involves extracting fields from an object to determine the
7279          position of other fields.  We don't try to encode this here.  The
7280          only user of this is Ada, which encodes the needed information using
7281          the names of types.  */
7282       return ret;
7283
7284     case VAR_DECL:
7285     case PARM_DECL:
7286       {
7287         rtx rtl = rtl_for_decl_location (loc);
7288         enum machine_mode mode = DECL_MODE (loc);
7289
7290         if (rtl == NULL_RTX)
7291           break;
7292         else if (CONSTANT_P (rtl))
7293           {
7294             ret = new_loc_descr (DW_OP_addr, 0, 0);
7295             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
7296             ret->dw_loc_oprnd1.v.val_addr = rtl;
7297             indirect_size = GET_MODE_SIZE (mode);
7298           }
7299         else
7300           {
7301             if (GET_CODE (rtl) == MEM)
7302               {
7303                 indirect_size = GET_MODE_SIZE (mode);
7304                 rtl = XEXP (rtl, 0);
7305               }
7306             ret = mem_loc_descriptor (rtl, mode);
7307           }
7308       }
7309       break;
7310
7311     case INDIRECT_REF:
7312       ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
7313       indirect_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (loc)));
7314       break;
7315
7316     case NOP_EXPR:
7317     case CONVERT_EXPR:
7318     case NON_LVALUE_EXPR:
7319     case SAVE_EXPR:
7320       return loc_descriptor_from_tree (TREE_OPERAND (loc, 0), addressp);
7321
7322     case COMPONENT_REF:
7323     case BIT_FIELD_REF:
7324     case ARRAY_REF:
7325       {
7326         tree obj, offset;
7327         HOST_WIDE_INT bitsize, bitpos, bytepos;
7328         enum machine_mode mode;
7329         int volatilep;
7330         unsigned int alignment;
7331
7332         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
7333                                    &unsignedp, &volatilep, &alignment);
7334         ret = loc_descriptor_from_tree (obj, 1);
7335
7336         if (offset != NULL_TREE)
7337           {
7338             /* Variable offset.  */
7339             add_loc_descr (&ret, loc_descriptor_from_tree (offset, 0));
7340             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
7341           }
7342
7343         if (addressp)
7344           {
7345             /* We cannot address anything not on a unit boundary.  */
7346             if (bitpos % BITS_PER_UNIT != 0)
7347               abort ();
7348           }
7349         else
7350           {
7351             if (bitpos % BITS_PER_UNIT != 0
7352                 || bitsize % BITS_PER_UNIT != 0)
7353               {
7354                 /* ??? We could handle this by loading and shifting etc.
7355                    Wait until someone needs it before expending the effort.  */
7356                 abort ();
7357               }
7358
7359             indirect_size = bitsize / BITS_PER_UNIT;
7360           }
7361
7362         bytepos = bitpos / BITS_PER_UNIT;
7363         if (bytepos > 0)
7364           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
7365         else if (bytepos < 0)
7366           {
7367             add_loc_descr (&ret, int_loc_descriptor (bytepos));
7368             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
7369           }
7370         break;
7371       }
7372
7373     case INTEGER_CST:
7374       if (host_integerp (loc, 0))
7375         ret = int_loc_descriptor (tree_low_cst (loc, 0));
7376       break;
7377
7378     case BIT_AND_EXPR:
7379       op = DW_OP_and;
7380       goto do_binop;
7381     case BIT_XOR_EXPR:
7382       op = DW_OP_xor;
7383       goto do_binop;
7384     case BIT_IOR_EXPR:
7385       op = DW_OP_or;
7386       goto do_binop;
7387     case TRUNC_DIV_EXPR:
7388       op = DW_OP_div;
7389       goto do_binop;
7390     case MINUS_EXPR:
7391       op = DW_OP_minus;
7392       goto do_binop;
7393     case TRUNC_MOD_EXPR:
7394       op = DW_OP_mod;
7395       goto do_binop;
7396     case MULT_EXPR:
7397       op = DW_OP_mul;
7398       goto do_binop;
7399     case LSHIFT_EXPR:
7400       op = DW_OP_shl;
7401       goto do_binop;
7402     case RSHIFT_EXPR:
7403       op = (unsignedp ? DW_OP_shr : DW_OP_shra);
7404       goto do_binop;
7405     case PLUS_EXPR:
7406       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
7407           && host_integerp (TREE_OPERAND (loc, 1), 0))
7408         {
7409           ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
7410           add_loc_descr (&ret,
7411                          new_loc_descr (DW_OP_plus_uconst,
7412                                         tree_low_cst (TREE_OPERAND (loc, 1),
7413                                                       0),
7414                                         0));
7415           break;
7416         }
7417       op = DW_OP_plus;
7418       goto do_binop;
7419     case LE_EXPR:
7420       if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
7421         break;
7422       op = DW_OP_le;
7423       goto do_binop;
7424     case GE_EXPR:
7425       if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
7426         break;
7427       op = DW_OP_ge;
7428       goto do_binop;
7429     case LT_EXPR:
7430       if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
7431         break;
7432       op = DW_OP_lt;
7433       goto do_binop;
7434     case GT_EXPR:
7435       if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
7436         break;
7437       op = DW_OP_gt;
7438       goto do_binop;
7439     case EQ_EXPR:
7440       op = DW_OP_eq;
7441       goto do_binop;
7442     case NE_EXPR:
7443       op = DW_OP_ne;
7444       goto do_binop;
7445
7446     do_binop:
7447       ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
7448       add_loc_descr (&ret, loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0));
7449       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
7450       break;
7451
7452     case BIT_NOT_EXPR:
7453       op = DW_OP_not;
7454       goto do_unop;
7455     case ABS_EXPR:
7456       op = DW_OP_abs;
7457       goto do_unop;
7458     case NEGATE_EXPR:
7459       op = DW_OP_neg;
7460       goto do_unop;
7461
7462     do_unop:
7463       ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
7464       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
7465       break;
7466
7467     case MAX_EXPR:
7468       loc = build (COND_EXPR, TREE_TYPE (loc),
7469                    build (LT_EXPR, integer_type_node,
7470                           TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
7471                    TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
7472       /* FALLTHRU */
7473
7474     case COND_EXPR:
7475       {
7476         dw_loc_descr_ref bra_node, jump_node, tmp;
7477
7478         ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
7479         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
7480         add_loc_descr (&ret, bra_node);
7481
7482         tmp = loc_descriptor_from_tree (TREE_OPERAND (loc, 2), 0);
7483         add_loc_descr (&ret, tmp);
7484         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
7485         add_loc_descr (&ret, jump_node);
7486
7487         tmp = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
7488         add_loc_descr (&ret, tmp);
7489         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
7490         bra_node->dw_loc_oprnd1.v.val_loc = tmp;
7491
7492         /* ??? Need a node to point the skip at.  Use a nop.  */
7493         tmp = new_loc_descr (DW_OP_nop, 0, 0);
7494         add_loc_descr (&ret, tmp);
7495         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
7496         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
7497       }
7498       break;
7499
7500     default:
7501       abort ();
7502     }
7503
7504   /* If we can't fill the request for an address, die.  */
7505   if (addressp && indirect_size == 0)
7506     abort ();
7507
7508   /* If we've got an address and don't want one, dereference.  */
7509   if (!addressp && indirect_size > 0)
7510     {
7511       if (indirect_size > DWARF2_ADDR_SIZE)
7512         abort ();
7513       if (indirect_size == DWARF2_ADDR_SIZE)
7514         op = DW_OP_deref;
7515       else
7516         op = DW_OP_deref_size;
7517       add_loc_descr (&ret, new_loc_descr (op, indirect_size, 0));
7518     }
7519
7520   return ret;
7521 }
7522
7523 /* Given a value, round it up to the lowest multiple of `boundary'
7524    which is not less than the value itself.  */
7525
7526 static inline HOST_WIDE_INT
7527 ceiling (value, boundary)
7528      HOST_WIDE_INT value;
7529      unsigned int boundary;
7530 {
7531   return (((value + boundary - 1) / boundary) * boundary);
7532 }
7533
7534 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
7535    pointer to the declared type for the relevant field variable, or return
7536    `integer_type_node' if the given node turns out to be an
7537    ERROR_MARK node.  */
7538
7539 static inline tree
7540 field_type (decl)
7541      register tree decl;
7542 {
7543   register tree type;
7544
7545   if (TREE_CODE (decl) == ERROR_MARK)
7546     return integer_type_node;
7547
7548   type = DECL_BIT_FIELD_TYPE (decl);
7549   if (type == NULL_TREE)
7550     type = TREE_TYPE (decl);
7551
7552   return type;
7553 }
7554
7555 /* Given a pointer to a tree node, return the alignment in bits for
7556    it, or else return BITS_PER_WORD if the node actually turns out to
7557    be an ERROR_MARK node.  */
7558
7559 static inline unsigned
7560 simple_type_align_in_bits (type)
7561      register tree type;
7562 {
7563   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
7564 }
7565
7566 static inline unsigned
7567 simple_decl_align_in_bits (decl)
7568      register tree decl;
7569 {
7570   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
7571 }
7572
7573 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
7574    node, return the size in bits for the type if it is a constant, or else
7575    return the alignment for the type if the type's size is not constant, or
7576    else return BITS_PER_WORD if the type actually turns out to be an
7577    ERROR_MARK node.  */
7578
7579 static inline unsigned HOST_WIDE_INT
7580 simple_type_size_in_bits (type)
7581      register tree type;
7582 {
7583   tree type_size_tree;
7584
7585   if (TREE_CODE (type) == ERROR_MARK)
7586     return BITS_PER_WORD;
7587   type_size_tree = TYPE_SIZE (type);
7588
7589   if (type_size_tree == NULL_TREE)
7590     return 0;
7591   if (! host_integerp (type_size_tree, 1))
7592     return TYPE_ALIGN (type);
7593   return tree_low_cst (type_size_tree, 1);
7594 }
7595
7596 /* Given a pointer to what is assumed to be a FIELD_DECL node, compute and
7597    return the byte offset of the lowest addressed byte of the "containing
7598    object" for the given FIELD_DECL, or return 0 if we are unable to
7599    determine what that offset is, either because the argument turns out to
7600    be a pointer to an ERROR_MARK node, or because the offset is actually
7601    variable.  (We can't handle the latter case just yet).  */
7602
7603 static HOST_WIDE_INT
7604 field_byte_offset (decl)
7605      register tree decl;
7606 {
7607   unsigned int type_align_in_bits;
7608   unsigned int decl_align_in_bits;
7609   unsigned HOST_WIDE_INT type_size_in_bits;
7610   HOST_WIDE_INT object_offset_in_bits;
7611   HOST_WIDE_INT object_offset_in_bytes;
7612   tree type;
7613   tree field_size_tree;
7614   HOST_WIDE_INT bitpos_int;
7615   HOST_WIDE_INT deepest_bitpos;
7616   unsigned HOST_WIDE_INT field_size_in_bits;
7617
7618   if (TREE_CODE (decl) == ERROR_MARK)
7619     return 0;
7620
7621   if (TREE_CODE (decl) != FIELD_DECL)
7622     abort ();
7623
7624   type = field_type (decl);
7625   field_size_tree = DECL_SIZE (decl);
7626
7627   /* The size could be unspecified if there was an error, or for
7628      a flexible array member.  */
7629   if (! field_size_tree)
7630     field_size_tree = bitsize_zero_node;
7631
7632   /* We cannot yet cope with fields whose positions are variable, so
7633      for now, when we see such things, we simply return 0.  Someday, we may
7634      be able to handle such cases, but it will be damn difficult.  */
7635   if (! host_integerp (bit_position (decl), 0))
7636     return 0;
7637
7638   bitpos_int = int_bit_position (decl);
7639
7640   /* If we don't know the size of the field, pretend it's a full word.  */
7641   if (host_integerp (field_size_tree, 1))
7642     field_size_in_bits = tree_low_cst (field_size_tree, 1);
7643   else
7644     field_size_in_bits = BITS_PER_WORD;
7645
7646   type_size_in_bits = simple_type_size_in_bits (type);
7647   type_align_in_bits = simple_type_align_in_bits (type);
7648   decl_align_in_bits = simple_decl_align_in_bits (decl);
7649
7650   /* Note that the GCC front-end doesn't make any attempt to keep track of
7651      the starting bit offset (relative to the start of the containing
7652      structure type) of the hypothetical "containing object" for a bit-
7653      field.  Thus, when computing the byte offset value for the start of the
7654      "containing object" of a bit-field, we must deduce this information on
7655      our own. This can be rather tricky to do in some cases.  For example,
7656      handling the following structure type definition when compiling for an
7657      i386/i486 target (which only aligns long long's to 32-bit boundaries)
7658      can be very tricky:
7659
7660          struct S { int field1; long long field2:31; };
7661
7662      Fortunately, there is a simple rule-of-thumb which can be
7663      used in such cases.  When compiling for an i386/i486, GCC will allocate
7664      8 bytes for the structure shown above.  It decides to do this based upon
7665      one simple rule for bit-field allocation.  Quite simply, GCC allocates
7666      each "containing object" for each bit-field at the first (i.e. lowest
7667      addressed) legitimate alignment boundary (based upon the required
7668      minimum alignment for the declared type of the field) which it can
7669      possibly use, subject to the condition that there is still enough
7670      available space remaining in the containing object (when allocated at
7671      the selected point) to fully accommodate all of the bits of the
7672      bit-field itself.  This simple rule makes it obvious why GCC allocates
7673      8 bytes for each object of the structure type shown above.  When looking
7674      for a place to allocate the "containing object" for `field2', the
7675      compiler simply tries to allocate a 64-bit "containing object" at each
7676      successive 32-bit boundary (starting at zero) until it finds a place to
7677      allocate that 64- bit field such that at least 31 contiguous (and
7678      previously unallocated) bits remain within that selected 64 bit field.
7679      (As it turns out, for the example above, the compiler finds that it is
7680      OK to allocate the "containing object" 64-bit field at bit-offset zero
7681      within the structure type.) Here we attempt to work backwards from the
7682      limited set of facts we're given, and we try to deduce from those facts,
7683      where GCC must have believed that the containing object started (within
7684      the structure type). The value we deduce is then used (by the callers of
7685      this routine) to generate DW_AT_location and DW_AT_bit_offset attributes
7686      for fields (both bit-fields and, in the case of DW_AT_location, regular
7687      fields as well).  */
7688
7689   /* Figure out the bit-distance from the start of the structure to the
7690      "deepest" bit of the bit-field.  */
7691   deepest_bitpos = bitpos_int + field_size_in_bits;
7692
7693   /* This is the tricky part.  Use some fancy footwork to deduce where the
7694      lowest addressed bit of the containing object must be.  */
7695   object_offset_in_bits = deepest_bitpos - type_size_in_bits;
7696
7697   /* Round up to type_align by default.  This works best for bitfields.  */
7698   object_offset_in_bits += type_align_in_bits - 1;
7699   object_offset_in_bits /= type_align_in_bits;
7700   object_offset_in_bits *= type_align_in_bits;
7701
7702   if (object_offset_in_bits > bitpos_int)
7703     {
7704       /* Sigh, the decl must be packed.  */
7705       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
7706
7707       /* Round up to decl_align instead.  */
7708       object_offset_in_bits += decl_align_in_bits - 1;
7709       object_offset_in_bits /= decl_align_in_bits;
7710       object_offset_in_bits *= decl_align_in_bits;
7711     }
7712
7713   object_offset_in_bytes = object_offset_in_bits / BITS_PER_UNIT;
7714
7715   return object_offset_in_bytes;
7716 }
7717 \f
7718 /* The following routines define various Dwarf attributes and any data
7719    associated with them.  */
7720
7721 /* Add a location description attribute value to a DIE.
7722
7723    This emits location attributes suitable for whole variables and
7724    whole parameters.  Note that the location attributes for struct fields are
7725    generated by the routine `data_member_location_attribute' below.  */
7726
7727 static void
7728 add_AT_location_description (die, attr_kind, rtl)
7729      dw_die_ref die;
7730      enum dwarf_attribute attr_kind;
7731      register rtx rtl;
7732 {
7733   /* Handle a special case.  If we are about to output a location descriptor
7734      for a variable or parameter which has been optimized out of existence,
7735      don't do that.  A variable which has been optimized out
7736      of existence will have a DECL_RTL value which denotes a pseudo-reg.
7737      Currently, in some rare cases, variables can have DECL_RTL values which
7738      look like (MEM (REG pseudo-reg#)).  These cases are due to bugs
7739      elsewhere in the compiler.  We treat such cases as if the variable(s) in
7740      question had been optimized out of existence.  */
7741
7742   if (is_pseudo_reg (rtl)
7743       || (GET_CODE (rtl) == MEM
7744           && is_pseudo_reg (XEXP (rtl, 0)))
7745       /* This can happen for a PARM_DECL with a DECL_INCOMING_RTL which
7746          references the internal argument pointer (a pseudo) in a function
7747          where all references to the internal argument pointer were
7748          eliminated via the optimizers.  */
7749       || (GET_CODE (rtl) == MEM
7750           && GET_CODE (XEXP (rtl, 0)) == PLUS
7751           && is_pseudo_reg (XEXP (XEXP (rtl, 0), 0)))
7752       || (GET_CODE (rtl) == CONCAT
7753           && is_pseudo_reg (XEXP (rtl, 0))
7754           && is_pseudo_reg (XEXP (rtl, 1))))
7755     return;
7756
7757   add_AT_loc (die, attr_kind, loc_descriptor (rtl));
7758 }
7759
7760 /* Attach the specialized form of location attribute used for data
7761    members of struct and union types.  In the special case of a
7762    FIELD_DECL node which represents a bit-field, the "offset" part
7763    of this special location descriptor must indicate the distance
7764    in bytes from the lowest-addressed byte of the containing struct
7765    or union type to the lowest-addressed byte of the "containing
7766    object" for the bit-field.  (See the `field_byte_offset' function
7767    above).. For any given bit-field, the "containing object" is a
7768    hypothetical object (of some integral or enum type) within which
7769    the given bit-field lives.  The type of this hypothetical
7770    "containing object" is always the same as the declared type of
7771    the individual bit-field itself (for GCC anyway... the DWARF
7772    spec doesn't actually mandate this).  Note that it is the size
7773    (in bytes) of the hypothetical "containing object" which will
7774    be given in the DW_AT_byte_size attribute for this bit-field.
7775    (See the `byte_size_attribute' function below.)  It is also used
7776    when calculating the value of the DW_AT_bit_offset attribute.
7777    (See the `bit_offset_attribute' function below).  */
7778
7779 static void
7780 add_data_member_location_attribute (die, decl)
7781      register dw_die_ref die;
7782      register tree decl;
7783 {
7784   register unsigned long offset;
7785   register dw_loc_descr_ref loc_descr;
7786   register enum dwarf_location_atom op;
7787
7788   if (TREE_CODE (decl) == TREE_VEC)
7789     offset = tree_low_cst (BINFO_OFFSET (decl), 0);
7790   else
7791     offset = field_byte_offset (decl);
7792
7793   /* The DWARF2 standard says that we should assume that the structure address
7794      is already on the stack, so we can specify a structure field address
7795      by using DW_OP_plus_uconst.  */
7796
7797 #ifdef MIPS_DEBUGGING_INFO
7798   /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst operator
7799      correctly.  It works only if we leave the offset on the stack.  */
7800   op = DW_OP_constu;
7801 #else
7802   op = DW_OP_plus_uconst;
7803 #endif
7804
7805   loc_descr = new_loc_descr (op, offset, 0);
7806   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
7807 }
7808
7809 /* Attach an DW_AT_const_value attribute for a variable or a parameter which
7810    does not have a "location" either in memory or in a register.  These
7811    things can arise in GNU C when a constant is passed as an actual parameter
7812    to an inlined function.  They can also arise in C++ where declared
7813    constants do not necessarily get memory "homes".  */
7814
7815 static void
7816 add_const_value_attribute (die, rtl)
7817      register dw_die_ref die;
7818      register rtx rtl;
7819 {
7820   switch (GET_CODE (rtl))
7821     {
7822     case CONST_INT:
7823       /* Note that a CONST_INT rtx could represent either an integer
7824          or a floating-point constant.  A CONST_INT is used whenever
7825          the constant will fit into a single word.  In all such
7826          cases, the original mode of the constant value is wiped
7827          out, and the CONST_INT rtx is assigned VOIDmode.  */
7828       {
7829         HOST_WIDE_INT val = INTVAL (rtl);
7830         
7831         /* ??? We really should be using HOST_WIDE_INT throughout.  */
7832         if (val < 0)
7833           {
7834             if ((long) val != val)
7835               abort ();
7836             add_AT_int (die, DW_AT_const_value, (long) val);
7837           }
7838         else
7839           {
7840             if ((unsigned long) val != (unsigned HOST_WIDE_INT) val)
7841               abort ();
7842             add_AT_int (die, DW_AT_const_value, (unsigned long) val);
7843           }
7844       }
7845       break;
7846
7847     case CONST_DOUBLE:
7848       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
7849          floating-point constant.  A CONST_DOUBLE is used whenever the
7850          constant requires more than one word in order to be adequately
7851          represented.  We output CONST_DOUBLEs as blocks.  */
7852       {
7853         register enum machine_mode mode = GET_MODE (rtl);
7854
7855         if (GET_MODE_CLASS (mode) == MODE_FLOAT)
7856           {
7857             register unsigned length = GET_MODE_SIZE (mode) / 4;
7858             long *array = (long *) xmalloc (sizeof (long) * length);
7859             REAL_VALUE_TYPE rv;
7860
7861             REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
7862             switch (mode)
7863               {
7864               case SFmode:
7865                 REAL_VALUE_TO_TARGET_SINGLE (rv, array[0]);
7866                 break;
7867
7868               case DFmode:
7869                 REAL_VALUE_TO_TARGET_DOUBLE (rv, array);
7870                 break;
7871
7872               case XFmode:
7873               case TFmode:
7874                 REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, array);
7875                 break;
7876
7877               default:
7878                 abort ();
7879               }
7880
7881             add_AT_float (die, DW_AT_const_value, length, array);
7882           }
7883         else
7884           {
7885             /* ??? We really should be using HOST_WIDE_INT throughout.  */
7886             if (HOST_BITS_PER_LONG != HOST_BITS_PER_WIDE_INT)
7887               abort ();
7888             add_AT_long_long (die, DW_AT_const_value,
7889                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
7890           }
7891       }
7892       break;
7893
7894     case CONST_STRING:
7895       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
7896       break;
7897
7898     case SYMBOL_REF:
7899     case LABEL_REF:
7900     case CONST:
7901       add_AT_addr (die, DW_AT_const_value, save_rtx (rtl));
7902       break;
7903
7904     case PLUS:
7905       /* In cases where an inlined instance of an inline function is passed
7906          the address of an `auto' variable (which is local to the caller) we
7907          can get a situation where the DECL_RTL of the artificial local
7908          variable (for the inlining) which acts as a stand-in for the
7909          corresponding formal parameter (of the inline function) will look
7910          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
7911          exactly a compile-time constant expression, but it isn't the address
7912          of the (artificial) local variable either.  Rather, it represents the
7913          *value* which the artificial local variable always has during its
7914          lifetime.  We currently have no way to represent such quasi-constant
7915          values in Dwarf, so for now we just punt and generate nothing.  */
7916       break;
7917
7918     default:
7919       /* No other kinds of rtx should be possible here.  */
7920       abort ();
7921     }
7922
7923 }
7924
7925 static rtx
7926 rtl_for_decl_location (decl)
7927      tree decl;
7928 {
7929   register rtx rtl;
7930
7931   /* Here we have to decide where we are going to say the parameter "lives"
7932      (as far as the debugger is concerned).  We only have a couple of
7933      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
7934
7935      DECL_RTL normally indicates where the parameter lives during most of the
7936      activation of the function.  If optimization is enabled however, this
7937      could be either NULL or else a pseudo-reg.  Both of those cases indicate
7938      that the parameter doesn't really live anywhere (as far as the code
7939      generation parts of GCC are concerned) during most of the function's
7940      activation.  That will happen (for example) if the parameter is never
7941      referenced within the function.
7942
7943      We could just generate a location descriptor here for all non-NULL
7944      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
7945      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
7946      where DECL_RTL is NULL or is a pseudo-reg.
7947
7948      Note however that we can only get away with using DECL_INCOMING_RTL as
7949      a backup substitute for DECL_RTL in certain limited cases.  In cases
7950      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
7951      we can be sure that the parameter was passed using the same type as it is
7952      declared to have within the function, and that its DECL_INCOMING_RTL
7953      points us to a place where a value of that type is passed.
7954
7955      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
7956      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
7957      because in these cases DECL_INCOMING_RTL points us to a value of some
7958      type which is *different* from the type of the parameter itself.  Thus,
7959      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
7960      such cases, the debugger would end up (for example) trying to fetch a
7961      `float' from a place which actually contains the first part of a
7962      `double'.  That would lead to really incorrect and confusing
7963      output at debug-time.
7964
7965      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
7966      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
7967      are a couple of exceptions however.  On little-endian machines we can
7968      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
7969      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
7970      an integral type that is smaller than TREE_TYPE (decl). These cases arise
7971      when (on a little-endian machine) a non-prototyped function has a
7972      parameter declared to be of type `short' or `char'.  In such cases,
7973      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
7974      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
7975      passed `int' value.  If the debugger then uses that address to fetch
7976      a `short' or a `char' (on a little-endian machine) the result will be
7977      the correct data, so we allow for such exceptional cases below.
7978
7979      Note that our goal here is to describe the place where the given formal
7980      parameter lives during most of the function's activation (i.e. between
7981      the end of the prologue and the start of the epilogue).  We'll do that
7982      as best as we can. Note however that if the given formal parameter is
7983      modified sometime during the execution of the function, then a stack
7984      backtrace (at debug-time) will show the function as having been
7985      called with the *new* value rather than the value which was
7986      originally passed in.  This happens rarely enough that it is not
7987      a major problem, but it *is* a problem, and I'd like to fix it.
7988
7989      A future version of dwarf2out.c may generate two additional
7990      attributes for any given DW_TAG_formal_parameter DIE which will
7991      describe the "passed type" and the "passed location" for the
7992      given formal parameter in addition to the attributes we now
7993      generate to indicate the "declared type" and the "active
7994      location" for each parameter.  This additional set of attributes
7995      could be used by debuggers for stack backtraces. Separately, note
7996      that sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be
7997      NULL also.  This happens (for example) for inlined-instances of
7998      inline function formal parameters which are never referenced.
7999      This really shouldn't be happening.  All PARM_DECL nodes should
8000      get valid non-NULL DECL_INCOMING_RTL values, but integrate.c
8001      doesn't currently generate these values for inlined instances of
8002      inline function parameters, so when we see such cases, we are
8003      just out-of-luck for the time being (until integrate.c
8004      gets fixed).  */
8005
8006   /* Use DECL_RTL as the "location" unless we find something better.  */
8007   rtl = DECL_RTL_IF_SET (decl);
8008
8009   if (TREE_CODE (decl) == PARM_DECL)
8010     {
8011       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
8012         {
8013           tree declared_type = type_main_variant (TREE_TYPE (decl));
8014           tree passed_type = type_main_variant (DECL_ARG_TYPE (decl));
8015
8016           /* This decl represents a formal parameter which was optimized out.
8017              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
8018              all* cases where (rtl == NULL_RTX) just below.  */
8019           if (declared_type == passed_type)
8020             rtl = DECL_INCOMING_RTL (decl);
8021           else if (! BYTES_BIG_ENDIAN
8022                    && TREE_CODE (declared_type) == INTEGER_TYPE
8023                    && (GET_MODE_SIZE (TYPE_MODE (declared_type))
8024                        <= GET_MODE_SIZE (TYPE_MODE (passed_type))))
8025             rtl = DECL_INCOMING_RTL (decl);
8026         }
8027
8028       /* If the parm was passed in registers, but lives on the stack, then
8029          make a big endian correction if the mode of the type of the
8030          parameter is not the same as the mode of the rtl.  */
8031       /* ??? This is the same series of checks that are made in dbxout.c before
8032          we reach the big endian correction code there.  It isn't clear if all
8033          of these checks are necessary here, but keeping them all is the safe
8034          thing to do.  */
8035       else if (GET_CODE (rtl) == MEM
8036                && XEXP (rtl, 0) != const0_rtx
8037                && ! CONSTANT_P (XEXP (rtl, 0))
8038                /* Not passed in memory.  */
8039                && GET_CODE (DECL_INCOMING_RTL (decl)) != MEM
8040                /* Not passed by invisible reference.  */
8041                && (GET_CODE (XEXP (rtl, 0)) != REG
8042                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
8043                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
8044 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
8045                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
8046 #endif
8047                      )
8048                /* Big endian correction check.  */
8049                && BYTES_BIG_ENDIAN
8050                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
8051                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
8052                    < UNITS_PER_WORD))
8053         {
8054           int offset = (UNITS_PER_WORD
8055                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
8056           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
8057                              plus_constant (XEXP (rtl, 0), offset));
8058         }
8059     }
8060
8061   if (rtl != NULL_RTX)
8062     {
8063       rtl = eliminate_regs (rtl, 0, NULL_RTX);
8064 #ifdef LEAF_REG_REMAP
8065       if (current_function_uses_only_leaf_regs)
8066         leaf_renumber_regs_insn (rtl);
8067 #endif
8068     }
8069
8070   return rtl;
8071 }
8072
8073 /* Generate *either* an DW_AT_location attribute or else an DW_AT_const_value
8074    data attribute for a variable or a parameter.  We generate the
8075    DW_AT_const_value attribute only in those cases where the given variable
8076    or parameter does not have a true "location" either in memory or in a
8077    register.  This can happen (for example) when a constant is passed as an
8078    actual argument in a call to an inline function.  (It's possible that
8079    these things can crop up in other ways also.)  Note that one type of
8080    constant value which can be passed into an inlined function is a constant
8081    pointer.  This can happen for example if an actual argument in an inlined
8082    function call evaluates to a compile-time constant address.  */
8083
8084 static void
8085 add_location_or_const_value_attribute (die, decl)
8086      register dw_die_ref die;
8087      register tree decl;
8088 {
8089   register rtx rtl;
8090
8091   if (TREE_CODE (decl) == ERROR_MARK)
8092     return;
8093
8094   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != PARM_DECL)
8095     abort ();
8096
8097   rtl = rtl_for_decl_location (decl);
8098   if (rtl == NULL_RTX)
8099     return;
8100
8101   switch (GET_CODE (rtl))
8102     {
8103     case ADDRESSOF:
8104       /* The address of a variable that was optimized away; don't emit
8105          anything.  */
8106       break;
8107
8108     case CONST_INT:
8109     case CONST_DOUBLE:
8110     case CONST_STRING:
8111     case SYMBOL_REF:
8112     case LABEL_REF:
8113     case CONST:
8114     case PLUS:
8115       /* DECL_RTL could be (plus (reg ...) (const_int ...)) */
8116       add_const_value_attribute (die, rtl);
8117       break;
8118
8119     case MEM:
8120     case REG:
8121     case SUBREG:
8122     case CONCAT:
8123       add_AT_location_description (die, DW_AT_location, rtl);
8124       break;
8125
8126     default:
8127       abort ();
8128     }
8129 }
8130
8131 /* If we don't have a copy of this variable in memory for some reason (such
8132    as a C++ member constant that doesn't have an out-of-line definition),
8133    we should tell the debugger about the constant value.  */
8134
8135 static void
8136 tree_add_const_value_attribute (var_die, decl)
8137      dw_die_ref var_die;
8138      tree decl;
8139 {
8140   tree init = DECL_INITIAL (decl);
8141   tree type = TREE_TYPE (decl);
8142
8143   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init
8144       && initializer_constant_valid_p (init, type) == null_pointer_node)
8145     /* OK */;
8146   else
8147     return;
8148
8149   switch (TREE_CODE (type))
8150     {
8151     case INTEGER_TYPE:
8152       if (host_integerp (init, 0))
8153         add_AT_unsigned (var_die, DW_AT_const_value,
8154                          TREE_INT_CST_LOW (init));
8155       else
8156         add_AT_long_long (var_die, DW_AT_const_value,
8157                           TREE_INT_CST_HIGH (init),
8158                           TREE_INT_CST_LOW (init));
8159       break;
8160
8161     default:;
8162     }
8163 }
8164
8165 /* Generate an DW_AT_name attribute given some string value to be included as
8166    the value of the attribute.  */
8167
8168 static inline void
8169 add_name_attribute (die, name_string)
8170      register dw_die_ref die;
8171      register const char *name_string;
8172 {
8173   if (name_string != NULL && *name_string != 0)
8174     {
8175       if (demangle_name_func)
8176         name_string = (*demangle_name_func) (name_string);
8177
8178       add_AT_string (die, DW_AT_name, name_string);
8179     }
8180 }
8181
8182 /* Given a tree node describing an array bound (either lower or upper) output
8183    a representation for that bound.  */
8184
8185 static void
8186 add_bound_info (subrange_die, bound_attr, bound)
8187      register dw_die_ref subrange_die;
8188      register enum dwarf_attribute bound_attr;
8189      register tree bound;
8190 {
8191   /* If this is an Ada unconstrained array type, then don't emit any debug
8192      info because the array bounds are unknown.  They are parameterized when
8193      the type is instantiated.  */
8194   if (contains_placeholder_p (bound))
8195     return;
8196
8197   switch (TREE_CODE (bound))
8198     {
8199     case ERROR_MARK:
8200       return;
8201
8202     /* All fixed-bounds are represented by INTEGER_CST nodes.        */
8203     case INTEGER_CST:
8204       if (! host_integerp (bound, 0)
8205           || (bound_attr == DW_AT_lower_bound
8206               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
8207                   || (is_fortran () && integer_onep (bound)))))
8208         /* use the default */
8209         ;
8210       else
8211         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
8212       break;
8213
8214     case CONVERT_EXPR:
8215     case NOP_EXPR:
8216     case NON_LVALUE_EXPR:
8217       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
8218       break;
8219
8220     case SAVE_EXPR:
8221       /* If optimization is turned on, the SAVE_EXPRs that describe how to
8222          access the upper bound values may be bogus.  If they refer to a
8223          register, they may only describe how to get at these values at the
8224          points in the generated code right after they have just been
8225          computed.  Worse yet, in the typical case, the upper bound values
8226          will not even *be* computed in the optimized code (though the
8227          number of elements will), so these SAVE_EXPRs are entirely
8228          bogus. In order to compensate for this fact, we check here to see
8229          if optimization is enabled, and if so, we don't add an attribute
8230          for the (unknown and unknowable) upper bound.  This should not
8231          cause too much trouble for existing (stupid?)  debuggers because
8232          they have to deal with empty upper bounds location descriptions
8233          anyway in order to be able to deal with incomplete array types.
8234          Of course an intelligent debugger (GDB?)  should be able to
8235          comprehend that a missing upper bound specification in a array
8236          type used for a storage class `auto' local array variable
8237          indicates that the upper bound is both unknown (at compile- time)
8238          and unknowable (at run-time) due to optimization.
8239
8240          We assume that a MEM rtx is safe because gcc wouldn't put the
8241          value there unless it was going to be used repeatedly in the
8242          function, i.e. for cleanups.  */
8243       if (SAVE_EXPR_RTL (bound)
8244           && (! optimize || GET_CODE (SAVE_EXPR_RTL (bound)) == MEM))
8245         {
8246           register dw_die_ref ctx = lookup_decl_die (current_function_decl);
8247           register dw_die_ref decl_die = new_die (DW_TAG_variable, ctx);
8248           register rtx loc = SAVE_EXPR_RTL (bound);
8249
8250           /* If the RTL for the SAVE_EXPR is memory, handle the case where
8251              it references an outer function's frame.  */
8252
8253           if (GET_CODE (loc) == MEM)
8254             {
8255               rtx new_addr = fix_lexical_addr (XEXP (loc, 0), bound);
8256
8257               if (XEXP (loc, 0) != new_addr)
8258                 loc = gen_rtx_MEM (GET_MODE (loc), new_addr);
8259             }
8260
8261           add_AT_flag (decl_die, DW_AT_artificial, 1);
8262           add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
8263           add_AT_location_description (decl_die, DW_AT_location, loc);
8264           add_AT_die_ref (subrange_die, bound_attr, decl_die);
8265         }
8266
8267       /* Else leave out the attribute.  */
8268       break;
8269
8270     case VAR_DECL:
8271     case PARM_DECL:
8272       {
8273         dw_die_ref decl_die = lookup_decl_die (bound);
8274
8275         /* ??? Can this happen, or should the variable have been bound
8276            first?  Probably it can, since I imagine that we try to create
8277            the types of parameters in the order in which they exist in
8278            the list, and won't have created a forward reference to a
8279            later parameter.  */
8280         if (decl_die != NULL)
8281           add_AT_die_ref (subrange_die, bound_attr, decl_die);
8282         break;
8283       }
8284
8285     default:
8286       {
8287         /* Otherwise try to create a stack operation procedure to
8288            evaluate the value of the array bound.  */
8289
8290         dw_die_ref ctx, decl_die;
8291         dw_loc_descr_ref loc;
8292
8293         loc = loc_descriptor_from_tree (bound, 0);
8294         if (loc == NULL)
8295           break;
8296
8297         ctx = lookup_decl_die (current_function_decl);
8298
8299         decl_die = new_die (DW_TAG_variable, ctx);
8300         add_AT_flag (decl_die, DW_AT_artificial, 1);
8301         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
8302         add_AT_loc (decl_die, DW_AT_location, loc);
8303
8304         add_AT_die_ref (subrange_die, bound_attr, decl_die);
8305         break;
8306       }
8307     }
8308 }
8309
8310 /* Note that the block of subscript information for an array type also
8311    includes information about the element type of type given array type.  */
8312
8313 static void
8314 add_subscript_info (type_die, type)
8315      register dw_die_ref type_die;
8316      register tree type;
8317 {
8318 #ifndef MIPS_DEBUGGING_INFO
8319   register unsigned dimension_number;
8320 #endif
8321   register tree lower, upper;
8322   register dw_die_ref subrange_die;
8323
8324   /* The GNU compilers represent multidimensional array types as sequences of
8325      one dimensional array types whose element types are themselves array
8326      types.  Here we squish that down, so that each multidimensional array
8327      type gets only one array_type DIE in the Dwarf debugging info. The draft
8328      Dwarf specification say that we are allowed to do this kind of
8329      compression in C (because there is no difference between an array or
8330      arrays and a multidimensional array in C) but for other source languages
8331      (e.g. Ada) we probably shouldn't do this.  */
8332
8333   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
8334      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
8335      We work around this by disabling this feature.  See also
8336      gen_array_type_die.  */
8337 #ifndef MIPS_DEBUGGING_INFO
8338   for (dimension_number = 0;
8339        TREE_CODE (type) == ARRAY_TYPE;
8340        type = TREE_TYPE (type), dimension_number++)
8341     {
8342 #endif
8343       register tree domain = TYPE_DOMAIN (type);
8344
8345       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
8346          and (in GNU C only) variable bounds.  Handle all three forms
8347          here.  */
8348       subrange_die = new_die (DW_TAG_subrange_type, type_die);
8349       if (domain)
8350         {
8351           /* We have an array type with specified bounds.  */
8352           lower = TYPE_MIN_VALUE (domain);
8353           upper = TYPE_MAX_VALUE (domain);
8354
8355           /* define the index type.  */
8356           if (TREE_TYPE (domain))
8357             {
8358               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
8359                  TREE_TYPE field.  We can't emit debug info for this
8360                  because it is an unnamed integral type.  */
8361               if (TREE_CODE (domain) == INTEGER_TYPE
8362                   && TYPE_NAME (domain) == NULL_TREE
8363                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
8364                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
8365                 ;
8366               else
8367                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
8368                                     type_die);
8369             }
8370
8371           /* ??? If upper is NULL, the array has unspecified length,
8372              but it does have a lower bound.  This happens with Fortran
8373                dimension arr(N:*)
8374              Since the debugger is definitely going to need to know N
8375              to produce useful results, go ahead and output the lower
8376              bound solo, and hope the debugger can cope.  */
8377
8378           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
8379           if (upper)
8380             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
8381         }
8382       else
8383         /* We have an array type with an unspecified length.  The DWARF-2
8384              spec does not say how to handle this; let's just leave out the
8385              bounds.  */
8386         {;}
8387
8388 #ifndef MIPS_DEBUGGING_INFO
8389     }
8390 #endif
8391 }
8392
8393 static void
8394 add_byte_size_attribute (die, tree_node)
8395      dw_die_ref die;
8396      register tree tree_node;
8397 {
8398   register unsigned size;
8399
8400   switch (TREE_CODE (tree_node))
8401     {
8402     case ERROR_MARK:
8403       size = 0;
8404       break;
8405     case ENUMERAL_TYPE:
8406     case RECORD_TYPE:
8407     case UNION_TYPE:
8408     case QUAL_UNION_TYPE:
8409       size = int_size_in_bytes (tree_node);
8410       break;
8411     case FIELD_DECL:
8412       /* For a data member of a struct or union, the DW_AT_byte_size is
8413          generally given as the number of bytes normally allocated for an
8414          object of the *declared* type of the member itself.  This is true
8415          even for bit-fields.  */
8416       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
8417       break;
8418     default:
8419       abort ();
8420     }
8421
8422   /* Note that `size' might be -1 when we get to this point.  If it is, that
8423      indicates that the byte size of the entity in question is variable.  We
8424      have no good way of expressing this fact in Dwarf at the present time,
8425      so just let the -1 pass on through.  */
8426
8427   add_AT_unsigned (die, DW_AT_byte_size, size);
8428 }
8429
8430 /* For a FIELD_DECL node which represents a bit-field, output an attribute
8431    which specifies the distance in bits from the highest order bit of the
8432    "containing object" for the bit-field to the highest order bit of the
8433    bit-field itself.
8434
8435    For any given bit-field, the "containing object" is a hypothetical
8436    object (of some integral or enum type) within which the given bit-field
8437    lives.  The type of this hypothetical "containing object" is always the
8438    same as the declared type of the individual bit-field itself.  The
8439    determination of the exact location of the "containing object" for a
8440    bit-field is rather complicated.  It's handled by the
8441    `field_byte_offset' function (above).
8442
8443    Note that it is the size (in bytes) of the hypothetical "containing object"
8444    which will be given in the DW_AT_byte_size attribute for this bit-field.
8445    (See `byte_size_attribute' above).  */
8446
8447 static inline void
8448 add_bit_offset_attribute (die, decl)
8449      register dw_die_ref die;
8450      register tree decl;
8451 {
8452   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
8453   tree type = DECL_BIT_FIELD_TYPE (decl);
8454   HOST_WIDE_INT bitpos_int;
8455   HOST_WIDE_INT highest_order_object_bit_offset;
8456   HOST_WIDE_INT highest_order_field_bit_offset;
8457   HOST_WIDE_INT unsigned bit_offset;
8458
8459   /* Must be a field and a bit field.  */
8460   if (!type
8461       || TREE_CODE (decl) != FIELD_DECL)
8462     abort ();
8463
8464   /* We can't yet handle bit-fields whose offsets are variable, so if we
8465      encounter such things, just return without generating any attribute
8466      whatsoever.  Likewise for variable or too large size.  */
8467   if (! host_integerp (bit_position (decl), 0)
8468       || ! host_integerp (DECL_SIZE (decl), 1))
8469     return;
8470
8471   bitpos_int = int_bit_position (decl);
8472
8473   /* Note that the bit offset is always the distance (in bits) from the
8474      highest-order bit of the "containing object" to the highest-order bit of
8475      the bit-field itself.  Since the "high-order end" of any object or field
8476      is different on big-endian and little-endian machines, the computation
8477      below must take account of these differences.  */
8478   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
8479   highest_order_field_bit_offset = bitpos_int;
8480
8481   if (! BYTES_BIG_ENDIAN)
8482     {
8483       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
8484       highest_order_object_bit_offset += simple_type_size_in_bits (type);
8485     }
8486
8487   bit_offset
8488     = (! BYTES_BIG_ENDIAN
8489        ? highest_order_object_bit_offset - highest_order_field_bit_offset
8490        : highest_order_field_bit_offset - highest_order_object_bit_offset);
8491
8492   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
8493 }
8494
8495 /* For a FIELD_DECL node which represents a bit field, output an attribute
8496    which specifies the length in bits of the given field.  */
8497
8498 static inline void
8499 add_bit_size_attribute (die, decl)
8500      register dw_die_ref die;
8501      register tree decl;
8502 {
8503   /* Must be a field and a bit field.  */
8504   if (TREE_CODE (decl) != FIELD_DECL
8505       || ! DECL_BIT_FIELD_TYPE (decl))
8506     abort ();
8507
8508   if (host_integerp (DECL_SIZE (decl), 1))
8509     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
8510 }
8511
8512 /* If the compiled language is ANSI C, then add a 'prototyped'
8513    attribute, if arg types are given for the parameters of a function.  */
8514
8515 static inline void
8516 add_prototyped_attribute (die, func_type)
8517      register dw_die_ref die;
8518      register tree func_type;
8519 {
8520   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
8521       && TYPE_ARG_TYPES (func_type) != NULL)
8522     add_AT_flag (die, DW_AT_prototyped, 1);
8523 }
8524
8525 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
8526    by looking in either the type declaration or object declaration
8527    equate table.  */
8528
8529 static inline void
8530 add_abstract_origin_attribute (die, origin)
8531      register dw_die_ref die;
8532      register tree origin;
8533 {
8534   dw_die_ref origin_die = NULL;
8535
8536   if (TREE_CODE (origin) != FUNCTION_DECL)
8537     {
8538       /* We may have gotten separated from the block for the inlined
8539          function, if we're in an exception handler or some such; make
8540          sure that the abstract function has been written out.
8541
8542          Doing this for nested functions is wrong, however; functions are
8543          distinct units, and our context might not even be inline.  */
8544       tree fn = origin;
8545       if (TYPE_P (fn))
8546         fn = TYPE_STUB_DECL (fn);
8547       fn = decl_function_context (fn);
8548       if (fn)
8549         dwarf2out_abstract_function (fn);
8550     }
8551
8552   if (DECL_P (origin))
8553     origin_die = lookup_decl_die (origin);
8554   else if (TYPE_P (origin))
8555     origin_die = lookup_type_die (origin);
8556
8557   if (origin_die == NULL)
8558     abort ();
8559
8560   add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
8561 }
8562
8563 /* We do not currently support the pure_virtual attribute.  */
8564
8565 static inline void
8566 add_pure_or_virtual_attribute (die, func_decl)
8567      register dw_die_ref die;
8568      register tree func_decl;
8569 {
8570   if (DECL_VINDEX (func_decl))
8571     {
8572       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
8573
8574       if (host_integerp (DECL_VINDEX (func_decl), 0))
8575         add_AT_loc (die, DW_AT_vtable_elem_location,
8576                     new_loc_descr (DW_OP_constu,
8577                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
8578                                    0));
8579
8580       /* GNU extension: Record what type this method came from originally.  */
8581       if (debug_info_level > DINFO_LEVEL_TERSE)
8582         add_AT_die_ref (die, DW_AT_containing_type,
8583                         lookup_type_die (DECL_CONTEXT (func_decl)));
8584     }
8585 }
8586 \f
8587 /* Add source coordinate attributes for the given decl.  */
8588
8589 static void
8590 add_src_coords_attributes (die, decl)
8591      register dw_die_ref die;
8592      register tree decl;
8593 {
8594   register unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
8595
8596   add_AT_unsigned (die, DW_AT_decl_file, file_index);
8597   add_AT_unsigned (die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
8598 }
8599
8600 /* Add an DW_AT_name attribute and source coordinate attribute for the
8601    given decl, but only if it actually has a name.  */
8602
8603 static void
8604 add_name_and_src_coords_attributes (die, decl)
8605      register dw_die_ref die;
8606      register tree decl;
8607 {
8608   register tree decl_name;
8609
8610   decl_name = DECL_NAME (decl);
8611   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
8612     {
8613       add_name_attribute (die, dwarf2_name (decl, 0));
8614       if (! DECL_ARTIFICIAL (decl))
8615         add_src_coords_attributes (die, decl);
8616
8617       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
8618           && TREE_PUBLIC (decl)
8619           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
8620           && !DECL_ABSTRACT (decl))
8621         add_AT_string (die, DW_AT_MIPS_linkage_name,
8622                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
8623     }
8624 }
8625
8626 /* Push a new declaration scope.  */
8627
8628 static void
8629 push_decl_scope (scope)
8630      tree scope;
8631 {
8632   /* Make room in the decl_scope_table, if necessary.  */
8633   if (decl_scope_table_allocated == decl_scope_depth)
8634     {
8635       decl_scope_table_allocated += DECL_SCOPE_TABLE_INCREMENT;
8636       decl_scope_table
8637         = (tree *) xrealloc (decl_scope_table,
8638                              decl_scope_table_allocated * sizeof (tree));
8639     }
8640
8641   decl_scope_table[decl_scope_depth] = scope;
8642   decl_scope_depth++;
8643 }
8644
8645 /* Pop a declaration scope.  */
8646 static inline void
8647 pop_decl_scope ()
8648 {
8649   if (decl_scope_depth <= 0)
8650     abort ();
8651   --decl_scope_depth;
8652 }
8653
8654 /* Return the DIE for the scope that immediately contains this type.
8655    Non-named types get global scope.  Named types nested in other
8656    types get their containing scope if it's open, or global scope
8657    otherwise.  All other types (i.e. function-local named types) get
8658    the current active scope.  */
8659
8660 static dw_die_ref
8661 scope_die_for (t, context_die)
8662      register tree t;
8663      register dw_die_ref context_die;
8664 {
8665   register dw_die_ref scope_die = NULL;
8666   register tree containing_scope;
8667   register int i;
8668
8669   /* Non-types always go in the current scope.  */
8670   if (! TYPE_P (t))
8671     abort ();
8672
8673   containing_scope = TYPE_CONTEXT (t);
8674
8675   /* Ignore namespaces for the moment.  */
8676   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
8677     containing_scope = NULL_TREE;
8678
8679   /* Ignore function type "scopes" from the C frontend.  They mean that
8680      a tagged type is local to a parmlist of a function declarator, but
8681      that isn't useful to DWARF.  */
8682   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
8683     containing_scope = NULL_TREE;
8684
8685   if (containing_scope == NULL_TREE)
8686     scope_die = comp_unit_die;
8687   else if (TYPE_P (containing_scope))
8688     {
8689       /* For types, we can just look up the appropriate DIE.  But
8690          first we check to see if we're in the middle of emitting it
8691          so we know where the new DIE should go.  */
8692
8693       for (i = decl_scope_depth - 1; i >= 0; --i)
8694         if (decl_scope_table[i] == containing_scope)
8695           break;
8696
8697       if (i < 0)
8698         {
8699           if (debug_info_level > DINFO_LEVEL_TERSE
8700               && !TREE_ASM_WRITTEN (containing_scope))
8701             abort ();
8702
8703           /* If none of the current dies are suitable, we get file scope.  */
8704           scope_die = comp_unit_die;
8705         }
8706       else
8707         scope_die = lookup_type_die (containing_scope);
8708     }
8709   else
8710     scope_die = context_die;
8711
8712   return scope_die;
8713 }
8714
8715 /* Returns nonzero iff CONTEXT_DIE is internal to a function.  */
8716
8717 static inline int local_scope_p PARAMS ((dw_die_ref));
8718 static inline int
8719 local_scope_p (context_die)
8720      dw_die_ref context_die;
8721 {
8722   for (; context_die; context_die = context_die->die_parent)
8723     if (context_die->die_tag == DW_TAG_inlined_subroutine
8724         || context_die->die_tag == DW_TAG_subprogram)
8725       return 1;
8726   return 0;
8727 }
8728
8729 /* Returns nonzero iff CONTEXT_DIE is a class.  */
8730
8731 static inline int class_scope_p PARAMS ((dw_die_ref));
8732 static inline int
8733 class_scope_p (context_die)
8734      dw_die_ref context_die;
8735 {
8736   return (context_die
8737           && (context_die->die_tag == DW_TAG_structure_type
8738               || context_die->die_tag == DW_TAG_union_type));
8739 }
8740
8741 /* Many forms of DIEs require a "type description" attribute.  This
8742    routine locates the proper "type descriptor" die for the type given
8743    by 'type', and adds an DW_AT_type attribute below the given die.  */
8744
8745 static void
8746 add_type_attribute (object_die, type, decl_const, decl_volatile, context_die)
8747      register dw_die_ref object_die;
8748      register tree type;
8749      register int decl_const;
8750      register int decl_volatile;
8751      register dw_die_ref context_die;
8752 {
8753   register enum tree_code code  = TREE_CODE (type);
8754   register dw_die_ref type_die  = NULL;
8755
8756   /* ??? If this type is an unnamed subrange type of an integral or
8757      floating-point type, use the inner type.  This is because we have no
8758      support for unnamed types in base_type_die.  This can happen if this is
8759      an Ada subrange type.  Correct solution is emit a subrange type die.  */
8760   if ((code == INTEGER_TYPE || code == REAL_TYPE)
8761       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
8762     type = TREE_TYPE (type), code = TREE_CODE (type);
8763
8764   if (code == ERROR_MARK)
8765     return;
8766
8767   /* Handle a special case.  For functions whose return type is void, we
8768      generate *no* type attribute.  (Note that no object may have type
8769      `void', so this only applies to function return types).  */
8770   if (code == VOID_TYPE)
8771     return;
8772
8773   type_die = modified_type_die (type,
8774                                 decl_const || TYPE_READONLY (type),
8775                                 decl_volatile || TYPE_VOLATILE (type),
8776                                 context_die);
8777   if (type_die != NULL)
8778     add_AT_die_ref (object_die, DW_AT_type, type_die);
8779 }
8780
8781 /* Given a tree pointer to a struct, class, union, or enum type node, return
8782    a pointer to the (string) tag name for the given type, or zero if the type
8783    was declared without a tag.  */
8784
8785 static const char *
8786 type_tag (type)
8787      register tree type;
8788 {
8789   register const char *name = 0;
8790
8791   if (TYPE_NAME (type) != 0)
8792     {
8793       register tree t = 0;
8794
8795       /* Find the IDENTIFIER_NODE for the type name.  */
8796       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8797         t = TYPE_NAME (type);
8798
8799       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
8800          a TYPE_DECL node, regardless of whether or not a `typedef' was
8801          involved.  */
8802       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8803                && ! DECL_IGNORED_P (TYPE_NAME (type)))
8804         t = DECL_NAME (TYPE_NAME (type));
8805
8806       /* Now get the name as a string, or invent one.  */
8807       if (t != 0)
8808         name = IDENTIFIER_POINTER (t);
8809     }
8810
8811   return (name == 0 || *name == '\0') ? 0 : name;
8812 }
8813
8814 /* Return the type associated with a data member, make a special check
8815    for bit field types.  */
8816
8817 static inline tree
8818 member_declared_type (member)
8819      register tree member;
8820 {
8821   return (DECL_BIT_FIELD_TYPE (member)
8822           ? DECL_BIT_FIELD_TYPE (member)
8823           : TREE_TYPE (member));
8824 }
8825
8826 /* Get the decl's label, as described by its RTL. This may be different
8827    from the DECL_NAME name used in the source file.  */
8828
8829 #if 0
8830 static const char *
8831 decl_start_label (decl)
8832      register tree decl;
8833 {
8834   rtx x;
8835   const char *fnname;
8836   x = DECL_RTL (decl);
8837   if (GET_CODE (x) != MEM)
8838     abort ();
8839
8840   x = XEXP (x, 0);
8841   if (GET_CODE (x) != SYMBOL_REF)
8842     abort ();
8843
8844   fnname = XSTR (x, 0);
8845   return fnname;
8846 }
8847 #endif
8848 \f
8849 /* These routines generate the internal representation of the DIE's for
8850    the compilation unit.  Debugging information is collected by walking
8851    the declaration trees passed in from dwarf2out_decl().  */
8852
8853 static void
8854 gen_array_type_die (type, context_die)
8855      register tree type;
8856      register dw_die_ref context_die;
8857 {
8858   register dw_die_ref scope_die = scope_die_for (type, context_die);
8859   register dw_die_ref array_die;
8860   register tree element_type;
8861
8862   /* ??? The SGI dwarf reader fails for array of array of enum types unless
8863      the inner array type comes before the outer array type.  Thus we must
8864      call gen_type_die before we call new_die.  See below also.  */
8865 #ifdef MIPS_DEBUGGING_INFO
8866   gen_type_die (TREE_TYPE (type), context_die);
8867 #endif
8868
8869   array_die = new_die (DW_TAG_array_type, scope_die);
8870
8871 #if 0
8872   /* We default the array ordering.  SDB will probably do
8873      the right things even if DW_AT_ordering is not present.  It's not even
8874      an issue until we start to get into multidimensional arrays anyway.  If
8875      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
8876      then we'll have to put the DW_AT_ordering attribute back in.  (But if
8877      and when we find out that we need to put these in, we will only do so
8878      for multidimensional arrays.  */
8879   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
8880 #endif
8881
8882 #ifdef MIPS_DEBUGGING_INFO
8883   /* The SGI compilers handle arrays of unknown bound by setting
8884      AT_declaration and not emitting any subrange DIEs.  */
8885   if (! TYPE_DOMAIN (type))
8886     add_AT_unsigned (array_die, DW_AT_declaration, 1);
8887   else
8888 #endif
8889     add_subscript_info (array_die, type);
8890
8891   add_name_attribute (array_die, type_tag (type));
8892   equate_type_number_to_die (type, array_die);
8893
8894   /* Add representation of the type of the elements of this array type.  */
8895   element_type = TREE_TYPE (type);
8896
8897   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
8898      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
8899      We work around this by disabling this feature.  See also
8900      add_subscript_info.  */
8901 #ifndef MIPS_DEBUGGING_INFO
8902   while (TREE_CODE (element_type) == ARRAY_TYPE)
8903     element_type = TREE_TYPE (element_type);
8904
8905   gen_type_die (element_type, context_die);
8906 #endif
8907
8908   add_type_attribute (array_die, element_type, 0, 0, context_die);
8909 }
8910
8911 static void
8912 gen_set_type_die (type, context_die)
8913      register tree type;
8914      register dw_die_ref context_die;
8915 {
8916   register dw_die_ref type_die
8917     = new_die (DW_TAG_set_type, scope_die_for (type, context_die));
8918
8919   equate_type_number_to_die (type, type_die);
8920   add_type_attribute (type_die, TREE_TYPE (type), 0, 0, context_die);
8921 }
8922
8923 #if 0
8924 static void
8925 gen_entry_point_die (decl, context_die)
8926      register tree decl;
8927      register dw_die_ref context_die;
8928 {
8929   register tree origin = decl_ultimate_origin (decl);
8930   register dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die);
8931   if (origin != NULL)
8932     add_abstract_origin_attribute (decl_die, origin);
8933   else
8934     {
8935       add_name_and_src_coords_attributes (decl_die, decl);
8936       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
8937                           0, 0, context_die);
8938     }
8939
8940   if (DECL_ABSTRACT (decl))
8941     equate_decl_number_to_die (decl, decl_die);
8942   else
8943     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
8944 }
8945 #endif
8946
8947 /* Remember a type in the incomplete_types_list.  */
8948
8949 static void
8950 add_incomplete_type (type)
8951      tree type;
8952 {
8953   if (incomplete_types == incomplete_types_allocated)
8954     {
8955       incomplete_types_allocated += INCOMPLETE_TYPES_INCREMENT;
8956       incomplete_types_list
8957         = (tree *) xrealloc (incomplete_types_list,
8958                              sizeof (tree) * incomplete_types_allocated);
8959     }
8960
8961   incomplete_types_list[incomplete_types++] = type;
8962 }
8963
8964 /* Walk through the list of incomplete types again, trying once more to
8965    emit full debugging info for them.  */
8966
8967 static void
8968 retry_incomplete_types ()
8969 {
8970   register tree type;
8971
8972   while (incomplete_types)
8973     {
8974       --incomplete_types;
8975       type = incomplete_types_list[incomplete_types];
8976       gen_type_die (type, comp_unit_die);
8977     }
8978 }
8979
8980 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
8981
8982 static void
8983 gen_inlined_enumeration_type_die (type, context_die)
8984      register tree type;
8985      register dw_die_ref context_die;
8986 {
8987   register dw_die_ref type_die = new_die (DW_TAG_enumeration_type,
8988                                           context_die);
8989   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
8990      be incomplete and such types are not marked.  */
8991   add_abstract_origin_attribute (type_die, type);
8992 }
8993
8994 /* Generate a DIE to represent an inlined instance of a structure type.  */
8995
8996 static void
8997 gen_inlined_structure_type_die (type, context_die)
8998      register tree type;
8999      register dw_die_ref context_die;
9000 {
9001   register dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die);
9002
9003   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
9004      be incomplete and such types are not marked.  */
9005   add_abstract_origin_attribute (type_die, type);
9006 }
9007
9008 /* Generate a DIE to represent an inlined instance of a union type.  */
9009
9010 static void
9011 gen_inlined_union_type_die (type, context_die)
9012      register tree type;
9013      register dw_die_ref context_die;
9014 {
9015   register dw_die_ref type_die = new_die (DW_TAG_union_type, context_die);
9016
9017   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
9018      be incomplete and such types are not marked.  */
9019   add_abstract_origin_attribute (type_die, type);
9020 }
9021
9022 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
9023    include all of the information about the enumeration values also. Each
9024    enumerated type name/value is listed as a child of the enumerated type
9025    DIE.  */
9026
9027 static void
9028 gen_enumeration_type_die (type, context_die)
9029      register tree type;
9030      register dw_die_ref context_die;
9031 {
9032   register dw_die_ref type_die = lookup_type_die (type);
9033
9034   if (type_die == NULL)
9035     {
9036       type_die = new_die (DW_TAG_enumeration_type,
9037                           scope_die_for (type, context_die));
9038       equate_type_number_to_die (type, type_die);
9039       add_name_attribute (type_die, type_tag (type));
9040     }
9041   else if (! TYPE_SIZE (type))
9042     return;
9043   else
9044     remove_AT (type_die, DW_AT_declaration);
9045
9046   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
9047      given enum type is incomplete, do not generate the DW_AT_byte_size
9048      attribute or the DW_AT_element_list attribute.  */
9049   if (TYPE_SIZE (type))
9050     {
9051       register tree link;
9052
9053       TREE_ASM_WRITTEN (type) = 1;
9054       add_byte_size_attribute (type_die, type);
9055       if (TYPE_STUB_DECL (type) != NULL_TREE)
9056         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
9057
9058       /* If the first reference to this type was as the return type of an
9059          inline function, then it may not have a parent.  Fix this now.  */
9060       if (type_die->die_parent == NULL)
9061         add_child_die (scope_die_for (type, context_die), type_die);
9062
9063       for (link = TYPE_FIELDS (type);
9064            link != NULL; link = TREE_CHAIN (link))
9065         {
9066           register dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die);
9067
9068           add_name_attribute (enum_die,
9069                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
9070
9071           if (host_integerp (TREE_VALUE (link), 0))
9072             {
9073               if (tree_int_cst_sgn (TREE_VALUE (link)) < 0)
9074                 add_AT_int (enum_die, DW_AT_const_value,
9075                             tree_low_cst (TREE_VALUE (link), 0));
9076               else
9077                 add_AT_unsigned (enum_die, DW_AT_const_value,
9078                                  tree_low_cst (TREE_VALUE (link), 0));
9079             }
9080         }
9081     }
9082   else
9083     add_AT_flag (type_die, DW_AT_declaration, 1);
9084 }
9085
9086 /* Generate a DIE to represent either a real live formal parameter decl or to
9087    represent just the type of some formal parameter position in some function
9088    type.
9089
9090    Note that this routine is a bit unusual because its argument may be a
9091    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
9092    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
9093    node.  If it's the former then this function is being called to output a
9094    DIE to represent a formal parameter object (or some inlining thereof).  If
9095    it's the latter, then this function is only being called to output a
9096    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
9097    argument type of some subprogram type.  */
9098
9099 static dw_die_ref
9100 gen_formal_parameter_die (node, context_die)
9101      register tree node;
9102      register dw_die_ref context_die;
9103 {
9104   register dw_die_ref parm_die
9105     = new_die (DW_TAG_formal_parameter, context_die);
9106   register tree origin;
9107
9108   switch (TREE_CODE_CLASS (TREE_CODE (node)))
9109     {
9110     case 'd':
9111       origin = decl_ultimate_origin (node);
9112       if (origin != NULL)
9113         add_abstract_origin_attribute (parm_die, origin);
9114       else
9115         {
9116           add_name_and_src_coords_attributes (parm_die, node);
9117           add_type_attribute (parm_die, TREE_TYPE (node),
9118                               TREE_READONLY (node),
9119                               TREE_THIS_VOLATILE (node),
9120                               context_die);
9121           if (DECL_ARTIFICIAL (node))
9122             add_AT_flag (parm_die, DW_AT_artificial, 1);
9123         }
9124
9125       equate_decl_number_to_die (node, parm_die);
9126       if (! DECL_ABSTRACT (node))
9127         add_location_or_const_value_attribute (parm_die, node);
9128
9129       break;
9130
9131     case 't':
9132       /* We were called with some kind of a ..._TYPE node.  */
9133       add_type_attribute (parm_die, node, 0, 0, context_die);
9134       break;
9135
9136     default:
9137       abort ();
9138     }
9139
9140   return parm_die;
9141 }
9142
9143 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
9144    at the end of an (ANSI prototyped) formal parameters list.  */
9145
9146 static void
9147 gen_unspecified_parameters_die (decl_or_type, context_die)
9148      register tree decl_or_type ATTRIBUTE_UNUSED;
9149      register dw_die_ref context_die;
9150 {
9151   new_die (DW_TAG_unspecified_parameters, context_die);
9152 }
9153
9154 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
9155    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
9156    parameters as specified in some function type specification (except for
9157    those which appear as part of a function *definition*).  */
9158
9159 static void
9160 gen_formal_types_die (function_or_method_type, context_die)
9161      register tree function_or_method_type;
9162      register dw_die_ref context_die;
9163 {
9164   register tree link;
9165   register tree formal_type = NULL;
9166   register tree first_parm_type;
9167   tree arg;
9168
9169   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
9170     {
9171       arg = DECL_ARGUMENTS (function_or_method_type);
9172       function_or_method_type = TREE_TYPE (function_or_method_type);
9173     }
9174   else
9175     arg = NULL_TREE;
9176   
9177   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
9178
9179   /* Make our first pass over the list of formal parameter types and output a
9180      DW_TAG_formal_parameter DIE for each one.  */
9181   for (link = first_parm_type; link; )
9182     {
9183       register dw_die_ref parm_die;
9184
9185       formal_type = TREE_VALUE (link);
9186       if (formal_type == void_type_node)
9187         break;
9188
9189       /* Output a (nameless) DIE to represent the formal parameter itself.  */
9190       parm_die = gen_formal_parameter_die (formal_type, context_die);
9191       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
9192            && link == first_parm_type)
9193           || (arg && DECL_ARTIFICIAL (arg)))
9194         add_AT_flag (parm_die, DW_AT_artificial, 1);
9195
9196       link = TREE_CHAIN (link);
9197       if (arg)
9198         arg = TREE_CHAIN (arg);
9199     }
9200
9201   /* If this function type has an ellipsis, add a
9202      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
9203   if (formal_type != void_type_node)
9204     gen_unspecified_parameters_die (function_or_method_type, context_die);
9205
9206   /* Make our second (and final) pass over the list of formal parameter types
9207      and output DIEs to represent those types (as necessary).  */
9208   for (link = TYPE_ARG_TYPES (function_or_method_type);
9209        link;
9210        link = TREE_CHAIN (link))
9211     {
9212       formal_type = TREE_VALUE (link);
9213       if (formal_type == void_type_node)
9214         break;
9215
9216       gen_type_die (formal_type, context_die);
9217     }
9218 }
9219
9220 /* We want to generate the DIE for TYPE so that we can generate the
9221    die for MEMBER, which has been defined; we will need to refer back
9222    to the member declaration nested within TYPE.  If we're trying to
9223    generate minimal debug info for TYPE, processing TYPE won't do the
9224    trick; we need to attach the member declaration by hand.  */
9225
9226 static void
9227 gen_type_die_for_member (type, member, context_die)
9228      tree type, member;
9229      dw_die_ref context_die;
9230 {
9231   gen_type_die (type, context_die);
9232
9233   /* If we're trying to avoid duplicate debug info, we may not have
9234      emitted the member decl for this function.  Emit it now.  */
9235   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
9236       && ! lookup_decl_die (member))
9237     {
9238       if (decl_ultimate_origin (member))
9239         abort ();
9240
9241       push_decl_scope (type);
9242       if (TREE_CODE (member) == FUNCTION_DECL)
9243         gen_subprogram_die (member, lookup_type_die (type));
9244       else
9245         gen_variable_die (member, lookup_type_die (type));
9246       pop_decl_scope ();
9247     }
9248 }
9249
9250 /* Generate the DWARF2 info for the "abstract" instance
9251    of a function which we may later generate inlined and/or
9252    out-of-line instances of.  */
9253
9254 void
9255 dwarf2out_abstract_function (decl)
9256      tree decl;
9257 {
9258   register dw_die_ref old_die;
9259   tree save_fn;
9260   tree context;
9261   int was_abstract = DECL_ABSTRACT (decl);
9262
9263   /* Make sure we have the actual abstract inline, not a clone.  */
9264   decl = DECL_ORIGIN (decl);
9265
9266   old_die = lookup_decl_die (decl);  
9267   if (old_die && get_AT_unsigned (old_die, DW_AT_inline))
9268     /* We've already generated the abstract instance.  */
9269     return;
9270
9271   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
9272      we don't get confused by DECL_ABSTRACT.  */
9273   context = decl_class_context (decl);
9274   if (context)
9275     gen_type_die_for_member
9276       (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
9277  
9278   /* Pretend we've just finished compiling this function.  */
9279   save_fn = current_function_decl;
9280   current_function_decl = decl;
9281
9282   set_decl_abstract_flags (decl, 1);
9283   dwarf2out_decl (decl);
9284   if (! was_abstract)
9285     set_decl_abstract_flags (decl, 0);
9286
9287   current_function_decl = save_fn;
9288 }
9289
9290 /* Generate a DIE to represent a declared function (either file-scope or
9291    block-local).  */
9292
9293 static void
9294 gen_subprogram_die (decl, context_die)
9295      register tree decl;
9296      register dw_die_ref context_die;
9297 {
9298   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
9299   register tree origin = decl_ultimate_origin (decl);
9300   register dw_die_ref subr_die;
9301   register rtx fp_reg;
9302   register tree fn_arg_types;
9303   register tree outer_scope;
9304   register dw_die_ref old_die = lookup_decl_die (decl);
9305   register int declaration = (current_function_decl != decl
9306                               || class_scope_p (context_die));
9307
9308   /* Note that it is possible to have both DECL_ABSTRACT and `declaration'
9309      be true, if we started to generate the abstract instance of an inline,
9310      decided to output its containing class, and proceeded to emit the
9311      declaration of the inline from the member list for the class.  In that
9312      case, `declaration' takes priority; we'll get back to the abstract
9313      instance when we're done with the class.  */
9314
9315   /* The class-scope declaration DIE must be the primary DIE.  */
9316   if (origin && declaration && class_scope_p (context_die))
9317     {
9318       origin = NULL;
9319       if (old_die)
9320         abort ();
9321     }
9322
9323   if (origin != NULL)
9324     {
9325       if (declaration && ! local_scope_p (context_die))
9326         abort ();
9327
9328       /* Fixup die_parent for the abstract instance of a nested
9329          inline function.  */
9330       if (old_die && old_die->die_parent == NULL)
9331         add_child_die (context_die, old_die);
9332
9333       subr_die = new_die (DW_TAG_subprogram, context_die);
9334       add_abstract_origin_attribute (subr_die, origin);
9335     }
9336   else if (old_die)
9337     {
9338       unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
9339
9340       if (!get_AT_flag (old_die, DW_AT_declaration)
9341           /* We can have a normal definition following an inline one in the
9342              case of redefinition of GNU C extern inlines.
9343              It seems reasonable to use AT_specification in this case.  */
9344           && !get_AT_unsigned (old_die, DW_AT_inline))
9345         {
9346           /* ??? This can happen if there is a bug in the program, for
9347              instance, if it has duplicate function definitions.  Ideally,
9348              we should detect this case and ignore it.  For now, if we have
9349              already reported an error, any error at all, then assume that
9350              we got here because of a input error, not a dwarf2 bug.  */
9351           if (errorcount)
9352             return;
9353           abort ();
9354         }
9355
9356       /* If the definition comes from the same place as the declaration,
9357          maybe use the old DIE.  We always want the DIE for this function
9358          that has the *_pc attributes to be under comp_unit_die so the
9359          debugger can find it.  We also need to do this for abstract
9360          instances of inlines, since the spec requires the out-of-line copy
9361          to have the same parent.  For local class methods, this doesn't
9362          apply; we just use the old DIE.  */
9363       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
9364           && (DECL_ARTIFICIAL (decl)
9365               || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
9366                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
9367                       == (unsigned) DECL_SOURCE_LINE (decl)))))
9368         {
9369           subr_die = old_die;
9370
9371           /* Clear out the declaration attribute and the parm types.  */
9372           remove_AT (subr_die, DW_AT_declaration);
9373           remove_children (subr_die);
9374         }
9375       else
9376         {
9377           subr_die = new_die (DW_TAG_subprogram, context_die);
9378           add_AT_die_ref (subr_die, DW_AT_specification, old_die);
9379           if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
9380             add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
9381           if (get_AT_unsigned (old_die, DW_AT_decl_line)
9382               != (unsigned) DECL_SOURCE_LINE (decl))
9383             add_AT_unsigned
9384               (subr_die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
9385         }
9386     }
9387   else
9388     {
9389       subr_die = new_die (DW_TAG_subprogram, context_die);
9390
9391       if (TREE_PUBLIC (decl))
9392         add_AT_flag (subr_die, DW_AT_external, 1);
9393
9394       add_name_and_src_coords_attributes (subr_die, decl);
9395       if (debug_info_level > DINFO_LEVEL_TERSE)
9396         {
9397           register tree type = TREE_TYPE (decl);
9398
9399           add_prototyped_attribute (subr_die, type);
9400           add_type_attribute (subr_die, TREE_TYPE (type), 0, 0, context_die);
9401         }
9402
9403       add_pure_or_virtual_attribute (subr_die, decl);
9404       if (DECL_ARTIFICIAL (decl))
9405         add_AT_flag (subr_die, DW_AT_artificial, 1);
9406       if (TREE_PROTECTED (decl))
9407         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
9408       else if (TREE_PRIVATE (decl))
9409         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
9410     }
9411
9412   if (declaration)
9413     {
9414       if (!(old_die && get_AT_unsigned (old_die, DW_AT_inline)))
9415         {
9416           add_AT_flag (subr_die, DW_AT_declaration, 1);
9417
9418           /* The first time we see a member function, it is in the context of
9419              the class to which it belongs.  We make sure of this by emitting
9420              the class first.  The next time is the definition, which is
9421              handled above.  The two may come from the same source text.  */
9422           if (DECL_CONTEXT (decl) || DECL_ABSTRACT (decl))
9423             equate_decl_number_to_die (decl, subr_die);
9424         }
9425     }
9426   else if (DECL_ABSTRACT (decl))
9427     {
9428       if (DECL_INLINE (decl) && !flag_no_inline)
9429         {
9430           /* ??? Checking DECL_DEFER_OUTPUT is correct for static
9431              inline functions, but not for extern inline functions.
9432              We can't get this completely correct because information
9433              about whether the function was declared inline is not
9434              saved anywhere.  */
9435           if (DECL_DEFER_OUTPUT (decl))
9436             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
9437           else
9438             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
9439         }
9440       else
9441         add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
9442
9443       equate_decl_number_to_die (decl, subr_die);
9444     }
9445   else if (!DECL_EXTERNAL (decl))
9446     {
9447       if (!(old_die && get_AT_unsigned (old_die, DW_AT_inline)))
9448         equate_decl_number_to_die (decl, subr_die);
9449
9450       ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
9451                                    current_funcdef_number);
9452       add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
9453       ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
9454                                    current_funcdef_number);
9455       add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
9456
9457       add_pubname (decl, subr_die);
9458       add_arange (decl, subr_die);
9459
9460 #ifdef MIPS_DEBUGGING_INFO
9461       /* Add a reference to the FDE for this routine.  */
9462       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
9463 #endif
9464
9465       /* Define the "frame base" location for this routine.  We use the
9466          frame pointer or stack pointer registers, since the RTL for local
9467          variables is relative to one of them.  */
9468       fp_reg
9469         = frame_pointer_needed ? hard_frame_pointer_rtx : stack_pointer_rtx;
9470       add_AT_loc (subr_die, DW_AT_frame_base, reg_loc_descriptor (fp_reg));
9471
9472 #if 0
9473       /* ??? This fails for nested inline functions, because context_display
9474          is not part of the state saved/restored for inline functions.  */
9475       if (current_function_needs_context)
9476         add_AT_location_description (subr_die, DW_AT_static_link,
9477                                      lookup_static_chain (decl));
9478 #endif
9479     }
9480
9481   /* Now output descriptions of the arguments for this function. This gets
9482      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
9483      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
9484      `...' at the end of the formal parameter list.  In order to find out if
9485      there was a trailing ellipsis or not, we must instead look at the type
9486      associated with the FUNCTION_DECL.  This will be a node of type
9487      FUNCTION_TYPE. If the chain of type nodes hanging off of this
9488      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
9489      an ellipsis at the end.  */
9490
9491   /* In the case where we are describing a mere function declaration, all we
9492      need to do here (and all we *can* do here) is to describe the *types* of
9493      its formal parameters.  */
9494   if (debug_info_level <= DINFO_LEVEL_TERSE)
9495     ;
9496   else if (declaration)
9497     gen_formal_types_die (decl, subr_die);
9498   else
9499     {
9500       /* Generate DIEs to represent all known formal parameters */
9501       register tree arg_decls = DECL_ARGUMENTS (decl);
9502       register tree parm;
9503
9504       /* When generating DIEs, generate the unspecified_parameters DIE
9505          instead if we come across the arg "__builtin_va_alist" */
9506       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
9507         if (TREE_CODE (parm) == PARM_DECL)
9508           {
9509             if (DECL_NAME (parm)
9510                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
9511                             "__builtin_va_alist"))
9512               gen_unspecified_parameters_die (parm, subr_die);
9513             else
9514               gen_decl_die (parm, subr_die);
9515           }
9516
9517       /* Decide whether we need a unspecified_parameters DIE at the end.
9518          There are 2 more cases to do this for: 1) the ansi ... declaration -
9519          this is detectable when the end of the arg list is not a
9520          void_type_node 2) an unprototyped function declaration (not a
9521          definition).  This just means that we have no info about the
9522          parameters at all.  */
9523       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
9524       if (fn_arg_types != NULL)
9525         {
9526           /* this is the prototyped case, check for ...  */
9527           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
9528             gen_unspecified_parameters_die (decl, subr_die);
9529         }
9530       else if (DECL_INITIAL (decl) == NULL_TREE)
9531         gen_unspecified_parameters_die (decl, subr_die);
9532     }
9533
9534   /* Output Dwarf info for all of the stuff within the body of the function
9535      (if it has one - it may be just a declaration).  */
9536   outer_scope = DECL_INITIAL (decl);
9537
9538   /* Note that here, `outer_scope' is a pointer to the outermost BLOCK
9539      node created to represent a function. This outermost BLOCK actually
9540      represents the outermost binding contour for the function, i.e. the
9541      contour in which the function's formal parameters and labels get
9542      declared. Curiously, it appears that the front end doesn't actually
9543      put the PARM_DECL nodes for the current function onto the BLOCK_VARS
9544      list for this outer scope.  (They are strung off of the DECL_ARGUMENTS
9545      list for the function instead.) The BLOCK_VARS list for the
9546      `outer_scope' does provide us with a list of the LABEL_DECL nodes for
9547      the function however, and we output DWARF info for those in
9548      decls_for_scope.  Just within the `outer_scope' there will be a BLOCK
9549      node representing the function's outermost pair of curly braces, and
9550      any blocks used for the base and member initializers of a C++
9551      constructor function.  */
9552   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
9553     {
9554       current_function_has_inlines = 0;
9555       decls_for_scope (outer_scope, subr_die, 0);
9556
9557 #if 0 && defined (MIPS_DEBUGGING_INFO)
9558       if (current_function_has_inlines)
9559         {
9560           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
9561           if (! comp_unit_has_inlines)
9562             {
9563               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
9564               comp_unit_has_inlines = 1;
9565             }
9566         }
9567 #endif
9568     }
9569 }
9570
9571 /* Generate a DIE to represent a declared data object.  */
9572
9573 static void
9574 gen_variable_die (decl, context_die)
9575      register tree decl;
9576      register dw_die_ref context_die;
9577 {
9578   register tree origin = decl_ultimate_origin (decl);
9579   register dw_die_ref var_die = new_die (DW_TAG_variable, context_die);
9580
9581   dw_die_ref old_die = lookup_decl_die (decl);
9582   int declaration = (DECL_EXTERNAL (decl)
9583                      || class_scope_p (context_die));
9584
9585   if (origin != NULL)
9586     add_abstract_origin_attribute (var_die, origin);
9587   /* Loop unrolling can create multiple blocks that refer to the same
9588      static variable, so we must test for the DW_AT_declaration flag.  */
9589   /* ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
9590      copy decls and set the DECL_ABSTRACT flag on them instead of
9591      sharing them.  */
9592   else if (old_die && TREE_STATIC (decl)
9593            && get_AT_flag (old_die, DW_AT_declaration) == 1)
9594     {
9595       /* This is a definition of a C++ class level static.  */
9596       add_AT_die_ref (var_die, DW_AT_specification, old_die);
9597       if (DECL_NAME (decl))
9598         {
9599           unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
9600
9601           if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
9602             add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
9603
9604           if (get_AT_unsigned (old_die, DW_AT_decl_line)
9605               != (unsigned) DECL_SOURCE_LINE (decl))
9606
9607             add_AT_unsigned (var_die, DW_AT_decl_line,
9608                              DECL_SOURCE_LINE (decl));
9609         }
9610     }
9611   else
9612     {
9613       add_name_and_src_coords_attributes (var_die, decl);
9614       add_type_attribute (var_die, TREE_TYPE (decl),
9615                           TREE_READONLY (decl),
9616                           TREE_THIS_VOLATILE (decl), context_die);
9617
9618       if (TREE_PUBLIC (decl))
9619         add_AT_flag (var_die, DW_AT_external, 1);
9620
9621       if (DECL_ARTIFICIAL (decl))
9622         add_AT_flag (var_die, DW_AT_artificial, 1);
9623
9624       if (TREE_PROTECTED (decl))
9625         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
9626
9627       else if (TREE_PRIVATE (decl))
9628         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
9629     }
9630
9631   if (declaration)
9632     add_AT_flag (var_die, DW_AT_declaration, 1);
9633
9634   if (class_scope_p (context_die) || DECL_ABSTRACT (decl))
9635     equate_decl_number_to_die (decl, var_die);
9636
9637   if (! declaration && ! DECL_ABSTRACT (decl))
9638     {
9639       add_location_or_const_value_attribute (var_die, decl);
9640       add_pubname (decl, var_die);
9641     }
9642   else
9643     tree_add_const_value_attribute (var_die, decl);
9644 }
9645
9646 /* Generate a DIE to represent a label identifier.  */
9647
9648 static void
9649 gen_label_die (decl, context_die)
9650      register tree decl;
9651      register dw_die_ref context_die;
9652 {
9653   register tree origin = decl_ultimate_origin (decl);
9654   register dw_die_ref lbl_die = new_die (DW_TAG_label, context_die);
9655   register rtx insn;
9656   char label[MAX_ARTIFICIAL_LABEL_BYTES];
9657
9658   if (origin != NULL)
9659     add_abstract_origin_attribute (lbl_die, origin);
9660   else
9661     add_name_and_src_coords_attributes (lbl_die, decl);
9662
9663   if (DECL_ABSTRACT (decl))
9664     equate_decl_number_to_die (decl, lbl_die);
9665   else
9666     {
9667       insn = DECL_RTL (decl);
9668
9669       /* Deleted labels are programmer specified labels which have been
9670          eliminated because of various optimisations.  We still emit them
9671          here so that it is possible to put breakpoints on them.  */
9672       if (GET_CODE (insn) == CODE_LABEL
9673           || ((GET_CODE (insn) == NOTE
9674                && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL)))
9675         {
9676           /* When optimization is enabled (via -O) some parts of the compiler
9677              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
9678              represent source-level labels which were explicitly declared by
9679              the user.  This really shouldn't be happening though, so catch
9680              it if it ever does happen.  */
9681           if (INSN_DELETED_P (insn))
9682             abort ();
9683
9684           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
9685           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
9686         }
9687     }
9688 }
9689
9690 /* Generate a DIE for a lexical block.  */
9691
9692 static void
9693 gen_lexical_block_die (stmt, context_die, depth)
9694      register tree stmt;
9695      register dw_die_ref context_die;
9696      int depth;
9697 {
9698   register dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die);
9699   char label[MAX_ARTIFICIAL_LABEL_BYTES];
9700
9701   if (! BLOCK_ABSTRACT (stmt))
9702     {
9703       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
9704                                    BLOCK_NUMBER (stmt));
9705       add_AT_lbl_id (stmt_die, DW_AT_low_pc, label);
9706       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
9707                                    BLOCK_NUMBER (stmt));
9708       add_AT_lbl_id (stmt_die, DW_AT_high_pc, label);
9709     }
9710
9711   decls_for_scope (stmt, stmt_die, depth);
9712 }
9713
9714 /* Generate a DIE for an inlined subprogram.  */
9715
9716 static void
9717 gen_inlined_subroutine_die (stmt, context_die, depth)
9718      register tree stmt;
9719      register dw_die_ref context_die;
9720      int depth;
9721 {
9722   if (! BLOCK_ABSTRACT (stmt))
9723     {
9724       register dw_die_ref subr_die
9725         = new_die (DW_TAG_inlined_subroutine, context_die);
9726       register tree decl = block_ultimate_origin (stmt);
9727       char label[MAX_ARTIFICIAL_LABEL_BYTES];
9728
9729       /* Emit info for the abstract instance first, if we haven't yet.  */
9730       dwarf2out_abstract_function (decl);
9731
9732       add_abstract_origin_attribute (subr_die, decl);
9733       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
9734                                    BLOCK_NUMBER (stmt));
9735       add_AT_lbl_id (subr_die, DW_AT_low_pc, label);
9736       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
9737                                    BLOCK_NUMBER (stmt));
9738       add_AT_lbl_id (subr_die, DW_AT_high_pc, label);
9739       decls_for_scope (stmt, subr_die, depth);
9740       current_function_has_inlines = 1;
9741     }
9742 }
9743
9744 /* Generate a DIE for a field in a record, or structure.  */
9745
9746 static void
9747 gen_field_die (decl, context_die)
9748      register tree decl;
9749      register dw_die_ref context_die;
9750 {
9751   register dw_die_ref decl_die = new_die (DW_TAG_member, context_die);
9752
9753   add_name_and_src_coords_attributes (decl_die, decl);
9754   add_type_attribute (decl_die, member_declared_type (decl),
9755                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
9756                       context_die);
9757
9758   /* If this is a bit field...  */
9759   if (DECL_BIT_FIELD_TYPE (decl))
9760     {
9761       add_byte_size_attribute (decl_die, decl);
9762       add_bit_size_attribute (decl_die, decl);
9763       add_bit_offset_attribute (decl_die, decl);
9764     }
9765
9766   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
9767     add_data_member_location_attribute (decl_die, decl);
9768
9769   if (DECL_ARTIFICIAL (decl))
9770     add_AT_flag (decl_die, DW_AT_artificial, 1);
9771
9772   if (TREE_PROTECTED (decl))
9773     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
9774
9775   else if (TREE_PRIVATE (decl))
9776     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
9777 }
9778
9779 #if 0
9780 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
9781    Use modified_type_die instead.
9782    We keep this code here just in case these types of DIEs may be needed to
9783    represent certain things in other languages (e.g. Pascal) someday.  */
9784 static void
9785 gen_pointer_type_die (type, context_die)
9786      register tree type;
9787      register dw_die_ref context_die;
9788 {
9789   register dw_die_ref ptr_die
9790     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die));
9791
9792   equate_type_number_to_die (type, ptr_die);
9793   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
9794   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
9795 }
9796
9797 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
9798    Use modified_type_die instead.
9799    We keep this code here just in case these types of DIEs may be needed to
9800    represent certain things in other languages (e.g. Pascal) someday.  */
9801 static void
9802 gen_reference_type_die (type, context_die)
9803      register tree type;
9804      register dw_die_ref context_die;
9805 {
9806   register dw_die_ref ref_die
9807     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die));
9808
9809   equate_type_number_to_die (type, ref_die);
9810   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
9811   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
9812 }
9813 #endif
9814
9815 /* Generate a DIE for a pointer to a member type.  */
9816 static void
9817 gen_ptr_to_mbr_type_die (type, context_die)
9818      register tree type;
9819      register dw_die_ref context_die;
9820 {
9821   register dw_die_ref ptr_die
9822     = new_die (DW_TAG_ptr_to_member_type, scope_die_for (type, context_die));
9823
9824   equate_type_number_to_die (type, ptr_die);
9825   add_AT_die_ref (ptr_die, DW_AT_containing_type,
9826                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
9827   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
9828 }
9829
9830 /* Generate the DIE for the compilation unit.  */
9831
9832 static dw_die_ref
9833 gen_compile_unit_die (filename)
9834      register const char *filename;
9835 {
9836   register dw_die_ref die;
9837   char producer[250];
9838   const char *wd = getpwd ();
9839   int language;
9840
9841   die = new_die (DW_TAG_compile_unit, NULL);
9842   add_name_attribute (die, filename);
9843
9844   if (wd != NULL && filename[0] != DIR_SEPARATOR)
9845     add_AT_string (die, DW_AT_comp_dir, wd);
9846
9847   sprintf (producer, "%s %s", language_string, version_string);
9848
9849 #ifdef MIPS_DEBUGGING_INFO
9850   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
9851      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
9852      not appear in the producer string, the debugger reaches the conclusion
9853      that the object file is stripped and has no debugging information.
9854      To get the MIPS/SGI debugger to believe that there is debugging
9855      information in the object file, we add a -g to the producer string.  */
9856   if (debug_info_level > DINFO_LEVEL_TERSE)
9857     strcat (producer, " -g");
9858 #endif
9859
9860   add_AT_string (die, DW_AT_producer, producer);
9861
9862   if (strcmp (language_string, "GNU C++") == 0)
9863     language = DW_LANG_C_plus_plus;
9864   else if (strcmp (language_string, "GNU Ada") == 0)
9865     language = DW_LANG_Ada83;
9866   else if (strcmp (language_string, "GNU F77") == 0)
9867     language = DW_LANG_Fortran77;
9868   else if (strcmp (language_string, "GNU Pascal") == 0)
9869     language = DW_LANG_Pascal83;
9870   else if (strcmp (language_string, "GNU Java") == 0)
9871     language = DW_LANG_Java;
9872   else if (flag_traditional)
9873     language = DW_LANG_C;
9874   else
9875     language = DW_LANG_C89;
9876
9877   add_AT_unsigned (die, DW_AT_language, language);
9878
9879   return die;
9880 }
9881
9882 /* Generate a DIE for a string type.  */
9883
9884 static void
9885 gen_string_type_die (type, context_die)
9886      register tree type;
9887      register dw_die_ref context_die;
9888 {
9889   register dw_die_ref type_die
9890     = new_die (DW_TAG_string_type, scope_die_for (type, context_die));
9891
9892   equate_type_number_to_die (type, type_die);
9893
9894   /* Fudge the string length attribute for now.  */
9895
9896   /* TODO: add string length info.
9897    string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
9898                               bound_representation (upper_bound, 0, 'u'); */
9899 }
9900
9901 /* Generate the DIE for a base class.  */
9902
9903 static void
9904 gen_inheritance_die (binfo, context_die)
9905      register tree binfo;
9906      register dw_die_ref context_die;
9907 {
9908   dw_die_ref die = new_die (DW_TAG_inheritance, context_die);
9909
9910   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
9911   add_data_member_location_attribute (die, binfo);
9912
9913   if (TREE_VIA_VIRTUAL (binfo))
9914     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
9915   if (TREE_VIA_PUBLIC (binfo))
9916     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
9917   else if (TREE_VIA_PROTECTED (binfo))
9918     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
9919 }
9920
9921 /* Generate a DIE for a class member.  */
9922
9923 static void
9924 gen_member_die (type, context_die)
9925      register tree type;
9926      register dw_die_ref context_die;
9927 {
9928   register tree member;
9929   dw_die_ref child;
9930
9931   /* If this is not an incomplete type, output descriptions of each of its
9932      members. Note that as we output the DIEs necessary to represent the
9933      members of this record or union type, we will also be trying to output
9934      DIEs to represent the *types* of those members. However the `type'
9935      function (above) will specifically avoid generating type DIEs for member
9936      types *within* the list of member DIEs for this (containing) type execpt
9937      for those types (of members) which are explicitly marked as also being
9938      members of this (containing) type themselves.  The g++ front- end can
9939      force any given type to be treated as a member of some other
9940      (containing) type by setting the TYPE_CONTEXT of the given (member) type
9941      to point to the TREE node representing the appropriate (containing)
9942      type.  */
9943
9944   /* First output info about the base classes.  */
9945   if (TYPE_BINFO (type) && TYPE_BINFO_BASETYPES (type))
9946     {
9947       register tree bases = TYPE_BINFO_BASETYPES (type);
9948       register int n_bases = TREE_VEC_LENGTH (bases);
9949       register int i;
9950
9951       for (i = 0; i < n_bases; i++)
9952         gen_inheritance_die (TREE_VEC_ELT (bases, i), context_die);
9953     }
9954
9955   /* Now output info about the data members and type members.  */
9956   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
9957     {
9958       /* If we thought we were generating minimal debug info for TYPE
9959          and then changed our minds, some of the member declarations
9960          may have already been defined.  Don't define them again, but
9961          do put them in the right order.  */
9962
9963       child = lookup_decl_die (member);
9964       if (child)
9965         splice_child_die (context_die, child);
9966       else
9967         gen_decl_die (member, context_die);
9968     }
9969
9970   /* Now output info about the function members (if any).  */
9971   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
9972     {
9973       /* Don't include clones in the member list.  */
9974       if (DECL_ABSTRACT_ORIGIN (member))
9975         continue;
9976
9977       child = lookup_decl_die (member);
9978       if (child)
9979         splice_child_die (context_die, child);
9980       else
9981         gen_decl_die (member, context_die);
9982     }
9983 }
9984
9985 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
9986    is set, we pretend that the type was never defined, so we only get the
9987    member DIEs needed by later specification DIEs.  */
9988
9989 static void
9990 gen_struct_or_union_type_die (type, context_die)
9991      register tree type;
9992      register dw_die_ref context_die;
9993 {
9994   register dw_die_ref type_die = lookup_type_die (type);
9995   register dw_die_ref scope_die = 0;
9996   register int nested = 0;
9997   int complete = (TYPE_SIZE (type)
9998                   && (! TYPE_STUB_DECL (type)
9999                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
10000
10001   if (type_die && ! complete)
10002     return;
10003
10004   if (TYPE_CONTEXT (type) != NULL_TREE
10005       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type)))
10006     nested = 1;
10007
10008   scope_die = scope_die_for (type, context_die);
10009
10010   if (! type_die || (nested && scope_die == comp_unit_die))
10011     /* First occurrence of type or toplevel definition of nested class.  */
10012     {
10013       register dw_die_ref old_die = type_die;
10014
10015       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
10016                           ? DW_TAG_structure_type : DW_TAG_union_type,
10017                           scope_die);
10018       equate_type_number_to_die (type, type_die);
10019       if (old_die)
10020         add_AT_die_ref (type_die, DW_AT_specification, old_die);
10021       else
10022         add_name_attribute (type_die, type_tag (type));
10023     }
10024   else
10025     remove_AT (type_die, DW_AT_declaration);
10026
10027   /* If this type has been completed, then give it a byte_size attribute and
10028      then give a list of members.  */
10029   if (complete)
10030     {
10031       /* Prevent infinite recursion in cases where the type of some member of
10032          this type is expressed in terms of this type itself.  */
10033       TREE_ASM_WRITTEN (type) = 1;
10034       add_byte_size_attribute (type_die, type);
10035       if (TYPE_STUB_DECL (type) != NULL_TREE)
10036         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
10037
10038       /* If the first reference to this type was as the return type of an
10039          inline function, then it may not have a parent.  Fix this now.  */
10040       if (type_die->die_parent == NULL)
10041         add_child_die (scope_die, type_die);
10042
10043       push_decl_scope (type);
10044       gen_member_die (type, type_die);
10045       pop_decl_scope ();
10046
10047       /* GNU extension: Record what type our vtable lives in.  */
10048       if (TYPE_VFIELD (type))
10049         {
10050           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
10051
10052           gen_type_die (vtype, context_die);
10053           add_AT_die_ref (type_die, DW_AT_containing_type,
10054                           lookup_type_die (vtype));
10055         }
10056     }
10057   else
10058     {
10059       add_AT_flag (type_die, DW_AT_declaration, 1);
10060
10061       /* We don't need to do this for function-local types.  */
10062       if (! decl_function_context (TYPE_STUB_DECL (type)))
10063         add_incomplete_type (type);
10064     }
10065 }
10066
10067 /* Generate a DIE for a subroutine _type_.  */
10068
10069 static void
10070 gen_subroutine_type_die (type, context_die)
10071      register tree type;
10072      register dw_die_ref context_die;
10073 {
10074   register tree return_type = TREE_TYPE (type);
10075   register dw_die_ref subr_die
10076     = new_die (DW_TAG_subroutine_type, scope_die_for (type, context_die));
10077
10078   equate_type_number_to_die (type, subr_die);
10079   add_prototyped_attribute (subr_die, type);
10080   add_type_attribute (subr_die, return_type, 0, 0, context_die);
10081   gen_formal_types_die (type, subr_die);
10082 }
10083
10084 /* Generate a DIE for a type definition */
10085
10086 static void
10087 gen_typedef_die (decl, context_die)
10088      register tree decl;
10089      register dw_die_ref context_die;
10090 {
10091   register dw_die_ref type_die;
10092   register tree origin;
10093
10094   if (TREE_ASM_WRITTEN (decl))
10095     return;
10096   TREE_ASM_WRITTEN (decl) = 1;
10097
10098   type_die = new_die (DW_TAG_typedef, context_die);
10099   origin = decl_ultimate_origin (decl);
10100   if (origin != NULL)
10101     add_abstract_origin_attribute (type_die, origin);
10102   else
10103     {
10104       register tree type;
10105       add_name_and_src_coords_attributes (type_die, decl);
10106       if (DECL_ORIGINAL_TYPE (decl))
10107         {
10108           type = DECL_ORIGINAL_TYPE (decl);
10109
10110           if (type == TREE_TYPE (decl))
10111             abort ();
10112           else
10113             equate_type_number_to_die (TREE_TYPE (decl), type_die);
10114         }
10115       else
10116         type = TREE_TYPE (decl);
10117       add_type_attribute (type_die, type, TREE_READONLY (decl),
10118                           TREE_THIS_VOLATILE (decl), context_die);
10119     }
10120
10121   if (DECL_ABSTRACT (decl))
10122     equate_decl_number_to_die (decl, type_die);
10123 }
10124
10125 /* Generate a type description DIE.  */
10126
10127 static void
10128 gen_type_die (type, context_die)
10129      register tree type;
10130      register dw_die_ref context_die;
10131 {
10132   int need_pop;
10133
10134   if (type == NULL_TREE || type == error_mark_node)
10135     return;
10136
10137   /* We are going to output a DIE to represent the unqualified version of
10138      this type (i.e. without any const or volatile qualifiers) so get the
10139      main variant (i.e. the unqualified version) of this type now.  */
10140   type = type_main_variant (type);
10141
10142   if (TREE_ASM_WRITTEN (type))
10143     return;
10144
10145   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10146       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
10147     {
10148       TREE_ASM_WRITTEN (type) = 1;
10149       gen_decl_die (TYPE_NAME (type), context_die);
10150       return;
10151     }
10152
10153   switch (TREE_CODE (type))
10154     {
10155     case ERROR_MARK:
10156       break;
10157
10158     case POINTER_TYPE:
10159     case REFERENCE_TYPE:
10160       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
10161          ensures that the gen_type_die recursion will terminate even if the
10162          type is recursive.  Recursive types are possible in Ada.  */
10163       /* ??? We could perhaps do this for all types before the switch
10164          statement.  */
10165       TREE_ASM_WRITTEN (type) = 1;
10166
10167       /* For these types, all that is required is that we output a DIE (or a
10168          set of DIEs) to represent the "basis" type.  */
10169       gen_type_die (TREE_TYPE (type), context_die);
10170       break;
10171
10172     case OFFSET_TYPE:
10173       /* This code is used for C++ pointer-to-data-member types.
10174          Output a description of the relevant class type.  */
10175       gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
10176
10177       /* Output a description of the type of the object pointed to.  */
10178       gen_type_die (TREE_TYPE (type), context_die);
10179
10180       /* Now output a DIE to represent this pointer-to-data-member type
10181          itself.  */
10182       gen_ptr_to_mbr_type_die (type, context_die);
10183       break;
10184
10185     case SET_TYPE:
10186       gen_type_die (TYPE_DOMAIN (type), context_die);
10187       gen_set_type_die (type, context_die);
10188       break;
10189
10190     case FILE_TYPE:
10191       gen_type_die (TREE_TYPE (type), context_die);
10192       abort ();                 /* No way to represent these in Dwarf yet!  */
10193       break;
10194
10195     case FUNCTION_TYPE:
10196       /* Force out return type (in case it wasn't forced out already).  */
10197       gen_type_die (TREE_TYPE (type), context_die);
10198       gen_subroutine_type_die (type, context_die);
10199       break;
10200
10201     case METHOD_TYPE:
10202       /* Force out return type (in case it wasn't forced out already).  */
10203       gen_type_die (TREE_TYPE (type), context_die);
10204       gen_subroutine_type_die (type, context_die);
10205       break;
10206
10207     case ARRAY_TYPE:
10208       if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
10209         {
10210           gen_type_die (TREE_TYPE (type), context_die);
10211           gen_string_type_die (type, context_die);
10212         }
10213       else
10214         gen_array_type_die (type, context_die);
10215       break;
10216
10217     case VECTOR_TYPE:
10218       gen_type_die (TYPE_DEBUG_REPRESENTATION_TYPE (type), context_die);
10219       break;
10220
10221     case ENUMERAL_TYPE:
10222     case RECORD_TYPE:
10223     case UNION_TYPE:
10224     case QUAL_UNION_TYPE:
10225       /* If this is a nested type whose containing class hasn't been
10226          written out yet, writing it out will cover this one, too.
10227          This does not apply to instantiations of member class templates;
10228          they need to be added to the containing class as they are
10229          generated.  FIXME: This hurts the idea of combining type decls
10230          from multiple TUs, since we can't predict what set of template
10231          instantiations we'll get.  */
10232       if (TYPE_CONTEXT (type)
10233           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
10234           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
10235         {
10236           gen_type_die (TYPE_CONTEXT (type), context_die);
10237
10238           if (TREE_ASM_WRITTEN (type))
10239             return;
10240
10241           /* If that failed, attach ourselves to the stub.  */
10242           push_decl_scope (TYPE_CONTEXT (type));
10243           context_die = lookup_type_die (TYPE_CONTEXT (type));
10244           need_pop = 1;
10245         }
10246       else
10247         need_pop = 0;
10248
10249       if (TREE_CODE (type) == ENUMERAL_TYPE)
10250         gen_enumeration_type_die (type, context_die);
10251       else
10252         gen_struct_or_union_type_die (type, context_die);
10253
10254       if (need_pop)
10255         pop_decl_scope ();
10256
10257       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
10258          it up if it is ever completed.  gen_*_type_die will set it for us
10259          when appropriate.  */
10260       return;
10261
10262     case VOID_TYPE:
10263     case INTEGER_TYPE:
10264     case REAL_TYPE:
10265     case COMPLEX_TYPE:
10266     case BOOLEAN_TYPE:
10267     case CHAR_TYPE:
10268       /* No DIEs needed for fundamental types.  */
10269       break;
10270
10271     case LANG_TYPE:
10272       /* No Dwarf representation currently defined.  */
10273       break;
10274
10275     default:
10276       abort ();
10277     }
10278
10279   TREE_ASM_WRITTEN (type) = 1;
10280 }
10281
10282 /* Generate a DIE for a tagged type instantiation.  */
10283
10284 static void
10285 gen_tagged_type_instantiation_die (type, context_die)
10286      register tree type;
10287      register dw_die_ref context_die;
10288 {
10289   if (type == NULL_TREE || type == error_mark_node)
10290     return;
10291
10292   /* We are going to output a DIE to represent the unqualified version of
10293      this type (i.e. without any const or volatile qualifiers) so make sure
10294      that we have the main variant (i.e. the unqualified version) of this
10295      type now.  */
10296   if (type != type_main_variant (type))
10297     abort ();
10298
10299   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
10300      an instance of an unresolved type.  */
10301
10302   switch (TREE_CODE (type))
10303     {
10304     case ERROR_MARK:
10305       break;
10306
10307     case ENUMERAL_TYPE:
10308       gen_inlined_enumeration_type_die (type, context_die);
10309       break;
10310
10311     case RECORD_TYPE:
10312       gen_inlined_structure_type_die (type, context_die);
10313       break;
10314
10315     case UNION_TYPE:
10316     case QUAL_UNION_TYPE:
10317       gen_inlined_union_type_die (type, context_die);
10318       break;
10319
10320     default:
10321       abort ();
10322     }
10323 }
10324
10325 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
10326    things which are local to the given block.  */
10327
10328 static void
10329 gen_block_die (stmt, context_die, depth)
10330      register tree stmt;
10331      register dw_die_ref context_die;
10332      int depth;
10333 {
10334   register int must_output_die = 0;
10335   register tree origin;
10336   register tree decl;
10337   register enum tree_code origin_code;
10338
10339   /* Ignore blocks never really used to make RTL.  */
10340
10341   if (stmt == NULL_TREE || !TREE_USED (stmt)
10342       || (!TREE_ASM_WRITTEN (stmt) && !BLOCK_ABSTRACT (stmt)))
10343     return;
10344
10345   /* Determine the "ultimate origin" of this block.  This block may be an
10346      inlined instance of an inlined instance of inline function, so we have
10347      to trace all of the way back through the origin chain to find out what
10348      sort of node actually served as the original seed for the creation of
10349      the current block.  */
10350   origin = block_ultimate_origin (stmt);
10351   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
10352
10353   /* Determine if we need to output any Dwarf DIEs at all to represent this
10354      block.  */
10355   if (origin_code == FUNCTION_DECL)
10356     /* The outer scopes for inlinings *must* always be represented.  We
10357        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
10358     must_output_die = 1;
10359   else
10360     {
10361       /* In the case where the current block represents an inlining of the
10362          "body block" of an inline function, we must *NOT* output any DIE for
10363          this block because we have already output a DIE to represent the
10364          whole inlined function scope and the "body block" of any function
10365          doesn't really represent a different scope according to ANSI C
10366          rules.  So we check here to make sure that this block does not
10367          represent a "body block inlining" before trying to set the
10368          `must_output_die' flag.  */
10369       if (! is_body_block (origin ? origin : stmt))
10370         {
10371           /* Determine if this block directly contains any "significant"
10372              local declarations which we will need to output DIEs for.  */
10373           if (debug_info_level > DINFO_LEVEL_TERSE)
10374             /* We are not in terse mode so *any* local declaration counts
10375                as being a "significant" one.  */
10376             must_output_die = (BLOCK_VARS (stmt) != NULL);
10377           else
10378             /* We are in terse mode, so only local (nested) function
10379                definitions count as "significant" local declarations.  */
10380             for (decl = BLOCK_VARS (stmt);
10381                  decl != NULL; decl = TREE_CHAIN (decl))
10382               if (TREE_CODE (decl) == FUNCTION_DECL
10383                   && DECL_INITIAL (decl))
10384                 {
10385                   must_output_die = 1;
10386                   break;
10387                 }
10388         }
10389     }
10390
10391   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
10392      DIE for any block which contains no significant local declarations at
10393      all.  Rather, in such cases we just call `decls_for_scope' so that any
10394      needed Dwarf info for any sub-blocks will get properly generated. Note
10395      that in terse mode, our definition of what constitutes a "significant"
10396      local declaration gets restricted to include only inlined function
10397      instances and local (nested) function definitions.  */
10398   if (must_output_die)
10399     {
10400       if (origin_code == FUNCTION_DECL)
10401         gen_inlined_subroutine_die (stmt, context_die, depth);
10402       else
10403         gen_lexical_block_die (stmt, context_die, depth);
10404     }
10405   else
10406     decls_for_scope (stmt, context_die, depth);
10407 }
10408
10409 /* Generate all of the decls declared within a given scope and (recursively)
10410    all of its sub-blocks.  */
10411
10412 static void
10413 decls_for_scope (stmt, context_die, depth)
10414      register tree stmt;
10415      register dw_die_ref context_die;
10416      int depth;
10417 {
10418   register tree decl;
10419   register tree subblocks;
10420
10421   /* Ignore blocks never really used to make RTL.  */
10422   if (stmt == NULL_TREE || ! TREE_USED (stmt))
10423     return;
10424
10425   /* Output the DIEs to represent all of the data objects and typedefs
10426      declared directly within this block but not within any nested
10427      sub-blocks.  Also, nested function and tag DIEs have been
10428      generated with a parent of NULL; fix that up now.  */
10429   for (decl = BLOCK_VARS (stmt);
10430        decl != NULL; decl = TREE_CHAIN (decl))
10431     {
10432       register dw_die_ref die;
10433
10434       if (TREE_CODE (decl) == FUNCTION_DECL)
10435         die = lookup_decl_die (decl);
10436       else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
10437         die = lookup_type_die (TREE_TYPE (decl));
10438       else
10439         die = NULL;
10440
10441       if (die != NULL && die->die_parent == NULL)
10442         add_child_die (context_die, die);
10443       else
10444         gen_decl_die (decl, context_die);
10445     }
10446
10447   /* Output the DIEs to represent all sub-blocks (and the items declared
10448      therein) of this block.  */
10449   for (subblocks = BLOCK_SUBBLOCKS (stmt);
10450        subblocks != NULL;
10451        subblocks = BLOCK_CHAIN (subblocks))
10452     gen_block_die (subblocks, context_die, depth + 1);
10453 }
10454
10455 /* Is this a typedef we can avoid emitting?  */
10456
10457 static inline int
10458 is_redundant_typedef (decl)
10459      register tree decl;
10460 {
10461   if (TYPE_DECL_IS_STUB (decl))
10462     return 1;
10463
10464   if (DECL_ARTIFICIAL (decl)
10465       && DECL_CONTEXT (decl)
10466       && is_tagged_type (DECL_CONTEXT (decl))
10467       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
10468       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
10469     /* Also ignore the artificial member typedef for the class name.  */
10470     return 1;
10471
10472   return 0;
10473 }
10474
10475 /* Generate Dwarf debug information for a decl described by DECL.  */
10476
10477 static void
10478 gen_decl_die (decl, context_die)
10479      register tree decl;
10480      register dw_die_ref context_die;
10481 {
10482   register tree origin;
10483
10484   if (TREE_CODE (decl) == ERROR_MARK)
10485     return;
10486
10487   /* If this ..._DECL node is marked to be ignored, then ignore it.  */
10488   if (DECL_IGNORED_P (decl))
10489     return;
10490
10491   switch (TREE_CODE (decl))
10492     {
10493     case CONST_DECL:
10494       /* The individual enumerators of an enum type get output when we output
10495          the Dwarf representation of the relevant enum type itself.  */
10496       break;
10497
10498     case FUNCTION_DECL:
10499       /* Don't output any DIEs to represent mere function declarations,
10500          unless they are class members or explicit block externs.  */
10501       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
10502           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
10503         break;
10504
10505       /* If we're emitting a clone, emit info for the abstract instance.  */
10506       if (DECL_ORIGIN (decl) != decl)
10507         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
10508       /* If we're emitting an out-of-line copy of an inline function,
10509          emit info for the abstract instance and set up to refer to it.  */
10510       else if (DECL_INLINE (decl) && ! DECL_ABSTRACT (decl)
10511                && ! class_scope_p (context_die)
10512                /* dwarf2out_abstract_function won't emit a die if this is just
10513                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
10514                   that case, because that works only if we have a die.  */
10515                && DECL_INITIAL (decl) != NULL_TREE)
10516         {
10517           dwarf2out_abstract_function (decl);
10518           set_decl_origin_self (decl);
10519         }
10520       /* Otherwise we're emitting the primary DIE for this decl.  */
10521       else if (debug_info_level > DINFO_LEVEL_TERSE)
10522         {
10523           /* Before we describe the FUNCTION_DECL itself, make sure that we
10524              have described its return type.  */
10525           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
10526
10527           /* And its virtual context.  */
10528           if (DECL_VINDEX (decl) != NULL_TREE)
10529             gen_type_die (DECL_CONTEXT (decl), context_die);
10530
10531           /* And its containing type.  */
10532           origin = decl_class_context (decl);
10533           if (origin != NULL_TREE)
10534             gen_type_die_for_member (origin, decl, context_die);
10535         }
10536
10537       /* Now output a DIE to represent the function itself.  */
10538       gen_subprogram_die (decl, context_die);
10539       break;
10540
10541     case TYPE_DECL:
10542       /* If we are in terse mode, don't generate any DIEs to represent any
10543          actual typedefs.  */
10544       if (debug_info_level <= DINFO_LEVEL_TERSE)
10545         break;
10546
10547       /* In the special case of a TYPE_DECL node representing the
10548          declaration of some type tag, if the given TYPE_DECL is marked as
10549          having been instantiated from some other (original) TYPE_DECL node
10550          (e.g. one which was generated within the original definition of an
10551          inline function) we have to generate a special (abbreviated)
10552          DW_TAG_structure_type, DW_TAG_union_type, or DW_TAG_enumeration_type
10553          DIE here.  */
10554       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
10555         {
10556           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
10557           break;
10558         }
10559
10560       if (is_redundant_typedef (decl))
10561         gen_type_die (TREE_TYPE (decl), context_die);
10562       else
10563         /* Output a DIE to represent the typedef itself.  */
10564         gen_typedef_die (decl, context_die);
10565       break;
10566
10567     case LABEL_DECL:
10568       if (debug_info_level >= DINFO_LEVEL_NORMAL)
10569         gen_label_die (decl, context_die);
10570       break;
10571
10572     case VAR_DECL:
10573       /* If we are in terse mode, don't generate any DIEs to represent any
10574          variable declarations or definitions.  */
10575       if (debug_info_level <= DINFO_LEVEL_TERSE)
10576         break;
10577
10578       /* Output any DIEs that are needed to specify the type of this data
10579          object.  */
10580       gen_type_die (TREE_TYPE (decl), context_die);
10581
10582       /* And its containing type.  */
10583       origin = decl_class_context (decl);
10584       if (origin != NULL_TREE)
10585         gen_type_die_for_member (origin, decl, context_die);
10586
10587       /* Now output the DIE to represent the data object itself.  This gets
10588          complicated because of the possibility that the VAR_DECL really
10589          represents an inlined instance of a formal parameter for an inline
10590          function.  */
10591       origin = decl_ultimate_origin (decl);
10592       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
10593         gen_formal_parameter_die (decl, context_die);
10594       else
10595         gen_variable_die (decl, context_die);
10596       break;
10597
10598     case FIELD_DECL:
10599       /* Ignore the nameless fields that are used to skip bits, but
10600          handle C++ anonymous unions.  */
10601       if (DECL_NAME (decl) != NULL_TREE
10602           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE)
10603         {
10604           gen_type_die (member_declared_type (decl), context_die);
10605           gen_field_die (decl, context_die);
10606         }
10607       break;
10608
10609     case PARM_DECL:
10610       gen_type_die (TREE_TYPE (decl), context_die);
10611       gen_formal_parameter_die (decl, context_die);
10612       break;
10613
10614     case NAMESPACE_DECL:
10615       /* Ignore for now.  */
10616       break;
10617
10618     default:
10619       abort ();
10620     }
10621 }
10622 \f
10623 /* Add Ada "use" clause information for SGI Workshop debugger.  */
10624
10625 void
10626 dwarf2out_add_library_unit_info (filename, context_list)
10627      const char *filename;
10628      const char *context_list;
10629 {
10630   unsigned int file_index;
10631
10632   if (filename != NULL)
10633     {
10634       dw_die_ref unit_die = new_die (DW_TAG_module, comp_unit_die);
10635       tree context_list_decl
10636         = build_decl (LABEL_DECL, get_identifier (context_list),
10637                       void_type_node);
10638
10639       TREE_PUBLIC (context_list_decl) = TRUE;
10640       add_name_attribute (unit_die, context_list);
10641       file_index = lookup_filename (filename);
10642       add_AT_unsigned (unit_die, DW_AT_decl_file, file_index);
10643       add_pubname (context_list_decl, unit_die);
10644     }
10645 }
10646
10647 /* Write the debugging output for DECL.  */
10648
10649 void
10650 dwarf2out_decl (decl)
10651      register tree decl;
10652 {
10653   register dw_die_ref context_die = comp_unit_die;
10654
10655   if (TREE_CODE (decl) == ERROR_MARK)
10656     return;
10657
10658   /* If this ..._DECL node is marked to be ignored, then ignore it.  */
10659   if (DECL_IGNORED_P (decl))
10660     return;
10661
10662   switch (TREE_CODE (decl))
10663     {
10664     case FUNCTION_DECL:
10665       /* Ignore this FUNCTION_DECL if it refers to a builtin declaration of a
10666          builtin function.  Explicit programmer-supplied declarations of
10667          these same functions should NOT be ignored however.  */
10668       if (DECL_EXTERNAL (decl) && DECL_BUILT_IN (decl))
10669         return;
10670
10671       /* What we would really like to do here is to filter out all mere
10672          file-scope declarations of file-scope functions which are never
10673          referenced later within this translation unit (and keep all of ones
10674          that *are* referenced later on) but we aren't clairvoyant, so we have
10675          no idea which functions will be referenced in the future (i.e. later
10676          on within the current translation unit). So here we just ignore all
10677          file-scope function declarations which are not also definitions.  If
10678          and when the debugger needs to know something about these functions,
10679          it will have to hunt around and find the DWARF information associated
10680          with the definition of the function.  Note that we can't just check
10681          `DECL_EXTERNAL' to find out which FUNCTION_DECL nodes represent
10682          definitions and which ones represent mere declarations.  We have to
10683          check `DECL_INITIAL' instead. That's because the C front-end
10684          supports some weird semantics for "extern inline" function
10685          definitions.  These can get inlined within the current translation
10686          unit (an thus, we need to generate DWARF info for their abstract
10687          instances so that the DWARF info for the concrete inlined instances
10688          can have something to refer to) but the compiler never generates any
10689          out-of-lines instances of such things (despite the fact that they
10690          *are* definitions).  The important point is that the C front-end
10691          marks these "extern inline" functions as DECL_EXTERNAL, but we need
10692          to generate DWARF for them anyway. Note that the C++ front-end also
10693          plays some similar games for inline function definitions appearing
10694          within include files which also contain
10695          `#pragma interface' pragmas.  */
10696       if (DECL_INITIAL (decl) == NULL_TREE)
10697         return;
10698
10699       /* If we're a nested function, initially use a parent of NULL; if we're
10700          a plain function, this will be fixed up in decls_for_scope.  If
10701          we're a method, it will be ignored, since we already have a DIE.  */
10702       if (decl_function_context (decl))
10703         context_die = NULL;
10704
10705       break;
10706
10707     case VAR_DECL:
10708       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
10709          declaration and if the declaration was never even referenced from
10710          within this entire compilation unit.  We suppress these DIEs in
10711          order to save space in the .debug section (by eliminating entries
10712          which are probably useless).  Note that we must not suppress
10713          block-local extern declarations (whether used or not) because that
10714          would screw-up the debugger's name lookup mechanism and cause it to
10715          miss things which really ought to be in scope at a given point.  */
10716       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
10717         return;
10718
10719       /* If we are in terse mode, don't generate any DIEs to represent any
10720          variable declarations or definitions.  */
10721       if (debug_info_level <= DINFO_LEVEL_TERSE)
10722         return;
10723       break;
10724
10725     case TYPE_DECL:
10726       /* Don't emit stubs for types unless they are needed by other DIEs.  */
10727       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
10728         return;
10729
10730       /* Don't bother trying to generate any DIEs to represent any of the
10731          normal built-in types for the language we are compiling.  */
10732       if (DECL_SOURCE_LINE (decl) == 0)
10733         {
10734           /* OK, we need to generate one for `bool' so GDB knows what type
10735              comparisons have.  */
10736           if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
10737                == DW_LANG_C_plus_plus)
10738               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE)
10739             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
10740
10741           return;
10742         }
10743
10744       /* If we are in terse mode, don't generate any DIEs for types.  */
10745       if (debug_info_level <= DINFO_LEVEL_TERSE)
10746         return;
10747
10748       /* If we're a function-scope tag, initially use a parent of NULL;
10749          this will be fixed up in decls_for_scope.  */
10750       if (decl_function_context (decl))
10751         context_die = NULL;
10752
10753       break;
10754
10755     default:
10756       return;
10757     }
10758
10759   gen_decl_die (decl, context_die);
10760 }
10761
10762 /* Output a marker (i.e. a label) for the beginning of the generated code for
10763    a lexical block.  */
10764
10765 void
10766 dwarf2out_begin_block (blocknum)
10767      register unsigned blocknum;
10768 {
10769   function_section (current_function_decl);
10770   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
10771 }
10772
10773 /* Output a marker (i.e. a label) for the end of the generated code for a
10774    lexical block.  */
10775
10776 void
10777 dwarf2out_end_block (blocknum)
10778      register unsigned blocknum;
10779 {
10780   function_section (current_function_decl);
10781   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
10782 }
10783
10784 /* Returns nonzero if it is appropriate not to emit any debugging
10785    information for BLOCK, because it doesn't contain any instructions.
10786
10787    Don't allow this for blocks with nested functions or local classes
10788    as we would end up with orphans, and in the presence of scheduling
10789    we may end up calling them anyway.  */
10790
10791 int
10792 dwarf2out_ignore_block (block)
10793      tree block;
10794 {
10795   tree decl;
10796   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
10797     if (TREE_CODE (decl) == FUNCTION_DECL
10798         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
10799       return 0;
10800   return 1;
10801 }
10802
10803 /* Lookup a filename (in the list of filenames that we know about here in
10804    dwarf2out.c) and return its "index".  The index of each (known) filename is
10805    just a unique number which is associated with only that one filename.
10806    We need such numbers for the sake of generating labels
10807    (in the .debug_sfnames section) and references to those
10808    files  numbers (in the .debug_srcinfo and.debug_macinfo sections).
10809    If the filename given as an argument is not found in our current list,
10810    add it to the list and assign it the next available unique index number.
10811    In order to speed up searches, we remember the index of the filename
10812    was looked up last.  This handles the majority of all searches.  */
10813
10814 static unsigned
10815 lookup_filename (file_name)
10816      const char *file_name;
10817 {
10818   register unsigned i;
10819
10820   /* ??? Why isn't DECL_SOURCE_FILE left null instead.  */
10821   if (strcmp (file_name, "<internal>") == 0
10822       || strcmp (file_name, "<built-in>") == 0)
10823     return 0;
10824
10825   /* Check to see if the file name that was searched on the previous
10826      call matches this file name.  If so, return the index.  */
10827   if (file_table.last_lookup_index != 0)
10828     if (strcmp (file_name, file_table.table[file_table.last_lookup_index]) == 0)
10829       return file_table.last_lookup_index;
10830
10831   /* Didn't match the previous lookup, search the table */
10832   for (i = 1; i < file_table.in_use; ++i)
10833     if (strcmp (file_name, file_table.table[i]) == 0)
10834       {
10835         file_table.last_lookup_index = i;
10836         return i;
10837       }
10838
10839   /* Prepare to add a new table entry by making sure there is enough space in
10840      the table to do so.  If not, expand the current table.  */
10841   if (i == file_table.allocated)
10842     {
10843       file_table.allocated = i + FILE_TABLE_INCREMENT;
10844       file_table.table = (char **)
10845         xrealloc (file_table.table, file_table.allocated * sizeof (char *));
10846     }
10847
10848   /* Add the new entry to the end of the filename table.  */
10849   file_table.table[i] = xstrdup (file_name);
10850   file_table.in_use = i + 1;
10851   file_table.last_lookup_index = i;
10852
10853   if (DWARF2_ASM_LINE_DEBUG_INFO)
10854     fprintf (asm_out_file, "\t.file %u \"%s\"\n", i, file_name);
10855
10856   return i;
10857 }
10858
10859 static void
10860 init_file_table ()
10861 {
10862   /* Allocate the initial hunk of the file_table.  */
10863   file_table.table = (char **) xcalloc (FILE_TABLE_INCREMENT, sizeof (char *));
10864   file_table.allocated = FILE_TABLE_INCREMENT;
10865
10866   /* Skip the first entry - file numbers begin at 1.  */
10867   file_table.in_use = 1;
10868   file_table.last_lookup_index = 0;
10869 }
10870
10871 /* Output a label to mark the beginning of a source code line entry
10872    and record information relating to this source line, in
10873    'line_info_table' for later output of the .debug_line section.  */
10874
10875 void
10876 dwarf2out_line (filename, line)
10877      register const char *filename;
10878      register unsigned line;
10879 {
10880   if (debug_info_level >= DINFO_LEVEL_NORMAL)
10881     {
10882       function_section (current_function_decl);
10883
10884       if (DWARF2_ASM_LINE_DEBUG_INFO)
10885         {
10886           unsigned file_num = lookup_filename (filename);
10887
10888           /* Emit the .loc directive understood by GNU as.  */
10889           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
10890
10891           /* Indicate that line number info exists.  */
10892           ++line_info_table_in_use;
10893
10894           /* Indicate that multiple line number tables exist.  */
10895           if (DECL_SECTION_NAME (current_function_decl))
10896             ++separate_line_info_table_in_use;
10897         }
10898       else if (DECL_SECTION_NAME (current_function_decl))
10899         {
10900           register dw_separate_line_info_ref line_info;
10901           ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, SEPARATE_LINE_CODE_LABEL,
10902                                      separate_line_info_table_in_use);
10903           if (flag_debug_asm)
10904             fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
10905                      filename, line);
10906
10907           /* expand the line info table if necessary */
10908           if (separate_line_info_table_in_use
10909               == separate_line_info_table_allocated)
10910             {
10911               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
10912               separate_line_info_table
10913                 = (dw_separate_line_info_ref)
10914                   xrealloc (separate_line_info_table,
10915                             separate_line_info_table_allocated
10916                             * sizeof (dw_separate_line_info_entry));
10917             }
10918
10919           /* Add the new entry at the end of the line_info_table.  */
10920           line_info
10921             = &separate_line_info_table[separate_line_info_table_in_use++];
10922           line_info->dw_file_num = lookup_filename (filename);
10923           line_info->dw_line_num = line;
10924           line_info->function = current_funcdef_number;
10925         }
10926       else
10927         {
10928           register dw_line_info_ref line_info;
10929
10930           ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, LINE_CODE_LABEL,
10931                                      line_info_table_in_use);
10932           if (flag_debug_asm)
10933             fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
10934                      filename, line);
10935
10936           /* Expand the line info table if necessary.  */
10937           if (line_info_table_in_use == line_info_table_allocated)
10938             {
10939               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
10940               line_info_table
10941                 = (dw_line_info_ref)
10942                   xrealloc (line_info_table,
10943                             (line_info_table_allocated
10944                              * sizeof (dw_line_info_entry)));
10945             }
10946
10947           /* Add the new entry at the end of the line_info_table.  */
10948           line_info = &line_info_table[line_info_table_in_use++];
10949           line_info->dw_file_num = lookup_filename (filename);
10950           line_info->dw_line_num = line;
10951         }
10952     }
10953 }
10954
10955 /* Record the beginning of a new source file, for later output
10956    of the .debug_macinfo section.  At present, unimplemented.  */
10957
10958 void
10959 dwarf2out_start_source_file (filename)
10960      register const char *filename ATTRIBUTE_UNUSED;
10961 {
10962   if (flag_eliminate_dwarf2_dups)
10963     {
10964       /* Record the beginning of the file for break_out_includes.  */
10965       dw_die_ref bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die);
10966       add_AT_string (bincl_die, DW_AT_name, filename);
10967     }
10968 }
10969
10970 /* Record the end of a source file, for later output
10971    of the .debug_macinfo section.  At present, unimplemented.  */
10972
10973 void
10974 dwarf2out_end_source_file ()
10975 {
10976   if (flag_eliminate_dwarf2_dups)
10977     {
10978       /* Record the end of the file for break_out_includes.  */
10979       new_die (DW_TAG_GNU_EINCL, comp_unit_die);
10980     }
10981 }
10982
10983 /* Called from check_newline in c-parse.y.  The `buffer' parameter contains
10984    the tail part of the directive line, i.e. the part which is past the
10985    initial whitespace, #, whitespace, directive-name, whitespace part.  */
10986
10987 void
10988 dwarf2out_define (lineno, buffer)
10989      register unsigned lineno ATTRIBUTE_UNUSED;
10990      register const char *buffer ATTRIBUTE_UNUSED;
10991 {
10992   static int initialized = 0;
10993   if (!initialized)
10994     {
10995       dwarf2out_start_source_file (primary_filename);
10996       initialized = 1;
10997     }
10998 }
10999
11000 /* Called from check_newline in c-parse.y.  The `buffer' parameter contains
11001    the tail part of the directive line, i.e. the part which is past the
11002    initial whitespace, #, whitespace, directive-name, whitespace part.  */
11003
11004 void
11005 dwarf2out_undef (lineno, buffer)
11006      register unsigned lineno ATTRIBUTE_UNUSED;
11007      register const char *buffer ATTRIBUTE_UNUSED;
11008 {
11009 }
11010
11011 /* Set up for Dwarf output at the start of compilation.  */
11012
11013 void
11014 dwarf2out_init (asm_out_file, main_input_filename)
11015      register FILE *asm_out_file;
11016      register const char *main_input_filename;
11017 {
11018   init_file_table ();
11019
11020   /* Remember the name of the primary input file.  */
11021   primary_filename = main_input_filename;
11022
11023   /* Add it to the file table first, under the assumption that we'll
11024      be emitting line number data for it first, which avoids having
11025      to add an initial DW_LNS_set_file.  */
11026   lookup_filename (main_input_filename);
11027
11028   /* Allocate the initial hunk of the decl_die_table.  */
11029   decl_die_table
11030     = (dw_die_ref *) xcalloc (DECL_DIE_TABLE_INCREMENT, sizeof (dw_die_ref));
11031   decl_die_table_allocated = DECL_DIE_TABLE_INCREMENT;
11032   decl_die_table_in_use = 0;
11033
11034   /* Allocate the initial hunk of the decl_scope_table.  */
11035   decl_scope_table
11036     = (tree *) xcalloc (DECL_SCOPE_TABLE_INCREMENT, sizeof (tree));
11037   decl_scope_table_allocated = DECL_SCOPE_TABLE_INCREMENT;
11038   decl_scope_depth = 0;
11039
11040   /* Allocate the initial hunk of the abbrev_die_table.  */
11041   abbrev_die_table
11042     = (dw_die_ref *) xcalloc (ABBREV_DIE_TABLE_INCREMENT,
11043                               sizeof (dw_die_ref));
11044   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
11045   /* Zero-th entry is allocated, but unused */
11046   abbrev_die_table_in_use = 1;
11047
11048   /* Allocate the initial hunk of the line_info_table.  */
11049   line_info_table
11050     = (dw_line_info_ref) xcalloc (LINE_INFO_TABLE_INCREMENT,
11051                                   sizeof (dw_line_info_entry));
11052   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
11053   /* Zero-th entry is allocated, but unused */
11054   line_info_table_in_use = 1;
11055
11056   /* Generate the initial DIE for the .debug section.  Note that the (string)
11057      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
11058      will (typically) be a relative pathname and that this pathname should be
11059      taken as being relative to the directory from which the compiler was
11060      invoked when the given (base) source file was compiled.  */
11061   comp_unit_die = gen_compile_unit_die (main_input_filename);
11062
11063   VARRAY_RTX_INIT (used_rtx_varray, 32, "used_rtx_varray");
11064   ggc_add_rtx_varray_root (&used_rtx_varray, 1);
11065
11066   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
11067   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label, ABBREV_SECTION_LABEL, 0);
11068   if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
11069     ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
11070   else
11071     strcpy (text_section_label, stripattributes (TEXT_SECTION));
11072   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
11073                                DEBUG_INFO_SECTION_LABEL, 0);
11074   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
11075                                DEBUG_LINE_SECTION_LABEL, 0);
11076
11077   ASM_OUTPUT_SECTION (asm_out_file, ABBREV_SECTION);
11078   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
11079   if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
11080     {
11081       ASM_OUTPUT_SECTION (asm_out_file, TEXT_SECTION);
11082       ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
11083     }
11084   ASM_OUTPUT_SECTION (asm_out_file, DEBUG_INFO_SECTION);
11085   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11086   ASM_OUTPUT_SECTION (asm_out_file, DEBUG_LINE_SECTION);
11087   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
11088 }
11089
11090 /* Output stuff that dwarf requires at the end of every file,
11091    and generate the DWARF-2 debugging info.  */
11092
11093 void
11094 dwarf2out_finish ()
11095 {
11096   limbo_die_node *node, *next_node;
11097   dw_die_ref die;
11098
11099   /* Traverse the limbo die list, and add parent/child links.  The only
11100      dies without parents that should be here are concrete instances of
11101      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
11102      For concrete instances, we can get the parent die from the abstract
11103      instance.  */
11104   for (node = limbo_die_list; node; node = next_node)
11105     {
11106       next_node = node->next;
11107       die = node->die;
11108
11109       if (die->die_parent == NULL)
11110         {
11111           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
11112           if (origin)
11113             add_child_die (origin->die_parent, die);
11114           else if (die == comp_unit_die)
11115             ;
11116           else
11117             abort ();
11118         }
11119       free (node);
11120     }
11121   limbo_die_list = NULL;
11122
11123   /* Walk through the list of incomplete types again, trying once more to
11124      emit full debugging info for them.  */
11125   retry_incomplete_types ();
11126
11127   /* We need to reverse all the dies before break_out_includes, or
11128      we'll see the end of an include file before the beginning.  */
11129   reverse_all_dies (comp_unit_die);
11130
11131   /* Generate separate CUs for each of the include files we've seen.
11132      They will go into limbo_die_list.  */
11133   if (flag_eliminate_dwarf2_dups)
11134     break_out_includes (comp_unit_die);
11135
11136   /* Traverse the DIE's and add add sibling attributes to those DIE's
11137      that have children.  */
11138   add_sibling_attributes (comp_unit_die);
11139   for (node = limbo_die_list; node; node = node->next)
11140     add_sibling_attributes (node->die);
11141
11142   /* Output a terminator label for the .text section.  */
11143   ASM_OUTPUT_SECTION (asm_out_file, TEXT_SECTION);
11144   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, TEXT_END_LABEL, 0);
11145
11146 #if 0
11147   /* Output a terminator label for the .data section.  */
11148   ASM_OUTPUT_SECTION (asm_out_file, DATA_SECTION);
11149   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, DATA_END_LABEL, 0);
11150
11151   /* Output a terminator label for the .bss section.  */
11152   ASM_OUTPUT_SECTION (asm_out_file, BSS_SECTION);
11153   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, BSS_END_LABEL, 0);
11154 #endif
11155
11156   /* Output the source line correspondence table.  */
11157   if (line_info_table_in_use > 1 || separate_line_info_table_in_use)
11158     {
11159       if (! DWARF2_ASM_LINE_DEBUG_INFO)
11160         {
11161           ASM_OUTPUT_SECTION (asm_out_file, DEBUG_LINE_SECTION);
11162           output_line_info ();
11163         }
11164
11165       /* We can only use the low/high_pc attributes if all of the code
11166          was in .text.  */
11167       if (separate_line_info_table_in_use == 0)
11168         {
11169           add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
11170           add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
11171         }
11172
11173       add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
11174                          debug_line_section_label);
11175     }
11176
11177 #if 0 /* unimplemented */
11178   if (debug_info_level >= DINFO_LEVEL_VERBOSE && primary)
11179     add_AT_unsigned (die, DW_AT_macro_info, 0);
11180 #endif
11181
11182   /* Output all of the compilation units.  We put the main one last so that
11183      the offsets are available to output_pubnames.  */
11184   for (node = limbo_die_list; node; node = node->next)
11185     output_comp_unit (node->die);
11186   output_comp_unit (comp_unit_die);
11187
11188   /* Output the abbreviation table.  */
11189   ASM_OUTPUT_SECTION (asm_out_file, ABBREV_SECTION);
11190   output_abbrev_section ();
11191
11192   if (pubname_table_in_use)
11193     {
11194       /* Output public names table.  */
11195       ASM_OUTPUT_SECTION (asm_out_file, PUBNAMES_SECTION);
11196       output_pubnames ();
11197     }
11198
11199   /* We only put functions in the arange table, so don't write it out if
11200      we don't have any.  */
11201   if (fde_table_in_use)
11202     {
11203       /* Output the address range information.  */
11204       ASM_OUTPUT_SECTION (asm_out_file, ARANGES_SECTION);
11205       output_aranges ();
11206     }
11207 }
11208 #endif /* DWARF2_DEBUGGING_INFO */