OSDN Git Service

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