OSDN Git Service

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