OSDN Git Service

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