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