OSDN Git Service

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