OSDN Git Service

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