OSDN Git Service

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